mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-16 16:07:45 +08:00
test(frontend): make cross-tab refresh retry timing deterministic
This commit is contained in:
@@ -98,6 +98,9 @@ describe('CrossTabRefreshCoordinator', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
|
vi.clearAllTimers()
|
||||||
|
vi.restoreAllMocks()
|
||||||
|
vi.useRealTimers()
|
||||||
localStorage.clear()
|
localStorage.clear()
|
||||||
channelRegistry.clear()
|
channelRegistry.clear()
|
||||||
})
|
})
|
||||||
@@ -139,7 +142,14 @@ describe('CrossTabRefreshCoordinator', () => {
|
|||||||
second.destroy()
|
second.destroy()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('treats peer failure as a retry hint and verifies the session locally', async () => {
|
it.each([0, 1])('treats peer failure as a retry hint with %i ms between clock reads', async (clockStepMs) => {
|
||||||
|
vi.useFakeTimers()
|
||||||
|
let timestamp = Date.now()
|
||||||
|
vi.spyOn(Date, 'now').mockImplementation(() => {
|
||||||
|
const current = timestamp
|
||||||
|
timestamp += clockStepMs
|
||||||
|
return current
|
||||||
|
})
|
||||||
const refreshError = new Error('refresh failed')
|
const refreshError = new Error('refresh failed')
|
||||||
const firstAttempt = createDeferred<string>()
|
const firstAttempt = createDeferred<string>()
|
||||||
const firstExecutor = vi
|
const firstExecutor = vi
|
||||||
@@ -161,12 +171,14 @@ describe('CrossTabRefreshCoordinator', () => {
|
|||||||
await Promise.resolve()
|
await Promise.resolve()
|
||||||
const secondRun = second.run(secondExecutor)
|
const secondRun = second.run(secondExecutor)
|
||||||
|
|
||||||
|
const firstOutcome = expect(firstRun).rejects.toThrow('refresh failed')
|
||||||
|
const secondOutcome = expect(secondRun).resolves.toBe('verified-in-second-tab')
|
||||||
firstAttempt.reject(refreshError)
|
firstAttempt.reject(refreshError)
|
||||||
|
|
||||||
await expect(firstRun).rejects.toThrow('refresh failed')
|
await vi.runAllTimersAsync()
|
||||||
await expect(secondRun).resolves.toBe('verified-in-second-tab')
|
await firstOutcome
|
||||||
expect(firstExecutor.mock.calls.length).toBeGreaterThanOrEqual(2)
|
await secondOutcome
|
||||||
expect(firstExecutor.mock.calls.length).toBeLessThanOrEqual(3)
|
expect(firstExecutor).toHaveBeenCalledTimes(clockStepMs === 0 ? 3 : 2)
|
||||||
expect(secondExecutor).toHaveBeenCalledTimes(1)
|
expect(secondExecutor).toHaveBeenCalledTimes(1)
|
||||||
|
|
||||||
first.destroy()
|
first.destroy()
|
||||||
|
|||||||
Reference in New Issue
Block a user