Search in sources :

Example 1 with FutureResultCallback

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

the class Fixture method initializeCollection.

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

Example 2 with FutureResultCallback

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

the class Fixture method dropDatabase.

public static void dropDatabase(final String name) {
    if (name == null) {
        return;
    }
    try {
        FutureResultCallback<Document> futureResultCallback = new FutureResultCallback<Document>();
        getMongoClient().getDatabase(name).runCommand(new Document("dropDatabase", 1), futureResultCallback);
        futureResultCallback.get(60, SECONDS);
    } catch (MongoCommandException e) {
        if (!e.getErrorMessage().startsWith("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 3 with FutureResultCallback

use of com.mongodb.async.FutureResultCallback 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 4 with FutureResultCallback

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

the class LoadBalancedClusterTest method shouldSelectServerAsynchronouslyWhenThereIsSRVLookup.

@Test
public void shouldSelectServerAsynchronouslyWhenThereIsSRVLookup() {
    // 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
    FutureResultCallback<ServerTuple> callback = new FutureResultCallback<>();
    cluster.selectServerAsync(mock(ServerSelector.class), callback);
    ServerTuple serverTuple = callback.get();
    // then
    assertServerTupleExpectations(resolvedServerAddress, expectedServer, serverTuple);
}
Also used : FutureResultCallback(com.mongodb.async.FutureResultCallback) ClusterSettings(com.mongodb.connection.ClusterSettings) 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 5 with FutureResultCallback

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

the class LoadBalancedClusterTest method shouldSelectServerWhenThereIsNoSRVLookup.

@Test
public void shouldSelectServerWhenThereIsNoSRVLookup() {
    // given
    ServerAddress serverAddress = new ServerAddress("host1");
    ClusterableServer expectedServer = mock(ClusterableServer.class);
    ClusterSettings clusterSettings = ClusterSettings.builder().mode(ClusterConnectionMode.LOAD_BALANCED).hosts(Collections.singletonList(serverAddress)).build();
    ClusterableServerFactory serverFactory = mockServerFactory(serverAddress, expectedServer);
    cluster = new LoadBalancedCluster(new ClusterId(), clusterSettings, serverFactory, mock(DnsSrvRecordMonitorFactory.class));
    // when
    ServerTuple serverTuple = cluster.selectServer(mock(ServerSelector.class));
    // then
    assertServerTupleExpectations(serverAddress, expectedServer, serverTuple);
    // when
    FutureResultCallback<ServerTuple> callback = new FutureResultCallback<>();
    cluster.selectServerAsync(mock(ServerSelector.class), callback);
    serverTuple = callback.get();
    // then
    assertServerTupleExpectations(serverAddress, expectedServer, serverTuple);
}
Also used : FutureResultCallback(com.mongodb.async.FutureResultCallback) 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)

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