summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/shell_write_assertions.js
blob: c89e881c6049be36623921c85a0054bc059f0a49 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
 * Tests for the write assertion functions in mongo/shell/assert.js.
 *
 * @tags: [requires_replication]
 */

load("jstests/libs/write_concern_util.js");

(() => {
    "use strict";

    const kReallyShortTimeoutMS = 500;

    const replTest = new ReplSetTest({nodes: 1});
    replTest.startSet();
    replTest.initiate();

    const conn = replTest.getPrimary();
    const db = conn.getDB("writeAssertions");
    assert.neq(null, conn, "mongodb was unable to start up");
    const tests = [];

    function setup() {
        db.coll.drop();
    }

    function _doFailedWrite(collection) {
        const duplicateId = 42;

        const res = collection.insert({_id: duplicateId});
        assert.writeOK(res, "write to collection should have been successful");
        const failedRes = collection.insert({_id: duplicateId});
        assert.writeError(failedRes, "duplicate key write should have failed");
        return failedRes;
    }

    /* writeOK tests */
    tests.push(function writeOKSuccessfulWriteDoesNotCallMsgFunction() {
        var msgFunctionCalled = false;

        const result = db.coll.insert({data: "hello world"});
        assert.doesNotThrow(() => {
            assert.writeOK(result, () => {
                msgFunctionCalled = true;
            });
        });

        assert.eq(false, msgFunctionCalled, "message function should not have been called");
    });

    tests.push(function writeOKUnsuccessfulWriteDoesCallMsgFunction() {
        var msgFunctionCalled = false;

        const failedResult = _doFailedWrite(db.coll);
        assert.throws(() => {
            assert.writeOK(failedResult, () => {
                msgFunctionCalled = true;
            });
        });

        assert.eq(true, msgFunctionCalled, "message function should have been called");
    });

    /* writeError tests */
    tests.push(function writeErrorSuccessfulWriteDoesCallMsgFunction() {
        var msgFunctionCalled = false;

        const result = db.coll.insert({data: "hello world"});
        assert.throws(() => {
            assert.writeError(result, () => {
                msgFunctionCalled = true;
            });
        });

        assert.eq(true, msgFunctionCalled, "message function should have been called");
    });

    tests.push(function writeErrorUnsuccessfulWriteDoesNotCallMsgFunction() {
        var msgFunctionCalled = false;

        const failedResult = _doFailedWrite(db.coll);
        assert.doesNotThrow(() => {
            assert.writeError(failedResult, () => {
                msgFunctionCalled = true;
            });
        });

        assert.eq(false, msgFunctionCalled, "message function should not have been called");
    });

    tests.push(function writeConcernErrorIsCaughtFromInsert() {
        const result = db.coll.insert(
            {data: "hello world"}, {writeConcern: {w: 'invalid', wtimeout: kReallyShortTimeoutMS}});

        assert.throws(() => {
            assert.writeOK(result);
        });
    });

    tests.push(function writeConcernErrorCanBeIgnored() {
        const result = db.coll.insert(
            {data: "hello world"}, {writeConcern: {w: 'invalid', wtimeout: kReallyShortTimeoutMS}});

        assert.doesNotThrow(() => {
            assert.writeOK(
                result, 'write can ignore writeConcern', {ignoreWriteConcernErrors: true});
        });
    });

    /* main */

    tests.forEach((test) => {
        jsTest.log(`Starting tests '${test.name}'`);
        setup();
        test();
    });

    /* cleanup */
    replTest.stopSet();
})();