From b360c1fb37f48bf51fd1fc32981fe4a3f15a078a Mon Sep 17 00:00:00 2001 From: David Glivar Date: Sat, 4 May 2024 13:30:19 -0700 Subject: [PATCH] test: add tests for retries --- test/worker.test.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/test/worker.test.ts b/test/worker.test.ts index 460d19e..3b054e3 100644 --- a/test/worker.test.ts +++ b/test/worker.test.ts @@ -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); + }); });