From 88a720c8f712ec717ba074b79cc5525b3fb537d1 Mon Sep 17 00:00:00 2001 From: Manuel Astudillo Date: Thu, 21 Nov 2024 17:31:39 +0100 Subject: [PATCH] test(worker): improve flaky test --- tests/test_worker.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/tests/test_worker.ts b/tests/test_worker.ts index 75539d0c5b..c8f7e38f11 100644 --- a/tests/test_worker.ts +++ b/tests/test_worker.ts @@ -510,9 +510,17 @@ describe('workers', function () { await worker.close(); }); - it('do not call moveToActive more than number of jobs + 1', async () => { + it('do not call moveToActive more than number of jobs + 2', async () => { const numJobs = 50; let completedJobs = 0; + + const jobs: Promise[] = []; + for (let i = 0; i < numJobs; i++) { + jobs.push(queue.add('test', { foo: 'bar' })); + } + + await Promise.all(jobs); + const worker = new Worker( queueName, async job => { @@ -521,7 +529,6 @@ describe('workers', function () { }, { connection, prefix, concurrency: 100 }, ); - await worker.waitUntilReady(); // Add spy to worker.moveToActive const spy = sinon.spy(worker, 'moveToActive'); @@ -529,14 +536,9 @@ describe('workers', function () { await worker.blockingConnection.client, 'bzpopmin', ); + await worker.waitUntilReady(); - for (let i = 0; i < numJobs; i++) { - const job = await queue.add('test', { foo: 'bar' }); - expect(job.id).to.be.ok; - expect(job.data.foo).to.be.eql('bar'); - } - - expect(bclientSpy.callCount).to.be.equal(1); + expect(bclientSpy.callCount).to.be.equal(0); await new Promise((resolve, reject) => { worker.on('completed', (job: Job, result: any) => { @@ -547,9 +549,11 @@ describe('workers', function () { }); }); + expect(completedJobs).to.be.equal(numJobs); + expect(bclientSpy.callCount).to.be.equal(2); + // Check moveToActive was called numJobs + 2 times expect(spy.callCount).to.be.equal(numJobs + 2); - expect(bclientSpy.callCount).to.be.equal(3); await worker.close(); });