Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / cds-access-client / src / test / java / org / opendaylight / controller / cluster / access / client / ReconnectingClientConnectionTest.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.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertSame;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.Mockito.after;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.verify;
17
18 import java.util.OptionalLong;
19 import java.util.function.Consumer;
20 import org.junit.Test;
21 import org.opendaylight.controller.cluster.access.ABIVersion;
22 import org.opendaylight.controller.cluster.access.commands.TransactionAbortSuccess;
23 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
24 import org.opendaylight.controller.cluster.access.concepts.Request;
25 import org.opendaylight.controller.cluster.access.concepts.RequestException;
26 import org.opendaylight.controller.cluster.access.concepts.RequestSuccess;
27 import org.opendaylight.controller.cluster.access.concepts.Response;
28 import org.opendaylight.controller.cluster.access.concepts.ResponseEnvelope;
29 import org.opendaylight.controller.cluster.access.concepts.SuccessEnvelope;
30 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
31
32 public class ReconnectingClientConnectionTest
33         extends AbstractClientConnectionTest<ReconnectingClientConnection<BackendInfo>, BackendInfo> {
34
35     @Test
36     public void testCheckTimeoutConnectionTimedout() {
37         final Consumer<Response<?, ?>> callback = mock(Consumer.class);
38         connection.sendRequest(createRequest(replyToProbe.ref()), callback);
39         final long now = context.ticker().read() + ConnectedClientConnection.DEFAULT_BACKEND_ALIVE_TIMEOUT_NANOS;
40         final OptionalLong timeout = connection.checkTimeout(now);
41         assertNotNull(timeout);
42         assertTrue(timeout.isPresent());
43     }
44
45     @Override
46     protected ReconnectingClientConnection<BackendInfo> createConnection() {
47         final BackendInfo backend = new BackendInfo(backendProbe.ref(), "test", 0L, ABIVersion.BORON, 10);
48         final ConnectingClientConnection<BackendInfo> connectingConn = new ConnectingClientConnection<>(context, 0L,
49                 backend.getName());
50         final ConnectedClientConnection<BackendInfo> connectedConn =
51                 new ConnectedClientConnection<>(connectingConn, backend);
52         return new ReconnectingClientConnection<>(connectedConn, mock(RequestException.class));
53     }
54
55     @Override
56     @Test
57     public void testReconnectConnection() {
58         final ClientActorBehavior<BackendInfo> behavior = mock(ClientActorBehavior.class);
59         assertSame(behavior, connection.lockedReconnect(behavior, mock(RequestException.class)));
60     }
61
62     @Override
63     @Test
64     public void testSendRequestReceiveResponse() {
65         final Consumer<Response<?, ?>> callback = mock(Consumer.class);
66         final Request<?, ?> request = createRequest(replyToProbe.ref());
67         connection.sendRequest(request, callback);
68         backendProbe.expectNoMessage();
69         final LocalHistoryIdentifier historyId = new LocalHistoryIdentifier(CLIENT_ID, 0L);
70         final RequestSuccess<?, ?> message = new TransactionAbortSuccess(new TransactionIdentifier(historyId, 0L), 0L);
71         final ResponseEnvelope<?> envelope = new SuccessEnvelope(message, 0L, 0L, 0L);
72         connection.receiveResponse(envelope);
73         verify(callback, after(1000).never()).accept(any());
74     }
75 }