summaryrefslogtreecommitdiff
path: root/jstests/replsets/capped_insert_order.js
blob: 0b17f9ff144ab6135ef7a318f2d6ceaa38a78243 (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
// Check that inserts to capped collections have the same order on primary and secondary.
// See SERVER-21483.

(function() {
    "use strict";

    var replTest = new ReplSetTest({name: 'capped_insert_order', nodes: 2});
    replTest.startSet();
    replTest.initiate();

    var master = replTest.getPrimary();
    var slave = replTest.liveNodes.slaves[0];

    var dbName = "db";
    var masterDb = master.getDB(dbName);
    var slaveDb = slave.getDB(dbName);

    var collectionName = "collection";
    var masterColl = masterDb[collectionName];
    var slaveColl = slaveDb[collectionName];

    // Making a large capped collection to ensure that every document fits.
    masterDb.createCollection(collectionName, {capped: true, size: 1024 * 1024});

    // Insert 1000 docs with _id from 0 to 999 inclusive.
    const nDocuments = 1000;
    var batch = masterColl.initializeOrderedBulkOp();
    for (var i = 0; i < nDocuments; i++) {
        batch.insert({_id: i});
    }
    assert.writeOK(batch.execute());
    replTest.awaitReplication();

    function checkCollection(coll) {
        assert.eq(coll.count(), nDocuments);

        var i = 0;
        coll.find().forEach(function(doc) {
            assert.eq(doc._id, i);
            i++;
        });
        assert.eq(i, nDocuments);
    }

    checkCollection(masterColl);
    checkCollection(slaveColl);

    replTest.stopSet();
})();