summaryrefslogtreecommitdiff
path: root/jstests/concurrency/fsm_workloads/remove_single_document.js
blob: 1bfdb2b68971941997d836d79a5db4ea4627d233 (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
'use strict';

/**
 * remove_single_document.js
 *
 * Repeatedly remove a document from the collection.
 */
var $config = (function() {

    var states = {
        remove: function remove(db, collName) {
            // try removing a random document
            var res = this.doRemove(db, collName, {rand: {$gte: Random.rand()}}, {justOne: true});
            assertAlways.lte(res.nRemoved, 1);
            if (res.nRemoved === 0) {
                // The above remove() can fail to remove a document when the random value
                // in the query is greater than any of the random values in the collection.
                // When that situation occurs, just remove an arbitrary document instead.
                res = this.doRemove(db, collName, {}, {justOne: true});
                assertAlways.lte(res.nRemoved, 1);
            }
            this.assertResult(res);
        }
    };

    var transitions = {remove: {remove: 1}};

    function setup(db, collName, cluster) {
        // insert enough documents so that each thread can remove exactly one per iteration
        var num = this.threadCount * this.iterations;
        for (var i = 0; i < num; ++i) {
            db[collName].insert({i: i, rand: Random.rand()});
        }
        assertWhenOwnColl.eq(db[collName].find().itcount(), num);
    }

    return {
        threadCount: 10,
        iterations: 20,
        states: states,
        transitions: transitions,
        setup: setup,
        data: {
            doRemove: function doRemove(db, collName, query, options) {
                return db[collName].remove(query, options);
            },
            assertResult: function assertResult(res) {
                assertAlways.writeOK(res);
                // when running on its own collection,
                // this iteration should remove exactly one document
                assertWhenOwnColl.eq(1, res.nRemoved, tojson(res));
            }
        },
        startState: 'remove'
    };

})();