From 1b9e60b4f2ff88bac67e2b5292358c7dcfebbd36 Mon Sep 17 00:00:00 2001 From: Erick Wendel Date: Tue, 1 Feb 2022 18:06:13 -0300 Subject: [PATCH 1/4] worker: improve code coverage for assignEnvironmentData function --- test/parallel/test-worker-environmentdata.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/parallel/test-worker-environmentdata.js b/test/parallel/test-worker-environmentdata.js index cac97b746acef8..aef0e1213ff6af 100644 --- a/test/parallel/test-worker-environmentdata.js +++ b/test/parallel/test-worker-environmentdata.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --expose-internals require('../common'); const { @@ -8,6 +9,8 @@ const { threadId, } = require('worker_threads'); +const { assignEnvironmentData } = require('internal/worker'); + const { deepStrictEqual, strictEqual, @@ -26,6 +29,8 @@ if (!process.env.HAS_STARTED_WORKER) { strictEqual(getEnvironmentData('foo'), 'bar'); deepStrictEqual(getEnvironmentData('hello'), { value: 'world' }); strictEqual(getEnvironmentData(1), undefined); + assignEnvironmentData(undefined); // It won't setup any key. + strictEqual(getEnvironmentData(undefined), undefined); // Recurse to make sure the environment data is inherited if (threadId <= 2) From 0a3e3c705f42ae4e1c012b5f983a368045c3a221 Mon Sep 17 00:00:00 2001 From: Erick Wendel Date: Tue, 1 Feb 2022 19:10:22 -0300 Subject: [PATCH 2/4] worker: improve code coverage for getHeapSnapshot --- lib/internal/worker.js | 1 + test/parallel/test-worker-heap-snapshot.js | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 test/parallel/test-worker-heap-snapshot.js diff --git a/lib/internal/worker.js b/lib/internal/worker.js index 1d2cd8cefd2996..2c767d49459ec0 100644 --- a/lib/internal/worker.js +++ b/lib/internal/worker.js @@ -496,6 +496,7 @@ function eventLoopUtilization(util1, util2) { } module.exports = { + kHandle, ownsProcessState, isMainThread, SHARE_ENV, diff --git a/test/parallel/test-worker-heap-snapshot.js b/test/parallel/test-worker-heap-snapshot.js new file mode 100644 index 00000000000000..01bbd908623c9b --- /dev/null +++ b/test/parallel/test-worker-heap-snapshot.js @@ -0,0 +1,22 @@ +'use strict'; + +// Flags: --expose-internals +const common = require('../common'); +const assert = require('assert'); +const { Worker } = require('worker_threads'); +const { HeapSnapshotStream } = require('internal/heap_utils'); +const { kHandle } = require('internal/worker'); + +const worker = new Worker(__filename); +const snapShotResult = { ondone: () => {} }; +worker[kHandle].takeHeapSnapshot = common.mustCall(() => snapShotResult); + +worker.on('online', () => { + const snapShotResponse = worker.getHeapSnapshot(); + snapShotResult.ondone({}); + + snapShotResponse.then(common.mustCall((heapSnapshotStream) => { + assert.ok(heapSnapshotStream instanceof HeapSnapshotStream); + worker.terminate(); + })); +}); From 0bceab4293b766b2093f4bbd699490fef81c1269 Mon Sep 17 00:00:00 2001 From: Erick Wendel Date: Tue, 1 Feb 2022 19:46:05 -0300 Subject: [PATCH 3/4] worker: add mustCall to the event to ensure the assert will be called in order --- test/parallel/test-worker-heap-snapshot.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-worker-heap-snapshot.js b/test/parallel/test-worker-heap-snapshot.js index 01bbd908623c9b..faa897991852d5 100644 --- a/test/parallel/test-worker-heap-snapshot.js +++ b/test/parallel/test-worker-heap-snapshot.js @@ -11,7 +11,7 @@ const worker = new Worker(__filename); const snapShotResult = { ondone: () => {} }; worker[kHandle].takeHeapSnapshot = common.mustCall(() => snapShotResult); -worker.on('online', () => { +worker.on('online', common.mustCall(() => { const snapShotResponse = worker.getHeapSnapshot(); snapShotResult.ondone({}); @@ -19,4 +19,4 @@ worker.on('online', () => { assert.ok(heapSnapshotStream instanceof HeapSnapshotStream); worker.terminate(); })); -}); +})); From 3027e158e01d37a7ba237355ed6ce2660f6b0f8c Mon Sep 17 00:00:00 2001 From: Erick Wendel Date: Wed, 2 Feb 2022 09:08:16 -0300 Subject: [PATCH 4/4] worker: add test for heapsnapshot as an integration test Signed-off-by: Erick Wendel --- lib/internal/worker.js | 1 - test/parallel/test-worker-heap-snapshot.js | 24 ++++++++-------------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/lib/internal/worker.js b/lib/internal/worker.js index 2c767d49459ec0..1d2cd8cefd2996 100644 --- a/lib/internal/worker.js +++ b/lib/internal/worker.js @@ -496,7 +496,6 @@ function eventLoopUtilization(util1, util2) { } module.exports = { - kHandle, ownsProcessState, isMainThread, SHARE_ENV, diff --git a/test/parallel/test-worker-heap-snapshot.js b/test/parallel/test-worker-heap-snapshot.js index faa897991852d5..5358f2effca508 100644 --- a/test/parallel/test-worker-heap-snapshot.js +++ b/test/parallel/test-worker-heap-snapshot.js @@ -1,22 +1,16 @@ 'use strict'; -// Flags: --expose-internals const common = require('../common'); const assert = require('assert'); const { Worker } = require('worker_threads'); -const { HeapSnapshotStream } = require('internal/heap_utils'); -const { kHandle } = require('internal/worker'); +const { once } = require('events'); -const worker = new Worker(__filename); -const snapShotResult = { ondone: () => {} }; -worker[kHandle].takeHeapSnapshot = common.mustCall(() => snapShotResult); +// Ensure that worker.getHeapSnapshot() returns a valid JSON +(async () => { + const worker = new Worker('setInterval(() => {}, 1000);', { eval: true }); + await once(worker, 'online'); + const stream = await worker.getHeapSnapshot(); + assert.ok(JSON.parse(stream.read())); -worker.on('online', common.mustCall(() => { - const snapShotResponse = worker.getHeapSnapshot(); - snapShotResult.ondone({}); - - snapShotResponse.then(common.mustCall((heapSnapshotStream) => { - assert.ok(heapSnapshotStream instanceof HeapSnapshotStream); - worker.terminate(); - })); -})); + await worker.terminate(); +})().then(common.mustCall());