Add TransmitQueue unit tests
[controller.git] / opendaylight / md-sal / cds-access-client / src / test / java / org / opendaylight / controller / cluster / access / client / AbstractClientConnectionTest.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.access.client;
9
10 import static org.hamcrest.CoreMatchers.hasItems;
11 import static org.mockito.Matchers.anyLong;
12 import static org.mockito.Matchers.argThat;
13 import static org.mockito.Matchers.isA;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.timeout;
16 import static org.mockito.Mockito.verify;
17 import static org.opendaylight.controller.cluster.access.client.ConnectionEntryMatcher.entryWithRequest;
18
19 import akka.actor.ActorRef;
20 import akka.actor.ActorSystem;
21 import akka.testkit.JavaTestKit;
22 import akka.testkit.TestProbe;
23 import java.util.Optional;
24 import java.util.function.Consumer;
25 import org.junit.After;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.MockitoAnnotations;
30 import org.opendaylight.controller.cluster.access.commands.AbortLocalTransactionRequest;
31 import org.opendaylight.controller.cluster.access.commands.TransactionAbortSuccess;
32 import org.opendaylight.controller.cluster.access.commands.TransactionFailure;
33 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
34 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
35 import org.opendaylight.controller.cluster.access.concepts.FrontendType;
36 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
37 import org.opendaylight.controller.cluster.access.concepts.MemberName;
38 import org.opendaylight.controller.cluster.access.concepts.Request;
39 import org.opendaylight.controller.cluster.access.concepts.RequestEnvelope;
40 import org.opendaylight.controller.cluster.access.concepts.RequestSuccess;
41 import org.opendaylight.controller.cluster.access.concepts.Response;
42 import org.opendaylight.controller.cluster.access.concepts.ResponseEnvelope;
43 import org.opendaylight.controller.cluster.access.concepts.RuntimeRequestException;
44 import org.opendaylight.controller.cluster.access.concepts.SuccessEnvelope;
45 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
46 import scala.concurrent.duration.FiniteDuration;
47
48 public abstract class AbstractClientConnectionTest<T extends AbstractClientConnection<U>, U extends BackendInfo> {
49
50     protected static final MemberName MEMBER_NAME = MemberName.forName("member-1");
51     protected static final FrontendType FRONTEND_TYPE =
52             FrontendType.forName(ClientActorContextTest.class.getSimpleName());
53     protected static final FrontendIdentifier FRONTEND_ID = FrontendIdentifier.create(MEMBER_NAME, FRONTEND_TYPE);
54     protected static final ClientIdentifier CLIENT_ID = ClientIdentifier.create(FRONTEND_ID, 0);
55     protected static final String PERSISTENCE_ID = "per-1";
56
57     protected T connection;
58     protected ClientActorContext context;
59     protected ActorSystem system;
60     protected TestProbe backendProbe;
61     protected TestProbe contextProbe;
62     protected TestProbe replyToProbe;
63
64     @Before
65     public void setUp() throws Exception {
66         MockitoAnnotations.initMocks(this);
67         system = ActorSystem.apply();
68         backendProbe = new TestProbe(system);
69         contextProbe = new TestProbe(system);
70         context = new ClientActorContext(contextProbe.ref(), system.scheduler(), system.dispatcher(),
71                 PERSISTENCE_ID, CLIENT_ID);
72         replyToProbe = new TestProbe(system);
73         connection = createConnection();
74     }
75
76     protected abstract T createConnection();
77
78     @Test
79     public void testLocalActor() throws Exception {
80         Assert.assertEquals(contextProbe.ref(), connection.localActor());
81     }
82
83     @Test
84     public abstract void testReconnectConnection() throws Exception;
85
86     @Test
87     public void testPoison() throws Exception {
88         final Consumer<Response<?, ?>> callback = mock(Consumer.class);
89         final Request<?, ?> request = createRequest(replyToProbe.ref());
90         final ConnectionEntry entry = new ConnectionEntry(request, callback, 0L);
91         connection.enqueueEntry(entry, 0L);
92         connection.poison(new RuntimeRequestException("fail", new RuntimeException("fail")));
93         verify(callback, timeout(1000)).accept(isA(TransactionFailure.class));
94     }
95
96     @Test
97     public void testSendRequestReceiveResponse() throws Exception {
98         final Consumer<Response<?, ?>> callback = mock(Consumer.class);
99         final Request<?, ?> request = createRequest(replyToProbe.ref());
100         connection.sendRequest(request, callback);
101         final RequestEnvelope requestEnvelope = backendProbe.expectMsgClass(RequestEnvelope.class);
102         Assert.assertEquals(request, requestEnvelope.getMessage());
103         final LocalHistoryIdentifier historyId = new LocalHistoryIdentifier(CLIENT_ID, 0L);
104         final RequestSuccess<?, ?> message = new TransactionAbortSuccess(new TransactionIdentifier(historyId, 0L), 0L);
105         final ResponseEnvelope<?> envelope = new SuccessEnvelope(message, 0L, 0L, 0L);
106         connection.receiveResponse(envelope);
107         verify(callback, timeout(1000)).accept(isA(TransactionAbortSuccess.class));
108     }
109
110     @Test
111     public void testRun() throws Exception {
112         final ClientActorBehavior<U> behavior = mock(ClientActorBehavior.class);
113         Assert.assertSame(behavior, connection.runTimer(behavior));
114     }
115
116     @Test
117     public void testCheckTimeoutEmptyQueue() throws Exception {
118         final Optional<FiniteDuration> timeout = connection.checkTimeout(context.ticker().read());
119         Assert.assertFalse(timeout.isPresent());
120     }
121
122     @Test
123     public void testCheckTimeoutConnectionTimeouted() throws Exception {
124         final Consumer<Response<?, ?>> callback = mock(Consumer.class);
125         connection.sendRequest(createRequest(replyToProbe.ref()), callback);
126         final long now = context.ticker().read() + ConnectedClientConnection.REQUEST_TIMEOUT_NANOS;
127         final Optional<FiniteDuration> timeout = connection.checkTimeout(now);
128         Assert.assertNull(timeout);
129     }
130
131     @Test
132     public void testCheckTimeout() throws Exception {
133         final Consumer<Response<?, ?>> callback = mock(Consumer.class);
134         connection.sendRequest(createRequest(replyToProbe.ref()), callback);
135         final long now = context.ticker().read();
136         final Optional<FiniteDuration> timeout = connection.checkTimeout(now);
137         Assert.assertTrue(timeout.isPresent());
138     }
139
140     @Test
141     public void testReplay() throws Exception {
142         final Consumer<Response<?, ?>> callback = mock(Consumer.class);
143         final Request<?, ?> request1 = createRequest(replyToProbe.ref());
144         final Request<?, ?> request2 = createRequest(replyToProbe.ref());
145         connection.sendRequest(request1, callback);
146         connection.sendRequest(request2, callback);
147         final Iterable<ConnectionEntry> entries = connection.startReplay();
148         Assert.assertThat(entries, hasItems(entryWithRequest(request1), entryWithRequest(request2)));
149         final ReconnectForwarder forwarder = mock(ReconnectForwarder.class);
150         connection.finishReplay(forwarder);
151         verify(forwarder).forwardEntry(argThat(entryWithRequest(request1)), anyLong());
152         verify(forwarder).forwardEntry(argThat(entryWithRequest(request2)), anyLong());
153     }
154
155     @After
156     public void tearDown() throws Exception {
157         JavaTestKit.shutdownActorSystem(system);
158     }
159
160     protected Request<?, ?> createRequest(final ActorRef replyTo) {
161         final TransactionIdentifier identifier =
162                 new TransactionIdentifier(new LocalHistoryIdentifier(CLIENT_ID, 0L), 0L);
163         return new AbortLocalTransactionRequest(identifier, replyTo);
164     }
165
166 }