|
7 | 7 | import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
|
8 | 8 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
9 | 9 | import static org.junit.jupiter.api.Assertions.fail;
|
| 10 | +import static org.tarantool.TestUtils.findCause; |
10 | 11 | import static org.tarantool.TestUtils.makeDefaultClientConfig;
|
11 | 12 | import static org.tarantool.TestUtils.makeTestClient;
|
12 | 13 |
|
|
27 | 28 | import java.util.concurrent.TimeUnit;
|
28 | 29 | import java.util.concurrent.atomic.AtomicBoolean;
|
29 | 30 | import java.util.concurrent.atomic.AtomicInteger;
|
| 31 | +import java.util.concurrent.atomic.AtomicReference; |
30 | 32 | import java.util.concurrent.atomic.AtomicReferenceArray;
|
31 | 33 | import java.util.concurrent.locks.LockSupport;
|
32 | 34 |
|
@@ -412,13 +414,86 @@ public void testReconnectWrongAuth() throws Exception {
|
412 | 414 | client.close();
|
413 | 415 | }
|
414 | 416 |
|
415 |
| - private TestSocketChannelProvider makeZeroLingerProvider() { |
| 417 | + @Test |
| 418 | + void testFirstConnectionRefused() { |
| 419 | + RuntimeException error = new RuntimeException("Fake error"); |
| 420 | + TarantoolClientConfig config = makeDefaultClientConfig(); |
| 421 | + config.initTimeoutMillis = 100; |
| 422 | + Throwable exception = assertThrows( |
| 423 | + CommunicationException.class, |
| 424 | + () -> new TarantoolClientImpl(makeErroredProvider(error), config) |
| 425 | + ); |
| 426 | + assertTrue(findCause(exception, error)); |
| 427 | + } |
| 428 | + |
| 429 | + @Test |
| 430 | + void testConnectionRefusedAfterConnect() { |
| 431 | + TarantoolClientImpl client = new TarantoolClientImpl(makeErroredProvider(null), makeDefaultClientConfig()); |
| 432 | + client.ping(); |
| 433 | + |
| 434 | + testHelper.stopInstance(); |
| 435 | + CommunicationException exception = assertThrows(CommunicationException.class, client::ping); |
| 436 | + |
| 437 | + Throwable origin = exception.getCause(); |
| 438 | + assertEquals(origin, client.getThumbstone()); |
| 439 | + |
| 440 | + testHelper.startInstance(); |
| 441 | + } |
| 442 | + |
| 443 | + @Test |
| 444 | + void testSingleSocketProviderRefused() { |
| 445 | + TarantoolClientConfig config = makeDefaultClientConfig(); |
| 446 | + RuntimeException error = new RuntimeException("Fake error"); |
| 447 | + config.initTimeoutMillis = 1000; |
| 448 | + |
| 449 | + SingleSocketChannelProviderImpl socketProvider = new SingleSocketChannelProviderImpl("localhost:3301"); |
| 450 | + |
| 451 | + testHelper.stopInstance(); |
| 452 | + Throwable exception = assertThrows( |
| 453 | + CommunicationException.class, |
| 454 | + () -> new TarantoolClientImpl(TestUtils.wrapByErroredProvider(socketProvider, error), config) |
| 455 | + ); |
| 456 | + testHelper.startInstance(); |
| 457 | + assertTrue(findCause(exception, error)); |
| 458 | + } |
| 459 | + |
| 460 | + @Test |
| 461 | + void testSingleSocketProviderRefusedAfterConnect() { |
| 462 | + TarantoolClientImpl client = new TarantoolClientImpl(socketChannelProvider, makeDefaultClientConfig()); |
| 463 | + |
| 464 | + client.ping(); |
| 465 | + testHelper.stopInstance(); |
| 466 | + |
| 467 | + CommunicationException exception = assertThrows(CommunicationException.class, client::ping); |
| 468 | + Throwable origin = exception.getCause(); |
| 469 | + assertEquals(origin, client.getThumbstone()); |
| 470 | + |
| 471 | + testHelper.startInstance(); |
| 472 | + } |
| 473 | + |
| 474 | + private SocketChannelProvider makeZeroLingerProvider() { |
416 | 475 | return new TestSocketChannelProvider(
|
417 | 476 | TarantoolTestHelper.HOST, TarantoolTestHelper.PORT, RESTART_TIMEOUT
|
418 | 477 | ).setSoLinger(0);
|
419 | 478 | }
|
420 | 479 |
|
421 |
| - TarantoolClient makeClient(SocketChannelProvider provider) { |
| 480 | + private SocketChannelProvider makeErroredProvider(RuntimeException error) { |
| 481 | + return new SocketChannelProvider() { |
| 482 | + private final SocketChannelProvider delegate = makeZeroLingerProvider(); |
| 483 | + private AtomicReference<RuntimeException> errorReference = new AtomicReference<>(error); |
| 484 | + |
| 485 | + @Override |
| 486 | + public SocketChannel get(int retryNumber, Throwable lastError) { |
| 487 | + RuntimeException rawError = errorReference.get(); |
| 488 | + if (rawError != null) { |
| 489 | + throw rawError; |
| 490 | + } |
| 491 | + return delegate.get(retryNumber, lastError); |
| 492 | + } |
| 493 | + }; |
| 494 | + } |
| 495 | + |
| 496 | + private TarantoolClient makeClient(SocketChannelProvider provider) { |
422 | 497 | return new TarantoolClientImpl(provider, makeDefaultClientConfig());
|
423 | 498 | }
|
424 | 499 |
|
|
0 commit comments