Skip to content

Commit

Permalink
test: add test for sandbox ttl pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
manast committed Jan 13, 2025
1 parent 0f024f1 commit 3c15d4b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tests/fixtures/fixture_processor_ttl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* A processor file to be used in tests.
*
*/
'use strict';

// This processor will timeout in 10 seconds.
const MAX_TTL = 1_000;
const CLEANUP_TTL = 500;

const TTL_EXIT_CODE = 10;

module.exports = async function (job) {
let hasCompleted = false;
const harKillTimeout = setTimeout(() => {
if (!hasCompleted) {
process.exit(TTL_EXIT_CODE);
}
}, MAX_TTL);

const softKillTimeout = setTimeout(async () => {
await doCleanup(job);
}, CLEANUP_TTL);

try {
// If doAsyncWork is CPU intensive and blocks NodeJS loop forever, the timeout will never be triggered.
await doAsyncWork(job);
hasCompleted = true;
} finally {
// Important to clear the timeouts before returning as this process will be reused.
clearTimeout(harKillTimeout);
clearTimeout(softKillTimeout);
}
};

const doAsyncWork = async job => {
// Simulate a long running operation.
await new Promise(resolve => setTimeout(resolve, 10000));
};

const doCleanup = async job => {
// Simulate a cleanup operation.
await job.updateProgress(50);
};
33 changes: 33 additions & 0 deletions tests/test_sandboxed_process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,39 @@ function sandboxProcessTests(
);

await worker.close();

const failedJobs = await queue.getFailed();
expect(failedJobs).to.have.lengthOf(1);
expect(failedJobs[0].failedReason).to.be.equal(
'Unexpected exit code: 1 signal: null',
);
});

it('should fail if wrapping with ttl pattern', async () => {
const processFile = __dirname + '/fixtures/fixture_processor_ttl.js';

const worker = new Worker(queueName, processFile, {
connection,
prefix,
drainDelay: 1,
useWorkerThreads,
});

const job = await queue.add('test', { exitCode: 1 });

await expect(job.waitUntilFinished(queueEvents)).to.be.rejectedWith(
'Unexpected exit code: 10 signal: null',
);

await worker.close();

const failedJobs = await queue.getFailed();
expect(failedJobs).to.have.lengthOf(1);
expect(failedJobs[0].failedReason).to.be.equal(
'Unexpected exit code: 10 signal: null',
);

expect(failedJobs[0].progress).to.be.equal(50);
});

it('should fail if the process file is broken', async () => {
Expand Down

0 comments on commit 3c15d4b

Please sign in to comment.