diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2019-04-14 17:34:08 +0200 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2019-04-26 18:43:11 +0200 |
commit | 1d022e825356dfba9bef2d6f194b9e93d63b726d (patch) | |
tree | b102789e5cdf7c399596b96e3818cbff96f62e89 /test/parallel/test-worker-process-cwd.js | |
parent | 55147d7d9939cce1bbc2c8a70ac7ebe03e91e5cf (diff) | |
download | android-node-v8-1d022e825356dfba9bef2d6f194b9e93d63b726d.tar.gz android-node-v8-1d022e825356dfba9bef2d6f194b9e93d63b726d.tar.bz2 android-node-v8-1d022e825356dfba9bef2d6f194b9e93d63b726d.zip |
process: improve cwd performance
This caches the current working directory and only updates the variable
if `process.chdir()` is called.
PR-URL: https://github.com/nodejs/node/pull/27224
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Diffstat (limited to 'test/parallel/test-worker-process-cwd.js')
-rw-r--r-- | test/parallel/test-worker-process-cwd.js | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/test/parallel/test-worker-process-cwd.js b/test/parallel/test-worker-process-cwd.js new file mode 100644 index 0000000000..dec70ac07c --- /dev/null +++ b/test/parallel/test-worker-process-cwd.js @@ -0,0 +1,44 @@ +'use strict'; +const assert = require('assert'); +const common = require('../common'); +const { Worker, isMainThread, parentPort } = require('worker_threads'); + +// 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'); + } + const w = new Worker(__filename); + process.chdir('..'); + w.on('message', common.mustCall((message) => { + 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(); + 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(); + }); + })); +} 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(); + }); +} |