| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | /* eslint-disable @typescript-eslint/no-explicit-any */ |
| b69ab31 | | | 9 | /* eslint-disable @typescript-eslint/no-unused-vars */ |
| b69ab31 | | | 10 | /* eslint-disable @typescript-eslint/no-this-alias */ |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | import type {Writable} from 'shared/typeUtils'; |
| b69ab31 | | | 13 | import type {LocalWebSocketEventBus as LocalWebSocketEventBusType} from '../LocalWebSocketEventBus'; |
| b69ab31 | | | 14 | import type {PlatformName} from '../types'; |
| b69ab31 | | | 15 | |
| b69ab31 | | | 16 | const LocalWebSocketEventBus = |
| b69ab31 | | | 17 | // eslint-disable-next-line @typescript-eslint/consistent-type-imports |
| b69ab31 | | | 18 | (jest.requireActual('../LocalWebSocketEventBus') as typeof import('../LocalWebSocketEventBus')) |
| b69ab31 | | | 19 | .LocalWebSocketEventBus; |
| b69ab31 | | | 20 | |
| b69ab31 | | | 21 | let globalMockWs: MockWebSocketImpl; |
| b69ab31 | | | 22 | class MockWebSocketImpl extends EventTarget implements WebSocket { |
| b69ab31 | | | 23 | constructor( |
| b69ab31 | | | 24 | public url: string, |
| b69ab31 | | | 25 | _protocols?: string | string[] | undefined, |
| b69ab31 | | | 26 | ) { |
| b69ab31 | | | 27 | super(); |
| b69ab31 | | | 28 | globalMockWs = this as unknown as MockWebSocketImpl; // keep track of each new instance as a global to use in tests |
| b69ab31 | | | 29 | } |
| b69ab31 | | | 30 | |
| b69ab31 | | | 31 | binaryType = 'blob' as const; |
| b69ab31 | | | 32 | bufferedAmount = 0; |
| b69ab31 | | | 33 | extensions = ''; |
| b69ab31 | | | 34 | |
| b69ab31 | | | 35 | onclose = null; |
| b69ab31 | | | 36 | onerror = null; |
| b69ab31 | | | 37 | onmessage = null; |
| b69ab31 | | | 38 | onopen = null; |
| b69ab31 | | | 39 | protocol = ''; |
| b69ab31 | | | 40 | readyState = 0; |
| b69ab31 | | | 41 | |
| b69ab31 | | | 42 | readonly OPEN = 1 as const; |
| b69ab31 | | | 43 | readonly CONNECTING = 0 as const; |
| b69ab31 | | | 44 | readonly CLOSED = 3 as const; |
| b69ab31 | | | 45 | readonly CLOSING = 2 as const; |
| b69ab31 | | | 46 | |
| b69ab31 | | | 47 | send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void { |
| b69ab31 | | | 48 | this.sentMessages.push(data as string); |
| b69ab31 | | | 49 | } |
| b69ab31 | | | 50 | |
| b69ab31 | | | 51 | // eslint-disable-next-line @typescript-eslint/no-empty-function |
| b69ab31 | | | 52 | close(_code?: number, _reason?: string): void {} |
| b69ab31 | | | 53 | |
| b69ab31 | | | 54 | // -------- Additional APIs for testing -------- |
| b69ab31 | | | 55 | |
| b69ab31 | | | 56 | simulateIncomingMessage(message: string) { |
| b69ab31 | | | 57 | const e = new Event('message'); |
| b69ab31 | | | 58 | (e as Writable<MessageEvent<string>>).data = message; |
| b69ab31 | | | 59 | this.dispatchEvent(e); |
| b69ab31 | | | 60 | } |
| b69ab31 | | | 61 | simulateServerConnected() { |
| b69ab31 | | | 62 | this.dispatchEvent(new Event('open')); |
| b69ab31 | | | 63 | } |
| b69ab31 | | | 64 | simulateServerDisconnected() { |
| b69ab31 | | | 65 | this.dispatchEvent(new Event('close')); |
| b69ab31 | | | 66 | } |
| b69ab31 | | | 67 | |
| b69ab31 | | | 68 | public sentMessages: Array<string> = []; |
| b69ab31 | | | 69 | } |
| b69ab31 | | | 70 | const MockWebSocket = MockWebSocketImpl as unknown as typeof WebSocket; |
| b69ab31 | | | 71 | |
| b69ab31 | | | 72 | const DEFAULT_HOST = 'localhost:8080'; |
| b69ab31 | | | 73 | |
| b69ab31 | | | 74 | function createMessageBus(): LocalWebSocketEventBusType { |
| b69ab31 | | | 75 | return new LocalWebSocketEventBus(DEFAULT_HOST, MockWebSocket, { |
| b69ab31 | | | 76 | token: '1234', |
| b69ab31 | | | 77 | platformName: 'test' as string as PlatformName, |
| b69ab31 | | | 78 | }); |
| b69ab31 | | | 79 | } |
| b69ab31 | | | 80 | |
| b69ab31 | | | 81 | describe('LocalWebsocketEventBus', () => { |
| b69ab31 | | | 82 | it('opens and sends messages', () => { |
| b69ab31 | | | 83 | const bus = createMessageBus(); |
| b69ab31 | | | 84 | globalMockWs.simulateServerConnected(); |
| b69ab31 | | | 85 | bus.postMessage('my message'); |
| b69ab31 | | | 86 | expect(globalMockWs.sentMessages).toEqual(['my message']); |
| b69ab31 | | | 87 | }); |
| b69ab31 | | | 88 | |
| b69ab31 | | | 89 | it('queues messages while connecting', () => { |
| b69ab31 | | | 90 | const bus = createMessageBus(); |
| b69ab31 | | | 91 | bus.postMessage('first'); |
| b69ab31 | | | 92 | bus.postMessage('second'); |
| b69ab31 | | | 93 | expect(globalMockWs.sentMessages).toEqual([]); |
| b69ab31 | | | 94 | globalMockWs.simulateServerConnected(); |
| b69ab31 | | | 95 | bus.postMessage('third'); |
| b69ab31 | | | 96 | expect(globalMockWs.sentMessages).toEqual(['first', 'second', 'third']); |
| b69ab31 | | | 97 | }); |
| b69ab31 | | | 98 | |
| b69ab31 | | | 99 | it('handles incoming messages', () => { |
| b69ab31 | | | 100 | const bus = createMessageBus(); |
| b69ab31 | | | 101 | const onMessage1 = jest.fn(); |
| b69ab31 | | | 102 | const onMessage2 = jest.fn(); |
| b69ab31 | | | 103 | bus.onMessage(onMessage1); |
| b69ab31 | | | 104 | bus.onMessage(onMessage2); |
| b69ab31 | | | 105 | |
| b69ab31 | | | 106 | globalMockWs.simulateServerConnected(); |
| b69ab31 | | | 107 | globalMockWs.simulateIncomingMessage('incoming message'); |
| b69ab31 | | | 108 | |
| b69ab31 | | | 109 | expect(onMessage1).toHaveBeenCalledWith(expect.objectContaining({data: 'incoming message'})); |
| b69ab31 | | | 110 | expect(onMessage2).toHaveBeenCalledWith(expect.objectContaining({data: 'incoming message'})); |
| b69ab31 | | | 111 | }); |
| b69ab31 | | | 112 | |
| b69ab31 | | | 113 | it('notifies about status', () => { |
| b69ab31 | | | 114 | const bus = createMessageBus(); |
| b69ab31 | | | 115 | const changeStatus = jest.fn(); |
| b69ab31 | | | 116 | bus.onChangeStatus(changeStatus); |
| b69ab31 | | | 117 | |
| b69ab31 | | | 118 | expect(changeStatus).toHaveBeenCalledWith({type: 'initializing'}); |
| b69ab31 | | | 119 | globalMockWs.simulateServerConnected(); |
| b69ab31 | | | 120 | |
| b69ab31 | | | 121 | expect(changeStatus).toHaveBeenCalledWith({type: 'open'}); |
| b69ab31 | | | 122 | expect(changeStatus).toHaveBeenCalledTimes(2); |
| b69ab31 | | | 123 | changeStatus.mockClear(); |
| b69ab31 | | | 124 | |
| b69ab31 | | | 125 | globalMockWs.simulateServerDisconnected(); |
| b69ab31 | | | 126 | expect(changeStatus).toHaveBeenCalledWith({type: 'reconnecting'}); |
| b69ab31 | | | 127 | expect(changeStatus).not.toHaveBeenCalledWith({type: 'open'}); |
| b69ab31 | | | 128 | |
| b69ab31 | | | 129 | globalMockWs.simulateServerConnected(); |
| b69ab31 | | | 130 | expect(changeStatus).toHaveBeenCalledWith({type: 'open'}); |
| b69ab31 | | | 131 | }); |
| b69ab31 | | | 132 | |
| b69ab31 | | | 133 | it('disposes status handlers properly', () => { |
| b69ab31 | | | 134 | const bus = createMessageBus(); |
| b69ab31 | | | 135 | |
| b69ab31 | | | 136 | const changeStatus1 = jest.fn(); |
| b69ab31 | | | 137 | bus.onChangeStatus(changeStatus1); |
| b69ab31 | | | 138 | |
| b69ab31 | | | 139 | const changeStatus2 = jest.fn(); |
| b69ab31 | | | 140 | const disposable2 = bus.onChangeStatus(changeStatus2); |
| b69ab31 | | | 141 | |
| b69ab31 | | | 142 | const changeStatus3 = jest.fn(); |
| b69ab31 | | | 143 | bus.onChangeStatus(changeStatus3); |
| b69ab31 | | | 144 | |
| b69ab31 | | | 145 | expect(changeStatus1).toHaveBeenCalledWith({type: 'initializing'}); |
| b69ab31 | | | 146 | expect(changeStatus2).toHaveBeenCalledWith({type: 'initializing'}); |
| b69ab31 | | | 147 | expect(changeStatus3).toHaveBeenCalledWith({type: 'initializing'}); |
| b69ab31 | | | 148 | |
| b69ab31 | | | 149 | disposable2.dispose(); |
| b69ab31 | | | 150 | |
| b69ab31 | | | 151 | globalMockWs.simulateServerConnected(); |
| b69ab31 | | | 152 | |
| b69ab31 | | | 153 | expect(changeStatus1).toHaveBeenCalledWith({type: 'open'}); |
| b69ab31 | | | 154 | expect(changeStatus2).not.toHaveBeenCalledWith({type: 'open'}); |
| b69ab31 | | | 155 | expect(changeStatus3).toHaveBeenCalledWith({type: 'open'}); |
| b69ab31 | | | 156 | }); |
| b69ab31 | | | 157 | |
| b69ab31 | | | 158 | it('queues up messages while disconnected', () => { |
| b69ab31 | | | 159 | const bus = createMessageBus(); |
| b69ab31 | | | 160 | globalMockWs.simulateServerConnected(); |
| b69ab31 | | | 161 | |
| b69ab31 | | | 162 | expect(globalMockWs.sentMessages).toEqual([]); |
| b69ab31 | | | 163 | |
| b69ab31 | | | 164 | globalMockWs.simulateServerDisconnected(); |
| b69ab31 | | | 165 | |
| b69ab31 | | | 166 | bus.postMessage('hi'); |
| b69ab31 | | | 167 | expect(globalMockWs.sentMessages).toEqual([]); |
| b69ab31 | | | 168 | globalMockWs.simulateServerConnected(); |
| b69ab31 | | | 169 | expect(globalMockWs.sentMessages).toEqual(['hi']); |
| b69ab31 | | | 170 | }); |
| b69ab31 | | | 171 | |
| b69ab31 | | | 172 | it('previous onMessage handlers exist after reconnection', () => { |
| b69ab31 | | | 173 | const bus = createMessageBus(); |
| b69ab31 | | | 174 | |
| b69ab31 | | | 175 | const onMessage = jest.fn(); |
| b69ab31 | | | 176 | bus.onMessage(onMessage); |
| b69ab31 | | | 177 | |
| b69ab31 | | | 178 | globalMockWs.simulateServerConnected(); |
| b69ab31 | | | 179 | |
| b69ab31 | | | 180 | globalMockWs.simulateIncomingMessage('one'); |
| b69ab31 | | | 181 | expect(onMessage).toHaveBeenCalledWith(expect.objectContaining({data: 'one'})); |
| b69ab31 | | | 182 | |
| b69ab31 | | | 183 | globalMockWs.simulateServerDisconnected(); |
| b69ab31 | | | 184 | globalMockWs.simulateServerConnected(); |
| b69ab31 | | | 185 | |
| b69ab31 | | | 186 | globalMockWs.simulateIncomingMessage('two'); |
| b69ab31 | | | 187 | expect(onMessage).toHaveBeenCalledWith(expect.objectContaining({data: 'two'})); |
| b69ab31 | | | 188 | }); |
| b69ab31 | | | 189 | |
| b69ab31 | | | 190 | it('clears queued messages after sending them', () => { |
| b69ab31 | | | 191 | const bus = createMessageBus(); |
| b69ab31 | | | 192 | globalMockWs.simulateServerConnected(); |
| b69ab31 | | | 193 | |
| b69ab31 | | | 194 | expect(globalMockWs.sentMessages).toEqual([]); |
| b69ab31 | | | 195 | |
| b69ab31 | | | 196 | globalMockWs.simulateServerDisconnected(); |
| b69ab31 | | | 197 | |
| b69ab31 | | | 198 | bus.postMessage('hi'); |
| b69ab31 | | | 199 | expect(globalMockWs.sentMessages).toEqual([]); |
| b69ab31 | | | 200 | globalMockWs.simulateServerConnected(); |
| b69ab31 | | | 201 | expect(globalMockWs.sentMessages).toEqual(['hi']); |
| b69ab31 | | | 202 | |
| b69ab31 | | | 203 | globalMockWs.simulateServerDisconnected(); |
| b69ab31 | | | 204 | globalMockWs.simulateServerConnected(); |
| b69ab31 | | | 205 | |
| b69ab31 | | | 206 | expect(globalMockWs.sentMessages).toEqual(['hi']); |
| b69ab31 | | | 207 | }); |
| b69ab31 | | | 208 | |
| b69ab31 | | | 209 | it('disposes handlers properly', () => { |
| b69ab31 | | | 210 | const bus = createMessageBus(); |
| b69ab31 | | | 211 | globalMockWs.simulateServerConnected(); |
| b69ab31 | | | 212 | |
| b69ab31 | | | 213 | const onMessage = jest.fn(); |
| b69ab31 | | | 214 | const disposable = bus.onMessage(onMessage); |
| b69ab31 | | | 215 | |
| b69ab31 | | | 216 | globalMockWs.simulateServerConnected(); |
| b69ab31 | | | 217 | globalMockWs.simulateIncomingMessage('incoming message'); |
| b69ab31 | | | 218 | |
| b69ab31 | | | 219 | expect(onMessage).toHaveBeenCalledWith(expect.objectContaining({data: 'incoming message'})); |
| b69ab31 | | | 220 | disposable.dispose(); |
| b69ab31 | | | 221 | globalMockWs.simulateIncomingMessage('another after dispose'); |
| b69ab31 | | | 222 | expect(onMessage).not.toHaveBeenCalledWith( |
| b69ab31 | | | 223 | expect.objectContaining({data: 'another after dispose'}), |
| b69ab31 | | | 224 | ); |
| b69ab31 | | | 225 | }); |
| b69ab31 | | | 226 | |
| b69ab31 | | | 227 | it('disposes only one handler at a time', () => { |
| b69ab31 | | | 228 | const bus = createMessageBus(); |
| b69ab31 | | | 229 | globalMockWs.simulateServerConnected(); |
| b69ab31 | | | 230 | |
| b69ab31 | | | 231 | const onMessage1 = jest.fn(); |
| b69ab31 | | | 232 | bus.onMessage(onMessage1); |
| b69ab31 | | | 233 | |
| b69ab31 | | | 234 | const onMessage2 = jest.fn(); |
| b69ab31 | | | 235 | const disposable2 = bus.onMessage(onMessage2); |
| b69ab31 | | | 236 | |
| b69ab31 | | | 237 | const onMessage3 = jest.fn(); |
| b69ab31 | | | 238 | bus.onMessage(onMessage3); |
| b69ab31 | | | 239 | |
| b69ab31 | | | 240 | globalMockWs.simulateServerConnected(); |
| b69ab31 | | | 241 | globalMockWs.simulateIncomingMessage('incoming message'); |
| b69ab31 | | | 242 | |
| b69ab31 | | | 243 | expect(onMessage1).toHaveBeenCalledWith(expect.objectContaining({data: 'incoming message'})); |
| b69ab31 | | | 244 | expect(onMessage2).toHaveBeenCalledWith(expect.objectContaining({data: 'incoming message'})); |
| b69ab31 | | | 245 | expect(onMessage3).toHaveBeenCalledWith(expect.objectContaining({data: 'incoming message'})); |
| b69ab31 | | | 246 | |
| b69ab31 | | | 247 | disposable2.dispose(); |
| b69ab31 | | | 248 | |
| b69ab31 | | | 249 | globalMockWs.simulateIncomingMessage('another after dispose'); |
| b69ab31 | | | 250 | expect(onMessage2).not.toHaveBeenCalledWith( |
| b69ab31 | | | 251 | expect.objectContaining({data: 'another after dispose'}), |
| b69ab31 | | | 252 | ); |
| b69ab31 | | | 253 | // the other handlers still active |
| b69ab31 | | | 254 | expect(onMessage1).toHaveBeenCalledWith( |
| b69ab31 | | | 255 | expect.objectContaining({data: 'another after dispose'}), |
| b69ab31 | | | 256 | ); |
| b69ab31 | | | 257 | expect(onMessage3).toHaveBeenCalledWith( |
| b69ab31 | | | 258 | expect.objectContaining({data: 'another after dispose'}), |
| b69ab31 | | | 259 | ); |
| b69ab31 | | | 260 | }); |
| b69ab31 | | | 261 | |
| b69ab31 | | | 262 | it('can send messages as soon as connection is created', () => { |
| b69ab31 | | | 263 | const bus = createMessageBus(); |
| b69ab31 | | | 264 | bus.onChangeStatus(newStatus => { |
| b69ab31 | | | 265 | if (newStatus.type === 'open') { |
| b69ab31 | | | 266 | bus.postMessage('message once connected'); |
| b69ab31 | | | 267 | } |
| b69ab31 | | | 268 | }); |
| b69ab31 | | | 269 | globalMockWs.simulateServerConnected(); |
| b69ab31 | | | 270 | |
| b69ab31 | | | 271 | expect(globalMockWs.sentMessages).toEqual(['message once connected']); |
| b69ab31 | | | 272 | }); |
| b69ab31 | | | 273 | |
| b69ab31 | | | 274 | it('includes token from initialState', () => { |
| b69ab31 | | | 275 | createMessageBus(); |
| b69ab31 | | | 276 | expect(globalMockWs.url).toEqual(`ws://${DEFAULT_HOST}/ws?token=1234&platform=test`); |
| b69ab31 | | | 277 | }); |
| b69ab31 | | | 278 | |
| b69ab31 | | | 279 | describe('reconnect timing', () => { |
| b69ab31 | | | 280 | beforeEach(() => { |
| b69ab31 | | | 281 | jest.useFakeTimers(); |
| b69ab31 | | | 282 | }); |
| b69ab31 | | | 283 | afterEach(() => { |
| b69ab31 | | | 284 | jest.useRealTimers(); |
| b69ab31 | | | 285 | }); |
| b69ab31 | | | 286 | |
| b69ab31 | | | 287 | it('reconnects after a delay', () => { |
| b69ab31 | | | 288 | createMessageBus(); |
| b69ab31 | | | 289 | |
| b69ab31 | | | 290 | const initialWs = globalMockWs; |
| b69ab31 | | | 291 | |
| b69ab31 | | | 292 | globalMockWs.simulateServerConnected(); |
| b69ab31 | | | 293 | globalMockWs.simulateServerDisconnected(); |
| b69ab31 | | | 294 | expect(initialWs).toBe(globalMockWs); |
| b69ab31 | | | 295 | jest.runAllTimers(); |
| b69ab31 | | | 296 | // we have a new WebSocket instance which will re-try to connect |
| b69ab31 | | | 297 | expect(initialWs).not.toBe(globalMockWs); |
| b69ab31 | | | 298 | }); |
| b69ab31 | | | 299 | |
| b69ab31 | | | 300 | it("doesn't reconnect after disposing", () => { |
| b69ab31 | | | 301 | const bus = createMessageBus(); |
| b69ab31 | | | 302 | |
| b69ab31 | | | 303 | const previousWs = globalMockWs; |
| b69ab31 | | | 304 | globalMockWs.simulateServerConnected(); |
| b69ab31 | | | 305 | bus.dispose(); |
| b69ab31 | | | 306 | globalMockWs.simulateServerDisconnected(); |
| b69ab31 | | | 307 | expect(previousWs).toBe(globalMockWs); |
| b69ab31 | | | 308 | jest.runAllTimers(); |
| b69ab31 | | | 309 | expect(previousWs).toBe(globalMockWs); // we haven't made a new WebSocket, because we didn't try to reconnect |
| b69ab31 | | | 310 | }); |
| b69ab31 | | | 311 | |
| b69ab31 | | | 312 | it('reconnects with exponential backoff', () => { |
| b69ab31 | | | 313 | createMessageBus(); |
| b69ab31 | | | 314 | |
| b69ab31 | | | 315 | const initialWs = globalMockWs; |
| b69ab31 | | | 316 | |
| b69ab31 | | | 317 | globalMockWs.simulateServerConnected(); |
| b69ab31 | | | 318 | globalMockWs.simulateServerDisconnected(); |
| b69ab31 | | | 319 | expect(initialWs).toBe(globalMockWs); |
| b69ab31 | | | 320 | jest.advanceTimersByTime(LocalWebSocketEventBus.DEFAULT_RECONNECT_CHECK_TIME_MS + 10); |
| b69ab31 | | | 321 | expect(initialWs).not.toBe(globalMockWs); |
| b69ab31 | | | 322 | |
| b69ab31 | | | 323 | const nextWs = globalMockWs; |
| b69ab31 | | | 324 | |
| b69ab31 | | | 325 | // we failed to connect again |
| b69ab31 | | | 326 | globalMockWs.simulateServerDisconnected(); |
| b69ab31 | | | 327 | // with exponential backoff, we should need to wait another tick |
| b69ab31 | | | 328 | jest.advanceTimersByTime(LocalWebSocketEventBus.DEFAULT_RECONNECT_CHECK_TIME_MS + 10); |
| b69ab31 | | | 329 | expect(nextWs).toBe(globalMockWs); |
| b69ab31 | | | 330 | // but after another round, we're past the doubled time |
| b69ab31 | | | 331 | jest.advanceTimersByTime(LocalWebSocketEventBus.DEFAULT_RECONNECT_CHECK_TIME_MS + 10); |
| b69ab31 | | | 332 | expect(nextWs).not.toBe(globalMockWs); |
| b69ab31 | | | 333 | }); |
| b69ab31 | | | 334 | |
| b69ab31 | | | 335 | it('resets exponential backoff after a successful connection', () => { |
| b69ab31 | | | 336 | createMessageBus(); |
| b69ab31 | | | 337 | |
| b69ab31 | | | 338 | globalMockWs.simulateServerConnected(); |
| b69ab31 | | | 339 | |
| b69ab31 | | | 340 | // simulate 2 disconnects, which doubles backoff time |
| b69ab31 | | | 341 | globalMockWs.simulateServerDisconnected(); |
| b69ab31 | | | 342 | jest.advanceTimersByTime(LocalWebSocketEventBus.DEFAULT_RECONNECT_CHECK_TIME_MS + 10); |
| b69ab31 | | | 343 | globalMockWs.simulateServerDisconnected(); |
| b69ab31 | | | 344 | jest.advanceTimersByTime(2 * LocalWebSocketEventBus.DEFAULT_RECONNECT_CHECK_TIME_MS + 10); |
| b69ab31 | | | 345 | |
| b69ab31 | | | 346 | // now reconnect should reset backoff time |
| b69ab31 | | | 347 | globalMockWs.simulateServerConnected(); |
| b69ab31 | | | 348 | |
| b69ab31 | | | 349 | const initialWs = globalMockWs; |
| b69ab31 | | | 350 | globalMockWs.simulateServerDisconnected(); |
| b69ab31 | | | 351 | expect(initialWs).toBe(globalMockWs); |
| b69ab31 | | | 352 | // advancing by initial reconnect time creates a new ws |
| b69ab31 | | | 353 | jest.advanceTimersByTime(LocalWebSocketEventBus.DEFAULT_RECONNECT_CHECK_TIME_MS + 10); |
| b69ab31 | | | 354 | expect(initialWs).not.toBe(globalMockWs); |
| b69ab31 | | | 355 | }); |
| b69ab31 | | | 356 | |
| b69ab31 | | | 357 | it('caps out exponential backoff at maximum', () => { |
| b69ab31 | | | 358 | createMessageBus(); |
| b69ab31 | | | 359 | |
| b69ab31 | | | 360 | globalMockWs.simulateServerConnected(); |
| b69ab31 | | | 361 | |
| b69ab31 | | | 362 | // simulate a bunch of unsuccessful reconnects over time |
| b69ab31 | | | 363 | for (let i = 0; i < 100; i++) { |
| b69ab31 | | | 364 | globalMockWs.simulateServerDisconnected(); |
| b69ab31 | | | 365 | jest.advanceTimersByTime(LocalWebSocketEventBus.MAX_RECONNECT_CHECK_TIME_MS); |
| b69ab31 | | | 366 | } |
| b69ab31 | | | 367 | |
| b69ab31 | | | 368 | // now backoff time should have stopped doubling |
| b69ab31 | | | 369 | |
| b69ab31 | | | 370 | const initialWs = globalMockWs; |
| b69ab31 | | | 371 | globalMockWs.simulateServerDisconnected(); |
| b69ab31 | | | 372 | expect(initialWs).toBe(globalMockWs); |
| b69ab31 | | | 373 | // advancing by anything less than cap of reconnect time doesn't reconnect yet |
| b69ab31 | | | 374 | jest.advanceTimersByTime(LocalWebSocketEventBus.MAX_RECONNECT_CHECK_TIME_MS - 10); |
| b69ab31 | | | 375 | expect(initialWs).toBe(globalMockWs); |
| b69ab31 | | | 376 | |
| b69ab31 | | | 377 | // but just a little further pushes us over the edge and we reconnect |
| b69ab31 | | | 378 | jest.advanceTimersByTime(20); |
| b69ab31 | | | 379 | expect(initialWs).not.toBe(globalMockWs); |
| b69ab31 | | | 380 | }); |
| b69ab31 | | | 381 | }); |
| b69ab31 | | | 382 | }); |