Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix race condition in test-worker-process-cwd.js #28609

Closed
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 41 additions & 21 deletions test/parallel/test-worker-process-cwd.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,63 @@ const common = require('../common');
const assert = require('assert');
const { Worker, isMainThread, parentPort } = require('worker_threads');

// Verify that cwd changes from the main thread are handled correctly in
// workers.

// Do not use isMainThread directly, otherwise the test would time out in case
// it's started inside of another worker thread.
if (!process.env.HAS_STARTED_WORKER) {
process.env.HAS_STARTED_WORKER = '1';
if (!isMainThread) {
common.skip('This test can only run as main thread');
}
// Normalize the current working dir to also work with the root folder.
process.chdir(__dirname);

// 1. Start the first worker.
const w = new Worker(__filename);
process.chdir('..');
w.on('message', common.mustCall((message) => {
w.once('message', common.mustCall((message) => {
// 5. Change the cwd and send that to the spawned worker.
assert.strictEqual(message, process.cwd());
process.chdir('..');
w.postMessage(process.cwd());
}));
} else if (!process.env.SECOND_WORKER) {
process.env.SECOND_WORKER = '1';
const firstCwd = process.cwd();

// 2. Save the current cwd and verify that `process.cwd` includes the
// Atomics.load call and spawn a new worker.
const cwd = process.cwd();
assert(process.cwd.toString().includes('Atomics.load'));
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved

const w = new Worker(__filename);
w.on('message', common.mustCall((message) => {
assert.strictEqual(message, process.cwd());
parentPort.postMessage(firstCwd);
parentPort.onmessage = common.mustCall((obj) => {
const secondCwd = process.cwd();
assert.strictEqual(secondCwd, obj.data);
assert.notStrictEqual(secondCwd, firstCwd);
w.postMessage(secondCwd);
parentPort.unref();
});
w.once('message', common.mustCall((message) => {
// 4. Verify at the current cwd is identical to the received and the
// original one.
assert.strictEqual(process.cwd(), message);
assert.strictEqual(message, cwd);
parentPort.postMessage(cwd);
}));

parentPort.once('message', common.mustCall((message) => {
// 6. Verify that the current cwd is identical to the received one but not
// with the original one.
assert.strictEqual(process.cwd(), message);
assert.notStrictEqual(message, cwd);
w.postMessage(message);
}));
} else {
const firstCwd = process.cwd();
parentPort.postMessage(firstCwd);
parentPort.onmessage = common.mustCall((obj) => {
const secondCwd = process.cwd();
assert.strictEqual(secondCwd, obj.data);
assert.notStrictEqual(secondCwd, firstCwd);
process.exit();
});
// 3. Save the current cwd, post back to the "main" thread and verify that
// `process.cwd` includes the Atomics.load call.
const cwd = process.cwd();
// Send the current cwd to the parent.
parentPort.postMessage(cwd);
assert(process.cwd.toString().includes('Atomics.load'));

parentPort.once('message', common.mustCall((message) => {
// 6. Verify that the current cwd is identical to the received one but
// not with the original one.
assert.strictEqual(process.cwd(), message);
assert.notStrictEqual(message, cwd);
}));
}