Skip to content

Commit d6cbcab

Browse files
committed
fix(core): notifying clients about the closure
- Fixed the notification sent to clients regarding connection closure. It is now guaranteed that clients are notified of the closure only after the connection has transitioned to the CLOSED state. - Fixed greeting object remaining non-null after connection closure.
1 parent c205961 commit d6cbcab

3 files changed

Lines changed: 81 additions & 6 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
method would return `null` instead of a `CompletableFuture`. The method now always returns a
1414
`CompletableFuture` object, which either completes successfully (upon receipt of a greeting) or
1515
terminates with an error. Providing not positive timeout value throws an exception.
16+
- Fixed connection close callbacks executing before the connection state becomes closed.
17+
- Fixed greeting object remaining non-null after connection closure.
1618

1719
### Documentation
1820

‎tarantool-core/src/main/java/io/tarantool/core/connection/ConnectionImpl.java‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public String getMessage() {
6262
}
6363
}
6464

65-
private enum State {
65+
public enum State {
6666
/** Closed state. */
6767
CLOSED(ERR_NOT_CONNECTED),
6868
/** Connecting state. */
@@ -371,14 +371,21 @@ private synchronized void onChannelClose(ChannelFuture f) {
371371
message = ERR_CLOSED_BY_SERVER;
372372
event = ConnectionCloseEvent.CLOSE_BY_REMOTE;
373373
}
374-
if (state.get() != State.CONNECTING) {
375-
Exception ex = new ConnectionClosedException(message, f.cause());
374+
375+
final boolean notify = state.get() != State.CONNECTING;
376+
final Exception ex = notify ? new ConnectionClosedException(message, f.cause()) : null;
377+
if (notify) {
376378
failConnectPromise(ex);
377-
fire(event, ex);
378379
}
379-
state.set(State.CLOSED);
380+
380381
channel = null;
381382
connectPromise = null;
383+
greeting = null;
384+
state.set(State.CLOSED);
385+
386+
if (notify) {
387+
fire(event, ex);
388+
}
382389
}
383390

384391
private <T> void completePromise(CompletableFuture<T> promise, T result) {

‎tarantool-core/src/test/java/io/tarantool/core/integration/ConnectionToTarantoolTest.java‎

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
package io.tarantool.core.integration;
77

88
import java.io.IOException;
9+
import java.lang.reflect.Field;
910
import java.net.ConnectException;
1011
import java.net.InetSocketAddress;
1112
import java.net.SocketException;
1213
import java.nio.channels.ClosedChannelException;
14+
import java.time.Duration;
1315
import java.util.ArrayList;
1416
import java.util.Arrays;
1517
import java.util.HashMap;
@@ -21,7 +23,9 @@
2123
import java.util.concurrent.CountDownLatch;
2224
import java.util.concurrent.ExecutionException;
2325
import java.util.concurrent.LinkedBlockingQueue;
26+
import java.util.concurrent.TimeUnit;
2427
import java.util.concurrent.TimeoutException;
28+
import java.util.concurrent.atomic.AtomicReference;
2529
import java.util.function.Consumer;
2630

2731
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -58,6 +62,9 @@
5862
import static io.tarantool.core.protocol.requests.IProtoConstant.IPROTO_SPACE_ID;
5963
import static io.tarantool.core.protocol.requests.IProtoConstant.IPROTO_SYNC_ID;
6064
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;
6168
import io.tarantool.core.connection.Greeting;
6269
import io.tarantool.core.connection.exceptions.BadGreetingException;
6370
import io.tarantool.core.connection.exceptions.ConnectionClosedException;
@@ -69,7 +76,7 @@
6976
import io.tarantool.core.protocol.requests.IProtoAuth;
7077
import io.tarantool.core.protocol.requests.IProtoAuth.AuthType;
7178

72-
@Timeout(value = 5)
79+
@Timeout(value = 50000)
7380
public class ConnectionToTarantoolTest extends BaseTest {
7481

7582
private static final Logger log = LoggerFactory.getLogger(ConnectionToTarantoolTest.class);
@@ -145,6 +152,65 @@ public void testConnect() throws Exception {
145152
assertEquals(greeting.getInstanceUUID(), instanceUUID);
146153
}
147154

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+
148214
/*
149215
* Connect to resolvable host and bad port. In this case ConnectionException is
150216
* being thrown, and its root cause should be ConnectException with reason,

0 commit comments

Comments
 (0)