node/test/parallel/test-is-internal-thread.mjs
Carlos Espa 732744cc76
src,worker: add isInternalWorker
PR-URL: https://github.com/nodejs/node/pull/56469
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Bryan English <bryan@bryanenglish.com>
2025-01-14 18:24:30 +00:00

37 lines
1.3 KiB
JavaScript

import { spawnPromisified } from '../common/index.mjs';
import * as fixtures from '../common/fixtures.mjs';
import assert from 'node:assert';
import { execPath } from 'node:process';
import { describe, it } from 'node:test';
import { isInternalThread, Worker } from 'node:worker_threads';
import * as common from '../common/index.mjs';
describe('worker_threads.isInternalThread', { concurrency: !process.env.TEST_PARALLEL }, () => {
it('should be true inside the loader thread', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('loader-is-internal-thread.js'),
'--eval',
'setTimeout(() => {},99)',
]);
assert.strictEqual(stderr, '');
assert.match(stdout, /isInternalThread: true/);
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
});
it('should be false inside the main thread', async () => {
assert.strictEqual(isInternalThread, false);
});
it('should be false inside a regular worker thread', async () => {
const worker = new Worker(fixtures.path('worker-is-internal-thread.js'));
worker.on('message', common.mustCall((message) => {
assert.strictEqual(message, 'isInternalThread: false');
}));
});
});