Merge "Fix compilation warnings: replace deprecated org.apache.felix classes"
[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 org.junit.After;
17 import org.junit.Before;
18 import org.junit.Test;
19 import scala.concurrent.duration.FiniteDuration;
20
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.locks.ReentrantLock;
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     @Before
31     public void setUp() throws Exception {
32         config = new CommonConfig.Builder<>("testsystem").build();
33         actorSystem = ActorSystem.create("testsystem", config.get());
34     }
35
36     @After
37     public void tearDown() throws Exception {
38        if (actorSystem != null)
39            actorSystem.shutdown();
40     }
41
42     @Test
43     public void shouldSendMsgToDeadLetterWhenQueueIsFull() throws InterruptedException {
44         final JavaTestKit mockReceiver = new JavaTestKit(actorSystem);
45         actorSystem.eventStream().subscribe(mockReceiver.getRef(), DeadLetter.class);
46
47
48         final FiniteDuration TWENTY_SEC = new FiniteDuration(20, TimeUnit.SECONDS);
49
50         ActorRef pingPongActor = actorSystem.actorOf(PingPongActor.props(lock).withMailbox(config.getMailBoxName()),
51                                                      "pingpongactor");
52
53         actorSystem.mailboxes().settings();
54         lock.lock();
55         //queue capacity = 10
56         //need to send 12 messages; 1 message is dequeued and actor waits on lock,
57         //2nd to 11th messages are put on the queue
58         //12th message is sent to dead letter.
59         for (int i=0;i<12;i++){
60             pingPongActor.tell("ping", mockReceiver.getRef());
61         }
62
63         mockReceiver.expectMsgClass(TWENTY_SEC, DeadLetter.class);
64
65         lock.unlock();
66
67         Object[] eleven = mockReceiver.receiveN(11, TWENTY_SEC);
68     }
69
70     /**
71      * For testing
72      */
73     public static class PingPongActor extends UntypedActor{
74
75         ReentrantLock lock;
76
77         private PingPongActor(ReentrantLock lock){
78             this.lock = lock;
79         }
80
81         public static Props props(final ReentrantLock lock){
82             return Props.create(PingPongActor.class, lock);
83         }
84
85         @Override
86         public void onReceive(Object message) throws Exception {
87             lock.lock();
88             try {
89                 if ("ping".equals(message))
90                     getSender().tell("pong", getSelf());
91             } finally {
92                 lock.unlock();
93             }
94         }
95     }
96 }