summaryrefslogtreecommitdiff
path: root/jstests/change_streams/change_stream_rename_resumability.js
blob: 46a3b3575b03761c113706dd5c4fcedbd334b239 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Tests resuming on a change stream that was invalidated due to rename.

(function() {
    "use strict";

    load("jstests/libs/collection_drop_recreate.js");  // For assert[Drop|Create]Collection.

    let coll = assertDropAndRecreateCollection(db, "change_stream_invalidate_resumability");

    // Drop the collection we'll rename to _before_ starting the changeStream, so that we don't
    // get accidentally an invalidate when running on the whole DB or cluster.
    assertDropCollection(db, coll.getName() + "_renamed");

    const cursor = coll.watch();
    assert(!cursor.hasNext());

    // Create an 'insert' oplog entry.
    assert.writeOK(coll.insert({_id: 1}));

    assert.commandWorked(coll.renameCollection(coll.getName() + "_renamed"));

    // Update 'coll' to point to the renamed collection.
    coll = db[coll.getName() + "_renamed"];

    // Insert another document after the rename.
    assert.writeOK(coll.insert({_id: 2}));

    // We should get 2 oplog entries of type insert and invalidate.
    assert.soon(() => cursor.hasNext());
    let change = cursor.next();
    assert.eq(change.operationType, "insert", tojson(change));
    assert.docEq(change.fullDocument, {_id: 1});

    assert.soon(() => cursor.hasNext());
    change = cursor.next();
    assert.eq(change.operationType, "invalidate", tojson(change));
    assert(cursor.isExhausted());

    // TODO SERVER-34789: The code below should throw an error. We exercise this behavior here to
    // be sure that it doesn't crash the server, but the ability to resume a change stream after an
    // invalidate is a bug, not a feature.

    // Try resuming from the invalidate.
    assert.doesNotThrow(function() {
        const resumeCursor = coll.watch([], {resumeAfter: change._id});
        assert.soon(() => resumeCursor.hasNext());
        // Not checking the contents of the document returned, because we do not technically
        // support this behavior.
        resumeCursor.next();
    });
}());