Search in sources :

Example 11 with ClusterSettings

use of com.mongodb.connection.ClusterSettings in project mongo-java-driver by mongodb.

the class LoadBalancedClusterTest method shouldTimeoutSelectServerWhenThereIsSRVLookup.

@Test
public void shouldTimeoutSelectServerWhenThereIsSRVLookup() {
    // given
    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);
    DnsSrvRecordMonitorFactory dnsSrvRecordMonitorFactory = mock(DnsSrvRecordMonitorFactory.class);
    when(dnsSrvRecordMonitorFactory.create(eq(srvHostName), eq(clusterSettings.getSrvServiceName()), any())).thenAnswer(invocation -> new TestDnsSrvRecordMonitor(invocation.getArgument(2)).sleepTime(Duration.ofHours(1)));
    cluster = new LoadBalancedCluster(new ClusterId(), clusterSettings, serverFactory, dnsSrvRecordMonitorFactory);
    MongoTimeoutException exception = assertThrows(MongoTimeoutException.class, () -> cluster.selectServer(mock(ServerSelector.class)));
    assertEquals("Timed out after 5 ms while waiting to resolve SRV records for foo.bar.com.", exception.getMessage());
}
Also used : ClusterSettings(com.mongodb.connection.ClusterSettings) ClusterId(com.mongodb.connection.ClusterId) ServerAddress(com.mongodb.ServerAddress) MongoTimeoutException(com.mongodb.MongoTimeoutException) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 12 with ClusterSettings

use of com.mongodb.connection.ClusterSettings in project mongo-java-driver by mongodb.

the class LoadBalancedClusterTest method shouldTimeoutSelectServerAsynchronouslyWhenThereIsSRVLookup.

@Test
public void shouldTimeoutSelectServerAsynchronouslyWhenThereIsSRVLookup() {
    // given
    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);
    DnsSrvRecordMonitorFactory dnsSrvRecordMonitorFactory = mock(DnsSrvRecordMonitorFactory.class);
    when(dnsSrvRecordMonitorFactory.create(eq(srvHostName), eq(clusterSettings.getSrvServiceName()), any())).thenAnswer(invocation -> new TestDnsSrvRecordMonitor(invocation.getArgument(2)).sleepTime(Duration.ofHours(1)));
    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 5 ms while waiting to resolve SRV records for foo.bar.com.", exception.getMessage());
}
Also used : FutureResultCallback(com.mongodb.async.FutureResultCallback) ClusterSettings(com.mongodb.connection.ClusterSettings) ClusterId(com.mongodb.connection.ClusterId) ServerAddress(com.mongodb.ServerAddress) MongoTimeoutException(com.mongodb.MongoTimeoutException) ServerSelector(com.mongodb.selector.ServerSelector) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 13 with ClusterSettings

use of com.mongodb.connection.ClusterSettings in project mongo-java-driver by mongodb.

the class LoadBalancedClusterTest method shouldFailSelectServerWhenThereIsSRVMisconfiguration.

@Test
public void shouldFailSelectServerWhenThereIsSRVMisconfiguration() {
    // given
    String srvHostName = "foo.bar.com";
    ClusterSettings clusterSettings = ClusterSettings.builder().mode(ClusterConnectionMode.LOAD_BALANCED).srvHost(srvHostName).build();
    ClusterableServerFactory serverFactory = mockServerFactory();
    DnsSrvRecordMonitorFactory dnsSrvRecordMonitorFactory = mock(DnsSrvRecordMonitorFactory.class);
    when(dnsSrvRecordMonitorFactory.create(eq(srvHostName), eq(clusterSettings.getSrvServiceName()), any())).thenAnswer(invocation -> new TestDnsSrvRecordMonitor(invocation.getArgument(2)).hosts(Arrays.asList(new ServerAddress("host1"), new ServerAddress("host2"))));
    cluster = new LoadBalancedCluster(new ClusterId(), clusterSettings, serverFactory, dnsSrvRecordMonitorFactory);
    MongoClientException exception = assertThrows(MongoClientException.class, () -> cluster.selectServer(mock(ServerSelector.class)));
    assertEquals("In load balancing mode, the host must resolve to a single SRV record, but instead it resolved to multiple hosts", exception.getMessage());
}
Also used : ClusterSettings(com.mongodb.connection.ClusterSettings) MongoClientException(com.mongodb.MongoClientException) ClusterId(com.mongodb.connection.ClusterId) ServerAddress(com.mongodb.ServerAddress) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 14 with ClusterSettings

