|
1 | 1 | package org.tarantool;
|
2 | 2 |
|
3 |
| -import static org.junit.jupiter.api.Assertions.assertEquals; |
4 |
| -import static org.junit.jupiter.api.Assertions.assertFalse; |
5 |
| -import static org.junit.jupiter.api.Assertions.assertNotNull; |
6 |
| -import static org.junit.jupiter.api.Assertions.assertThrows; |
7 |
| -import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; |
8 |
| -import static org.junit.jupiter.api.Assertions.assertTrue; |
9 |
| -import static org.junit.jupiter.api.Assertions.fail; |
10 |
| -import static org.tarantool.TestUtils.makeDefaultClientConfig; |
11 |
| -import static org.tarantool.TestUtils.makeTestClient; |
12 |
| - |
13 | 3 | import org.junit.jupiter.api.AfterEach;
|
14 | 4 | import org.junit.jupiter.api.BeforeAll;
|
15 | 5 | import org.junit.jupiter.api.BeforeEach;
|
16 | 6 | import org.junit.jupiter.api.Test;
|
17 | 7 | import org.junit.jupiter.api.function.Executable;
|
18 | 8 |
|
| 9 | +import java.net.ConnectException; |
19 | 10 | import java.nio.channels.SocketChannel;
|
20 | 11 | import java.time.Duration;
|
21 | 12 | import java.util.Collections;
|
|
27 | 18 | import java.util.concurrent.TimeUnit;
|
28 | 19 | import java.util.concurrent.atomic.AtomicBoolean;
|
29 | 20 | import java.util.concurrent.atomic.AtomicInteger;
|
| 21 | +import java.util.concurrent.atomic.AtomicReference; |
30 | 22 | import java.util.concurrent.atomic.AtomicReferenceArray;
|
31 | 23 | import java.util.concurrent.locks.LockSupport;
|
32 | 24 |
|
| 25 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 28 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 29 | +import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 31 | +import static org.junit.jupiter.api.Assertions.fail; |
| 32 | +import static org.tarantool.TestUtils.findCause; |
| 33 | +import static org.tarantool.TestUtils.makeDefaultClientConfig; |
| 34 | +import static org.tarantool.TestUtils.makeTestClient; |
| 35 | + |
33 | 36 | public class ClientReconnectIT {
|
34 | 37 |
|
35 | 38 | private static final int RESTART_TIMEOUT = 2000;
|
@@ -377,7 +380,7 @@ public void run() {
|
377 | 380 | /**
|
378 | 381 | * Verify that we don't exceed a file descriptor limit (and so likely don't
|
379 | 382 | * leak file descriptors) when trying to connect to an existing node with
|
380 |
| - * wrong authentification credentials. |
| 383 | + * wrong authentication credentials. |
381 | 384 | * <p>
|
382 | 385 | * The test sets SO_LINGER to 0 for outgoing connections to avoid producing
|
383 | 386 | * many TIME_WAIT sockets, because an available port range can be
|
@@ -412,13 +415,103 @@ public void testReconnectWrongAuth() throws Exception {
|
412 | 415 | client.close();
|
413 | 416 | }
|
414 | 417 |
|
415 |
| - private TestSocketChannelProvider makeZeroLingerProvider() { |
| 418 | + @Test |
| 419 | + void testFirstConnectionRefused() { |
| 420 | + RuntimeException error = new RuntimeException("Fake error"); |
| 421 | + TarantoolClientConfig config = makeDefaultClientConfig(); |
| 422 | + config.initTimeoutMillis = 100; |
| 423 | + Throwable exception = assertThrows( |
| 424 | + CommunicationException.class, |
| 425 | + () -> new TarantoolClientImpl(makeErroredProvider(error), config) |
| 426 | + ); |
| 427 | + assertTrue(findCause(exception, error)); |
| 428 | + } |
| 429 | + |
| 430 | + @Test |
| 431 | + void testConnectionRefusedAfterConnect() { |
| 432 | + TarantoolClientImpl client = new TarantoolClientImpl(makeErroredProvider(null), makeDefaultClientConfig()); |
| 433 | + client.ping(); |
| 434 | + |
| 435 | + testHelper.stopInstance(); |
| 436 | + CommunicationException exception = assertThrows(CommunicationException.class, client::ping); |
| 437 | + |
| 438 | + Throwable origin = exception.getCause(); |
| 439 | + assertEquals(origin, client.getThumbstone()); |
| 440 | + |
| 441 | + testHelper.startInstance(); |
| 442 | + } |
| 443 | + |
| 444 | + @Test |
| 445 | + void testSocketProviderRefusedByFakeReason() { |
| 446 | + TarantoolClientConfig config = makeDefaultClientConfig(); |
| 447 | + RuntimeException error = new RuntimeException("Fake error"); |
| 448 | + config.initTimeoutMillis = 1000; |
| 449 | + |
| 450 | + SingleSocketChannelProviderImpl socketProvider = new SingleSocketChannelProviderImpl("localhost:3301"); |
| 451 | + |
| 452 | + testHelper.stopInstance(); |
| 453 | + Throwable exception = assertThrows( |
| 454 | + CommunicationException.class, |
| 455 | + () -> new TarantoolClientImpl(TestUtils.wrapByErroredProvider(socketProvider, error), config) |
| 456 | + ); |
| 457 | + testHelper.startInstance(); |
| 458 | + assertTrue(findCause(exception, error)); |
| 459 | + } |
| 460 | + |
| 461 | + @Test |
| 462 | + void testSingleSocketProviderRefused() { |
| 463 | + testHelper.stopInstance(); |
| 464 | + |
| 465 | + TarantoolClientConfig config = makeDefaultClientConfig(); |
| 466 | + config.initTimeoutMillis = 1000; |
| 467 | + |
| 468 | + SingleSocketChannelProviderImpl socketProvider = new SingleSocketChannelProviderImpl("localhost:3301"); |
| 469 | + |
| 470 | + Throwable exception = assertThrows( |
| 471 | + CommunicationException.class, |
| 472 | + () -> new TarantoolClientImpl(socketProvider, config) |
| 473 | + ); |
| 474 | + testHelper.startInstance(); |
| 475 | + assertTrue(findCause(exception, ConnectException.class)); |
| 476 | + } |
| 477 | + |
| 478 | + @Test |
| 479 | + void testSingleSocketProviderRefusedAfterConnect() { |
| 480 | + TarantoolClientImpl client = new TarantoolClientImpl(socketChannelProvider, makeDefaultClientConfig()); |
| 481 | + |
| 482 | + client.ping(); |
| 483 | + testHelper.stopInstance(); |
| 484 | + |
| 485 | + CommunicationException exception = assertThrows(CommunicationException.class, client::ping); |
| 486 | + Throwable origin = exception.getCause(); |
| 487 | + assertEquals(origin, client.getThumbstone()); |
| 488 | + |
| 489 | + testHelper.startInstance(); |
| 490 | + } |
| 491 | + |
| 492 | + private SocketChannelProvider makeZeroLingerProvider() { |
416 | 493 | return new TestSocketChannelProvider(
|
417 | 494 | TarantoolTestHelper.HOST, TarantoolTestHelper.PORT, RESTART_TIMEOUT
|
418 | 495 | ).setSoLinger(0);
|
419 | 496 | }
|
420 | 497 |
|
421 |
| - TarantoolClient makeClient(SocketChannelProvider provider) { |
| 498 | + private SocketChannelProvider makeErroredProvider(RuntimeException error) { |
| 499 | + return new SocketChannelProvider() { |
| 500 | + private final SocketChannelProvider delegate = makeZeroLingerProvider(); |
| 501 | + private AtomicReference<RuntimeException> errorReference = new AtomicReference<>(error); |
| 502 | + |
| 503 | + @Override |
| 504 | + public SocketChannel get(int retryNumber, Throwable lastError) { |
| 505 | + RuntimeException rawError = errorReference.get(); |
| 506 | + if (rawError != null) { |
| 507 | + throw rawError; |
| 508 | + } |
| 509 | + return delegate.get(retryNumber, lastError); |
| 510 | + } |
| 511 | + }; |
| 512 | + } |
| 513 | + |
| 514 | + private TarantoolClient makeClient(SocketChannelProvider provider) { |
422 | 515 | return new TarantoolClientImpl(provider, makeDefaultClientConfig());
|
423 | 516 | }
|
424 | 517 |
|
|
0 commit comments