Skip to content

Commit 88a720c

Browse files
committed
test(worker): improve flaky test
1 parent 09f2571 commit 88a720c

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

tests/test_worker.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -510,9 +510,17 @@ describe('workers', function () {
510510
await worker.close();
511511
});
512512

513-
it('do not call moveToActive more than number of jobs + 1', async () => {
513+
it('do not call moveToActive more than number of jobs + 2', async () => {
514514
const numJobs = 50;
515515
let completedJobs = 0;
516+
517+
const jobs: Promise<Job>[] = [];
518+
for (let i = 0; i < numJobs; i++) {
519+
jobs.push(queue.add('test', { foo: 'bar' }));
520+
}
521+
522+
await Promise.all(jobs);
523+
516524
const worker = new Worker(
517525
queueName,
518526
async job => {
@@ -521,22 +529,16 @@ describe('workers', function () {
521529
},
522530
{ connection, prefix, concurrency: 100 },
523531
);
524-
await worker.waitUntilReady();
525532

526533
// Add spy to worker.moveToActive
527534
const spy = sinon.spy(worker, 'moveToActive');
528535
const bclientSpy = sinon.spy(
529536
await worker.blockingConnection.client,
530537
'bzpopmin',
531538
);
539+
await worker.waitUntilReady();
532540

533-
for (let i = 0; i < numJobs; i++) {
534-
const job = await queue.add('test', { foo: 'bar' });
535-
expect(job.id).to.be.ok;
536-
expect(job.data.foo).to.be.eql('bar');
537-
}
538-
539-
expect(bclientSpy.callCount).to.be.equal(1);
541+
expect(bclientSpy.callCount).to.be.equal(0);
540542

541543
await new Promise<void>((resolve, reject) => {
542544
worker.on('completed', (job: Job, result: any) => {
@@ -547,9 +549,11 @@ describe('workers', function () {
547549
});
548550
});
549551

552+
expect(completedJobs).to.be.equal(numJobs);
553+
expect(bclientSpy.callCount).to.be.equal(2);
554+
550555
// Check moveToActive was called numJobs + 2 times
551556
expect(spy.callCount).to.be.equal(numJobs + 2);
552-
expect(bclientSpy.callCount).to.be.equal(3);
553557

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

0 commit comments

Comments
 (0)