use of com.mongodb.event.CommandStartedEvent in project mongo-java-driver by mongodb.
the class TestCommandListener method eventsWereDelivered.
public void eventsWereDelivered(final List<CommandEvent> expectedEvents) {
assertEquals(expectedEvents.size(), events.size());
int currentlyExpectedRequestId = 0;
for (int i = 0; i < events.size(); i++) {
CommandEvent actual = events.get(i);
CommandEvent expected = expectedEvents.get(i);
if (actual instanceof CommandStartedEvent) {
currentlyExpectedRequestId = actual.getRequestId();
} else {
assertEquals(currentlyExpectedRequestId, actual.getRequestId());
}
assertEventEquivalence(actual, expected);
}
}
use of com.mongodb.event.CommandStartedEvent in project mongo-java-driver by mongodb.
the class TestCommandListener method eventsWereDelivered.
public void eventsWereDelivered(final List<CommandEvent> expectedEvents) {
lock.lock();
try {
assertEquals(expectedEvents.size(), events.size());
int currentlyExpectedRequestId = 0;
for (int i = 0; i < events.size(); i++) {
CommandEvent actual = events.get(i);
CommandEvent expected = expectedEvents.get(i);
if (actual instanceof CommandStartedEvent) {
currentlyExpectedRequestId = actual.getRequestId();
} else {
assertEquals(currentlyExpectedRequestId, actual.getRequestId());
}
assertEventEquivalence(actual, expected);
}
} finally {
lock.unlock();
}
}
use of com.mongodb.event.CommandStartedEvent in project mongo-java-driver by mongodb.
the class CommandMonitoringTestHelper method massageActualCommandStartedEvent.
private static CommandStartedEvent massageActualCommandStartedEvent(final CommandStartedEvent event, @Nullable final Map<String, BsonDocument> lsidMap, final CommandStartedEvent expectedCommandStartedEvent) {
BsonDocument command = getWritableCloneOfCommand(event.getCommand());
massageCommand(event, command);
if (command.containsKey("readConcern") && (command.getDocument("readConcern").containsKey("afterClusterTime"))) {
command.getDocument("readConcern").put("afterClusterTime", new BsonInt32(42));
}
// Tests expect maxTimeMS to be int32, but Java API requires maxTime to be a long. This massage seems preferable to casting
if (command.containsKey("maxTimeMS")) {
command.put("maxTimeMS", new BsonInt32(command.getNumber("maxTimeMS").intValue()));
}
// Tests do not expect the "ns" field in a result after running createIndex.
if (command.containsKey("createIndexes") && command.containsKey("indexes")) {
massageCommandIndexes(command.getArray("indexes"));
}
massageActualCommand(command, expectedCommandStartedEvent.getCommand());
return new CommandStartedEvent(event.getRequestId(), event.getConnectionDescription(), event.getDatabaseName(), event.getCommandName(), command);
}
use of com.mongodb.event.CommandStartedEvent in project mongo-java-driver by mongodb.
the class CommandMonitoringTestHelper method massageExpectedCommandStartedEvent.
private static CommandStartedEvent massageExpectedCommandStartedEvent(final CommandStartedEvent event, final CommandStartedEvent actualEvent, @Nullable final Map<String, BsonDocument> lsidMap) {
BsonDocument command = getWritableCloneOfCommand(event.getCommand());
massageCommand(event, command);
if (lsidMap == null) {
command.remove("lsid");
} else if (command.containsKey("lsid")) {
command.put("lsid", lsidMap.get(command.getString("lsid").getValue()));
}
if (command.containsKey("txnNumber") && command.isNull("txnNumber")) {
command.remove("txnNumber");
}
if (command.containsKey("stmtId") && command.isNull("stmtId")) {
command.remove("stmtId");
}
if (command.containsKey("startTransaction") && command.isNull("startTransaction")) {
command.remove("startTransaction");
}
if (command.containsKey("autocommit") && command.isNull("autocommit")) {
command.remove("autocommit");
}
if (command.containsKey("maxTimeMS") && command.isNull("maxTimeMS")) {
command.remove("maxTimeMS");
}
if (command.containsKey("writeConcern") && command.isNull("writeConcern")) {
command.remove("writeConcern");
}
if (command.containsKey("allowDiskUse") && command.isNull("allowDiskUse")) {
command.remove("allowDiskUse");
}
if (command.containsKey("readConcern")) {
if (command.isNull("readConcern")) {
command.remove("readConcern");
}
}
if (command.containsKey("recoveryToken")) {
command.remove("recoveryToken");
}
if (command.containsKey("query")) {
command.remove("query");
}
if (command.containsKey("filter") && command.getDocument("filter").isEmpty()) {
command.remove("filter");
}
if (command.containsKey("mapReduce")) {
command.remove("mapReduce");
}
replaceTypeAssertionWithActual(command, actualEvent.getCommand());
return new CommandStartedEvent(event.getRequestId(), event.getConnectionDescription(), event.getDatabaseName(), event.getCommandName(), command);
}
use of com.mongodb.event.CommandStartedEvent in project mongo-java-driver by mongodb.
the class CommandMonitoringTestHelper method getExpectedEvents.
public static List<CommandEvent> getExpectedEvents(final BsonArray expectedEventDocuments, final String databaseName, final BsonDocument operation) {
List<CommandEvent> expectedEvents = new ArrayList<CommandEvent>(expectedEventDocuments.size());
for (BsonValue expectedEventDocument : expectedEventDocuments) {
BsonDocument curExpectedEventDocument = expectedEventDocument.asDocument();
String eventType = curExpectedEventDocument.keySet().iterator().next();
BsonDocument eventDescriptionDocument = curExpectedEventDocument.getDocument(eventType);
CommandEvent commandEvent;
String commandName = eventDescriptionDocument.getString("command_name", new BsonString("")).getValue();
if (eventType.equals("command_started_event")) {
BsonDocument commandDocument = eventDescriptionDocument.getDocument("command");
String actualDatabaseName = eventDescriptionDocument.containsKey("database_name") ? eventDescriptionDocument.getString("database_name").getValue() : databaseName;
// If the spec test supplies a $db field in the command, then use that database.
if (commandDocument.containsKey("$db")) {
actualDatabaseName = commandDocument.getString("$db").getValue();
} else if (commandName.equals("")) {
commandName = commandDocument.keySet().iterator().next();
}
if (isAdminCommand(commandName)) {
actualDatabaseName = "admin";
}
// Not clear whether these global fields should be included, but also not clear how to efficiently exclude them
if (serverVersionAtLeast(3, 6)) {
commandDocument.put("$db", new BsonString(actualDatabaseName));
if (operation != null && operation.containsKey("read_preference")) {
commandDocument.put("$readPreference", operation.getDocument("read_preference"));
}
}
commandEvent = new CommandStartedEvent(1, null, actualDatabaseName, commandName, commandDocument);
} else if (eventType.equals("command_succeeded_event")) {
BsonDocument replyDocument = eventDescriptionDocument.get("reply").asDocument();
commandEvent = new CommandSucceededEvent(1, null, commandName, replyDocument, 1);
} else if (eventType.equals("command_failed_event")) {
commandEvent = new CommandFailedEvent(1, null, commandName, 1, null);
} else {
throw new UnsupportedOperationException("Unsupported command event type: " + eventType);
}
expectedEvents.add(commandEvent);
}
return expectedEvents;
}
Aggregations