2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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 static org.mockito.Mockito.never;
11 import static org.mockito.Mockito.timeout;
12 import static org.mockito.Mockito.verify;
14 import akka.actor.ActorRef;
15 import akka.actor.ActorSystem;
16 import akka.actor.Address;
17 import akka.event.Logging;
18 import akka.japi.Effect;
19 import akka.remote.AssociationErrorEvent;
20 import akka.remote.InvalidAssociation;
21 import akka.remote.UniqueAddress;
22 import akka.remote.artery.ThisActorSystemQuarantinedEvent;
23 import akka.testkit.javadsl.TestKit;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.Mock;
29 import org.mockito.junit.MockitoJUnitRunner;
32 @RunWith(MockitoJUnitRunner.StrictStubs.class)
33 public class QuarantinedMonitorActorTest {
35 private static final Address LOCAL = Address.apply("http", "local");
36 private static final Address REMOTE = Address.apply("http", "remote");
39 private Effect callback;
40 private ActorSystem system;
41 private ActorRef actor;
45 system = ActorSystem.apply();
46 actor = system.actorOf(QuarantinedMonitorActor.props(callback));
50 public void tearDown() {
51 TestKit.shutdownActorSystem(system);
55 public void testOnReceiveQuarantined() throws Exception {
56 final Throwable t = new RuntimeException("Remote has quarantined this system");
57 final InvalidAssociation cause = InvalidAssociation.apply(LOCAL, REMOTE, t, Option.apply(null));
58 final UniqueAddress localAddress = new UniqueAddress(LOCAL, 1);
59 final UniqueAddress remoteAddress = new UniqueAddress(REMOTE, 2);
60 final ThisActorSystemQuarantinedEvent event = new ThisActorSystemQuarantinedEvent(localAddress, remoteAddress);
61 actor.tell(event, ActorRef.noSender());
62 verify(callback, timeout(1000)).apply();
66 public void testOnReceiveQuarantinedAsAssociation() throws Exception {
67 for (int i = 0; i < 9; i++) {
69 new RuntimeException("The remote system has a UID that has been quarantined. Association aborted.");
70 final InvalidAssociation cause = InvalidAssociation.apply(LOCAL, REMOTE, t, Option.apply(null));
71 final AssociationErrorEvent event =
72 new AssociationErrorEvent(cause, LOCAL, REMOTE, true, Logging.ErrorLevel());
73 actor.tell(event, ActorRef.noSender());
76 final Address local1 = Address.apply("http", "local1");
77 final Address remote1 = Address.apply("http", "remote1");
79 new RuntimeException("The remote system has a UID that has been quarantined. Association aborted.");
80 final InvalidAssociation cause1 = InvalidAssociation.apply(local1, remote1, t1, Option.apply(null));
81 final AssociationErrorEvent event1 =
82 new AssociationErrorEvent(cause1, local1, remote1, true, Logging.ErrorLevel());
83 actor.tell(event1, ActorRef.noSender());
84 verify(callback, timeout(1000)).apply();
88 public void testOnReceiveAnother() throws Exception {
89 final Address local = Address.apply("http", "local");
90 final Address remote = Address.apply("http", "remote");
91 final Throwable t = new RuntimeException("Another exception");
92 final InvalidAssociation cause = InvalidAssociation.apply(local, remote, t, Option.apply(null));
93 final AssociationErrorEvent event = new AssociationErrorEvent(cause, local, remote, true, Logging.ErrorLevel());
94 actor.tell(event, ActorRef.noSender());
95 verify(callback, never()).apply();