Skip to content

Commit

Permalink
test: add tests for retries
Browse files Browse the repository at this point in the history
  • Loading branch information
davidglivar committed May 4, 2024
1 parent 73eb187 commit b360c1f
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions test/worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,39 @@ describe('Supaworker', () => {
const { data } = await supaworker.client.from('jobs').select('id').eq('queue', 'test');
expect(data).toHaveLength(0);
});

test('should retry failed jobs', async () => {
let attempts = 1;
const supaworker = createSupaworker(clientOptions, workerOptions, async (job) => {
if (attempts === 1) {
attempts += 1;
throw new Error('Test error');
}
expect(job.attempts).toBe(2);
await worker.stop();
});
worker = supaworker.worker;
await supaworker.enqueue([{ queue: 'test', options: { max_attempts: 2 } }]);
await worker.start();
const { data } = await supaworker.client.from('jobs').select('id').eq('queue', 'test');
expect(data).toHaveLength(0);
expect(attempts).toBe(2);
});

test('should retry failed jobs up to the maximum attempts', async () => {
let attempts = 0;
const supaworker = createSupaworker(clientOptions, workerOptions, async (job) => {
attempts += 1;
expect(job.attempts).toBe(attempts);
expect(job.attempts).toBeLessThanOrEqual(3);
throw new Error('Test error');
});
setTimeout(() => worker.stop(), 500);
worker = supaworker.worker;
await supaworker.enqueue([{ queue: 'test', options: { max_attempts: 3 } }]);
await worker.start();
const { data } = await supaworker.client.from('jobs').select('id').eq('queue', 'test');
expect(data).toHaveLength(0);
expect(attempts).toBe(3);
});
});

0 comments on commit b360c1f

Please sign in to comment.