|
| 1 | +'use strict'; |
| 2 | +const assert = require('assert'); |
| 3 | +const common = require('../common'); |
| 4 | +const { Worker, isMainThread, parentPort } = require('worker_threads'); |
| 5 | + |
| 6 | +// Do not use isMainThread directly, otherwise the test would time out in case |
| 7 | +// it's started inside of another worker thread. |
| 8 | +if (!process.env.HAS_STARTED_WORKER) { |
| 9 | + process.env.HAS_STARTED_WORKER = '1'; |
| 10 | + if (!isMainThread) { |
| 11 | + common.skip('This test can only run as main thread'); |
| 12 | + } |
| 13 | + const w = new Worker(__filename); |
| 14 | + process.chdir('..'); |
| 15 | + w.on('message', common.mustCall((message) => { |
| 16 | + assert.strictEqual(message, process.cwd()); |
| 17 | + process.chdir('..'); |
| 18 | + w.postMessage(process.cwd()); |
| 19 | + })); |
| 20 | +} else if (!process.env.SECOND_WORKER) { |
| 21 | + process.env.SECOND_WORKER = '1'; |
| 22 | + const firstCwd = process.cwd(); |
| 23 | + const w = new Worker(__filename); |
| 24 | + w.on('message', common.mustCall((message) => { |
| 25 | + assert.strictEqual(message, process.cwd()); |
| 26 | + parentPort.postMessage(firstCwd); |
| 27 | + parentPort.onmessage = common.mustCall((obj) => { |
| 28 | + const secondCwd = process.cwd(); |
| 29 | + assert.strictEqual(secondCwd, obj.data); |
| 30 | + assert.notStrictEqual(secondCwd, firstCwd); |
| 31 | + w.postMessage(secondCwd); |
| 32 | + parentPort.unref(); |
| 33 | + }); |
| 34 | + })); |
| 35 | +} else { |
| 36 | + const firstCwd = process.cwd(); |
| 37 | + parentPort.postMessage(firstCwd); |
| 38 | + parentPort.onmessage = common.mustCall((obj) => { |
| 39 | + const secondCwd = process.cwd(); |
| 40 | + assert.strictEqual(secondCwd, obj.data); |
| 41 | + assert.notStrictEqual(secondCwd, firstCwd); |
| 42 | + process.exit(); |
| 43 | + }); |
| 44 | +} |
0 commit comments