-
Notifications
You must be signed in to change notification settings - Fork 30.1k
/
test-worker-process-cwd.js
67 lines (59 loc) · 2.4 KB
/
test-worker-process-cwd.js
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
'use strict';
const common = require('../common');
const assert = require('assert');
const { Worker, isMainThread, parentPort } = require('worker_threads');
// Verify that cwd changes from the main thread are handled correctly in
// workers.
// Do not use isMainThread directly, otherwise the test would time out in case
// it's started inside of another worker thread.
if (!process.env.HAS_STARTED_WORKER) {
process.env.HAS_STARTED_WORKER = '1';
if (!isMainThread) {
common.skip('This test can only run as main thread');
}
// Normalize the current working dir to also work with the root folder.
process.chdir(__dirname);
assert.doesNotMatch(process.cwd.toString(), /AtomicsLoad/);
// 1. Start the first worker.
const w = new Worker(__filename);
w.once('message', common.mustCall((message) => {
// 5. Change the cwd and send that to the spawned worker.
assert.strictEqual(message, process.cwd());
process.chdir('..');
w.postMessage(process.cwd());
}));
} else if (!process.env.SECOND_WORKER) {
process.env.SECOND_WORKER = '1';
// 2. Save the current cwd and verify that `process.cwd` includes the
// Atomics.load call and spawn a new worker.
const cwd = process.cwd();
assert.match(process.cwd.toString(), /AtomicsLoad/);
const w = new Worker(__filename);
w.once('message', common.mustCall((message) => {
// 4. Verify at the current cwd is identical to the received and the
// original one.
assert.strictEqual(process.cwd(), message);
assert.strictEqual(message, cwd);
parentPort.postMessage(cwd);
}));
parentPort.once('message', common.mustCall((message) => {
// 6. Verify that the current cwd is identical to the received one but not
// with the original one.
assert.strictEqual(process.cwd(), message);
assert.notStrictEqual(message, cwd);
w.postMessage(message);
}));
} else {
// 3. Save the current cwd, post back to the "main" thread and verify that
// `process.cwd` includes the Atomics.load call.
const cwd = process.cwd();
// Send the current cwd to the parent.
parentPort.postMessage(cwd);
assert.match(process.cwd.toString(), /AtomicsLoad/);
parentPort.once('message', common.mustCall((message) => {
// 7. Verify that the current cwd is identical to the received one but
// not with the original one.
assert.strictEqual(process.cwd(), message);
assert.notStrictEqual(message, cwd);
}));
}