Skip to content

Commit

Permalink
esm: spawn only one hooks thread
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoffreyBooth committed Dec 8, 2023
1 parent 8cdb7ca commit 28a5bc5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
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);
8 changes: 8 additions & 0 deletions test/fixtures/es-module-loaders/workers-spawned.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Worker } from 'worker_threads';
import { fileURLToPath } from 'node:url';

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

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

0 comments on commit 28a5bc5

Please sign in to comment.