@Test public void testIsConnecting() throws UnknownHostException { ClusterDescription description = new ClusterDescription( Multiple, ReplicaSet, Arrays.asList( ServerDescription.builder() .state(Connecting) .address(new ServerAddress()) .type(ReplicaSetPrimary) .build())); assertTrue(description.isConnecting()); description = new ClusterDescription( Multiple, ReplicaSet, Arrays.asList( ServerDescription.builder() .state(Connected) .address(new ServerAddress()) .type(ReplicaSetPrimary) .build())); assertFalse(description.isConnecting()); }
@Test public void clusterDescriptionWithAnIncompatibleServerShouldBeIncompatible() throws UnknownHostException { ClusterDescription description = new ClusterDescription( Multiple, Unknown, Arrays.asList( ServerDescription.builder() .state(Connecting) .address(new ServerAddress("loc:27019")) .build(), ServerDescription.builder() .state(Connected) .ok(true) .address(new ServerAddress("loc:27018")) .minWireVersion(MAX_DRIVER_WIRE_VERSION + 1) .maxWireVersion(MAX_DRIVER_WIRE_VERSION + 1) .build(), ServerDescription.builder() .state(Connecting) .address(new ServerAddress("loc:27017")) .build())); assertFalse(description.isCompatibleWithDriver()); }
@Test public void testObjectOverrides() throws UnknownHostException { ClusterDescription description = new ClusterDescription( Multiple, Unknown, Arrays.asList( ServerDescription.builder() .state(Connecting) .address(new ServerAddress("loc:27019")) .build(), ServerDescription.builder() .state(Connecting) .address(new ServerAddress("loc:27018")) .build(), ServerDescription.builder() .state(Connecting) .address(new ServerAddress("loc:27017")) .build())); ClusterDescription descriptionTwo = new ClusterDescription( Multiple, Unknown, Arrays.asList( ServerDescription.builder() .state(Connecting) .address(new ServerAddress("loc:27019")) .build(), ServerDescription.builder() .state(Connecting) .address(new ServerAddress("loc:27018")) .build(), ServerDescription.builder() .state(Connecting) .address(new ServerAddress("loc:27017")) .build())); assertEquals(description, descriptionTwo); assertEquals(description.hashCode(), descriptionTwo.hashCode()); assertTrue(description.toString().startsWith("ClusterDescription")); }
@Test public void clusterDescriptionWithCompatibleServerShouldBeCompatible() throws UnknownHostException { ClusterDescription description = new ClusterDescription( Multiple, Unknown, Arrays.asList( ServerDescription.builder() .state(Connecting) .address(new ServerAddress("loc:27019")) .build(), ServerDescription.builder() .state(Connecting) .address(new ServerAddress("loc:27018")) .build(), ServerDescription.builder() .state(Connecting) .address(new ServerAddress("loc:27017")) .build())); assertTrue(description.isCompatibleWithDriver()); }
@Test public void testSortingOfAll() throws UnknownHostException { ClusterDescription description = new ClusterDescription( Multiple, Unknown, Arrays.asList( ServerDescription.builder() .state(Connecting) .address(new ServerAddress("loc:27019")) .build(), ServerDescription.builder() .state(Connecting) .address(new ServerAddress("loc:27018")) .build(), ServerDescription.builder() .state(Connecting) .address(new ServerAddress("loc:27017")) .build())); Iterator<ServerDescription> iter = description.getAll().iterator(); assertEquals(new ServerAddress("loc:27017"), iter.next().getAddress()); assertEquals(new ServerAddress("loc:27018"), iter.next().getAddress()); assertEquals(new ServerAddress("loc:27019"), iter.next().getAddress()); }
@Override public List<ServerDescription> choose(final ClusterDescription clusterDescription) { return getServersWithAcceptableLatencyDifference( clusterDescription.getAll(), getBestPingTimeNanos(clusterDescription.getAll())); }
@Test public void testEmptySet() { ClusterDescription description = new ClusterDescription(Multiple, Unknown, Collections.<ServerDescription>emptyList()); assertTrue(description.getAll().isEmpty()); }
@Test public void testMode() { ClusterDescription description = new ClusterDescription(Multiple, Unknown, Collections.<ServerDescription>emptyList()); assertEquals(Multiple, description.getConnectionMode()); }