@@ -3,43 +3,63 @@ const common = require('../common');
33const assert = require ( 'assert' ) ;
44const { Worker, isMainThread, parentPort } = require ( 'worker_threads' ) ;
55
6+ // Verify that cwd changes from the main thread are handled correctly in
7+ // workers.
8+
69// Do not use isMainThread directly, otherwise the test would time out in case
710// it's started inside of another worker thread.
811if ( ! process . env . HAS_STARTED_WORKER ) {
912 process . env . HAS_STARTED_WORKER = '1' ;
1013 if ( ! isMainThread ) {
1114 common . skip ( 'This test can only run as main thread' ) ;
1215 }
16+ // Normalize the current working dir to also work with the root folder.
1317 process . chdir ( __dirname ) ;
18+
19+ // 1. Start the first worker.
1420 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
1723 assert . strictEqual ( message , process . cwd ( ) ) ;
1824 process . chdir ( '..' ) ;
1925 w . postMessage ( process . cwd ( ) ) ;
2026 } ) ) ;
2127} else if ( ! process . env . SECOND_WORKER ) {
2228 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+
2435 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 ) ;
3550 } ) ) ;
3651} 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+ } ) ) ;
4565}
0 commit comments