-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex2.test.js
More file actions
47 lines (38 loc) · 1.12 KB
/
ex2.test.js
File metadata and controls
47 lines (38 loc) · 1.12 KB
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
'use strict';
const { runInWorker } = require('./ex2');
function assert(condition, message) {
if (!condition) throw new Error(message || 'Assertion failed');
}
(async () => {
{
const result = await runInWorker(
(input) => ({
sum: input.a + input.b,
isMainThread: require('node:worker_threads').isMainThread,
}),
{ a: 20, b: 22 }
);
assert(result && result.sum === 42, 'Expected worker computation result');
assert(result.isMainThread === false, 'Expected function to run in worker thread');
}
{
globalThis.__outerMutationCount = 0;
const value = await runInWorker(
(input) => {
globalThis.__outerMutationCount = (globalThis.__outerMutationCount || 0) + 1;
return input * 3;
},
7
);
assert(value === 21, 'Expected computed value from worker');
assert(
globalThis.__outerMutationCount === 0,
'Expected outer scope state not to be mutated by worker execution'
);
delete globalThis.__outerMutationCount;
}
console.log('ex2 tests passed');
})().catch((err) => {
console.error(err);
process.exit(1);
});