Skip to content

Commit

Permalink
test(worker): add timeout test case (#2271)
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf authored Nov 11, 2023
1 parent 9a469da commit 4bd7498
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions tests/test_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2560,6 +2560,88 @@ describe('workers', function () {
await worker.close();
});

describe('when timeout is provided', () => {
it('should check if timeout is reached in each step', async function () {
enum Step {
Initial,
Second,
Finish,
}

const worker = new Worker(
queueName,
async job => {
let { step, timeout } = job.data;
let timeoutReached = false;

setTimeout(() => {
timeoutReached = true;
}, timeout);
console.log(step, timeoutReached, job.data.timeout);
while (step !== Step.Finish) {
switch (step) {
case Step.Initial: {
await delay(1000);
if (timeoutReached) {
console.log('reaached1');
throw new Error('Timeout');
}
await job.updateData({
step: Step.Second,
timeout,
});
step = Step.Second;
break;
}
case Step.Second: {
await delay(1000);
if (timeoutReached) {
throw new Error('Timeout');
}
await job.updateData({
step: Step.Finish,
timeout,
});
step = Step.Finish;
return Step.Finish;
}
default: {
throw new Error('invalid step');
}
}
}
},
{ connection, prefix },
);

await worker.waitUntilReady();

const start = Date.now();
await queue.add(
'test',
{ step: Step.Initial, timeout: 1500 },
{
attempts: 3,
backoff: 500,
},
);

await new Promise<void>(resolve => {
worker.on('completed', job => {
const elapse = Date.now() - start;
expect(elapse).to.be.greaterThan(3000);
expect(elapse).to.be.lessThan(4000);
expect(job.failedReason).to.be.eql('Timeout');
expect(job.returnvalue).to.be.eql(Step.Finish);
expect(job.attemptsMade).to.be.eql(2);
resolve();
});
});

await worker.close();
});
});

describe('when moving job to delayed in one step', () => {
it('should retry job after a delay time, keeping the current step', async function () {
this.timeout(8000);
Expand Down

0 comments on commit 4bd7498

Please sign in to comment.