Move MessageTrackerTest
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / common / actor / MeteredBoundedMailboxTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.common.actor;
9
10 import akka.actor.ActorRef;
11 import akka.actor.ActorSystem;
12 import akka.actor.DeadLetter;
13 import akka.actor.Props;
14 import akka.actor.UntypedActor;
15 import akka.testkit.JavaTestKit;
16 import com.typesafe.config.Config;
17 import com.typesafe.config.ConfigFactory;
18 import java.util.concurrent.TimeUnit;
19 import java.util.concurrent.locks.ReentrantLock;
20 import org.junit.AfterClass;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import scala.concurrent.duration.FiniteDuration;
24
25 public class MeteredBoundedMailboxTest {
26
27     private static ActorSystem actorSystem;
28     private static CommonConfig config;
29     private final ReentrantLock lock = new ReentrantLock();
30
31     @BeforeClass
32     public static void setUp() throws Exception {
33         config = new CommonConfig.Builder<>("testsystem").withConfigReader(new AkkaConfigurationReader() {
34             @Override
35             public Config read() {
36                 return ConfigFactory.load();
37             }
38         }).build();
39         actorSystem = ActorSystem.create("testsystem", config.get());
40     }
41
42     @AfterClass
43     public static void tearDown() throws Exception {
44         if (actorSystem != null) {
45             actorSystem.terminate();
46             actorSystem = null;
47         }
48     }
49
50     @Test
51     public void shouldSendMsgToDeadLetterWhenQueueIsFull() throws InterruptedException {
52         final JavaTestKit mockReceiver = new JavaTestKit(actorSystem);
53         actorSystem.eventStream().subscribe(mockReceiver.getRef(), DeadLetter.class);
54
55
56         final FiniteDuration TWENTY_SEC = new FiniteDuration(20, TimeUnit.SECONDS);
57
58         ActorRef pingPongActor = actorSystem.actorOf(PingPongActor.props(lock).withMailbox(config.getMailBoxName()),
59                                                      "pingpongactor");
60
61         actorSystem.mailboxes().settings();
62         lock.lock();
63         //queue capacity = 10
64         //need to send 12 messages; 1 message is dequeued and actor waits on lock,
65         //2nd to 11th messages are put on the queue
66         //12th message is sent to dead letter.
67         for (int i=0;i<12;i++){
68             pingPongActor.tell("ping", mockReceiver.getRef());
69         }
70
71         mockReceiver.expectMsgClass(TWENTY_SEC, DeadLetter.class);
72
73         lock.unlock();
74
75         Object[] eleven = mockReceiver.receiveN(11, TWENTY_SEC);
76     }
77
78     /**
79      * For testing
80      */
81     public static class PingPongActor extends UntypedActor{
82
83         ReentrantLock lock;
84
85         private PingPongActor(final ReentrantLock lock){
86             this.lock = lock;
87         }
88
89         public static Props props(final ReentrantLock lock){
90             return Props.create(PingPongActor.class, lock);
91         }
92
93         @Override
94         public void onReceive(final Object message) throws Exception {
95             lock.lock();
96             try {
97                 if ("ping".equals(message)) {
98                     getSender().tell("pong", getSelf());
99                 }
100             } finally {
101                 lock.unlock();
102             }
103         }
104     }
105 }