Files
windmill/frontend/src/lib/multiplayerConnection.test.ts
T
Ruben FiszelandClaude Opus 5.5 e4d74a3c97 back off multiplayer reconnects and re-sign the token per attempt (#12166)
* fix: back off multiplayer reconnects and re-sign the token per attempt

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: drop a collaboration connection that opens after it was turned off

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: declare the collaboration generation before the init-time join

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 06:00:04 +01:00

62 lines
1.8 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import * as Y from 'yjs'
let signed = 0
vi.mock('$lib/components/debug/debugUtils', () => ({
signMultiplayerRequest: vi.fn(async () => `token-${++signed}`)
}))
vi.mock('$lib/wsUrl', () => ({ buildWsUrl: (path: string) => `ws://mp.test${path}` }))
import { MultiplayerConnection } from './multiplayerConnection'
const attempts: string[] = []
// What the multiplayer server does with a token it rejects: accept the upgrade,
// then close with 4403.
class RejectingWebSocket {
binaryType = ''
onopen: (() => void) | null = null
onclose: ((e: { code: number }) => void) | null = null
onerror: (() => void) | null = null
onmessage: (() => void) | null = null
constructor(url: string) {
attempts.push(url)
setTimeout(() => {
this.onopen?.()
this.onclose?.({ code: 4403 })
}, 0)
}
send() {}
close() {}
}
describe('MultiplayerConnection', () => {
beforeEach(() => {
vi.useFakeTimers()
vi.stubGlobal('WebSocket', RejectingWebSocket)
attempts.length = 0
signed = 0
})
afterEach(() => {
vi.unstubAllGlobals()
vi.useRealTimers()
})
it('backs off on rejected connections and signs a fresh token for each attempt', async () => {
const connection = await MultiplayerConnection.open('ws', 'ws/room', new Y.Doc())
await vi.advanceTimersByTimeAsync(5 * 60_000)
// 1s doubling to a 60s cap, halved by jitter at worst: at most 15 in 5 minutes,
// where y-websocket's own reconnect makes thousands.
expect(attempts.length).toBeGreaterThan(3)
expect(attempts.length).toBeLessThanOrEqual(15)
const tokens = attempts.map((u) => new URL(u).searchParams.get('token'))
expect(new Set(tokens).size).toBe(attempts.length)
connection.destroy()
const after = attempts.length
await vi.advanceTimersByTimeAsync(5 * 60_000)
expect(attempts.length).toBe(after)
})
})