mirror of
https://github.com/mongodb/mongo.git
synced 2024-11-24 00:17:37 +01:00
668a6f4e9e
GitOrigin-RevId: 1cd8a1cdb3d45876003ad3ccddd4d466cd9fb66c
85 lines
3.3 KiB
JavaScript
85 lines
3.3 KiB
JavaScript
// Basic tests for $changeStream against all collections in a database.
|
|
// Do not run in whole-cluster passthrough since this test assumes that the change stream will be
|
|
// invalidated by a database drop.
|
|
// @tags: [do_not_run_in_whole_cluster_passthrough]
|
|
import {assertDropCollection} from "jstests/libs/collection_drop_recreate.js";
|
|
import {FixtureHelpers} from "jstests/libs/fixture_helpers.js";
|
|
import {
|
|
assertInvalidChangeStreamNss,
|
|
ChangeStreamTest
|
|
} from "jstests/libs/query/change_stream_util.js";
|
|
|
|
const testDb = db.getSiblingDB(jsTestName());
|
|
assert.commandWorked(testDb.dropDatabase());
|
|
|
|
// Test that a single-database change stream cannot be opened on "admin", "config", or "local".
|
|
assertInvalidChangeStreamNss("admin", 1);
|
|
assertInvalidChangeStreamNss("config", 1);
|
|
if (!FixtureHelpers.isMongos(testDb)) {
|
|
assertInvalidChangeStreamNss("local", 1);
|
|
}
|
|
|
|
let cst = new ChangeStreamTest(testDb);
|
|
let cursor = cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: 1});
|
|
|
|
// Test that if there are no changes, we return an empty batch.
|
|
assert.eq(0, cursor.firstBatch.length, "Cursor had changes: " + tojson(cursor));
|
|
|
|
// Test that the change stream returns an inserted doc.
|
|
assert.commandWorked(testDb.t1.insert({_id: 0, a: 1}));
|
|
let expected = {
|
|
documentKey: {_id: 0},
|
|
fullDocument: {_id: 0, a: 1},
|
|
ns: {db: testDb.getName(), coll: "t1"},
|
|
operationType: "insert",
|
|
};
|
|
cst.assertNextChangesEqual({cursor: cursor, expectedChanges: [expected]});
|
|
|
|
// Test that the change stream returns another inserted doc in a different collection but still
|
|
// in the target db.
|
|
assert.commandWorked(testDb.t2.insert({_id: 0, a: 2}));
|
|
expected = {
|
|
documentKey: {_id: 0},
|
|
fullDocument: {_id: 0, a: 2},
|
|
ns: {db: testDb.getName(), coll: "t2"},
|
|
operationType: "insert",
|
|
};
|
|
cst.assertNextChangesEqual({cursor: cursor, expectedChanges: [expected]});
|
|
|
|
// Test that the change stream returns an inserted doc on a user-created collection whose name
|
|
// includes "system" but is not considered an internal collection.
|
|
const validSystemColls = ["system", "systems.views", "ssystem.views", "test.system"];
|
|
validSystemColls.forEach(collName => {
|
|
cursor = cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: 1});
|
|
const coll = testDb.getCollection(collName);
|
|
assert.commandWorked(coll.insert({_id: 0, a: 1}));
|
|
expected = [
|
|
{
|
|
documentKey: {_id: 0},
|
|
fullDocument: {_id: 0, a: 1},
|
|
ns: {db: testDb.getName(), coll: collName},
|
|
operationType: "insert",
|
|
},
|
|
];
|
|
cst.assertNextChangesEqual({cursor: cursor, expectedChanges: expected});
|
|
|
|
// Drop the collection and verify that the change stream picks up the "drop" notification.
|
|
assertDropCollection(testDb, collName);
|
|
// Insert to the test collection to queue up another change after the drop. This is needed
|
|
// since the number of 'drop' notifications is not deterministic in the sharded passthrough
|
|
// suites.
|
|
assert.commandWorked(coll.insert({_id: 0}));
|
|
cst.consumeDropUpTo({
|
|
cursor: cursor,
|
|
dropType: "drop",
|
|
expectedNext: {
|
|
documentKey: {_id: 0},
|
|
fullDocument: {_id: 0},
|
|
ns: {db: testDb.getName(), coll: collName},
|
|
operationType: "insert",
|
|
},
|
|
});
|
|
});
|
|
|
|
cst.cleanUp();
|