Skip to content

Commit 8e1c9f9

Browse files
committed
improve setTimeout mock
1 parent 25fa0d2 commit 8e1c9f9

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

packages/federation-sdk/src/queues/per-destination.queue.spec.ts

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { beforeEach, describe, expect, it, jest } from 'bun:test';
1+
import { afterEach, beforeEach, describe, expect, it, jest } from 'bun:test';
22

33
import type { BaseEDU } from '@rocket.chat/federation-core';
44
import type { Pdu } from '@rocket.chat/federation-room';
@@ -315,44 +315,51 @@ describe('PerDestinationQueue', () => {
315315
expect(mockRequestService.put).toHaveBeenCalledTimes(1);
316316
});
317317

318-
it('should not schedule setTimeout with infinite waitTime when nextRetryAt is Infinity', async () => {
319-
mockRequestService.put = jest.fn().mockRejectedValue(new Error('Server unreachable'));
318+
describe('setTimeout spy', () => {
319+
let setTimeoutSpy: ReturnType<typeof jest.spyOn>;
320320

321-
// Spy on setTimeout to verify it's not called with Infinity
322-
const setTimeoutSpy = jest.spyOn(global, 'setTimeout');
321+
beforeEach(() => {
322+
setTimeoutSpy = jest.spyOn(global, 'setTimeout');
323+
});
323324

324-
queue = new PerDestinationQueue(destination, origin, mockRequestService, {
325-
maxRetries: 20,
326-
initialBackoffMs: 4000000, // Exceeds 1 hour on first retry
327-
maxBackoffMs: 7200000,
328-
backoffMultiplier: 1,
325+
afterEach(() => {
326+
setTimeoutSpy.mockRestore();
329327
});
330328

331-
queue.enqueuePDU(createMockPdu('$event1'));
329+
it('should not schedule setTimeout with infinite waitTime when nextRetryAt is Infinity', async () => {
330+
mockRequestService.put = jest.fn().mockRejectedValue(new Error('Server unreachable'));
332331

333-
// First attempt fails, triggers 1-hour threshold and sets nextRetryAt to Infinity
334-
await new Promise((resolve) => setTimeout(resolve, 50));
335-
expect(mockRequestService.put).toHaveBeenCalledTimes(1);
336-
expect(queue.isEmpty()).toBe(true); // Queue emptied due to threshold
332+
queue = new PerDestinationQueue(destination, origin, mockRequestService, {
333+
maxRetries: 20,
334+
initialBackoffMs: 4000000, // Exceeds 1 hour on first retry
335+
maxBackoffMs: 7200000,
336+
backoffMultiplier: 1,
337+
});
337338

338-
// Clear previous setTimeout calls
339-
setTimeoutSpy.mockClear();
339+
queue.enqueuePDU(createMockPdu('$event1'));
340340

341-
// Enqueue new event after nextRetryAt is set to Infinity
342-
queue.enqueuePDU(createMockPdu('$event2'));
341+
// First attempt fails, triggers 1-hour threshold and sets nextRetryAt to Infinity
342+
await new Promise((resolve) => setTimeout(resolve, 50));
343+
expect(mockRequestService.put).toHaveBeenCalledTimes(1);
344+
expect(queue.isEmpty()).toBe(true); // Queue emptied due to threshold
343345

344-
// Wait a bit to ensure processQueue is called
345-
await new Promise((resolve) => setTimeout(resolve, 50));
346+
// Clear previous setTimeout calls
347+
setTimeoutSpy.mockClear();
346348

347-
// Verify setTimeout was not called with Infinity
348-
const setTimeoutCalls = setTimeoutSpy.mock.calls;
349-
const hasInfiniteTimeout = setTimeoutCalls.some((call) => !Number.isFinite(call[1]));
350-
expect(hasInfiniteTimeout).toBe(false);
349+
// Enqueue new event after nextRetryAt is set to Infinity
350+
queue.enqueuePDU(createMockPdu('$event2'));
351351

352-
// Queue should still have the event (not dropped, but not processing)
353-
expect(queue.isEmpty()).toBe(false);
352+
// Wait a bit to ensure processQueue is called
353+
await new Promise((resolve) => setTimeout(resolve, 50));
354+
355+
// Verify setTimeout was not called with Infinity
356+
const setTimeoutCalls = setTimeoutSpy.mock.calls;
357+
const hasInfiniteTimeout = setTimeoutCalls.some((call: unknown[]) => !Number.isFinite(call[1]));
358+
expect(hasInfiniteTimeout).toBe(false);
354359

355-
setTimeoutSpy.mockRestore();
360+
// Queue should still have the event (not dropped, but not processing)
361+
expect(queue.isEmpty()).toBe(false);
362+
});
356363
});
357364

358365
it('should handle multiple enqueue attempts when parked with infinite backoff', async () => {

0 commit comments

Comments
 (0)