summaryrefslogtreecommitdiff
path: root/jstests/core/snapshot_queries.js
blob: e4aec435b106509894f95ee383d2a0be4a519b5b (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
// Regression test for edge cases in which .snapshot() queries could historically miss documents or
// return the same document twice.
(function() {
    'use strict';

    var cursor;
    var coll = db.snapshot_queries;
    coll.drop();

    //
    // Test that a large update to the document we just returned won't cause us to return it again.
    //

    for (var i = 0; i < 3; i++) {
        assert.writeOK(coll.insert({_id: i}));
    }

    cursor = coll.find().batchSize(2).snapshot();
    assert.eq(0, cursor.next()["_id"]);
    assert.eq(1, cursor.next()["_id"]);

    // Force a document move (on MMAP) while the query is yielded for a getMore.
    var bigString = Array(1024 * 1024).toString();
    assert.writeOK(coll.update({_id: 1}, {$set: {padding: bigString}}));

    assert.eq(2, cursor.next()["_id"]);
    assert(!cursor.hasNext());

    //
    // Test that a large update to the document we are about to return won't cause us to skip that
    // doc.
    //

    coll.drop();
    assert.writeOK(coll.insert({_id: 0, padding: Array(1000).toString()}));
    for (var i = 1; i <= 3; i++) {
        assert.writeOK(coll.insert({_id: i}));
    }

    // On MMAP, this will leave space at the beginning of the collection. A document can be moved
    // into this free space.
    assert.writeOK(coll.remove({_id: 0}));

    cursor = coll.find().snapshot().batchSize(2);
    assert.eq(1, cursor.next()["_id"]);
    assert.eq(2, cursor.next()["_id"]);

    // Force a document move (on MMAP) while the query is yielded for a getMore.
    assert.writeOK(coll.update({_id: 3}, {$set: {padding: Array(100).toString()}}));

    assert.eq(3, cursor.next()["_id"]);
    assert(!cursor.hasNext());
})();