From ba5b5acaf10799206229793f71a4f6542235439f Mon Sep 17 00:00:00 2001 From: Erick Wendel Date: Fri, 11 Feb 2022 08:37:40 -0300 Subject: [PATCH] test: improve `worker_threads ` coverage PR-URL: https://github.com/nodejs/node/pull/41818 Refs: https://coverage.nodejs.org/coverage-7123a00b03a90862/lib/internal/worker.js.html#L412 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: Benjamin Gruenbaum Reviewed-By: James M Snell --- test/parallel/test-worker-environmentdata.js | 5 +++++ test/parallel/test-worker-heap-snapshot.js | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 test/parallel/test-worker-heap-snapshot.js diff --git a/test/parallel/test-worker-environmentdata.js b/test/parallel/test-worker-environmentdata.js index cac97b746ac..aef0e1213ff 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) diff --git a/test/parallel/test-worker-heap-snapshot.js b/test/parallel/test-worker-heap-snapshot.js new file mode 100644 index 00000000000..5358f2effca --- /dev/null +++ b/test/parallel/test-worker-heap-snapshot.js @@ -0,0 +1,16 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const { Worker } = require('worker_threads'); +const { once } = require('events'); + +// 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())); + + await worker.terminate(); +})().then(common.mustCall());