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.access.client;
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Mockito.after;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.verify;
15 import java.util.Optional;
16 import java.util.function.Consumer;
17 import org.junit.Assert;
18 import org.junit.Test;
19 import org.opendaylight.controller.cluster.access.ABIVersion;
20 import org.opendaylight.controller.cluster.access.commands.TransactionAbortSuccess;
21 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
22 import org.opendaylight.controller.cluster.access.concepts.Request;
23 import org.opendaylight.controller.cluster.access.concepts.RequestException;
24 import org.opendaylight.controller.cluster.access.concepts.RequestSuccess;
25 import org.opendaylight.controller.cluster.access.concepts.Response;
26 import org.opendaylight.controller.cluster.access.concepts.ResponseEnvelope;
27 import org.opendaylight.controller.cluster.access.concepts.SuccessEnvelope;
28 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
30 public class ReconnectingClientConnectionTest
31 extends AbstractClientConnectionTest<ReconnectingClientConnection<BackendInfo>, BackendInfo> {
34 public void testCheckTimeoutConnectionTimedout() throws Exception {
35 final Consumer<Response<?, ?>> callback = mock(Consumer.class);
36 connection.sendRequest(createRequest(replyToProbe.ref()), callback);
37 final long now = context.ticker().read() + ConnectedClientConnection.BACKEND_ALIVE_TIMEOUT_NANOS;
38 final Optional<Long> timeout = connection.checkTimeout(now);
39 Assert.assertNotNull(timeout);
40 Assert.assertTrue(timeout.isPresent());
44 protected ReconnectingClientConnection<BackendInfo> createConnection() {
45 final BackendInfo backend = new BackendInfo(backendProbe.ref(), 0L, ABIVersion.BORON, 10);
47 final ConnectedClientConnection<BackendInfo> oldConnection =
48 new ConnectedClientConnection<>(context, 0L, backend);
49 return new ReconnectingClientConnection<>(oldConnection, mock(RequestException.class));
54 public void testReconnectConnection() throws Exception {
55 final ClientActorBehavior<BackendInfo> behavior = mock(ClientActorBehavior.class);
56 Assert.assertSame(behavior, connection.lockedReconnect(behavior, mock(RequestException.class)));
61 public void testSendRequestReceiveResponse() throws Exception {
62 final Consumer<Response<?, ?>> callback = mock(Consumer.class);
63 final Request<?, ?> request = createRequest(replyToProbe.ref());
64 connection.sendRequest(request, callback);
65 backendProbe.expectNoMsg();
66 final LocalHistoryIdentifier historyId = new LocalHistoryIdentifier(CLIENT_ID, 0L);
67 final RequestSuccess<?, ?> message = new TransactionAbortSuccess(new TransactionIdentifier(historyId, 0L), 0L);
68 final ResponseEnvelope<?> envelope = new SuccessEnvelope(message, 0L, 0L, 0L);
69 connection.receiveResponse(envelope);
70 verify(callback, after(1000).never()).accept(any());