Skip to content

Commit 7dd092e

Browse files
committed
fix(core): inconsistency connection state
In the current version of the code, if a timeout value less than or equal to 0 is passed, an exception is thrown, but the connection remains stuck in the `CONNECTING` state. Subsequent calls to the `Connection#connect(...)` method on the same object return `null` instead of a proper `CompletableFuture<Greeting>`. In other words, failures or errors during the execution of `Connection#connect(...)` should not leave the `Connection` object in an inconsistent state. Subsequent calls should be handled correctly; the method must return a `CompletableFuture<Greeting>`—either one that completes successfully or one that contains an error, as per the contract of the `CompletionStage<T>` interface.
1 parent 00205cd commit 7dd092e

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ public Connection listen(Consumer<IProtoResponse> listener) {
181181
@Override
182182
public synchronized CompletableFuture<Greeting> connect(InetSocketAddress address, long timeoutMs)
183183
throws IllegalStateException {
184+
if (timeoutMs <= 0) {
185+
throw new IllegalStateException("Timeout must be greater than zero!");
186+
}
187+
184188
if (state.compareAndSet(State.CLOSED, State.CONNECTING)) {
185189
closedBy.set(CloseReason.NO_REASON);
186190
log.debug("{} is connecting to {}", this, address);
@@ -407,9 +411,6 @@ private void fire(ConnectionCloseEvent event, Throwable ex) {
407411

408412
private void scheduleGreetingTimeout(CompletableFuture<Greeting> promise, long timeoutMs) {
409413
log.debug("Schedule timeout for connect in milliseconds: {}", timeoutMs);
410-
if (timeoutMs <= 0) {
411-
throw new IllegalStateException("Timeout must be greater than zero!");
412-
}
413414
timeoutHandler =
414415
this.timerService.newTimeout(
415416
timeoutHandler -> {

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import static org.msgpack.value.ValueFactory.newMap;
3535
import io.netty.channel.ConnectTimeoutException;
3636
import org.junit.jupiter.api.AfterAll;
37+
import org.junit.jupiter.api.Assertions;
3738
import org.junit.jupiter.api.BeforeAll;
3839
import org.junit.jupiter.api.RepeatedTest;
3940
import org.junit.jupiter.api.Test;
@@ -390,6 +391,36 @@ public void testConnectAndSendByMultipleThreads() throws InterruptedException {
390391
assertEquals(CONCURRENT_THREADS_COUNT, success);
391392
}
392393

394+
/**
395+
* This test checks for an inconsistent state caused by passing a non-positive timeout during a
396+
* connection attempt, followed by a subsequent attempt to connect.
397+
*/
398+
@Test
399+
void testConnectWithNonPositiveTimeoutForFirstAttempt() throws Exception {
400+
try (Connection connection = factory.create()) {
401+
InetSocketAddress address = tt.mappedAddress();
402+
403+
boolean errorOccurred = false;
404+
try {
405+
connection.connect(address, -1L).join();
406+
} catch (IllegalStateException e) {
407+
errorOccurred = true;
408+
}
409+
Assertions.assertTrue(errorOccurred, "Error must be occurred!");
410+
411+
final CompletableFuture<Greeting> connectionFuture = connection.connect(address, 1_000L);
412+
Assertions.assertNotNull(connectionFuture, "Connection future must be not null!");
413+
414+
final Greeting greeting = connectionFuture.join();
415+
Assertions.assertNotNull(greeting, "Greeting must be not null!");
416+
Assertions.assertTrue(connection.isConnected(), "Connection must be connected");
417+
418+
final Greeting secondGreeting = connection.connect(address, 5_000L).join();
419+
420+
Assertions.assertSame(greeting, secondGreeting, "Must be same!");
421+
}
422+
}
423+
393424
private IProtoRequest createSelectRequest(int syncId) {
394425
Map<Value, Value> rawHeader = new HashMap<>();
395426
rawHeader.put(newInteger(IPROTO_REQUEST_TYPE), newInteger(IPROTO_SELECT));

0 commit comments

Comments
 (0)