|
| 1 | +/* |
| 2 | + * !++ |
| 3 | + * QDS - Quick Data Signalling Library |
| 4 | + * !- |
| 5 | + * Copyright (C) 2002 - 2019 Devexperts LLC |
| 6 | + * !- |
| 7 | + * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 8 | + * If a copy of the MPL was not distributed with this file, You can obtain one at |
| 9 | + * http://mozilla.org/MPL/2.0/. |
| 10 | + * !__ |
| 11 | + */ |
| 12 | +package com.dxfeed.api.test; |
| 13 | + |
| 14 | +import com.devexperts.logging.Logging; |
| 15 | +import com.devexperts.test.ThreadCleanCheck; |
| 16 | +import com.dxfeed.api.DXEndpoint; |
| 17 | +import org.junit.After; |
| 18 | +import org.junit.Before; |
| 19 | +import org.junit.Test; |
| 20 | + |
| 21 | +import java.beans.PropertyChangeEvent; |
| 22 | +import java.util.concurrent.CountDownLatch; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | + |
| 25 | +import static org.junit.Assert.assertEquals; |
| 26 | +import static org.junit.Assert.assertTrue; |
| 27 | + |
| 28 | +public class DXEndpointListenerTest { |
| 29 | + private static final Logging log = Logging.getLogging(DXEndpointListenerTest.class); |
| 30 | + private static final int AWAIT_TIMEOUT = 10_000; |
| 31 | + |
| 32 | + private volatile CountDownLatch pass; |
| 33 | + private volatile DXEndpoint.State expectedState; |
| 34 | + |
| 35 | + @Before |
| 36 | + public void setUp() throws Exception { |
| 37 | + ThreadCleanCheck.before(); |
| 38 | + } |
| 39 | + |
| 40 | + @After |
| 41 | + public void tearDown() throws Exception { |
| 42 | + ThreadCleanCheck.after(); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testSimpleListener() throws InterruptedException { |
| 47 | + DXEndpoint endpoint = DXEndpoint.create(DXEndpoint.Role.PUBLISHER); |
| 48 | + pass = new CountDownLatch(1); |
| 49 | + expectedState = DXEndpoint.State.CONNECTED; |
| 50 | + endpoint.addStateChangeListener(evt -> { |
| 51 | + logChangeEvent(evt); |
| 52 | + if (evt.getNewValue() == expectedState) |
| 53 | + pass.countDown(); |
| 54 | + }); |
| 55 | + endpoint.connect(":0"); |
| 56 | + await("CONNECTED state reached", pass); |
| 57 | + pass = new CountDownLatch(1); |
| 58 | + expectedState = DXEndpoint.State.NOT_CONNECTED; |
| 59 | + endpoint.disconnect(); |
| 60 | + await("NOT_CONNECTED state reached", pass); |
| 61 | + pass = new CountDownLatch(1); |
| 62 | + expectedState = DXEndpoint.State.CLOSED; |
| 63 | + endpoint.close(); |
| 64 | + await("CLOSED state reached", pass); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testDisconnectFromListener() throws InterruptedException { |
| 69 | + DXEndpoint endpoint = DXEndpoint.create(DXEndpoint.Role.PUBLISHER); |
| 70 | + endpoint.addStateChangeListener(evt -> { |
| 71 | + logChangeEvent(evt); |
| 72 | + if (evt.getNewValue() == DXEndpoint.State.CONNECTED) { |
| 73 | + endpoint.disconnect(); |
| 74 | + } |
| 75 | + }); |
| 76 | + endpoint.connect(":0"); |
| 77 | + endpoint.awaitNotConnected(); |
| 78 | + endpoint.close(); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testInnerAwaitNotConnected() throws InterruptedException { |
| 83 | + DXEndpoint endpoint = DXEndpoint.newBuilder() |
| 84 | + .withRole(DXEndpoint.Role.PUBLISHER) |
| 85 | + .withProperty(DXEndpoint.DXFEED_THREAD_POOL_SIZE_PROPERTY, "4") |
| 86 | + .build(); |
| 87 | + pass = new CountDownLatch(1); |
| 88 | + CountDownLatch innerWaitSucceded = new CountDownLatch(1); |
| 89 | + endpoint.addStateChangeListener(evt -> { |
| 90 | + logChangeEvent(evt); |
| 91 | + if (evt.getNewValue() == DXEndpoint.State.CONNECTED) { |
| 92 | + pass.countDown(); |
| 93 | + log.info("Inner wait for disconnect..."); |
| 94 | + try { |
| 95 | + endpoint.awaitNotConnected(); |
| 96 | + } catch (InterruptedException e) { |
| 97 | + e.printStackTrace(); |
| 98 | + } |
| 99 | + innerWaitSucceded.countDown(); |
| 100 | + } |
| 101 | + }); |
| 102 | + endpoint.connect(":0"); |
| 103 | + await("CONNECTED state reached", pass); |
| 104 | + log.info("Disconnecting..."); |
| 105 | + endpoint.disconnect(); |
| 106 | + await("NOT_CONNECTED witnessed by inner wait", innerWaitSucceded); |
| 107 | + log.info("Closing..."); |
| 108 | + endpoint.close(); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testInnerAwaitClosed() throws InterruptedException { |
| 113 | + DXEndpoint endpoint = DXEndpoint.newBuilder() |
| 114 | + .withRole(DXEndpoint.Role.PUBLISHER) |
| 115 | + .withProperty(DXEndpoint.DXFEED_THREAD_POOL_SIZE_PROPERTY, "4") |
| 116 | + .build(); |
| 117 | + pass = new CountDownLatch(1); |
| 118 | + CountDownLatch innerWaitSucceded = new CountDownLatch(1); |
| 119 | + endpoint.addStateChangeListener(evt -> { |
| 120 | + logChangeEvent(evt); |
| 121 | + if (evt.getNewValue() == DXEndpoint.State.CONNECTED) { |
| 122 | + pass.countDown(); |
| 123 | + } |
| 124 | + if (evt.getNewValue() == DXEndpoint.State.NOT_CONNECTED) { |
| 125 | + pass.countDown(); |
| 126 | + log.info("Inner wait for disconnect..."); |
| 127 | + try { |
| 128 | + endpoint.closeAndAwaitTermination(); |
| 129 | + } catch (InterruptedException e) { |
| 130 | + e.printStackTrace(); |
| 131 | + } |
| 132 | + innerWaitSucceded.countDown(); |
| 133 | + } |
| 134 | + }); |
| 135 | + endpoint.connect(":0"); |
| 136 | + // It's important to get connected state here. |
| 137 | + // Fast disconnect may cause that listener will never be notified by current contract. |
| 138 | + await("CONNECTED state reached", pass); |
| 139 | + pass = new CountDownLatch(1); |
| 140 | + log.info("Disconnecting..."); |
| 141 | + endpoint.disconnect(); |
| 142 | + await("NOT_CONNECTED state reached", pass); |
| 143 | + log.info("Waiting inner close..."); |
| 144 | + await("Closed by inner wait", innerWaitSucceded); |
| 145 | + assertEquals(DXEndpoint.State.CLOSED, endpoint.getState()); |
| 146 | + } |
| 147 | + |
| 148 | + private void await(String message, CountDownLatch pass) throws InterruptedException { |
| 149 | + assertTrue(message, pass.await(AWAIT_TIMEOUT, TimeUnit.MILLISECONDS)); |
| 150 | + } |
| 151 | + |
| 152 | + private void logChangeEvent(PropertyChangeEvent evt) { |
| 153 | + log.info("****** " + evt.getPropertyName() + ": " + evt.getOldValue() + " -> " + evt.getNewValue()); |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments