Skip to content

Commit

Permalink
test(dragonfly): fix flaky tests (#2618)
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf authored Jun 23, 2024
1 parent fb7115b commit 75eb119
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
22 changes: 11 additions & 11 deletions tests/test_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,14 @@ describe('events', function () {
const worker = new Worker(
queueName,
async () => {
await delay(100);
await delay(50);
await queue.add(testName, { foo: 'bar' }, { jobId: 'a1' });
await delay(50);
},
{ connection, prefix },
{ autorun: false, connection, prefix },
);
await worker.waitUntilReady();

let duplicatedEvent = false;

const completed = new Promise<void>(resolve => {
worker.on('completed', async function () {
resolve();
Expand All @@ -383,17 +383,17 @@ describe('events', function () {

await queue.add(testName, { foo: 'bar' }, { jobId: 'a1' });

queueEvents.once('duplicated', ({ jobId }) => {
expect(jobId).to.be.equal('a1');
duplicatedEvent = true;
});
worker.run();

await queue.add(testName, { foo: 'bar' }, { jobId: 'a1' });
await new Promise<void>(resolve => {
queueEvents.once('duplicated', ({ jobId }) => {
expect(jobId).to.be.equal('a1');
resolve();
});
});

await completed;

expect(duplicatedEvent).to.be.true;

await worker.close();
});
});
Expand Down
6 changes: 3 additions & 3 deletions tests/test_metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ describe('metrics', function () {
throw new Error('test');
},
{
autorun: false,
connection,
prefix,
metrics: {
Expand All @@ -329,11 +330,10 @@ describe('metrics', function () {
await queue.add('test', { index: i });
}

worker.run();
await completing;

const closing = worker.close();

await closing;
await worker.close();

const metrics = await queue.getMetrics('failed');

Expand Down
6 changes: 4 additions & 2 deletions tests/test_pause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,6 @@ describe('Pause', function () {

describe('when backoff is 0', () => {
it('moves job into paused queue', async () => {
await queue.add('test', { foo: 'bar' }, { attempts: 2, backoff: 0 });

let worker: Worker;
const processing = new Promise<void>(resolve => {
worker = new Worker(
Expand All @@ -372,6 +370,7 @@ describe('Pause', function () {
resolve();
},
{
autorun: false,
connection,
prefix,
},
Expand All @@ -389,6 +388,9 @@ describe('Pause', function () {
});
});

await queue.add('test', { foo: 'bar' }, { attempts: 2, backoff: 0 });

worker!.run();
await waitingEvent;
await processing;

Expand Down
5 changes: 3 additions & 2 deletions tests/test_repeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ describe('repeat', function () {
queue = new Queue(queueName, { connection, prefix });
repeat = new Repeat(queueName, { connection, prefix });
queueEvents = new QueueEvents(queueName, { connection, prefix });
await queue.waitUntilReady();
await queueEvents.waitUntilReady();
});

Expand Down Expand Up @@ -183,13 +184,13 @@ describe('repeat', function () {
delayStub.restore();
});

it('should create multiple jobs if they have the same cron pattern', async function () {
it('should create multiple jobs if they have the same cron pattern and different name', async function () {
const cron = '*/10 * * * * *';

await Promise.all([
queue.add('test1', {}, { repeat: { pattern: cron } }),
queue.add('test2', {}, { repeat: { pattern: cron } }),
queue.add('test3', {}),
queue.add('test3', {}, { repeat: { pattern: cron } }),
]);

const count = await queue.count();
Expand Down
9 changes: 7 additions & 2 deletions tests/test_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ describe('workers', function () {

await delay(100);
/* Try to gracefully close while having a job that will be failed running */
worker.close();
const closing = worker.close();

await new Promise<void>(resolve => {
worker.once('failed', async (job, err) => {
Expand All @@ -871,6 +871,7 @@ describe('workers', function () {
const count = await queue.getJobCounts('active', 'failed');
expect(count.active).to.be.eq(0);
expect(count.failed).to.be.eq(1);
await closing;
});

it('process a job that complete after worker close', async () => {
Expand Down Expand Up @@ -1732,6 +1733,7 @@ describe('workers', function () {
const processing = new Promise<void>((resolve, reject) => {
processor = async (job: Job) => {
try {
await delay(10);
expect(job.data.num).to.be.equal(counter);
expect(job.data.foo).to.be.equal('bar');
if (counter === maxJobs) {
Expand Down Expand Up @@ -2716,13 +2718,14 @@ describe('workers', function () {
const worker = new Worker(
queueName,
async job => {
await delay(10);
expect(job.attemptsMade).to.be.eql(tries);
tries++;
if (job.attemptsMade < 1) {
throw new Error('Not yet!');
}
},
{ connection, prefix },
{ autorun: false, connection, prefix },
);

await worker.waitUntilReady();
Expand All @@ -2735,6 +2738,8 @@ describe('workers', function () {
},
);

worker.run();

await new Promise(resolve => {
worker.on('completed', resolve);
});
Expand Down

0 comments on commit 75eb119

Please sign in to comment.