Migrate to UntypedAbstractActor
[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.UntypedAbstractActor;
15 import akka.testkit.TestKit;
16 import com.typesafe.config.ConfigFactory;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.locks.ReentrantLock;
19 import org.junit.AfterClass;
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22 import scala.concurrent.duration.FiniteDuration;
23
24 public class MeteredBoundedMailboxTest {
25
26     private static ActorSystem actorSystem;
27     private static CommonConfig config;
28     private final ReentrantLock lock = new ReentrantLock();
29
30     @BeforeClass
31     public static void setUp() {
32         config = new CommonConfig.Builder<>("testsystem").withConfigReader(ConfigFactory::load).build();
33         actorSystem = ActorSystem.create("testsystem", config.get());
34     }
35
36     @AfterClass
37     public static void tearDown() {
38         if (actorSystem != null) {
39             actorSystem.terminate();
40             actorSystem = null;
41         }
42     }
43
44     @Test
45     public void shouldSendMsgToDeadLetterWhenQueueIsFull() {
46         final TestKit mockReceiver = new TestKit(actorSystem);
47         actorSystem.eventStream().subscribe(mockReceiver.testActor(), DeadLetter.class);
48
49         final FiniteDuration twentySeconds = new FiniteDuration(20, TimeUnit.SECONDS);
50
51         ActorRef pingPongActor = actorSystem.actorOf(PingPongActor.props(lock).withMailbox(config.getMailBoxName()),
52                                                      "pingpongactor");
53
54         actorSystem.mailboxes().settings();
55         lock.lock();
56         try {
57             //queue capacity = 10
58             //need to send 12 messages; 1 message is dequeued and actor waits on lock,
59             //2nd to 11th messages are put on the queue
60             //12th message is sent to dead letter.
61             for (int i = 0; i < 12; i++) {
62                 pingPongActor.tell("ping", mockReceiver.testActor());
63             }
64
65             mockReceiver.expectMsgClass(twentySeconds, DeadLetter.class);
66         } finally {
67             lock.unlock();
68         }
69
70         mockReceiver.receiveN(11, twentySeconds);
71     }
72
73     /**
74      * For testing.
75      */
76     public static class PingPongActor extends UntypedAbstractActor {
77
78         ReentrantLock lock;
79
80         PingPongActor(final ReentrantLock lock) {
81             this.lock = lock;
82         }
83
84         public static Props props(final ReentrantLock lock) {
85             return Props.create(PingPongActor.class, lock);
86         }
87
88         @Override
89         public void onReceive(final Object message) {
90             lock.lock();
91             try {
92                 if ("ping".equals(message)) {
93                     getSender().tell("pong", getSelf());
94                 }
95             } finally {
96                 lock.unlock();
97             }
98         }
99     }
100 }