summaryrefslogtreecommitdiff
path: root/jstests/concurrency/fsm_workloads/schema_validator_with_expr_variables.js
blob: ea847215a299b5aa3b873fb3dfc3538427d1475c (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
/**
 * Test to verify that the schema validator works correctly in a multi-threaded environment, when
 * $expr uses expressions which mutate variable values while executing ($let, $map etc).
 * @tags: [requires_non_retryable_writes]
 */

"use strict";

var $config = (function() {
    function setup(db, collName) {
        for (let i = 0; i < 200; ++i) {
            assertAlways.commandWorked(
                db[collName].insert({_id: i, a: i, one: 1, counter: 0, array: [0, i]}));
        }

        // Add a validator which checks that field 'a' has value 5 and sum of the elements in field
        // 'array' is 5. The expression is purposefully complex so that it can create a stress on
        // expressions with variables.
        assertAlways.commandWorked(db.runCommand({
            collMod: collName,
            validator: {
                $expr: {
                    $and: [
                        {
                          $eq: [
                              5,
                              {
                                $let: {
                                    vars: {item: {$multiply: ["$a", "$one"]}},
                                    in : {$multiply: ["$$item", "$one"]}
                                }
                              }
                          ]
                        },
                        {
                          $eq: [
                              5,
                              {
                                $sum: {
                                    $map:
                                        {"input": "$array", "as": "item", "in": "$$item"}
                                }
                              }
                          ]
                        }
                    ]
                }
            }
        }));
    }

    const states = {
        applyValidator: function(db, collName) {
            assertAlways.commandWorked(db[collName].update({_id: 5}, {$inc: {counter: 1}}));
            assertAlways.commandFailedWithCode(
                db[collName].update({_id: 4}, {$set: {a: 4}, $inc: {counter: 1}}),
                ErrorCodes.DocumentValidationFailure);

            // Update all the documents in the collection.
            assertAlways.commandWorked(db[collName].update(
                {}, {$set: {a: 5, array: [2, 3]}, $inc: {counter: 1}}, {multi: true}));

            // Validation fails when elements of 'array' doesn't add up to 5.
            assertAlways.commandFailedWithCode(
                db[collName].update({_id: 4}, {$set: {a: 5, array: [2, 2]}}),
                ErrorCodes.DocumentValidationFailure);
        }
    };

    let transitions = {applyValidator: {applyValidator: 1}};

    return {
        threadCount: 50,
        iterations: 100,
        states: states,
        startState: "applyValidator",
        transitions: transitions,
        setup: setup
    };
})();