|
27 | 27 | import java.util.concurrent.TimeUnit;
|
28 | 28 | import java.util.concurrent.atomic.AtomicBoolean;
|
29 | 29 | import java.util.concurrent.atomic.AtomicInteger;
|
| 30 | +import java.util.concurrent.atomic.AtomicReference; |
30 | 31 | import java.util.concurrent.atomic.AtomicReferenceArray;
|
31 | 32 | import java.util.concurrent.locks.LockSupport;
|
32 | 33 |
|
@@ -412,13 +413,59 @@ public void testReconnectWrongAuth() throws Exception {
|
412 | 413 | client.close();
|
413 | 414 | }
|
414 | 415 |
|
415 |
| - private TestSocketChannelProvider makeZeroLingerProvider() { |
| 416 | + @Test |
| 417 | + void testFirstConnectionRefused() { |
| 418 | + String errorText = "Fake error"; |
| 419 | + RuntimeException error = new RuntimeException(errorText); |
| 420 | + TarantoolClientConfig config = makeDefaultClientConfig(); |
| 421 | + config.initTimeoutMillis = 100; |
| 422 | + CommunicationException exception = assertThrows( |
| 423 | + CommunicationException.class, |
| 424 | + () -> new TarantoolClientImpl(makeErroredProvider(error), config) |
| 425 | + ); |
| 426 | + Throwable wrapped = exception.getCause(); |
| 427 | + Throwable origin = wrapped.getCause(); |
| 428 | + assertEquals(errorText, wrapped.getMessage()); |
| 429 | + assertEquals(error, origin); |
| 430 | + } |
| 431 | + |
| 432 | + @Test |
| 433 | + void testConnectionRefusedAfterConnect() { |
| 434 | + TarantoolClientImpl client = new TarantoolClientImpl(makeErroredProvider(null), makeDefaultClientConfig()); |
| 435 | + client.ping(); |
| 436 | + |
| 437 | + testHelper.stopInstance(); |
| 438 | + CommunicationException exception = assertThrows(CommunicationException.class, client::ping); |
| 439 | + |
| 440 | + Throwable origin = exception.getCause(); |
| 441 | + assertEquals(origin, client.getThumbstone()); |
| 442 | + |
| 443 | + testHelper.startInstance(); |
| 444 | + } |
| 445 | + |
| 446 | + private SocketChannelProvider makeZeroLingerProvider() { |
416 | 447 | return new TestSocketChannelProvider(
|
417 | 448 | TarantoolTestHelper.HOST, TarantoolTestHelper.PORT, RESTART_TIMEOUT
|
418 | 449 | ).setSoLinger(0);
|
419 | 450 | }
|
420 | 451 |
|
421 |
| - TarantoolClient makeClient(SocketChannelProvider provider) { |
| 452 | + private SocketChannelProvider makeErroredProvider(RuntimeException error) { |
| 453 | + return new SocketChannelProvider() { |
| 454 | + private final SocketChannelProvider delegate = makeZeroLingerProvider(); |
| 455 | + private AtomicReference<RuntimeException> errorReference = new AtomicReference<>(error); |
| 456 | + |
| 457 | + @Override |
| 458 | + public SocketChannel get(int retryNumber, Throwable lastError) { |
| 459 | + RuntimeException rawError = errorReference.get(); |
| 460 | + if (rawError != null) { |
| 461 | + throw rawError; |
| 462 | + } |
| 463 | + return delegate.get(retryNumber, lastError); |
| 464 | + } |
| 465 | + }; |
| 466 | + } |
| 467 | + |
| 468 | + private TarantoolClient makeClient(SocketChannelProvider provider) { |
422 | 469 | return new TarantoolClientImpl(provider, makeDefaultClientConfig());
|
423 | 470 | }
|
424 | 471 |
|
|
0 commit comments