use of com.mongodb.ServerAddress in project mongo-java-driver by mongodb.
the class ClusterDescriptionTest method testSortingOfAll.
@Test
public void testSortingOfAll() {
ClusterDescription description = new ClusterDescription(MULTIPLE, UNKNOWN, asList(builder().state(CONNECTING).address(new ServerAddress("loc:27019")).build(), builder().state(CONNECTING).address(new ServerAddress("loc:27018")).build(), builder().state(CONNECTING).address(new ServerAddress("loc:27017")).build()));
Iterator<ServerDescription> iter = getAll(description).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());
}
use of com.mongodb.ServerAddress in project mongo-java-driver by mongodb.
the class ClusterDescriptionTest method clusterDescriptionWithAnIncompatiblyNewServerShouldBeIncompatible.
@Test
public void clusterDescriptionWithAnIncompatiblyNewServerShouldBeIncompatible() {
ClusterDescription description = new ClusterDescription(MULTIPLE, UNKNOWN, asList(builder().state(CONNECTING).address(new ServerAddress("loc:27019")).build(), builder().state(CONNECTED).ok(true).address(new ServerAddress("loc:27018")).minWireVersion(MAX_DRIVER_WIRE_VERSION + 1).maxWireVersion(MAX_DRIVER_WIRE_VERSION + 1).build(), builder().state(CONNECTING).address(new ServerAddress("loc:27017")).build()));
assertFalse(description.isCompatibleWithDriver());
assertEquals(new ServerAddress("loc:27018"), description.findServerIncompatiblyNewerThanDriver().getAddress());
assertNull(description.findServerIncompatiblyOlderThanDriver());
}
use of com.mongodb.ServerAddress in project mongo-java-driver by mongodb.
the class LatencyMinimizingServerSelectorTest method testLatencyDifferentialMinimization.
@Test
public void testLatencyDifferentialMinimization() throws UnknownHostException {
LatencyMinimizingServerSelector selector = new LatencyMinimizingServerSelector(20, TimeUnit.MILLISECONDS);
ServerDescription primary = ServerDescription.builder().state(CONNECTED).address(new ServerAddress()).ok(true).type(ServerType.REPLICA_SET_PRIMARY).roundTripTime(10, TimeUnit.MILLISECONDS).build();
ServerDescription secondaryOne = ServerDescription.builder().state(CONNECTED).address(new ServerAddress("localhost:27018")).ok(true).type(ServerType.REPLICA_SET_SECONDARY).roundTripTime(15, TimeUnit.MILLISECONDS).build();
ServerDescription secondaryTwo = ServerDescription.builder().state(CONNECTED).address(new ServerAddress("localhost:27019")).ok(true).type(ServerType.REPLICA_SET_SECONDARY).roundTripTime(31, TimeUnit.MILLISECONDS).build();
ServerDescription secondaryThree = ServerDescription.builder().state(CONNECTED).address(new ServerAddress("localhost:27020")).ok(true).type(ServerType.REPLICA_SET_SECONDARY).roundTripTime(30, TimeUnit.MILLISECONDS).build();
assertEquals(Arrays.asList(primary, secondaryOne, secondaryThree), selector.select(new ClusterDescription(MULTIPLE, REPLICA_SET, Arrays.asList(primary, secondaryOne, secondaryTwo, secondaryThree))));
}
use of com.mongodb.ServerAddress in project mongo-java-driver by mongodb.
the class X509AuthenticatorUnitTest 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.createMongoX509Credential("CN=client,OU=kerneluser,O=10Gen,L=New York City,ST=New York,C=US");
subject = new X509Authenticator(new MongoCredentialWithCache(credential), ClusterConnectionMode.MULTIPLE, getServerApi());
}
use of com.mongodb.ServerAddress in project mongo-java-driver by mongodb.
the class AbstractUnifiedTest method executeOperations.
private void executeOperations(final BsonArray operations, final boolean throwExceptions) {
FailPoint failPoint = null;
ServerAddress currentPrimary = null;
try {
for (BsonValue cur : operations) {
final BsonDocument operation = cur.asDocument();
String operationName = operation.getString("name").getValue();
BsonValue expectedResult = operation.get("result");
String receiver = operation.getString("object").getValue();
ClientSession clientSession = receiver.startsWith("session") ? sessionsMap.get(receiver) : null;
if (clientSession == null) {
clientSession = operation.getDocument("arguments", new BsonDocument()).containsKey("session") ? sessionsMap.get(operation.getDocument("arguments").getString("session").getValue()) : null;
}
try {
if (operationName.equals("startTransaction")) {
BsonDocument arguments = operation.getDocument("arguments", new BsonDocument());
if (arguments.containsKey("options")) {
TransactionOptions transactionOptions = createTransactionOptions(arguments.getDocument("options"));
nonNullClientSession(clientSession).startTransaction(transactionOptions);
} else {
nonNullClientSession(clientSession).startTransaction();
}
} else if (operationName.equals("commitTransaction")) {
nonNullClientSession(clientSession).commitTransaction();
} else if (operationName.equals("abortTransaction")) {
nonNullClientSession(clientSession).abortTransaction();
} else if (operationName.equals("withTransaction")) {
final BsonDocument arguments = operation.getDocument("arguments", new BsonDocument());
TransactionOptions transactionOptions = null;
if (arguments.containsKey("options")) {
transactionOptions = createTransactionOptions(arguments.getDocument("options"));
}
if (transactionOptions == null) {
nonNullClientSession(clientSession).withTransaction(new TransactionBody<Object>() {
@Override
public Void execute() {
executeOperations(arguments.getDocument("callback").getArray("operations"), true);
return null;
}
});
} else {
nonNullClientSession(clientSession).withTransaction(new TransactionBody<Object>() {
@Override
public Void execute() {
executeOperations(arguments.getDocument("callback").getArray("operations"), true);
return null;
}
}, transactionOptions);
}
} else if (operationName.equals("targetedFailPoint")) {
assertNull(failPoint);
failPoint = new TargetedFailPoint(operation);
failPoint.executeFailPoint();
} else if (operationName.equals("configureFailPoint")) {
assertNull(failPoint);
failPoint = new FailPoint(operation);
failPoint.executeFailPoint();
} else if (operationName.equals("startThread")) {
String target = operation.getDocument("arguments").getString("name").getValue();
executorServiceMap.put(target, Executors.newSingleThreadExecutor());
} else if (operationName.equals("runOnThread")) {
String target = operation.getDocument("arguments").getString("name").getValue();
ExecutorService executorService = executorServiceMap.get(target);
Callable<Exception> callable = createCallable(operation.getDocument("arguments").getDocument("operation"));
futureMap.put(target, executorService.submit(callable));
} else if (operationName.equals("wait")) {
Thread.sleep(operation.getDocument("arguments").getNumber("ms").longValue());
} else if (operationName.equals("waitForThread")) {
String target = operation.getDocument("arguments").getString("name").getValue();
Exception exceptionFromFuture = futureMap.remove(target).get(5, SECONDS);
if (exceptionFromFuture != null) {
throw exceptionFromFuture;
}
} else if (operationName.equals("waitForEvent")) {
String event = operation.getDocument("arguments").getString("event").getValue();
int count = operation.getDocument("arguments").getNumber("count").intValue();
long timeoutMillis = TimeUnit.SECONDS.toMillis(5);
switch(event) {
case "PoolClearedEvent":
connectionPoolListener.waitForEvent(ConnectionPoolClearedEvent.class, count, timeoutMillis, MILLISECONDS);
break;
case "PoolReadyEvent":
connectionPoolListener.waitForEvent(ConnectionPoolReadyEvent.class, count, timeoutMillis, MILLISECONDS);
break;
case "ServerMarkedUnknownEvent":
serverListener.waitForEvent(ServerType.UNKNOWN, count, timeoutMillis, MILLISECONDS);
break;
default:
throw new UnsupportedOperationException("Unsupported event type: " + event);
}
} else if (operationName.equals("assertEventCount")) {
String event = operation.getDocument("arguments").getString("event").getValue();
int expectedCount = operation.getDocument("arguments").getNumber("count").intValue();
int actualCount = -1;
switch(event) {
case "PoolClearedEvent":
actualCount = connectionPoolListener.countEvents(ConnectionPoolClearedEvent.class);
break;
case "PoolReadyEvent":
actualCount = connectionPoolListener.countEvents(ConnectionPoolReadyEvent.class);
break;
case "ServerMarkedUnknownEvent":
actualCount = serverListener.countEvents(ServerType.UNKNOWN);
break;
default:
throw new UnsupportedOperationException("Unsupported event type: " + event);
}
assertEquals(event + " counts not equal", expectedCount, actualCount);
} else if (operationName.equals("recordPrimary")) {
currentPrimary = getCurrentPrimary();
} else if (operationName.equals("waitForPrimaryChange")) {
long startTimeMillis = System.currentTimeMillis();
int timeoutMillis = operation.getDocument("arguments").getNumber("timeoutMS").intValue();
ServerAddress newPrimary = getCurrentPrimary();
while (newPrimary == null || newPrimary.equals(currentPrimary)) {
if (startTimeMillis + timeoutMillis <= System.currentTimeMillis()) {
fail("Timed out waiting for primary change");
}
// noinspection BusyWait
Thread.sleep(50);
newPrimary = getCurrentPrimary();
}
} else if (operationName.equals("runAdminCommand")) {
collectionHelper.runAdminCommand(operation.getDocument("arguments").getDocument("command"));
} else if (operationName.equals("assertSessionPinned")) {
final BsonDocument arguments = operation.getDocument("arguments", new BsonDocument());
assertNotNull(sessionsMap.get(arguments.getString("session").getValue()).getPinnedServerAddress());
} else if (operationName.equals("assertSessionUnpinned")) {
final BsonDocument arguments = operation.getDocument("arguments", new BsonDocument());
assertNull(sessionsMap.get(arguments.getString("session").getValue()).getPinnedServerAddress());
} else if (operationName.equals("assertSessionTransactionState")) {
final BsonDocument arguments = operation.getDocument("arguments", new BsonDocument());
ClientSession session = sessionsMap.get(arguments.getString("session").getValue());
String state = arguments.getString("state").getValue();
if (state.equals("starting") || state.equals("in_progress")) {
assertTrue(session.hasActiveTransaction());
} else {
assertFalse(session.hasActiveTransaction());
}
} else if (operationName.equals("endSession")) {
clientSession.close();
} else if (operation.getBoolean("error", BsonBoolean.FALSE).getValue()) {
try {
helper.getOperationResults(operation, clientSession);
fail("Error expected but none thrown");
} catch (Exception e) {
// Expected failure ignore
}
} else if (operationName.equals("assertDifferentLsidOnLastTwoCommands")) {
List<CommandEvent> events = lastTwoCommandEvents();
String eventsJson = commandListener.getCommandStartedEvents().stream().map(e -> ((CommandStartedEvent) e).getCommand().toJson()).collect(Collectors.joining(", "));
assertNotEquals(eventsJson, ((CommandStartedEvent) events.get(0)).getCommand().getDocument("lsid"), ((CommandStartedEvent) events.get(1)).getCommand().getDocument("lsid"));
} else if (operationName.equals("assertSameLsidOnLastTwoCommands")) {
List<CommandEvent> events = lastTwoCommandEvents();
String eventsJson = commandListener.getCommandStartedEvents().stream().map(e -> ((CommandStartedEvent) e).getCommand().toJson()).collect(Collectors.joining(", "));
assertEquals(eventsJson, ((CommandStartedEvent) events.get(0)).getCommand().getDocument("lsid"), ((CommandStartedEvent) events.get(1)).getCommand().getDocument("lsid"));
} else if (operationName.equals("assertSessionDirty")) {
assertNotNull(clientSession);
assertNotNull(clientSession.getServerSession());
assertTrue(clientSession.getServerSession().isMarkedDirty());
} else if (operationName.equals("assertSessionNotDirty")) {
assertNotNull(clientSession);
assertNotNull(clientSession.getServerSession());
assertFalse(clientSession.getServerSession().isMarkedDirty());
} else if (operationName.equals("assertCollectionExists")) {
assertCollectionExists(operation, true);
} else if (operationName.equals("assertCollectionNotExists")) {
assertCollectionExists(operation, false);
} else if (operationName.equals("assertIndexExists")) {
assertIndexExists(operation, true);
} else if (operationName.equals("assertIndexNotExists")) {
assertIndexExists(operation, false);
} else {
BsonDocument actualOutcome = helper.getOperationResults(operation, clientSession);
if (expectedResult != null) {
BsonValue actualResult = actualOutcome.get("result");
if (actualResult.isDocument()) {
if (((BsonDocument) actualResult).containsKey("recoveryToken")) {
((BsonDocument) actualResult).remove("recoveryToken");
}
}
assertEquals("Expected operation result differs from actual", expectedResult, actualResult);
}
}
assertFalse(String.format("Expected error '%s' but none thrown for operation %s", getErrorContainsField(expectedResult), operationName), hasErrorContainsField(expectedResult));
assertFalse(String.format("Expected error code '%s' but none thrown for operation %s", getErrorCodeNameField(expectedResult), operationName), hasErrorCodeNameField(expectedResult));
} catch (RuntimeException e) {
if (!assertExceptionState(e, expectedResult, operationName) || throwExceptions) {
throw e;
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
} finally {
if (failPoint != null) {
failPoint.disableFailPoint();
}
}
}
Aggregations