Search in sources :

Example 26 with ClusterDescription

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

the class ReadPreferenceWithFallbackServerSelectorTest method shouldSelectCorrectServersWhenAllServersAreAtLeastMinimum.

@Test
public void shouldSelectCorrectServersWhenAllServersAreAtLeastMinimum() {
    ReadPreferenceWithFallbackServerSelector selector = new ReadPreferenceWithFallbackServerSelector(ReadPreference.secondary(), FIVE_DOT_ZERO_WIRE_VERSION, ReadPreference.primary());
    ClusterDescription clusterDescription = new ClusterDescription(ClusterConnectionMode.MULTIPLE, ClusterType.REPLICA_SET, asList(builder().ok(true).state(CONNECTED).type(REPLICA_SET_PRIMARY).address(new ServerAddress("localhost:27017")).maxWireVersion(FIVE_DOT_ZERO_WIRE_VERSION).build(), builder().ok(true).state(CONNECTED).type(REPLICA_SET_SECONDARY).address(new ServerAddress("localhost:27018")).maxWireVersion(FIVE_DOT_ZERO_WIRE_VERSION).build()));
    assertEquals(clusterDescription.getServerDescriptions().stream().filter(serverDescription -> serverDescription.getType() == REPLICA_SET_SECONDARY).collect(toList()), selector.select(clusterDescription));
    assertEquals(ReadPreference.secondary(), selector.getAppliedReadPreference());
}
Also used : ServerAddress(com.mongodb.ServerAddress) ClusterDescription(com.mongodb.connection.ClusterDescription) Test(org.junit.jupiter.api.Test)

Example 27 with ClusterDescription

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

the class ReadPreferenceWithFallbackServerSelectorTest method shouldSelectCorrectServersWhenAtLeastOneServerIsOlderThanMinimum.

@Test
public void shouldSelectCorrectServersWhenAtLeastOneServerIsOlderThanMinimum() {
    ReadPreferenceWithFallbackServerSelector selector = new ReadPreferenceWithFallbackServerSelector(ReadPreference.secondary(), FIVE_DOT_ZERO_WIRE_VERSION, ReadPreference.primary());
    ClusterDescription clusterDescription = new ClusterDescription(ClusterConnectionMode.MULTIPLE, ClusterType.REPLICA_SET, asList(builder().ok(true).state(CONNECTED).type(REPLICA_SET_PRIMARY).address(new ServerAddress("localhost:27017")).maxWireVersion(FOUR_DOT_FOUR_WIRE_VERSION).build(), builder().ok(true).state(CONNECTED).type(REPLICA_SET_SECONDARY).address(new ServerAddress("localhost:27018")).maxWireVersion(FIVE_DOT_ZERO_WIRE_VERSION).build()));
    assertEquals(clusterDescription.getServerDescriptions().stream().filter(serverDescription -> serverDescription.getType() == REPLICA_SET_PRIMARY).collect(toList()), selector.select(clusterDescription));
    assertEquals(ReadPreference.primary(), selector.getAppliedReadPreference());
}
Also used : ServerAddress(com.mongodb.ServerAddress) ClusterDescription(com.mongodb.connection.ClusterDescription) Test(org.junit.jupiter.api.Test)

Example 28 with ClusterDescription

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

the class CompositeServerSelectorTest method shouldPassOnClusterDescriptionWithCorrectServersAndSettings.

@Test
public void shouldPassOnClusterDescriptionWithCorrectServersAndSettings() {
    TestServerSelector firstSelector = new TestServerSelector();
    TestServerSelector secondSelector = new TestServerSelector();
    CompositeServerSelector composedSelector = new CompositeServerSelector(asList(firstSelector, secondSelector));
    composedSelector.select(new ClusterDescription(MULTIPLE, REPLICA_SET, asList(first, second, third), ClusterSettings.builder().hosts(asList(new ServerAddress())).build(), ServerSettings.builder().build()));
    assertTrue(secondSelector.clusterDescription.getServerDescriptions().isEmpty());
    assertNotNull(secondSelector.clusterDescription.getClusterSettings());
    assertNotNull(secondSelector.clusterDescription.getServerSettings());
}
Also used : ServerAddress(com.mongodb.ServerAddress) ClusterDescription(com.mongodb.connection.ClusterDescription) Test(org.junit.Test)

Example 29 with ClusterDescription

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

the class ServerSelectionWithinLatencyWindowTest method shouldPassAllOutcomes.

