|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { BackOffStrategy } from './BackOffStrategy'; |
| 3 | +import * as utils from './utils'; |
| 4 | + |
| 5 | +vi.mock('./utils', async () => { |
| 6 | + const actual = await vi.importActual('./utils'); |
| 7 | + return { |
| 8 | + ...actual, |
| 9 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 10 | + sleep: vi.fn((ms: number) => Promise.resolve()), |
| 11 | + extractProjectFromUrl: vi.fn((url: URL) => { |
| 12 | + // @ts-ignore |
| 13 | + return actual.extractProjectFromUrl(url); |
| 14 | + }), |
| 15 | + }; |
| 16 | +}); |
| 17 | + |
| 18 | +describe('BackOffStrategy', () => { |
| 19 | + beforeEach(() => { |
| 20 | + // Reset singleton and mocks before each test |
| 21 | + (BackOffStrategy as any)._instance = null; |
| 22 | + vi.clearAllMocks(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should return the same singleton instance', () => { |
| 26 | + const instance1 = BackOffStrategy.getInstance(); |
| 27 | + const instance2 = BackOffStrategy.getInstance(); |
| 28 | + expect(instance1).toBe(instance2); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should not add failed attempts for self-hosted URLs', () => { |
| 32 | + const strategy = BackOffStrategy.getInstance(); |
| 33 | + const selfHostedUrl = 'wss://my-server.com'; |
| 34 | + |
| 35 | + strategy.addFailedConnectionAttempt(selfHostedUrl); |
| 36 | + |
| 37 | + // Verify extractProjectFromUrl was called and returned null |
| 38 | + expect(utils.extractProjectFromUrl).toHaveBeenCalledWith(new URL(selfHostedUrl)); |
| 39 | + // Verify sleep was not called since no project name exists |
| 40 | + expect(utils.sleep).not.toHaveBeenCalled(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should apply exponential backoff for cloud URLs', () => { |
| 44 | + const strategy = BackOffStrategy.getInstance(); |
| 45 | + const cloudUrl = 'wss://myproject.livekit.cloud'; |
| 46 | + |
| 47 | + // First failure: 500ms |
| 48 | + strategy.addFailedConnectionAttempt(cloudUrl); |
| 49 | + expect(utils.sleep).toHaveBeenCalledWith(500); |
| 50 | + |
| 51 | + // Second failure: 1000ms |
| 52 | + strategy.addFailedConnectionAttempt(cloudUrl); |
| 53 | + expect(utils.sleep).toHaveBeenCalledWith(1000); |
| 54 | + |
| 55 | + // Third failure: 2000ms |
| 56 | + strategy.addFailedConnectionAttempt(cloudUrl); |
| 57 | + expect(utils.sleep).toHaveBeenCalledWith(2000); |
| 58 | + |
| 59 | + // Fourth failure: 4000ms |
| 60 | + strategy.addFailedConnectionAttempt(cloudUrl); |
| 61 | + expect(utils.sleep).toHaveBeenCalledWith(4000); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should cap backoff at maximum delay', () => { |
| 65 | + const strategy = BackOffStrategy.getInstance(); |
| 66 | + const cloudUrl = 'wss://myproject.livekit.cloud'; |
| 67 | + const maxBackoff = 15_000; |
| 68 | + |
| 69 | + // Simulate many failures to reach max backoff |
| 70 | + for (let i = 0; i < 10; i++) { |
| 71 | + strategy.addFailedConnectionAttempt(cloudUrl); |
| 72 | + } |
| 73 | + |
| 74 | + // Last call should be capped at 15000ms |
| 75 | + const lastCall = (utils.sleep as any).mock.calls[(utils.sleep as any).mock.calls.length - 1]; |
| 76 | + expect(lastCall[0]).toBeLessThanOrEqual(maxBackoff); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should reset failed attempts for a specific project', () => { |
| 80 | + const strategy = BackOffStrategy.getInstance(); |
| 81 | + const cloudUrl = 'wss://myproject.livekit.cloud'; |
| 82 | + |
| 83 | + // Add multiple failures |
| 84 | + strategy.addFailedConnectionAttempt(cloudUrl); |
| 85 | + strategy.addFailedConnectionAttempt(cloudUrl); |
| 86 | + strategy.addFailedConnectionAttempt(cloudUrl); |
| 87 | + |
| 88 | + // Reset the project |
| 89 | + strategy.resetFailedConnectionAttempts(cloudUrl); |
| 90 | + |
| 91 | + // Next failure should start from base delay again |
| 92 | + vi.clearAllMocks(); |
| 93 | + strategy.addFailedConnectionAttempt(cloudUrl); |
| 94 | + expect(utils.sleep).toHaveBeenCalledWith(500); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should isolate backoff state between different projects', () => { |
| 98 | + const strategy = BackOffStrategy.getInstance(); |
| 99 | + const project1Url = 'wss://project1.livekit.cloud'; |
| 100 | + const project2Url = 'wss://project2.livekit.cloud'; |
| 101 | + |
| 102 | + // Add failures to project1 |
| 103 | + strategy.addFailedConnectionAttempt(project1Url); |
| 104 | + strategy.addFailedConnectionAttempt(project1Url); |
| 105 | + |
| 106 | + // Add failures to project2 |
| 107 | + strategy.addFailedConnectionAttempt(project2Url); |
| 108 | + |
| 109 | + // Verify project2 starts at base delay despite project1 having multiple failures |
| 110 | + const calls = (utils.sleep as any).mock.calls; |
| 111 | + expect(calls[0][0]).toBe(500); // project1 first failure |
| 112 | + expect(calls[1][0]).toBe(1000); // project1 second failure |
| 113 | + expect(calls[2][0]).toBe(500); // project2 first failure (independent) |
| 114 | + }); |
| 115 | + |
| 116 | + it('should return correct backoff promise', async () => { |
| 117 | + const strategy = BackOffStrategy.getInstance(); |
| 118 | + const cloudUrl = 'wss://myproject.livekit.cloud'; |
| 119 | + const selfHostedUrl = 'wss://my-server.com'; |
| 120 | + |
| 121 | + // Add a failure to create a backoff promise |
| 122 | + strategy.addFailedConnectionAttempt(cloudUrl); |
| 123 | + const backoffPromise = strategy.getBackOffPromise(cloudUrl); |
| 124 | + |
| 125 | + // Should return a promise for cloud URLs with failures |
| 126 | + expect(backoffPromise).toBeInstanceOf(Promise); |
| 127 | + |
| 128 | + // Should return resolved promise for self-hosted URLs |
| 129 | + const selfHostedPromise = strategy.getBackOffPromise(selfHostedUrl); |
| 130 | + expect(selfHostedPromise).toBeInstanceOf(Promise); |
| 131 | + await expect(selfHostedPromise).resolves.toBeUndefined(); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should clear all state with resetAll', () => { |
| 135 | + const strategy = BackOffStrategy.getInstance(); |
| 136 | + const project1Url = 'wss://project1.livekit.cloud'; |
| 137 | + const project2Url = 'wss://project2.livekit.cloud'; |
| 138 | + |
| 139 | + // Add failures to multiple projects |
| 140 | + strategy.addFailedConnectionAttempt(project1Url); |
| 141 | + strategy.addFailedConnectionAttempt(project1Url); |
| 142 | + strategy.addFailedConnectionAttempt(project2Url); |
| 143 | + |
| 144 | + // Reset all state |
| 145 | + strategy.resetAll(); |
| 146 | + |
| 147 | + // Next failures should start from base delay for all projects |
| 148 | + vi.clearAllMocks(); |
| 149 | + strategy.addFailedConnectionAttempt(project1Url); |
| 150 | + strategy.addFailedConnectionAttempt(project2Url); |
| 151 | + |
| 152 | + const calls = (utils.sleep as any).mock.calls; |
| 153 | + expect(calls[0][0]).toBe(500); // project1 back to base |
| 154 | + expect(calls[1][0]).toBe(500); // project2 back to base |
| 155 | + }); |
| 156 | +}); |
0 commit comments