Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / common / actor / MeteredBoundedMailboxTest.java
index c027de0acf57fbeae8eeb129fc1d0a9be5921453..673b4b1876f0eeb2ec718634793bb0cfba1674bb 100644 (file)
@@ -11,14 +11,13 @@ import akka.actor.ActorRef;
 import akka.actor.ActorSystem;
 import akka.actor.DeadLetter;
 import akka.actor.Props;
-import akka.actor.UntypedActor;
-import akka.testkit.JavaTestKit;
-import com.typesafe.config.Config;
+import akka.actor.UntypedAbstractActor;
+import akka.testkit.TestKit;
 import com.typesafe.config.ConfigFactory;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.locks.ReentrantLock;
-import org.junit.After;
-import org.junit.Before;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import scala.concurrent.duration.FiniteDuration;
 
@@ -28,69 +27,66 @@ public class MeteredBoundedMailboxTest {
     private static CommonConfig config;
     private final ReentrantLock lock = new ReentrantLock();
 
-    @Before
-    public void setUp() throws Exception {
-        config = new CommonConfig.Builder<>("testsystem").withConfigReader(new AkkaConfigurationReader() {
-            @Override
-            public Config read() {
-                return ConfigFactory.load();
-            }
-        }).build();
+    @BeforeClass
+    public static void setUp() {
+        config = new CommonConfig.Builder<>("testsystem").withConfigReader(ConfigFactory::load).build();
         actorSystem = ActorSystem.create("testsystem", config.get());
     }
 
-    @After
-    public void tearDown() throws Exception {
-       if (actorSystem != null) {
-        actorSystem.shutdown();
-    }
+    @AfterClass
+    public static void tearDown() {
+        if (actorSystem != null) {
+            actorSystem.terminate();
+            actorSystem = null;
+        }
     }
 
     @Test
-    public void shouldSendMsgToDeadLetterWhenQueueIsFull() throws InterruptedException {
-        final JavaTestKit mockReceiver = new JavaTestKit(actorSystem);
-        actorSystem.eventStream().subscribe(mockReceiver.getRef(), DeadLetter.class);
-
+    public void shouldSendMsgToDeadLetterWhenQueueIsFull() {
+        final TestKit mockReceiver = new TestKit(actorSystem);
+        actorSystem.eventStream().subscribe(mockReceiver.testActor(), DeadLetter.class);
 
-        final FiniteDuration TWENTY_SEC = new FiniteDuration(20, TimeUnit.SECONDS);
+        final FiniteDuration twentySeconds = new FiniteDuration(20, TimeUnit.SECONDS);
 
         ActorRef pingPongActor = actorSystem.actorOf(PingPongActor.props(lock).withMailbox(config.getMailBoxName()),
                                                      "pingpongactor");
 
         actorSystem.mailboxes().settings();
         lock.lock();
-        //queue capacity = 10
-        //need to send 12 messages; 1 message is dequeued and actor waits on lock,
-        //2nd to 11th messages are put on the queue
-        //12th message is sent to dead letter.
-        for (int i=0;i<12;i++){
-            pingPongActor.tell("ping", mockReceiver.getRef());
-        }
-
-        mockReceiver.expectMsgClass(TWENTY_SEC, DeadLetter.class);
+        try {
+            //queue capacity = 10
+            //need to send 12 messages; 1 message is dequeued and actor waits on lock,
+            //2nd to 11th messages are put on the queue
+            //12th message is sent to dead letter.
+            for (int i = 0; i < 12; i++) {
+                pingPongActor.tell("ping", mockReceiver.testActor());
+            }
 
-        lock.unlock();
+            mockReceiver.expectMsgClass(twentySeconds, DeadLetter.class);
+        } finally {
+            lock.unlock();
+        }
 
-        Object[] eleven = mockReceiver.receiveN(11, TWENTY_SEC);
+        mockReceiver.receiveN(11, twentySeconds);
     }
 
     /**
-     * For testing
+     * For testing.
      */
-    public static class PingPongActor extends UntypedActor{
+    public static class PingPongActor extends UntypedAbstractActor {
 
         ReentrantLock lock;
 
-        private PingPongActor(ReentrantLock lock){
+        PingPongActor(final ReentrantLock lock) {
             this.lock = lock;
         }
 
-        public static Props props(final ReentrantLock lock){
+        public static Props props(final ReentrantLock lock) {
             return Props.create(PingPongActor.class, lock);
         }
 
         @Override
-        public void onReceive(Object message) throws Exception {
+        public void onReceive(final Object message) {
             lock.lock();
             try {
                 if ("ping".equals(message)) {
@@ -101,4 +97,4 @@ public class MeteredBoundedMailboxTest {
             }
         }
     }
-}
\ No newline at end of file
+}