2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.common.actor;
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;
22 import java.util.concurrent.TimeUnit;
23 import java.util.concurrent.locks.ReentrantLock;
25 public class MeteredBoundedMailboxTest {
27 private static ActorSystem actorSystem;
28 private final ReentrantLock lock = new ReentrantLock();
31 public void setUp() throws Exception {
32 actorSystem = ActorSystem.create("testsystem");
36 public void tearDown() throws Exception {
37 if (actorSystem != null)
38 actorSystem.shutdown();
42 public void test_WhenQueueIsFull_ShouldSendMsgToDeadLetter() throws InterruptedException {
43 final JavaTestKit mockReceiver = new JavaTestKit(actorSystem);
44 actorSystem.eventStream().subscribe(mockReceiver.getRef(), DeadLetter.class);
47 final FiniteDuration TWENTY_SEC = new FiniteDuration(20, TimeUnit.SECONDS);
49 String boundedMailBox = actorSystem.name() + ".bounded-mailbox";
50 ActorRef pingPongActor = actorSystem.actorOf(PingPongActor.props(lock).withMailbox(boundedMailBox),
53 actorSystem.mailboxes().settings();
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());
63 mockReceiver.expectMsgClass(TWENTY_SEC, DeadLetter.class);
67 Object[] eleven = mockReceiver.receiveN(11, TWENTY_SEC);
73 public static class PingPongActor extends UntypedActor{
77 private PingPongActor(ReentrantLock lock){
81 public static Props props(final ReentrantLock lock){
82 return Props.create(new Creator<PingPongActor>(){
84 public PingPongActor create() throws Exception {
85 return new PingPongActor(lock);
91 public void onReceive(Object message) throws Exception {
94 if ("ping".equals(message))
95 getSender().tell("pong", getSelf());