use of com.mongodb.connection.ClusterSettings in project mongo-java-driver by mongodb.

the class LoadBalancedClusterTest method shouldSelectServerWhenThereIsSRVLookup.

@Test
public void shouldSelectServerWhenThereIsSRVLookup() {
    // given
    String srvHostName = "foo.bar.com";
    ServerAddress resolvedServerAddress = new ServerAddress("host1");
    ClusterableServer expectedServer = mock(ClusterableServer.class);
    ClusterSettings clusterSettings = ClusterSettings.builder().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)));
    cluster = new LoadBalancedCluster(new ClusterId(), clusterSettings, serverFactory, dnsSrvRecordMonitorFactory);
    // when
    ServerTuple serverTuple = cluster.selectServer(mock(ServerSelector.class));
    // then
    assertServerTupleExpectations(resolvedServerAddress, expectedServer, serverTuple);
}
Also used : ServerSelector(com.mongodb.selector.ServerSelector) ClusterSettings(com.mongodb.connection.ClusterSettings) ClusterId(com.mongodb.connection.ClusterId) ServerAddress(com.mongodb.ServerAddress) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 15 with ClusterSettings

use of com.mongodb.connection.ClusterSettings in project mongo-java-driver by mongodb.

the class LoadBalancedClusterTest method synchronousConcurrentTest.

@RepeatedTest(value = 10, name = RepeatedTest.LONG_DISPLAY_NAME)
@Tag("Slow")
public void synchronousConcurrentTest() 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);
    when(dnsSrvRecordMonitorFactory.create(eq(srvHostName), eq(clusterSettings.getSrvServiceName()), any())).thenAnswer(invocation -> new TestDnsSrvRecordMonitor(invocation.getArgument(2)).sleepTime(srvResolutionTime));
    cluster = new LoadBalancedCluster(new ClusterId(), clusterSettings, serverFactory, dnsSrvRecordMonitorFactory);
    int numThreads = 100;
    ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
    List<Future<?>> futures = new ArrayList<>(numThreads);
    for (int i = 0; i < numThreads; i++) {
        futures.add(executorService.submit(() -> {
            boolean success = false;
            while (!success) {
                try {
                    cluster.selectServer(mock(ServerSelector.class));
                    success = true;
                } catch (MongoTimeoutException e) {
                // this is expected
                }
            }
            // Keep going for a little while
            for (int j = 0; j < 100; j++) {
                cluster.selectServer(mock(ServerSelector.class));
            }
        }));
    }
    for (Future<?> future : futures) {
        future.get(10, SECONDS);
    }
    executorService.shutdownNow();
}
Also used : ClusterSettings(com.mongodb.connection.ClusterSettings) ClusterId(com.mongodb.connection.ClusterId) ServerAddress(com.mongodb.ServerAddress) ArrayList(java.util.ArrayList) Duration(java.time.Duration) MongoTimeoutException(com.mongodb.MongoTimeoutException) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Tag(org.junit.jupiter.api.Tag)

Aggregations

ClusterSettings (com.mongodb.connection.ClusterSettings)24 ServerAddress (com.mongodb.ServerAddress)18 ClusterId (com.mongodb.connection.ClusterId)15 RepeatedTest (org.junit.jupiter.api.RepeatedTest)12 Test (org.junit.jupiter.api.Test)10 FutureResultCallback (com.mongodb.async.FutureResultCallback)6 ServerSelector (com.mongodb.selector.ServerSelector)6 MongoTimeoutException (com.mongodb.MongoTimeoutException)5 ConnectionString (com.mongodb.ConnectionString)4 MongoClientSettings (com.mongodb.async.client.MongoClientSettings)4 ConnectionPoolSettings (com.mongodb.connection.ConnectionPoolSettings)3 ServerSettings (com.mongodb.connection.ServerSettings)3 SocketSettings (com.mongodb.connection.SocketSettings)3 ArrayList (java.util.ArrayList)3 MongoClientException (com.mongodb.MongoClientException)2 MongoConfigurationException (com.mongodb.MongoConfigurationException)2 MongoCredential (com.mongodb.MongoCredential)2 Builder (com.mongodb.async.client.MongoClientSettings.Builder)2 SslSettings (com.mongodb.connection.SslSettings)2 Duration (java.time.Duration)2