897cb6193ad3678ba8a1e308a74506029d8a9081
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / common / actor / QuarantinedMonitorActorTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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 static org.mockito.Mockito.never;
11 import static org.mockito.Mockito.timeout;
12 import static org.mockito.Mockito.verify;
13
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;
30 import scala.Option;
31
32 @RunWith(MockitoJUnitRunner.StrictStubs.class)
33 public class QuarantinedMonitorActorTest {
34
35     private static final Address LOCAL = Address.apply("http", "local");
36     private static final Address REMOTE = Address.apply("http", "remote");
37
38     @Mock
39     private Effect callback;
40     private ActorSystem system;
41     private ActorRef actor;
42
43     @Before
44     public void setUp() {
45         system = ActorSystem.apply();
46         actor = system.actorOf(QuarantinedMonitorActor.props(callback));
47     }
48
49     @After
50     public void tearDown() {
51         TestKit.shutdownActorSystem(system);
52     }
53
54     @Test
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();
63     }
64
65     @Test
66     public void testOnReceiveQuarantinedAsAssociation() throws Exception {
67         for (int i = 0; i < 9; i++) {
68             final Throwable t =
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());
74         }
75
76         final Address local1 = Address.apply("http", "local1");
77         final Address remote1 = Address.apply("http", "remote1");
78         final Throwable t1 =
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();
85     }
86
87     @Test
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();
96     }
97 }