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.cluster.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.testkit.JavaTestKit;
16 import com.typesafe.config.Config;
17 import com.typesafe.config.ConfigFactory;
18 import java.util.concurrent.TimeUnit;
19 import java.util.concurrent.locks.ReentrantLock;
20 import org.junit.After;
21 import org.junit.Before;
22 import org.junit.Test;
23 import scala.concurrent.duration.FiniteDuration;
25 public class MeteredBoundedMailboxTest {
27 private static ActorSystem actorSystem;
28 private static CommonConfig config;
29 private final ReentrantLock lock = new ReentrantLock();
32 public void setUp() throws Exception {
33 config = new CommonConfig.Builder<>("testsystem").withConfigReader(new AkkaConfigurationReader() {
35 public Config read() {
36 return ConfigFactory.load();
39 actorSystem = ActorSystem.create("testsystem", config.get());
43 public void tearDown() throws Exception {
44 if (actorSystem != null) {
45 actorSystem.shutdown();
50 public void shouldSendMsgToDeadLetterWhenQueueIsFull() throws InterruptedException {
51 final JavaTestKit mockReceiver = new JavaTestKit(actorSystem);
52 actorSystem.eventStream().subscribe(mockReceiver.getRef(), DeadLetter.class);
55 final FiniteDuration TWENTY_SEC = new FiniteDuration(20, TimeUnit.SECONDS);
57 ActorRef pingPongActor = actorSystem.actorOf(PingPongActor.props(lock).withMailbox(config.getMailBoxName()),
60 actorSystem.mailboxes().settings();
63 //need to send 12 messages; 1 message is dequeued and actor waits on lock,
64 //2nd to 11th messages are put on the queue
65 //12th message is sent to dead letter.
66 for (int i=0;i<12;i++){
67 pingPongActor.tell("ping", mockReceiver.getRef());
70 mockReceiver.expectMsgClass(TWENTY_SEC, DeadLetter.class);
74 Object[] eleven = mockReceiver.receiveN(11, TWENTY_SEC);
80 public static class PingPongActor extends UntypedActor{
84 private PingPongActor(ReentrantLock lock){
88 public static Props props(final ReentrantLock lock){
89 return Props.create(PingPongActor.class, lock);
93 public void onReceive(Object message) throws Exception {
96 if ("ping".equals(message)) {
97 getSender().tell("pong", getSelf());