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 <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Erick Wendel 2022-02-11 08:37:40 -03:00 committed by GitHub
parent 3b338cfbe9
commit ba5b5acaf1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 0 deletions

View File

@ -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)

View File

@ -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());