summaryrefslogtreecommitdiff
path: root/test/parallel/test-worker-process-cwd.js
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2019-07-09 16:41:55 +0200
committerRich Trott <rtrott@gmail.com>2019-07-17 20:52:52 -0700
commit195239a5f697214a86ca8b8eeefa1947ce409385 (patch)
tree25bfbd78d0b83a432004959f5c5409957e7d8fa8 /test/parallel/test-worker-process-cwd.js
parent971915e89f1106444453eba39263ade92b3ed598 (diff)
downloadandroid-node-v8-195239a5f697214a86ca8b8eeefa1947ce409385.tar.gz
android-node-v8-195239a5f697214a86ca8b8eeefa1947ce409385.tar.bz2
android-node-v8-195239a5f697214a86ca8b8eeefa1947ce409385.zip
test: fix race condition in test-worker-process-cwd.js
This simplifies the test logic and fixes the race condition that could happen right now. PR-URL: https://github.com/nodejs/node/pull/28609 Refs: https://github.com/nodejs/node/issues/28193 Closes: https://github.com/nodejs/node/pull/28477 Fixes: https://github.com/nodejs/node/issues/27669 Fixes: https://github.com/nodejs/node/issues/28477 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'test/parallel/test-worker-process-cwd.js')
-rw-r--r--test/parallel/test-worker-process-cwd.js64
1 files changed, 43 insertions, 21 deletions
diff --git a/test/parallel/test-worker-process-cwd.js b/test/parallel/test-worker-process-cwd.js
index e281610a49..e24515cce7 100644
--- a/test/parallel/test-worker-process-cwd.js
+++ b/test/parallel/test-worker-process-cwd.js
@@ -3,6 +3,9 @@ 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) {
@@ -10,36 +13,55 @@ if (!process.env.HAS_STARTED_WORKER) {
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);
+
+ assert(!process.cwd.toString().includes('Atomics.load'));
+
+ // 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'));
+
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) => {
+ // 7. 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);
+ }));
}