use of com.mongodb.connection.ClusterId in project mongo-java-driver by mongodb.
the class LoadBalancedClusterTest method asynchronousConcurrentTest.
@RepeatedTest(value = 10, name = RepeatedTest.LONG_DISPLAY_NAME)
@Tag("Slow")
public void asynchronousConcurrentTest() throws InterruptedException, ExecutionException, TimeoutException {
String srvHostName = "foo.bar.com";
ServerAddress resolvedServerAddress = new ServerAddress("host1");
ClusterableServer expectedServer = mock(ClusterableServer.class);
ClusterSettings clusterSettings = ClusterSettings.builder().serverSelectionTimeout(5, MILLISECONDS).mode(ClusterConnectionMode.LOAD_BALANCED).srvHost(srvHostName).build();
ClusterableServerFactory serverFactory = mockServerFactory(resolvedServerAddress, expectedServer);
Duration srvResolutionTime = Duration.ofSeconds(5);
DnsSrvRecordMonitorFactory dnsSrvRecordMonitorFactory = mock(DnsSrvRecordMonitorFactory.class);
AtomicReference<TestDnsSrvRecordMonitor> dnsSrvRecordMonitorReference = new AtomicReference<>();
when(dnsSrvRecordMonitorFactory.create(eq(srvHostName), eq(clusterSettings.getSrvServiceName()), any())).thenAnswer(invocation -> {
TestDnsSrvRecordMonitor dnsSrvRecordMonitor = new TestDnsSrvRecordMonitor(invocation.getArgument(2)).sleepTime(srvResolutionTime);
dnsSrvRecordMonitorReference.set(dnsSrvRecordMonitor);
return dnsSrvRecordMonitor;
});
cluster = new LoadBalancedCluster(new ClusterId(), clusterSettings, serverFactory, dnsSrvRecordMonitorFactory);
int numThreads = 10;
List<List<FutureResultCallback<ServerTuple>>> callbacksList = new ArrayList<>(numThreads);
ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
List<Future<?>> futures = new ArrayList<>(numThreads);
for (int i = 0; i < numThreads; i++) {
List<FutureResultCallback<ServerTuple>> callbacks = new ArrayList<>();
callbacksList.add(callbacks);
futures.add(executorService.submit(() -> {
while (!dnsSrvRecordMonitorReference.get().isInitialized()) {
FutureResultCallback<ServerTuple> callback = new FutureResultCallback<>();
callbacks.add(callback);
cluster.selectServerAsync(mock(ServerSelector.class), callback);
}
// Keep going for a little while
for (int j = 0; j < 100; j++) {
FutureResultCallback<ServerTuple> callback = new FutureResultCallback<>();
callbacks.add(callback);
cluster.selectServerAsync(mock(ServerSelector.class), callback);
}
}));
}
for (Future<?> future : futures) {
future.get(10, SECONDS);
}
executorService.shutdownNow();
for (List<FutureResultCallback<ServerTuple>> callbacks : callbacksList) {
boolean foundFirstNonExceptionResult = false;
for (FutureResultCallback<ServerTuple> curCallback : callbacks) {
assertFalse(curCallback.wasInvokedMultipleTimes());
assertTrue(curCallback.isDone());
if (!curCallback.isCompletedExceptionally()) {
foundFirstNonExceptionResult = true;
}
if (foundFirstNonExceptionResult) {
assertFalse(curCallback.isCompletedExceptionally());
}
}
}
}
use of com.mongodb.connection.ClusterId in project mongo-java-driver by mongodb.
the class LoadBalancedClusterTest method shouldTimeoutSelectServerAsynchronouslyWhenThereIsSRVLookupException.
@Test
public void shouldTimeoutSelectServerAsynchronouslyWhenThereIsSRVLookupException() {
// given
String srvHostName = "foo.bar.com";
ServerAddress resolvedServerAddress = new ServerAddress("host1");
ClusterableServer expectedServer = mock(ClusterableServer.class);
ClusterSettings clusterSettings = ClusterSettings.builder().serverSelectionTimeout(10, MILLISECONDS).mode(ClusterConnectionMode.LOAD_BALANCED).srvHost(srvHostName).build();
ClusterableServerFactory serverFactory = mockServerFactory(resolvedServerAddress, expectedServer);
DnsSrvRecordMonitorFactory dnsSrvRecordMonitorFactory = mock(DnsSrvRecordMonitorFactory.class);
when(dnsSrvRecordMonitorFactory.create(eq(srvHostName), eq(clusterSettings.getSrvServiceName()), any())).thenAnswer(invocation -> new TestDnsSrvRecordMonitor(invocation.getArgument(2)).sleepTime(Duration.ofMillis(1)).exception(new MongoConfigurationException("Unable to resolve SRV record")));
cluster = new LoadBalancedCluster(new ClusterId(), clusterSettings, serverFactory, dnsSrvRecordMonitorFactory);
FutureResultCallback<ServerTuple> callback = new FutureResultCallback<>();
cluster.selectServerAsync(mock(ServerSelector.class), callback);
MongoTimeoutException exception = assertThrows(MongoTimeoutException.class, callback::get);
assertEquals("Timed out after 10 ms while waiting to resolve SRV records for foo.bar.com. " + "Resolution exception was 'com.mongodb.MongoConfigurationException: Unable to resolve SRV record'", exception.getMessage());
}
use of com.mongodb.connection.ClusterId in project mongo-java-driver by mongodb.
the class LoadBalancedClusterTest method shouldCloseServerWhenClosing.
@Test
void shouldCloseServerWhenClosing() {
// prepare mocks
ClusterableServerFactory serverFactory = mock(ClusterableServerFactory.class);
when(serverFactory.getSettings()).thenReturn(mock(ServerSettings.class));
ClusterableServer server = mock(ClusterableServer.class);
when(serverFactory.create(any(), any())).thenReturn(server);
// create `cluster` and check that it creates a `ClusterableServer`
LoadBalancedCluster cluster = new LoadBalancedCluster(new ClusterId(), ClusterSettings.builder().mode(ClusterConnectionMode.LOAD_BALANCED).build(), serverFactory, mock(DnsSrvRecordMonitorFactory.class));
verify(serverFactory, times(1)).create(any(), any());
// close `cluster` and check that it closes `server`
cluster.close();
verify(server, atLeastOnce()).close();
}
use of com.mongodb.connection.ClusterId in project mongo-java-driver by mongodb.
the class X509AuthenticatorNoUserNameTest method before.
@Before
public void before() {
connection = new TestInternalConnection(new ServerId(new ClusterId(), new ServerAddress("localhost", 27017)));
connectionDescriptionThreeTwo = new ConnectionDescription(new ConnectionId(new ServerId(new ClusterId(), new ServerAddress())), 4, ServerType.STANDALONE, 1000, 16000, 48000, Collections.<String>emptyList());
connectionDescriptionThreeFour = new ConnectionDescription(new ConnectionId(new ServerId(new ClusterId(), new ServerAddress())), 5, ServerType.STANDALONE, 1000, 16000, 48000, Collections.<String>emptyList());
}
Aggregations