summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/shell_scoped_thread.js
blob: 32f11f05bb25fd2435c0b87585715cc9746ec1d6 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
 * Tests Thread from jstests/libs/parallelTester.js.
 */

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

(() => {
    "use strict";

    const tests = [];

    tests.push(function checkTestData() {
        let testData = TestData;
        let worker = new Thread((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 Thread((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 Thread((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 Thread(() => {
                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 Thread(() => {
                assert.eq(TestData, undefined);
            });
            worker.start();
            worker.join();
            assert(!worker.hasFailed());
        } finally {
            TestData = oldTestData;
        }
    });

    function testUncaughtException(joinFn) {
        const thread = new Thread(function myFunction() {
            throw new Error("Intentionally thrown inside Thread");
        });
        thread.start();

        let error = assert.throws(joinFn, [thread]);
        assert(/Intentionally thrown inside Thread/.test(error.message),
               () => "Exception didn't include the message from the exception thrown in Thread: " +
                   tojson(error.message));
        assert(/myFunction@/.test(error.stack),
               () => "Exception doesn't contain stack frames from within the Thread: " +
                   tojson(error.stack));
        assert(/testUncaughtException@/.test(error.stack),
               () => "Exception doesn't contain stack frames from caller of the Thread: " +
                   tojson(error.stack));

        error = assert.throws(() => thread.join());
        assert.eq("Thread not running",
                  error.message,
                  "join() is expected to be called only once for the thread");

        assert.eq(true,
                  thread.hasFailed(),
                  "Uncaught exception didn't cause thread to be marked as having failed");
        assert.doesNotThrow(() => thread.returnData(),
                            [],
                            "returnData() threw an exception after join() had been called");
        assert.eq(undefined,
                  thread.returnData(),
                  "returnData() shouldn't have anything to return if the thread failed");
    }

    tests.push(function testUncaughtExceptionAndWaitUsingJoin() {
        testUncaughtException(thread => thread.join());
    });

    // The returnData() method internally calls the join() method and should also throw an exception
    // if the Thread had an uncaught exception.
    tests.push(function testUncaughtExceptionAndWaitUsingReturnData() {
        testUncaughtException(thread => thread.returnData());
    });

    tests.push(function testUncaughtExceptionInNativeCode() {
        const thread = new Thread(function myFunction() {
            new Timestamp(-1);
        });
        thread.start();

        const error = assert.throws(() => thread.join());
        assert(/Timestamp/.test(error.message),
               () => "Exception didn't include the message from the exception thrown in Thread: " +
                   tojson(error.message));
        assert(/myFunction@/.test(error.stack),
               () => "Exception doesn't contain stack frames from within the Thread: " +
                   tojson(error.stack));
    });

    tests.push(function testUncaughtExceptionFromNestedThreads() {
        const thread = new Thread(function myFunction1() {
            load("jstests/libs/parallelTester.js");

            const thread = new Thread(function myFunction2() {
                load("jstests/libs/parallelTester.js");

                const thread = new Thread(function myFunction3() {
                    throw new Error("Intentionally thrown inside Thread");
                });

                thread.start();
                thread.join();
            });

            thread.start();
            thread.join();
        });
        thread.start();

        const error = assert.throws(() => thread.join());
        assert(/Intentionally thrown inside Thread/.test(error.message),
               () => "Exception didn't include the message from the exception thrown in Thread: " +
                   tojson(error.message));
        assert(/myFunction3@/.test(error.stack),
               () => "Exception doesn't contain stack frames from within the innermost Thread: " +
                   tojson(error.stack));
        assert(/myFunction2@/.test(error.stack),
               () => "Exception doesn't contain stack frames from within an inner Thread: " +
                   tojson(error.stack));
        assert(/myFunction1@/.test(error.stack),
               () => "Exception doesn't contain stack frames from within the outermost Thread: " +
                   tojson(error.stack));
    });

    /* main */

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