|
35 | 35 | import org.junit.jupiter.api.RepeatedTest;
|
36 | 36 | import org.junit.jupiter.api.Tag;
|
37 | 37 | import org.junit.jupiter.api.Test;
|
| 38 | +import org.mockito.ArgumentCaptor; |
38 | 39 |
|
39 | 40 | import java.time.Duration;
|
40 | 41 | import java.util.ArrayList;
|
|
57 | 58 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
58 | 59 | import static org.mockito.ArgumentMatchers.any;
|
59 | 60 | import static org.mockito.ArgumentMatchers.eq;
|
| 61 | +import static org.mockito.Mockito.atLeastOnce; |
60 | 62 | import static org.mockito.Mockito.mock;
|
| 63 | +import static org.mockito.Mockito.never; |
| 64 | +import static org.mockito.Mockito.times; |
| 65 | +import static org.mockito.Mockito.verify; |
61 | 66 | import static org.mockito.Mockito.when;
|
62 | 67 |
|
63 | 68 | public class LoadBalancedClusterTest {
|
@@ -317,6 +322,42 @@ public void shouldTimeoutSelectServerAsynchronouslyWhenThereIsSRVLookupException
|
317 | 322 | exception.getMessage());
|
318 | 323 | }
|
319 | 324 |
|
| 325 | + @Test |
| 326 | + void shouldNotInitServerAfterClosing() { |
| 327 | + // prepare mocks |
| 328 | + ClusterableServerFactory serverFactory = mock(ClusterableServerFactory.class); |
| 329 | + when(serverFactory.getSettings()).thenReturn(mock(ServerSettings.class)); |
| 330 | + DnsSrvRecordMonitorFactory srvRecordMonitorFactory = mock(DnsSrvRecordMonitorFactory.class); |
| 331 | + when(srvRecordMonitorFactory.create(any(), any(DnsSrvRecordInitializer.class))).thenReturn(mock(DnsSrvRecordMonitor.class)); |
| 332 | + ArgumentCaptor<DnsSrvRecordInitializer> serverInitializerCaptor = ArgumentCaptor.forClass(DnsSrvRecordInitializer.class); |
| 333 | + // create `cluster` and capture its `DnsSrvRecordInitializer` (server initializer) |
| 334 | + LoadBalancedCluster cluster = new LoadBalancedCluster(new ClusterId(), |
| 335 | + ClusterSettings.builder().mode(ClusterConnectionMode.LOAD_BALANCED).srvHost("foo.bar.com").build(), |
| 336 | + serverFactory, srvRecordMonitorFactory); |
| 337 | + verify(srvRecordMonitorFactory, times(1)).create(any(), serverInitializerCaptor.capture()); |
| 338 | + // close `cluster`, call `DnsSrvRecordInitializer.initialize` and check that it does not result in creating a `ClusterableServer` |
| 339 | + cluster.close(); |
| 340 | + serverInitializerCaptor.getValue().initialize(Collections.singleton(new ServerAddress())); |
| 341 | + verify(serverFactory, never()).create(any(), any(), any(), any()); |
| 342 | + } |
| 343 | + |
| 344 | + @Test |
| 345 | + void shouldCloseServerWhenClosing() { |
| 346 | + // prepare mocks |
| 347 | + ClusterableServerFactory serverFactory = mock(ClusterableServerFactory.class); |
| 348 | + when(serverFactory.getSettings()).thenReturn(mock(ServerSettings.class)); |
| 349 | + ClusterableServer server = mock(ClusterableServer.class); |
| 350 | + when(serverFactory.create(any(), any(), any(), any())).thenReturn(server); |
| 351 | + // create `cluster` and check that it creates a `ClusterableServer` |
| 352 | + LoadBalancedCluster cluster = new LoadBalancedCluster(new ClusterId(), |
| 353 | + ClusterSettings.builder().mode(ClusterConnectionMode.LOAD_BALANCED).build(), serverFactory, |
| 354 | + mock(DnsSrvRecordMonitorFactory.class)); |
| 355 | + verify(serverFactory, times(1)).create(any(), any(), any(), any()); |
| 356 | + // close `cluster` and check that it closes `server` |
| 357 | + cluster.close(); |
| 358 | + verify(server, atLeastOnce()).close(); |
| 359 | + } |
| 360 | + |
320 | 361 | @RepeatedTest(value = 10, name = RepeatedTest.LONG_DISPLAY_NAME)
|
321 | 362 | @Tag("Slow")
|
322 | 363 | public void synchronousConcurrentTest() throws InterruptedException, ExecutionException, TimeoutException {
|
@@ -497,20 +538,17 @@ public boolean isInitialized() {
|
497 | 538 |
|
498 | 539 | @Override
|
499 | 540 | public void start() {
|
500 |
| - thread = new Thread(new Runnable() { |
501 |
| - @Override |
502 |
| - public void run() { |
503 |
| - try { |
504 |
| - Thread.sleep(sleepTime.toMillis()); |
505 |
| - if (exception != null) { |
506 |
| - initializer.initialize(exception); |
507 |
| - } else { |
508 |
| - initializer.initialize(hosts); |
509 |
| - } |
510 |
| - initialized = true; |
511 |
| - } catch (InterruptedException e) { |
512 |
| - // ignore |
| 541 | + thread = new Thread(() -> { |
| 542 | + try { |
| 543 | + Thread.sleep(sleepTime.toMillis()); |
| 544 | + if (exception != null) { |
| 545 | + initializer.initialize(exception); |
| 546 | + } else { |
| 547 | + initializer.initialize(hosts); |
513 | 548 | }
|
| 549 | + initialized = true; |
| 550 | + } catch (InterruptedException e) { |
| 551 | + // ignore |
514 | 552 | }
|
515 | 553 | });
|
516 | 554 | thread.start();
|
|
0 commit comments