|
1 | | -import { beforeEach, describe, expect, it, jest } from 'bun:test'; |
| 1 | +import { afterEach, beforeEach, describe, expect, it, jest } from 'bun:test'; |
2 | 2 |
|
3 | 3 | import type { BaseEDU } from '@rocket.chat/federation-core'; |
4 | 4 | import type { Pdu } from '@rocket.chat/federation-room'; |
@@ -315,44 +315,51 @@ describe('PerDestinationQueue', () => { |
315 | 315 | expect(mockRequestService.put).toHaveBeenCalledTimes(1); |
316 | 316 | }); |
317 | 317 |
|
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>; |
320 | 320 |
|
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 | + }); |
323 | 324 |
|
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(); |
329 | 327 | }); |
330 | 328 |
|
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')); |
332 | 331 |
|
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 | + }); |
337 | 338 |
|
338 | | - // Clear previous setTimeout calls |
339 | | - setTimeoutSpy.mockClear(); |
| 339 | + queue.enqueuePDU(createMockPdu('$event1')); |
340 | 340 |
|
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 |
343 | 345 |
|
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(); |
346 | 348 |
|
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')); |
351 | 351 |
|
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); |
354 | 359 |
|
355 | | - setTimeoutSpy.mockRestore(); |
| 360 | + // Queue should still have the event (not dropped, but not processing) |
| 361 | + expect(queue.isEmpty()).toBe(false); |
| 362 | + }); |
356 | 363 | }); |
357 | 364 |
|
358 | 365 | it('should handle multiple enqueue attempts when parked with infinite backoff', async () => { |
|
0 commit comments