Increasing the time-out to allow for message processing.
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / 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.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.japi.Creator;
16 import akka.testkit.JavaTestKit;
17 import org.junit.After;
18 import org.junit.Before;
19 import org.junit.Test;
20 import scala.concurrent.duration.FiniteDuration;
21
22 import java.util.concurrent.TimeUnit;
23 import java.util.concurrent.locks.ReentrantLock;
24
25 public class MeteredBoundedMailboxTest {
26
27     private static ActorSystem actorSystem;
28     private final ReentrantLock lock = new ReentrantLock();
29
30     @Before
31     public void setUp() throws Exception {
32         actorSystem = ActorSystem.create("testsystem");
33     }
34
35     @After
36     public void tearDown() throws Exception {
37        if (actorSystem != null)
38            actorSystem.shutdown();
39     }
40
41     @Test
42     public void test_WhenQueueIsFull_ShouldSendMsgToDeadLetter() throws InterruptedException {
43         final JavaTestKit mockReceiver = new JavaTestKit(actorSystem);
44         actorSystem.eventStream().subscribe(mockReceiver.getRef(), DeadLetter.class);
45
46
47         final FiniteDuration TEN_SEC = new FiniteDuration(10, TimeUnit.SECONDS);
48         String boundedMailBox = actorSystem.name() + ".bounded-mailbox";
49         ActorRef pingPongActor = actorSystem.actorOf(PingPongActor.props(lock).withMailbox(boundedMailBox),
50                                                      "pingpongactor");
51
52         actorSystem.mailboxes().settings();
53         lock.lock();
54         //queue capacity = 10
55         //need to send 12 messages; 1 message is dequeued and actor waits on lock,
56         //2nd to 11th messages are put on the queue
57         //12th message is sent to dead letter.
58         for (int i=0;i<12;i++){
59             pingPongActor.tell("ping", mockReceiver.getRef());
60         }
61
62         mockReceiver.expectMsgClass(TEN_SEC, DeadLetter.class);
63
64         lock.unlock();
65
66         Object[] eleven = mockReceiver.receiveN(11, TEN_SEC);
67     }
68
69     /**
70      * For testing
71      */
72     public static class PingPongActor extends UntypedActor{
73
74         ReentrantLock lock;
75
76         private PingPongActor(ReentrantLock lock){
77             this.lock = lock;
78         }
79
80         public static Props props(final ReentrantLock lock){
81             return Props.create(new Creator<PingPongActor>(){
82                 @Override
83                 public PingPongActor create() throws Exception {
84                     return new PingPongActor(lock);
85                 }
86             });
87         }
88
89         @Override
90         public void onReceive(Object message) throws Exception {
91             lock.lock();
92             if ("ping".equals(message))
93                 getSender().tell("pong", getSelf());
94         }
95     }
96 }