@@ -3,43 +3,63 @@ 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
+ // 1. Start the first worker.
14
20
const w = new Worker ( __filename ) ;
15
- process . chdir ( '..' ) ;
16
- w . on ( 'message' , common . mustCall ( ( message ) => {
21
+ w . once ( 'message' , common . mustCall ( ( message ) => {
22
+ // 5. step: change the cwd and send that to the spawned worker
17
23
assert . strictEqual ( message , process . cwd ( ) ) ;
18
24
process . chdir ( '..' ) ;
19
25
w . postMessage ( process . cwd ( ) ) ;
20
26
} ) ) ;
21
27
} else if ( ! process . env . SECOND_WORKER ) {
22
28
process . env . SECOND_WORKER = '1' ;
23
- const firstCwd = process . cwd ( ) ;
29
+
30
+ // 2. Save the current cwd and verify that `process.cwd` includes the
31
+ // Atomics.load call and spawn a new worker.
32
+ const cwd = process . cwd ( ) ;
33
+ assert ( process . cwd . toString ( ) . includes ( 'Atomics.load' ) ) ;
34
+
24
35
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
- } ) ;
36
+ w . once ( 'message' , common . mustCall ( ( message ) => {
37
+ // 4. Verify at the current cwd is identical to the received and the
38
+ // original one.
39
+ assert . strictEqual ( process . cwd ( ) , message ) ;
40
+ assert . strictEqual ( message , cwd ) ;
41
+ parentPort . postMessage ( cwd ) ;
42
+ } ) ) ;
43
+
44
+ parentPort . once ( 'message' , common . mustCall ( ( message ) => {
45
+ // 6. Verify that the current cwd is identical to the received one but not
46
+ // with the original one.
47
+ assert . strictEqual ( process . cwd ( ) , message ) ;
48
+ assert . notStrictEqual ( message , cwd ) ;
49
+ w . postMessage ( message ) ;
35
50
} ) ) ;
36
51
} 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
- } ) ;
52
+ // 3. Save the current cwd, post back to the "main" thread and verify that
53
+ // `process.cwd` includes the Atomics.load call.
54
+ const cwd = process . cwd ( ) ;
55
+ // Send the current cwd to the parent.
56
+ parentPort . postMessage ( cwd ) ;
57
+ assert ( process . cwd . toString ( ) . includes ( 'Atomics.load' ) ) ;
58
+
59
+ parentPort . once ( 'message' , common . mustCall ( ( message ) => {
60
+ // 6. Verify that the current cwd is identical to the received one but
61
+ // not with the original one.
62
+ assert . strictEqual ( process . cwd ( ) , message ) ;
63
+ assert . notStrictEqual ( message , cwd ) ;
64
+ } ) ) ;
45
65
}
0 commit comments