summaryrefslogtreecommitdiff
path: root/jstests/core/invalidated_legacy_cursors.js
blob: 95cb1a0802ffba6d989e2a8a441e999a6d8bf823 (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
52
53
54
55
56
57
58
/*
 * Test that all DBClientCursor cursor types throw an exception when the server returns
 * CursorNotFound.
 * @tags: [requires_getmore, requires_non_retryable_commands, assumes_balancer_off]
 */
(function() {
'use strict';

const testDB = db.getSiblingDB("invalidated_legacy_cursors");
const coll = testDB.test;
const nDocs = 10;
const batchSize = 2;  // The minimum DBClientCursor batch size is 2.

function setupCollection(isCapped) {
    coll.drop();
    if (isCapped) {
        assert.commandWorked(testDB.createCollection(coll.getName(), {capped: true, size: 4096}));
    }
    const bulk = coll.initializeUnorderedBulkOp();
    for (let i = 0; i < nDocs; ++i) {
        bulk.insert({_id: i, x: i});
    }
    assert.commandWorked(bulk.execute());
    assert.commandWorked(coll.createIndex({x: 1}));
}

function testLegacyCursorThrowsCursorNotFound(isTailable) {
    coll.getMongo().forceReadMode("legacy");
    setupCollection(isTailable);

    // Create a cursor and consume the docs in the first batch.
    let cursor = coll.find().batchSize(batchSize);
    if (isTailable) {
        cursor = cursor.tailable();
    }
    for (let i = 0; i < batchSize; i++) {
        cursor.next();
    }

    // Kill the cursor and assert that the cursor throws CursorNotFound on the first next() call.
    // Use killCursors instead of cursor.close() since we still want to send getMore requests
    // through the existing cursor.
    assert.commandWorked(
        testDB.runCommand({killCursors: coll.getName(), cursors: [cursor.getId()]}));
    const error = assert.throws(() => cursor.next());
    assert.eq(error.code, ErrorCodes.CursorNotFound);

    // Check the state of the cursor.
    assert(!cursor.hasNext());
    assert.eq(0, cursor.getId());
    assert.throws(() => cursor.next());
}

testLegacyCursorThrowsCursorNotFound(false);
if (!jsTest.options().mixedBinVersions) {
    testLegacyCursorThrowsCursorNotFound(true);
}
}());