summaryrefslogtreecommitdiff
path: root/jstests/core/list_namespaces_invalidation.js
blob: 8e94c1e6ded33855b497080bae78b12717504d1e (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
59
60
61
62
63
64
65
66
67
68
69
70
// @tags: [requires_non_retryable_commands, requires_fastcount, requires_getmore]
(function() {
'use strict';
let dbInvalidName = 'system_namespaces_invalidations';
let dbInvalid = db.getSiblingDB(dbInvalidName);
let num_collections = 3;
let DROP = 1;
let RENAME = 2;
let MOVE = 3;
function testNamespaceInvalidation(namespaceAction, batchSize) {
    dbInvalid.dropDatabase();

    // Create enough collections to necessitate multiple cursor batches.
    for (let i = 0; i < num_collections; i++) {
        assert.commandWorked(dbInvalid.createCollection('coll' + i.toString()));
    }

    // Get the first two namespaces using listCollections.
    let cmd = {listCollections: dbInvalidName};
    Object.extend(cmd, {cursor: {batchSize: batchSize}});
    let res = dbInvalid.runCommand(cmd);
    assert.commandWorked(res, 'could not run ' + tojson(cmd));
    printjson(res);

    // Ensure the cursor has data, invalidate the namespace, and exhaust the cursor.
    let cursor = new DBCommandCursor(dbInvalid, res);
    let errMsg = 'expected more data from command ' + tojson(cmd) + ', with result ' + tojson(res);
    assert(cursor.hasNext(), errMsg);
    if (namespaceAction == RENAME) {
        // Rename the collection to something that does not fit in the previously allocated
        // memory for the record.
        assert.commandWorked(
            dbInvalid['coll1'].renameCollection('coll1' +
                                                'lkdsahflaksjdhfsdkljhfskladhfkahfsakfla' +
                                                'skfjhaslfaslfkhasklfjhsakljhdsjksahkldjslh'));
    } else if (namespaceAction == DROP) {
        assert(dbInvalid['coll1'].drop());
    } else if (namespaceAction == MOVE) {
        let modCmd = {
            collMod: 'coll1',
            validator: {
                $or: [
                    {phone: {$type: "string"}},
                    {email: {$regex: /@mongodb\.com$/}},
                    {status: {$in: ["Unknown", "Incomplete"]}},
                    {address: {$type: "string"}},
                    {ssn: {$type: "string"}},
                    {favoriteBook: {$type: "string"}},
                    {favoriteColor: {$type: "string"}},
                    {favoriteBeverage: {$type: "string"}},
                    {favoriteDay: {$type: "string"}},
                    {favoriteFood: {$type: "string"}},
                    {favoriteSport: {$type: "string"}},
                    {favoriteMovie: {$type: "string"}},
                    {favoriteShow: {$type: "string"}}
                ]
            }
        };
        assert.commandWorked(dbInvalid.runCommand(modCmd));
    }
    assert.gt(cursor.itcount(), 0, errMsg);
}
// Test that we invalidate the old namespace record ID when we remove, rename, or move a
// namespace record.
for (let j = 2; j < 7; j++) {
    testNamespaceInvalidation(DROP, j);
    testNamespaceInvalidation(RENAME, j);
    testNamespaceInvalidation(MOVE, j);
}
}());