Skip to content

Commit

Permalink
add test for process.exit for import running for worker thread
Browse files Browse the repository at this point in the history
  • Loading branch information
dygabo committed Apr 29, 2024
1 parent ba9529e commit bb59bc8
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 3 deletions.
2 changes: 0 additions & 2 deletions lib/internal/modules/esm/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,6 @@ class HooksProxy {

constructor() {
const { InternalWorker, hooksPort } = require('internal/worker');
MessageChannel ??= require('internal/worker/io').MessageChannel;

const lock = new SharedArrayBuffer(SHARED_MEMORY_BYTE_LENGTH);
this.#lock = new Int32Array(lock);

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ class Worker extends EventEmitter {

if (!isInternal && hasCustomizations) {
// - send the second side of the channel to the hooks thread
loaderModule.getHooksProxy().makeAsyncRequest(
loaderModule.getHooksProxy().makeSyncRequest(
'#registerWorkerClient', [toWorkerThread], toWorkerThread);
}
// Use this to cache the Worker's loopStart value once available.
Expand Down
36 changes: 36 additions & 0 deletions test/es-module/test-esm-loader-threads.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,40 @@ describe('off-thread hooks', { concurrency: true }, () => {
strictEqual(code, 0);
strictEqual(signal, null);
});

it('propagates the exit code from worker thread import exiting from resolve hook', 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-exit-worker.mjs'))});
`)}`,
fixtures.path('es-module-loaders/worker-log-fail-worker-resolve.mjs'),
]);

strictEqual(stderr, '');
strictEqual(stdout.split('\n').filter((line) => line.startsWith('resolve process-exit-module-resolve')).length, 1);
strictEqual(code, 42);
strictEqual(signal, null);
});

it('propagates the exit code from worker thread import exiting from load hook', 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-exit-worker.mjs'))});
`)}`,
fixtures.path('es-module-loaders/worker-log-fail-worker-load.mjs'),
]);

strictEqual(stderr, '');
strictEqual(stdout.split('\n').filter((line) => line.startsWith('resolve process-exit-module-load')).length, 1);
strictEqual(stdout.split('\n').filter((line) => line.startsWith('load process-exit-on-load:///')).length, 1);
strictEqual(code, 43);
strictEqual(signal, null);
});

});
21 changes: 21 additions & 0 deletions test/fixtures/es-module-loaders/hooks-exit-worker.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { writeFileSync } from 'node:fs';

export function resolve(specifier, context, next) {
writeFileSync(1, `resolve ${specifier}\n`);
if (specifier === 'process-exit-module-resolve') {
process.exit(42);
}

if (specifier === 'process-exit-module-load') {
return { __proto__: null, shortCircuit: true, url: 'process-exit-on-load:///' }
}
return next(specifier, context);
}

export function load(url, context, next) {
writeFileSync(1, `load ${url}\n`);
if (url === 'process-exit-on-load:///') {
process.exit(43);
}
return next(url, context);
}
1 change: 1 addition & 0 deletions test/fixtures/es-module-loaders/worker-fail-on-load.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import 'process-exit-module-load';
1 change: 1 addition & 0 deletions test/fixtures/es-module-loaders/worker-fail-on-resolve.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import 'process-exit-module-resolve';
12 changes: 12 additions & 0 deletions test/fixtures/es-module-loaders/worker-log-fail-worker-load.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Worker } from 'worker_threads';
import { foo } from './module-named-exports.mjs';

const workerURLFailOnLoad = new URL('./worker-fail-on-load.mjs', import.meta.url);
console.log(foo);

// Spawn a worker that will fail to import a dependant module
new Worker(workerURLFailOnLoad);

process.on('exit', (code) => {
console.log(`process exit code: ${code}`)
});
12 changes: 12 additions & 0 deletions test/fixtures/es-module-loaders/worker-log-fail-worker-resolve.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Worker } from 'worker_threads';
import { foo } from './module-named-exports.mjs';

const workerURLFailOnResolve = new URL('./worker-fail-on-resolve.mjs', import.meta.url);
console.log(foo);

// Spawn a worker that will fail to import a dependant module
new Worker(workerURLFailOnResolve);

process.on('exit', (code) => {
console.log(`process exit code: ${code}`)
});

0 comments on commit bb59bc8

Please sign in to comment.