Search in sources :

Example 11 with ClusterId

use of com.mongodb.connection.ClusterId 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 12 with ClusterId

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

the class LoadBalancedClusterTest method shouldTimeoutSelectServerWhenThereIsSRVLookupException.

@Test
public void shouldTimeoutSelectServerWhenThereIsSRVLookupException() {
    // 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);
    MongoTimeoutException exception = assertThrows(MongoTimeoutException.class, () -> cluster.selectServer(mock(ServerSelector.class)));
    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 : MongoConfigurationException(com.mongodb.MongoConfigurationException) 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 13 with ClusterId

use of com.mongodb.connection.ClusterId 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)

Example 14 with ClusterId

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

the class LoadBalancedClusterTest method shouldNotInitServerAfterClosing.

@Test
void shouldNotInitServerAfterClosing() {
    // prepare mocks
    ClusterSettings clusterSettings = ClusterSettings.builder().mode(ClusterConnectionMode.LOAD_BALANCED).srvHost("foo.bar.com").build();
    ClusterableServerFactory serverFactory = mock(ClusterableServerFactory.class);
    when(serverFactory.getSettings()).thenReturn(mock(ServerSettings.class));
    DnsSrvRecordMonitorFactory srvRecordMonitorFactory = mock(DnsSrvRecordMonitorFactory.class);
    when(srvRecordMonitorFactory.create(any(), eq(clusterSettings.getSrvServiceName()), any(DnsSrvRecordInitializer.class))).thenReturn(mock(DnsSrvRecordMonitor.class));
    ArgumentCaptor<DnsSrvRecordInitializer> serverInitializerCaptor = ArgumentCaptor.forClass(DnsSrvRecordInitializer.class);
    // create `cluster` and capture its `DnsSrvRecordInitializer` (server initializer)
    LoadBalancedCluster cluster = new LoadBalancedCluster(new ClusterId(), clusterSettings, serverFactory, srvRecordMonitorFactory);
    verify(srvRecordMonitorFactory, times(1)).create(any(), eq(clusterSettings.getSrvServiceName()), serverInitializerCaptor.capture());
    // close `cluster`, call `DnsSrvRecordInitializer.initialize` and check that it does not result in creating a `ClusterableServer`
    cluster.close();
    serverInitializerCaptor.getValue().initialize(Collections.singleton(new ServerAddress()));
    verify(serverFactory, never()).create(any(), any());
}
Also used : ClusterSettings(com.mongodb.connection.ClusterSettings) ClusterId(com.mongodb.connection.ClusterId) ServerSettings(com.mongodb.connection.ServerSettings) ServerAddress(com.mongodb.ServerAddress) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 15 with ClusterId

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

the class PlainAuthenticatorUnitTest method before.

@Before
public void before() {
    connection = new TestInternalConnection(new ServerId(new ClusterId(), new ServerAddress("localhost", 27017)));
    connectionDescription = new ConnectionDescription(new ServerId(new ClusterId(), new ServerAddress()));
    credential = MongoCredential.createPlainCredential("user", "$external", "pencil".toCharArray());
    subject = new PlainAuthenticator(new MongoCredentialWithCache(credential), ClusterConnectionMode.MULTIPLE, getServerApi());
}
Also used : ConnectionDescription(com.mongodb.connection.ConnectionDescription) ServerId(com.mongodb.connection.ServerId) ClusterId(com.mongodb.connection.ClusterId) ServerAddress(com.mongodb.ServerAddress) Before(org.junit.Before)

Aggregations

ClusterId (com.mongodb.connection.ClusterId)24 ServerAddress (com.mongodb.ServerAddress)19 ClusterSettings (com.mongodb.connection.ClusterSettings)15 RepeatedTest (org.junit.jupiter.api.RepeatedTest)13 Test (org.junit.jupiter.api.Test)12 ServerId (com.mongodb.connection.ServerId)7 Before (org.junit.Before)7 FutureResultCallback (com.mongodb.async.FutureResultCallback)6 ServerSelector (com.mongodb.selector.ServerSelector)6 MongoTimeoutException (com.mongodb.MongoTimeoutException)5 ConnectionDescription (com.mongodb.connection.ConnectionDescription)5 ServerSettings (com.mongodb.connection.ServerSettings)3 MongoClientException (com.mongodb.MongoClientException)2 MongoConfigurationException (com.mongodb.MongoConfigurationException)2 ConnectionId (com.mongodb.connection.ConnectionId)2 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 ExecutorService (java.util.concurrent.ExecutorService)2 Future (java.util.concurrent.Future)2 BsonDocument (org.bson.BsonDocument)2