@@ -3,43 +3,65 @@ const common = require('../common');
3
3
const assert = require ( 'assert' ) ;
4
4
const { Worker, isMainThread, parentPort } = require ( 'worker_threads' ) ;
5
5
6
+ // Verify that cwd changes from the main thread are handled correctly in
7
+ // workers.
8
+
6
9
// Do not use isMainThread directly, otherwise the test would time out in case
7
10
// it's started inside of another worker thread.
8
11
if ( ! process . env . HAS_STARTED_WORKER ) {
9
12
process . env . HAS_STARTED_WORKER = '1' ;
10
13
if ( ! isMainThread ) {
11
14
common . skip ( 'This test can only run as main thread' ) ;
12
15
}
16
+ // Normalize the current working dir to also work with the root folder.
13
17
process . chdir ( __dirname ) ;
18
+
19
+ assert ( ! process . cwd . toString ( ) . includes ( 'Atomics.load' ) ) ;
20
+
21
+ // 1. Start the first worker.
14
22
const w = new Worker ( __filename ) ;
15
- process . chdir ( '..' ) ;
16
- w . on ( 'message' , common . mustCall ( ( message ) => {
23
+ w . once ( 'message' , common . mustCall ( ( message ) => {
24
+ // 5. Change the cwd and send that to the spawned worker.
17
25
assert . strictEqual ( message , process . cwd ( ) ) ;
18
26
process . chdir ( '..' ) ;
19
27
w . postMessage ( process . cwd ( ) ) ;
20
28
} ) ) ;
21
29
} else if ( ! process . env . SECOND_WORKER ) {
22
30
process . env . SECOND_WORKER = '1' ;
23
- const firstCwd = process . cwd ( ) ;
31
+
32
+ // 2. Save the current cwd and verify that `process.cwd` includes the
33
+ // Atomics.load call and spawn a new worker.
34
+ const cwd = process . cwd ( ) ;
35
+ assert ( ! process . cwd . toString ( ) . includes ( 'Atomics.load' ) ) ;
36
+
24
37
const w = new Worker ( __filename ) ;
25
- w . on ( 'message' , common . mustCall ( ( message ) => {
26
- assert . strictEqual ( message , process . cwd ( ) ) ;
27
- parentPort . postMessage ( firstCwd ) ;
28
- parentPort . onmessage = common . mustCall ( ( obj ) => {
29
- const secondCwd = process . cwd ( ) ;
30
- assert . strictEqual ( secondCwd , obj . data ) ;
31
- assert . notStrictEqual ( secondCwd , firstCwd ) ;
32
- w . postMessage ( secondCwd ) ;
33
- parentPort . unref ( ) ;
34
- } ) ;
38
+ w . once ( 'message' , common . mustCall ( ( message ) => {
39
+ // 4. Verify at the current cwd is identical to the received and the
40
+ // original one.
41
+ assert . strictEqual ( process . cwd ( ) , message ) ;
42
+ assert . strictEqual ( message , cwd ) ;
43
+ parentPort . postMessage ( cwd ) ;
44
+ } ) ) ;
45
+
46
+ parentPort . once ( 'message' , common . mustCall ( ( message ) => {
47
+ // 6. Verify that the current cwd is identical to the received one but not
48
+ // with the original one.
49
+ assert . strictEqual ( process . cwd ( ) , message ) ;
50
+ assert . notStrictEqual ( message , cwd ) ;
51
+ w . postMessage ( message ) ;
35
52
} ) ) ;
36
53
} else {
37
- const firstCwd = process . cwd ( ) ;
38
- parentPort . postMessage ( firstCwd ) ;
39
- parentPort . onmessage = common . mustCall ( ( obj ) => {
40
- const secondCwd = process . cwd ( ) ;
41
- assert . strictEqual ( secondCwd , obj . data ) ;
42
- assert . notStrictEqual ( secondCwd , firstCwd ) ;
43
- process . exit ( ) ;
44
- } ) ;
54
+ // 3. Save the current cwd, post back to the "main" thread and verify that
55
+ // `process.cwd` includes the Atomics.load call.
56
+ const cwd = process . cwd ( ) ;
57
+ // Send the current cwd to the parent.
58
+ parentPort . postMessage ( cwd ) ;
59
+ assert ( ! process . cwd . toString ( ) . includes ( 'Atomics.load' ) ) ;
60
+
61
+ parentPort . once ( 'message' , common . mustCall ( ( message ) => {
62
+ // 7. Verify that the current cwd is identical to the received one but
63
+ // not with the original one.
64
+ assert . strictEqual ( process . cwd ( ) , message ) ;
65
+ assert . notStrictEqual ( message , cwd ) ;
66
+ } ) ) ;
45
67
}
0 commit comments