Skip to content

Commit

Permalink
test(worker): improve flaky test
Browse files Browse the repository at this point in the history
  • Loading branch information
manast committed Nov 22, 2024
1 parent 09f2571 commit 88a720c
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions tests/test_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Job>[] = [];
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 => {
Expand All @@ -521,22 +529,16 @@ describe('workers', function () {
},
{ connection, prefix, concurrency: 100 },
);
await worker.waitUntilReady();

// Add spy to worker.moveToActive
const spy = sinon.spy(worker, 'moveToActive');
const bclientSpy = sinon.spy(
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<void>((resolve, reject) => {
worker.on('completed', (job: Job, result: any) => {
Expand All @@ -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();
});
Expand Down

0 comments on commit 88a720c

Please sign in to comment.