Skip to content

Commit

Permalink
test: fix test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf committed Oct 5, 2024
1 parent 9a43a3f commit 6e02943
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
9 changes: 2 additions & 7 deletions src/classes/child-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class ChildPool {
};
}

async retain(processFile: string, rejectCb: Function): Promise<Child> {
async retain(processFile: string, exitHandler: any): Promise<Child> {
let child = this.getFree(processFile).pop();

if (child) {
Expand All @@ -41,12 +41,7 @@ export class ChildPool {
workerThreadsOptions: this.opts.workerThreadsOptions,
});

child.on('exit', (exitCode: any, signal: any) => {
this.remove.bind(this, child);
rejectCb(
new Error('Unexpected exit code: ' + exitCode + ' signal: ' + signal),
);
});
child.on('exit', exitHandler);

try {
await child.init();
Expand Down
13 changes: 11 additions & 2 deletions src/classes/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@ const sandbox = <T, R, N extends string>(
return async function process(job: Job<T, R, N>, token?: string): Promise<R> {
let child: Child;
let msgHandler: any;
let exitHandler: any;
try {
const done: Promise<R> = new Promise((resolve, reject) => {
const initChild = async () => {
try {
child = await childPool.retain(processFile, reject);
exitHandler = (exitCode: any, signal: any) => {
reject(
new Error(
'Unexpected exit code: ' + exitCode + ' signal: ' + signal,
),
);
};

child = await childPool.retain(processFile, exitHandler);

msgHandler = async (msg: ChildMessage) => {
switch (msg.cmd) {
Expand Down Expand Up @@ -66,7 +75,7 @@ const sandbox = <T, R, N extends string>(
} finally {
if (child) {
child.off('message', msgHandler);

child.off('exit', exitHandler);
if (child.exitCode !== null || /SIG.*/.test(`${child.signalCode}`)) {
childPool.remove(child);
} else {
Expand Down
9 changes: 6 additions & 3 deletions tests/test_sandboxed_process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ function sandboxProcessTests(
});
});

it('should remove exited process', async () => {
it('should release exited process', async () => {
const processFile = __dirname + '/fixtures/fixture_processor_exit.js';

const worker = new Worker(queueName, processFile, {
Expand All @@ -1114,7 +1114,7 @@ function sandboxProcessTests(
expect(Object.keys(worker['childPool'].retained)).to.have.lengthOf(
0,
);
expect(worker['childPool'].getAllFree()).to.have.lengthOf(0);
expect(worker['childPool'].getAllFree()).to.have.lengthOf(1);
resolve();
} catch (err) {
reject(err);
Expand All @@ -1139,7 +1139,10 @@ function sandboxProcessTests(
});

// acquire and release a child here so we know it has it's full termination handler setup
const initializedChild = await worker['childPool'].retain(processFile);
const initializedChild = await worker['childPool'].retain(
processFile,
() => {},
);
await worker['childPool'].release(initializedChild);

// await this After we've added the job
Expand Down

0 comments on commit 6e02943

Please sign in to comment.