summaryrefslogtreecommitdiff
path: root/jstests/concurrency/fsm_libs/composer.js
blob: 495648fb01af867e91087ba8c3428bd0627c8d46 (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
load('jstests/concurrency/fsm_libs/fsm.js');

var composer = (function() {

    function runCombinedFSM(workloads, configs, mixProb) {
        // TODO: what if a workload depends on iterations?
        var iterations = 100;

        assert.eq(
            AssertLevel.ALWAYS, globalAssertLevel, 'global assertion level is not set as ALWAYS');

        var currentWorkload = getRandomElem(workloads, Random.rand());
        var currentState = configs[currentWorkload].startState;

        var myDB, collName;
        var first = true;
        workloads.forEach(function(workload) {
            var args = configs[workload];
            if (!first) {
                assert.eq(myDB, args.db, 'expected all workloads to use same database');
                assert.eq(collName, args.collName, 'expected all workloads to use same collection');
            }
            myDB = args.db;
            collName = args.collName;
            first = false;

            if (workload !== currentWorkload) {
                args.states[args.startState].call(args.data, myDB, collName);
            }
        });

        // Runs an interleaving of the specified workloads
        for (var i = 0; i < iterations; ++i) {
            var args = configs[currentWorkload];
            args.states[currentState].call(args.data, myDB, collName);

            // Transition to another valid state of the current workload,
            // with probability '1 - mixProb'
            if (Random.rand() >= mixProb) {
                var nextState =
                    fsm._getWeightedRandomChoice(args.transitions[currentState], Random.rand());
                currentState = nextState;
                continue;
            }

            // Transition to a state of another workload with probability 'mixProb'
            var otherStates = [];
            workloads.forEach(function(workload) {
                if (workload === currentWorkload) {
                    return;
                }

                var args = configs[workload];
                Object.keys(args.states)
                    .forEach(function(state) {
                        if (state !== args.startState) {
                            otherStates.push({workload: workload, state: state});
                        }
                    });
            });

            var next = getRandomElem(otherStates, Random.rand());
            currentWorkload = next.workload;
            currentState = next.state;
        }
    }

    function getRandomElem(items, randVal) {
        assert.gt(items.length, 0);
        return items[Math.floor(randVal * items.length)];
    }

    return {
        run: runCombinedFSM
    };

})();