|
6 | 6 | package io.tarantool.core.integration; |
7 | 7 |
|
8 | 8 | import java.io.IOException; |
| 9 | +import java.lang.reflect.Field; |
9 | 10 | import java.net.ConnectException; |
10 | 11 | import java.net.InetSocketAddress; |
11 | 12 | import java.net.SocketException; |
12 | 13 | import java.nio.channels.ClosedChannelException; |
| 14 | +import java.time.Duration; |
13 | 15 | import java.util.ArrayList; |
14 | 16 | import java.util.Arrays; |
15 | 17 | import java.util.HashMap; |
|
21 | 23 | import java.util.concurrent.CountDownLatch; |
22 | 24 | import java.util.concurrent.ExecutionException; |
23 | 25 | import java.util.concurrent.LinkedBlockingQueue; |
| 26 | +import java.util.concurrent.TimeUnit; |
24 | 27 | import java.util.concurrent.TimeoutException; |
| 28 | +import java.util.concurrent.atomic.AtomicReference; |
25 | 29 | import java.util.function.Consumer; |
26 | 30 |
|
27 | 31 | import static org.junit.jupiter.api.Assertions.assertEquals; |
|
58 | 62 | import static io.tarantool.core.protocol.requests.IProtoConstant.IPROTO_SPACE_ID; |
59 | 63 | import static io.tarantool.core.protocol.requests.IProtoConstant.IPROTO_SYNC_ID; |
60 | 64 | import io.tarantool.core.connection.Connection; |
| 65 | +import io.tarantool.core.connection.ConnectionCloseEvent; |
| 66 | +import io.tarantool.core.connection.ConnectionImpl; |
| 67 | +import io.tarantool.core.connection.ConnectionImpl.State; |
61 | 68 | import io.tarantool.core.connection.Greeting; |
62 | 69 | import io.tarantool.core.connection.exceptions.BadGreetingException; |
63 | 70 | import io.tarantool.core.connection.exceptions.ConnectionClosedException; |
|
69 | 76 | import io.tarantool.core.protocol.requests.IProtoAuth; |
70 | 77 | import io.tarantool.core.protocol.requests.IProtoAuth.AuthType; |
71 | 78 |
|
72 | | -@Timeout(value = 5) |
| 79 | +@Timeout(value = 50000) |
73 | 80 | public class ConnectionToTarantoolTest extends BaseTest { |
74 | 81 |
|
75 | 82 | private static final Logger log = LoggerFactory.getLogger(ConnectionToTarantoolTest.class); |
@@ -145,6 +152,65 @@ public void testConnect() throws Exception { |
145 | 152 | assertEquals(greeting.getInstanceUUID(), instanceUUID); |
146 | 153 | } |
147 | 154 |
|
| 155 | + @Test |
| 156 | + void testFireOnCloseCallbacksInClosedState() throws Throwable { |
| 157 | + try (Connection connection = factory.create()) { |
| 158 | + final CountDownLatch latch = new CountDownLatch(1); |
| 159 | + final InetSocketAddress address = tt.mappedAddress(); |
| 160 | + final AtomicReference<Throwable> ex = new AtomicReference<>(); |
| 161 | + |
| 162 | + connection.onClose( |
| 163 | + ConnectionCloseEvent.CLOSE_BY_CLIENT, |
| 164 | + (c, e) -> { |
| 165 | + try { |
| 166 | + final Field stateField = ConnectionImpl.class.getDeclaredField("state"); |
| 167 | + stateField.setAccessible(true); |
| 168 | + |
| 169 | + Assertions.assertEquals( |
| 170 | + State.CLOSED, ((AtomicReference<State>) stateField.get(connection)).get()); |
| 171 | + |
| 172 | + } catch (Throwable exception) { |
| 173 | + ex.set(exception); |
| 174 | + } finally { |
| 175 | + latch.countDown(); |
| 176 | + } |
| 177 | + }); |
| 178 | + |
| 179 | + connection.connect(address, Duration.ofSeconds(3).toMillis()).join(); |
| 180 | + connection.close(); |
| 181 | + |
| 182 | + if (!latch.await(2, TimeUnit.MINUTES)) { |
| 183 | + Assertions.fail("Test should complete"); |
| 184 | + } |
| 185 | + |
| 186 | + if (ex.get() != null) { |
| 187 | + throw ex.get(); |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + @Test |
| 193 | + void testGreetingNullAfterClosing() throws Exception { |
| 194 | + try (Connection connection = factory.create()) { |
| 195 | + final InetSocketAddress address = tt.mappedAddress(); |
| 196 | + final CountDownLatch latch = new CountDownLatch(1); |
| 197 | + connection.onClose(ConnectionCloseEvent.CLOSE_BY_CLIENT, (c, e) -> latch.countDown()); |
| 198 | + |
| 199 | + Assertions.assertFalse(connection.getGreeting().isPresent(), "Must be empty"); |
| 200 | + connection.connect(address, Duration.ofSeconds(3).toMillis()).join(); |
| 201 | + |
| 202 | + Assertions.assertTrue(connection.getGreeting().isPresent(), "Must not be empty"); |
| 203 | + |
| 204 | + connection.close(); |
| 205 | + |
| 206 | + if (!latch.await(2, TimeUnit.MINUTES)) { |
| 207 | + Assertions.fail("Test must begin"); |
| 208 | + } |
| 209 | + |
| 210 | + Assertions.assertFalse(connection.getGreeting().isPresent(), "Must be empty"); |
| 211 | + } |
| 212 | + } |
| 213 | + |
148 | 214 | /* |
149 | 215 | * Connect to resolvable host and bad port. In this case ConnectionException is |
150 | 216 | * being thrown, and its root cause should be ConnectException with reason, |
|
0 commit comments