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);
}
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());
}
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);
}
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());
}
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());
}
Aggregations