node/test/parallel/test-worker-terminate-source-map.js
Anna Henningsen a7cf8da631
test: fixup worker + source map test
The messaging code uses `Object.defineProperty()`, which accesses
`value` on `Object.prototype` by default, so some calls to the
getter here would actually be expected. Instead, make the list
of accessed properties more specific to the tested source map
code to avoid flakiness.

Refs: https://twitter.com/addaleax/status/1276289101671608320
Refs: https://github.com/nodejs/node/pull/34057

PR-URL: https://github.com/nodejs/node/pull/34446
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
2020-07-22 22:53:54 +02:00

42 lines
1.3 KiB
JavaScript

'use strict';
const common = require('../common');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
// Attempts to test that the source map JS code run on process shutdown
// does not call any user-defined JS code.
const { Worker, workerData, parentPort } = require('worker_threads');
if (!workerData) {
tmpdir.refresh();
process.env.NODE_V8_COVERAGE = tmpdir.path;
// Count the number of some calls that should not be made.
const callCount = new Int32Array(new SharedArrayBuffer(4));
const w = new Worker(__filename, { workerData: { callCount } });
w.on('message', common.mustCall(() => w.terminate()));
w.on('exit', common.mustCall(() => {
assert.strictEqual(callCount[0], 0);
}));
return;
}
const { callCount } = workerData;
function increaseCallCount() { callCount[0]++; }
// Increase the call count when a forbidden method is called.
Object.getPrototypeOf((new Map()).entries()).next = increaseCallCount;
Map.prototype.entries = increaseCallCount;
Object.keys = increaseCallCount;
Object.create = increaseCallCount;
Object.hasOwnProperty = increaseCallCount;
for (const property of ['_cache', 'lineLengths', 'url']) {
Object.defineProperty(Object.prototype, property, {
get: increaseCallCount,
set: increaseCallCount
});
}
parentPort.postMessage('done');