@Test
public void shouldPassAllOutcomes() {
    ServerSelector selector = new ReadPreferenceServerSelector(ReadPreference.nearest());
    Map<ServerAddress, List<ServerTuple>> selectionResultsGroupedByServerAddress = IntStream.range(0, iterations).mapToObj(i -> BaseCluster.selectServer(selector, clusterDescription, address -> Assertions.assertNotNull(serverCatalog.get(address)))).collect(groupingBy(serverTuple -> serverTuple.getServerDescription().getAddress()));
    Map<ServerAddress, BigDecimal> selectionFrequencies = selectionResultsGroupedByServerAddress.entrySet().stream().collect(toMap(Map.Entry::getKey, entry -> BigDecimal.valueOf(entry.getValue().size()).setScale(2, RoundingMode.UNNECESSARY).divide(BigDecimal.valueOf(iterations), RoundingMode.HALF_UP)));
    outcome.assertMatches(selectionFrequencies);
}
Also used : ReadPreference(com.mongodb.ReadPreference) IntStream(java.util.stream.IntStream) RunWith(org.junit.runner.RunWith) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) JsonPoweredTestHelper.testDir(util.JsonPoweredTestHelper.testDir) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue) BsonNumber(org.bson.BsonNumber) BigDecimal(java.math.BigDecimal) Collectors.toMap(java.util.stream.Collectors.toMap) Assertions(com.mongodb.assertions.Assertions) ServerSelector(com.mongodb.selector.ServerSelector) BsonArray(org.bson.BsonArray) Map(java.util.Map) ClusterDescription(com.mongodb.connection.ClusterDescription) Path(java.nio.file.Path) Parameterized(org.junit.runners.Parameterized) RoundingMode(java.math.RoundingMode) JsonPoweredTestHelper.testDocs(util.JsonPoweredTestHelper.testDocs) ServerAddress(com.mongodb.ServerAddress) Collection(java.util.Collection) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ServerSelectionSelectionTest.buildClusterDescription(com.mongodb.connection.ServerSelectionSelectionTest.buildClusterDescription) Mockito(org.mockito.Mockito) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ReadPreferenceServerSelector(com.mongodb.internal.selector.ReadPreferenceServerSelector) Assert.assertEquals(org.junit.Assert.assertEquals) ServerSelector(com.mongodb.selector.ServerSelector) ReadPreferenceServerSelector(com.mongodb.internal.selector.ReadPreferenceServerSelector) ServerAddress(com.mongodb.ServerAddress) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ReadPreferenceServerSelector(com.mongodb.internal.selector.ReadPreferenceServerSelector) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 30 with ClusterDescription

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

the class LatencyMinimizingServerSelectorTest method testZeroLatencyDifferentialTolerance.

@Test
public void testZeroLatencyDifferentialTolerance() throws UnknownHostException {
    LatencyMinimizingServerSelector selector = new LatencyMinimizingServerSelector(0, TimeUnit.NANOSECONDS);
    ServerDescription primary = ServerDescription.builder().state(CONNECTED).address(new ServerAddress()).ok(true).type(ServerType.REPLICA_SET_PRIMARY).roundTripTime(10, TimeUnit.NANOSECONDS).build();
    ServerDescription secondaryOne = ServerDescription.builder().state(CONNECTED).address(new ServerAddress("localhost:27018")).ok(true).type(ServerType.REPLICA_SET_SECONDARY).roundTripTime(11, TimeUnit.NANOSECONDS).build();
    assertEquals(Arrays.asList(primary), selector.select(new ClusterDescription(MULTIPLE, REPLICA_SET, Arrays.asList(primary, secondaryOne))));
}
Also used : ServerDescription(com.mongodb.connection.ServerDescription) ServerAddress(com.mongodb.ServerAddress) ClusterDescription(com.mongodb.connection.ClusterDescription) Test(org.junit.Test)

Aggregations

ClusterDescription (com.mongodb.connection.ClusterDescription)31 ServerAddress (com.mongodb.ServerAddress)15 ServerDescription (com.mongodb.connection.ServerDescription)12 Test (org.junit.Test)11 ServerSelector (com.mongodb.selector.ServerSelector)5 Test (org.junit.jupiter.api.Test)5 ReadPreferenceServerSelector (com.mongodb.internal.selector.ReadPreferenceServerSelector)3 List (java.util.List)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 BsonDocument (org.bson.BsonDocument)3 ClientSessionOptions (com.mongodb.ClientSessionOptions)2 MongoException (com.mongodb.MongoException)2 MongoInterruptedException (com.mongodb.MongoInterruptedException)2 ClusterDescriptionChangedEvent (com.mongodb.event.ClusterDescriptionChangedEvent)2 ClusterOpeningEvent (com.mongodb.event.ClusterOpeningEvent)2 LatencyMinimizingServerSelector (com.mongodb.internal.selector.LatencyMinimizingServerSelector)2 BsonArray (org.bson.BsonArray)2 MongoClientException (com.mongodb.MongoClientException)1 MongoTimeoutException (com.mongodb.MongoTimeoutException)1 ReadPreference (com.mongodb.ReadPreference)1