@@ -3,43 +3,65 @@ 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+ assert ( ! process . cwd . toString ( ) . includes ( 'Atomics.load' ) ) ;
20+
21+ // 1. Start the first worker.
1422 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.
1725 assert . strictEqual ( message , process . cwd ( ) ) ;
1826 process . chdir ( '..' ) ;
1927 w . postMessage ( process . cwd ( ) ) ;
2028 } ) ) ;
2129} else if ( ! process . env . SECOND_WORKER ) {
2230 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+
2437 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 ) ;
3552 } ) ) ;
3653} 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+ } ) ) ;
4567}
0 commit comments