Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

esm: spawn only one hooks thread #50752

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions test/es-module/test-esm-loader-threads.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { spawnPromisified } from '../common/index.mjs';
import * as fixtures from '../common/fixtures.mjs';
import { strictEqual } from 'node:assert';
import { execPath } from 'node:process';
import { describe, it } from 'node:test';

describe('off-thread hooks', { concurrency: true }, () => {
it('uses only one hooks thread to support multiple application threads', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--import',
`data:text/javascript,${encodeURIComponent(`
import { register } from 'node:module';
register(${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-log.mjs'))});
`)}`,
fixtures.path('es-module-loaders/workers-spawned.mjs'),
]);

strictEqual(stderr, '');
strictEqual(stdout.split('\n').filter(line => line.startsWith('initialize')).length, 1);
strictEqual(code, 0);
strictEqual(signal, null);
});
});
19 changes: 19 additions & 0 deletions test/fixtures/es-module-loaders/hooks-log.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { writeFileSync } from 'node:fs';

let initializeCount = 0;
let resolveCount = 0;
let loadCount = 0;

export function initialize() {
writeFileSync(1, `initialize ${++initializeCount}\n`);
}

export function resolve(specifier, context, next) {
writeFileSync(1, `resolve ${++resolveCount} ${specifier}\n`);
return next(specifier, context);
}

export function load(url, context, next) {
writeFileSync(1, `load ${++loadCount} ${url}\n`);
return next(url, context);
}
3 changes: 3 additions & 0 deletions test/fixtures/es-module-loaders/worker-log.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { foo } from './module-named-exports.mjs';

console.log(foo);
7 changes: 7 additions & 0 deletions test/fixtures/es-module-loaders/workers-spawned.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Worker } from 'worker_threads';

const workerURL = new URL('./worker-log.mjs', import.meta.url);

// Spawn two workers
new Worker(workerURL);
new Worker(workerURL);