From 4bd749861db46478a342c63e8f8cc79a139edbc1 Mon Sep 17 00:00:00 2001 From: Rogger Valverde Date: Fri, 10 Nov 2023 23:29:04 -0700 Subject: [PATCH] test(worker): add timeout test case (#2271) --- tests/test_worker.ts | 82 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/tests/test_worker.ts b/tests/test_worker.ts index a1ff28d4c3..d45a547160 100644 --- a/tests/test_worker.ts +++ b/tests/test_worker.ts @@ -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(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);