Merge "Bug 1029: Remove dead code: p2site"
[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.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 static CommonConfig config;
29     private final ReentrantLock lock = new ReentrantLock();
30
31     @Before
32     public void setUp() throws Exception {
33         config = new CommonConfig.Builder<>("testsystem").build();
34         actorSystem = ActorSystem.create("testsystem", config.get());
35     }
36
37     @After
38     public void tearDown() throws Exception {
39        if (actorSystem != null)
40            actorSystem.shutdown();
41     }
42
43     @Test
44     public void shouldSendMsgToDeadLetterWhenQueueIsFull() throws InterruptedException {
45         final JavaTestKit mockReceiver = new JavaTestKit(actorSystem);
46         actorSystem.eventStream().subscribe(mockReceiver.getRef(), DeadLetter.class);
47
48
49         final FiniteDuration TWENTY_SEC = 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         //queue capacity = 10
57         //need to send 12 messages; 1 message is dequeued and actor waits on lock,
58         //2nd to 11th messages are put on the queue
59         //12th message is sent to dead letter.
60         for (int i=0;i<12;i++){
61             pingPongActor.tell("ping", mockReceiver.getRef());
62         }
63
64         mockReceiver.expectMsgClass(TWENTY_SEC, DeadLetter.class);
65
66         lock.unlock();
67
68         Object[] eleven = mockReceiver.receiveN(11, TWENTY_SEC);
69     }
70
71     /**
72      * For testing
73      */
74     public static class PingPongActor extends UntypedActor{
75
76         ReentrantLock lock;
77
78         private PingPongActor(ReentrantLock lock){
79             this.lock = lock;
80         }
81
82         public static Props props(final ReentrantLock lock){
83             return Props.create(new Creator<PingPongActor>(){
84                 private static final long serialVersionUID = 1L;
85                 @Override
86                 public PingPongActor create() throws Exception {
87                     return new PingPongActor(lock);
88                 }
89             });
90         }
91
92         @Override
93         public void onReceive(Object message) throws Exception {
94             lock.lock();
95             try {
96                 if ("ping".equals(message))
97                     getSender().tell("pong", getSelf());
98             } finally {
99                 lock.unlock();
100             }
101         }
102     }
103 }