summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/shell_scoped_thread.js
blob: 7c2b92457a155853d64fabab48b465da271ffbb7 (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
/**
 * Tests ScopedThread from jstests/libs/parallelTester.js.
 */

load('jstests/libs/parallelTester.js');  // for ScopedThread

(() => {
    "use strict";

    const tests = [];

    tests.push(function checkTestData() {
        let testData = TestData;
        let worker = new ScopedThread((testData) => {
            assert.eq(TestData, testData);
        }, testData);
        worker.start();
        worker.join();
        assert(!worker.hasFailed());
    });

    tests.push(function checkTestDataWithOtherArgs() {
        let testData = TestData;
        let arg1 = 1;
        let arg2 = {a: 1};
        let worker = new ScopedThread((testData, arg1, arg2) => {
            assert.eq(TestData, testData);
            assert.eq(arg1, 1);
            assert.eq(arg2, {a: 1});
        }, testData, arg1, arg2);
        worker.start();
        worker.join();
        assert(!worker.hasFailed());
    });

    tests.push(function checkTestDataWithFunc() {
        let oldTestData = TestData;
        if (!TestData) {
            TestData = {};
        }
        TestData.func = function myfunc(x) {
            return x;
        };
        let testData = TestData;
        try {
            let worker = new ScopedThread((testData) => {
                // We cannot directly compare testData & TestData because the func object
                // has extra whitespace and line control.
                assert.eq(Object.keys(TestData), Object.keys(testData));
                for (var property in TestData) {
                    if (TestData.hasOwnProperty(property) && !TestData.property instanceof Code) {
                        assert.eq(TestData.property, testData.property);
                    }
                }
                assert.eq(testData.func(7), 7);
                assert.eq(TestData.func(7), 7);
            }, testData);
            worker.start();
            worker.join();
            assert(!worker.hasFailed());
        } finally {
            TestData = oldTestData;
        }
    });

    tests.push(function nullTestData() {
        let oldTestData = TestData;
        TestData = null;
        try {
            let worker = new ScopedThread(() => {
                assert.eq(TestData, null);
            });
            worker.start();
            worker.join();
            assert(!worker.hasFailed());
        } finally {
            TestData = oldTestData;
        }
    });

    tests.push(function undefinedTestData() {
        let oldTestData = TestData;
        TestData = undefined;
        try {
            let worker = new ScopedThread(() => {
                assert.eq(TestData, undefined);
            });
            worker.start();
            worker.join();
            assert(!worker.hasFailed());
        } finally {
            TestData = oldTestData;
        }
    });

    /* main */

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