summaryrefslogtreecommitdiff
path: root/jstests/core/top.js
blob: 02c1a3e0d1c85e14bc6703dfa0dd9f5706346e15 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
 * 1. check top numbers are correct
 *
 * This test attempts to perform read operations and get statistics using the top command. The
 * former operation may be routed to a secondary in the replica set, whereas the latter must be
 * routed to the primary.
 *
 * The test runs commands that are not allowed with security token: top.
 * @tags: [
 *   not_allowed_with_security_token,
 *    assumes_read_preference_unchanged,
 *    requires_fastcount,
 *    # This test contains assertions on the number of executed operations, and tenant migrations
 *    # passthrough suites automatically retry operations on TenantMigrationAborted errors.
 *    tenant_migration_incompatible,
 *    does_not_support_repeated_reads,
 *    requires_fcv_62,
 *    # TODO SERVER-67640: Verify 'top' and $collStats work correctly for queries in CQF.
 *    cqf_incompatible,
 * ]
 */

(function() {
load("jstests/libs/stats.js");

var name = "toptest";

var testDB = db.getSiblingDB(name);
var testColl = testDB[name + "coll"];
testColl.drop();

// Perform an operation on the collection so that it is present in the "top" command's output.
assert.eq(testColl.find({}).itcount(), 0);

//  This variable is used to get differential output
var lastTop = getTop(testColl);
if (lastTop === undefined) {
    return;
}

var numRecords = 100;

//  Insert
for (var i = 0; i < numRecords; i++) {
    assert.commandWorked(testColl.insert({_id: i}));
}
assertTopDiffEq(testColl, lastTop, "insert", numRecords);
lastTop = assertTopDiffEq(testColl, lastTop, "writeLock", numRecords);

// Update
for (i = 0; i < numRecords; i++) {
    assert.commandWorked(testColl.update({_id: i}, {x: i}));
}
lastTop = assertTopDiffEq(testColl, lastTop, "update", numRecords);

// Queries
var query = {};
for (i = 0; i < numRecords; i++) {
    query[i] = testColl.find({x: {$gte: i}}).batchSize(2);
    assert.eq(query[i].next()._id, i);
}
lastTop = assertTopDiffEq(testColl, lastTop, "queries", numRecords);

// Getmore
for (i = 0; i < numRecords / 2; i++) {
    assert.eq(query[i].next()._id, i + 1);
    assert.eq(query[i].next()._id, i + 2);
    assert.eq(query[i].next()._id, i + 3);
    assert.eq(query[i].next()._id, i + 4);
}
lastTop = assertTopDiffEq(testColl, lastTop, "getmore", numRecords);

// Remove
for (i = 0; i < numRecords; i++) {
    assert.commandWorked(testColl.remove({_id: 1}));
}
lastTop = assertTopDiffEq(testColl, lastTop, "remove", numRecords);

// Upsert, note that these are counted as updates, not inserts
for (i = 0; i < numRecords; i++) {
    assert.commandWorked(testColl.update({_id: i}, {x: i}, {upsert: 1}));
}
lastTop = assertTopDiffEq(testColl, lastTop, "update", numRecords);

// Commands
var res;

// "count" command
lastTop = getTop(testColl);  // ignore any commands before this
if (lastTop === undefined) {
    return;
}

for (i = 0; i < numRecords; i++) {
    res = assert.commandWorked(testDB.runCommand({count: testColl.getName()}));
    assert.eq(res.n, numRecords, tojson(res));
}
lastTop = assertTopDiffEq(testColl, lastTop, "commands", numRecords);

// "findAndModify" command
lastTop = getTop(testColl);
if (lastTop === undefined) {
    return;
}

for (i = 0; i < numRecords; i++) {
    res = assert.commandWorked(testDB.runCommand({
        findAndModify: testColl.getName(),
        query: {_id: i},
        update: {$inc: {x: 1}},
    }));
    assert.eq(res.value.x, i, tojson(res));
}
lastTop = assertTopDiffEq(testColl, lastTop, "commands", numRecords);

lastTop = getTop(testColl);
if (lastTop === undefined) {
    return;
}

for (i = 0; i < numRecords; i++) {
    res = assert.commandWorked(testDB.runCommand({
        findAndModify: testColl.getName(),
        query: {_id: i},
        remove: true,
    }));
    assert.eq(res.value.x, i + 1, tojson(res));
}
lastTop = assertTopDiffEq(testColl, lastTop, "commands", numRecords);

// aggregate
assert.eq(0, testColl.aggregate([]).itcount());  // All records were just deleted.
assertTopDiffEq(testColl, lastTop, "commands", 1);
lastTop = assertTopDiffEq(testColl, lastTop, "readLock", 1);

// getIndexes
assert.eq(1, testColl.getIndexes().length);
assertTopDiffEq(testColl, lastTop, "commands", 1);
lastTop = assertTopDiffEq(testColl, lastTop, "readLock", 1);

// aggregate with $indexStats
assert.doesNotThrow(() => testColl.aggregate([{$indexStats: {}}]).itcount());
assertTopDiffEq(testColl, lastTop, "commands", 1);
lastTop = assertTopDiffEq(testColl, lastTop, "readLock", 1);

// createIndex
res = assert.commandWorked(testColl.createIndex({x: 1}));
assertTopDiffEq(testColl, lastTop, "writeLock", 1);
lastTop = assertTopDiffEq(testColl, lastTop, "commands", 1);

// dropIndex
res = assert.commandWorked(testColl.dropIndex({x: 1}));
assertTopDiffEq(testColl, lastTop, "commands", 1);
lastTop = assertTopDiffEq(testColl, lastTop, "writeLock", 1);
}());