Search in sources :

Example 6 with FutureResultCallback

use of com.mongodb.async.FutureResultCallback in project mongo-java-driver by mongodb.

the class Fixture method drop.

public static void drop(final MongoNamespace namespace) {
    try {
        FutureResultCallback<Document> futureResultCallback = new FutureResultCallback<Document>();
        getMongoClient().getDatabase(namespace.getDatabaseName()).runCommand(new Document("drop", namespace.getCollectionName()), futureResultCallback);
        futureResultCallback.get();
    } catch (MongoCommandException e) {
        if (!e.getErrorMessage().contains("ns not found")) {
            throw e;
        }
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
}
Also used : FutureResultCallback(com.mongodb.async.FutureResultCallback) MongoCommandException(com.mongodb.MongoCommandException) Document(org.bson.Document)

Example 7 with FutureResultCallback

use of com.mongodb.async.FutureResultCallback in project mongo-java-driver by mongodb.

the class LoadBalancedClusterTest method shouldFailSelectServerAsynchronouslyWhenThereIsSRVMisconfiguration.

@Test
public void shouldFailSelectServerAsynchronouslyWhenThereIsSRVMisconfiguration() {
    // 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);
    FutureResultCallback<ServerTuple> callback = new FutureResultCallback<>();
    cluster.selectServerAsync(mock(ServerSelector.class), callback);
    MongoClientException exception = assertThrows(MongoClientException.class, callback::get);
    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 : FutureResultCallback(com.mongodb.async.FutureResultCallback) ClusterSettings(com.mongodb.connection.ClusterSettings) MongoClientException(com.mongodb.MongoClientException) ClusterId(com.mongodb.connection.ClusterId) ServerAddress(com.mongodb.ServerAddress) ServerSelector(com.mongodb.selector.ServerSelector) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 8 with FutureResultCallback

use of com.mongodb.async.FutureResultCallback 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());
            }
        }
    }
}
Also used : FutureResultCallback(com.mongodb.async.FutureResultCallback) ClusterSettings(com.mongodb.connection.ClusterSettings) ServerAddress(com.mongodb.ServerAddress) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ClusterId(com.mongodb.connection.ClusterId) Duration(java.time.Duration) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Tag(org.junit.jupiter.api.Tag)

Example 9 with FutureResultCallback

use of com.mongodb.async.FutureResultCallback 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());
}
Also used : FutureResultCallback(com.mongodb.async.FutureResultCallback) MongoConfigurationException(com.mongodb.MongoConfigurationException) 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)

Aggregations

FutureResultCallback (com.mongodb.async.FutureResultCallback)9 ServerAddress (com.mongodb.ServerAddress)6 ClusterId (com.mongodb.connection.ClusterId)6 ClusterSettings (com.mongodb.connection.ClusterSettings)6 RepeatedTest (org.junit.jupiter.api.RepeatedTest)6 ServerSelector (com.mongodb.selector.ServerSelector)5 Test (org.junit.jupiter.api.Test)5 MongoCommandException (com.mongodb.MongoCommandException)3 Document (org.bson.Document)3 MongoTimeoutException (com.mongodb.MongoTimeoutException)2 MongoClientException (com.mongodb.MongoClientException)1 MongoConfigurationException (com.mongodb.MongoConfigurationException)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Tag (org.junit.jupiter.api.Tag)1