Search in sources :

Example 11 with CommandStartedEvent

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);
    }
}
Also used : CommandStartedEvent(com.mongodb.event.CommandStartedEvent) CommandEvent(com.mongodb.event.CommandEvent)

Example 12 with CommandStartedEvent

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();
    }
}
Also used : CommandStartedEvent(com.mongodb.event.CommandStartedEvent) CommandEvent(com.mongodb.event.CommandEvent)

Example 13 with CommandStartedEvent

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);
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) CommandStartedEvent(com.mongodb.event.CommandStartedEvent)

Example 14 with CommandStartedEvent

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);
}
Also used : BsonDocument(org.bson.BsonDocument) CommandStartedEvent(com.mongodb.event.CommandStartedEvent)

Example 15 with CommandStartedEvent

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;
}
Also used : CommandFailedEvent(com.mongodb.event.CommandFailedEvent) CommandSucceededEvent(com.mongodb.event.CommandSucceededEvent) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) CommandStartedEvent(com.mongodb.event.CommandStartedEvent) CommandEvent(com.mongodb.event.CommandEvent) ArrayList(java.util.ArrayList) BsonString(org.bson.BsonString) BsonValue(org.bson.BsonValue)

Aggregations

CommandStartedEvent (com.mongodb.event.CommandStartedEvent)16 CommandEvent (com.mongodb.event.CommandEvent)13 BsonDocument (org.bson.BsonDocument)10 CommandSucceededEvent (com.mongodb.event.CommandSucceededEvent)6 BsonString (org.bson.BsonString)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 BsonValue (org.bson.BsonValue)4 MongoNamespace (com.mongodb.MongoNamespace)3 ClusterFixture.isDataLakeTest (com.mongodb.ClusterFixture.isDataLakeTest)2 MongoClientSettings (com.mongodb.MongoClientSettings)2 WriteConcern (com.mongodb.WriteConcern)2 Fixture.getMongoClient (com.mongodb.client.Fixture.getMongoClient)2 CollectionHelper (com.mongodb.client.test.CollectionHelper)2 TestCommandListener (com.mongodb.internal.connection.TestCommandListener)2 TestConnectionPoolListener (com.mongodb.internal.connection.TestConnectionPoolListener)2 Nullable (com.mongodb.lang.Nullable)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 BsonArray (org.bson.BsonArray)2