Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { Removable } from '../removable'
3import { defaultTimeoutProvider, timeoutManager } from '../timeoutManager'
4import { setIsServer } from './utils'
5import type { TimeoutCallback } from '../timeoutManager'
6β
7class RemovableTest extends Removable {
8 optionalRemoveSpy = vi.fn()
9β
10 // Expose the protected members so they can be exercised directly.
11 callScheduleGc(): void {
12 this.scheduleGc()
13 }
14β
15 callUpdateGcTime(newGcTime: number | undefined): void {
16 this.updateGcTime(newGcTime)
17 }
18β
19 callClearGcTimeout(): void {
20 this.clearGcTimeout()
21 }
22β
23 protected optionalRemove(): void {
24 this.optionalRemoveSpy()
25 }
26}
27β
28describe('Removable', () => {
29 function createMockProvider() {
30 return {
31 setTimeout: vi.fn((_callback: TimeoutCallback, _delay: number) => 123),
32 clearTimeout: vi.fn(),
33 setInterval: vi.fn((_callback: TimeoutCallback, _delay: number) => 456),
34 clearInterval: vi.fn(),
35 }
36 }
37β
38 let provider: ReturnType<typeof createMockProvider>
39β
40 beforeEach(() => {
41 provider = createMockProvider()
42 timeoutManager.setTimeoutProvider(provider)
43 })
44β
45 afterEach(() => {
46 timeoutManager.setTimeoutProvider(defaultTimeoutProvider)
47 vi.restoreAllMocks()
48 })
49β
50 describe('updateGcTime', () => {
51 it('should default to 5 minutes when no gcTime is provided on the client', () => {
52 const removable = new RemovableTest()
53β
54 removable.callUpdateGcTime(undefined)
55β
56 expect(removable.gcTime).toBe(5 * 60 * 1000)
57 })
58β
59 it('should use the provided gcTime when it is larger than the default', () => {
60 const removable = new RemovableTest()
61β
62 removable.callUpdateGcTime(10 * 60 * 1000)
63β
64 expect(removable.gcTime).toBe(10 * 60 * 1000)
65 })
66β
67 it('should use an explicit gcTime even when it is smaller than the default', () => {
68 const removable = new RemovableTest()
69β
70 removable.callUpdateGcTime(1000)
71β
72 expect(removable.gcTime).toBe(1000)
73 })
74β
75 it('should never decrease an already larger gcTime', () => {
76 const removable = new RemovableTest()
77β
78 removable.callUpdateGcTime(10 * 60 * 1000)
79 removable.callUpdateGcTime(1000)
80β
81 expect(removable.gcTime).toBe(10 * 60 * 1000)
82 })
83β
84 it('should default to Infinity on the server', () => {
85 const resetIsServer = setIsServer(true)
86 try {
87 const removable = new RemovableTest()
88β
89 removable.callUpdateGcTime(undefined)
90β
91 expect(removable.gcTime).toBe(Infinity)
92 } finally {
93 resetIsServer()
94 }
95 })
96 })
97β
98 describe('scheduleGc', () => {
99 it('should schedule optionalRemove after a valid gcTime', () => {
100 const removable = new RemovableTest()
101 removable.callUpdateGcTime(1000)
102β
103 removable.callScheduleGc()
104β
105 expect(provider.setTimeout).toHaveBeenCalledTimes(1)
106 expect(provider.setTimeout).toHaveBeenCalledWith(
107 expect.any(Function),
108 1000,
109 )
110β
111 // The scheduled callback should invoke optionalRemove.
112 const [scheduledCallback] = provider.setTimeout.mock.lastCall ?? []
113 expect(removable.optionalRemoveSpy).toHaveBeenCalledTimes(0)
114 scheduledCallback?.()
115 expect(removable.optionalRemoveSpy).toHaveBeenCalledTimes(1)
116 })
117β
118 it('should not schedule when gcTime is not a valid timeout', () => {
119 const removable = new RemovableTest()
120 removable.gcTime = Infinity
121β
122 removable.callScheduleGc()
123β
124 expect(provider.setTimeout).not.toHaveBeenCalled()
125 })
126β
127 it('should clear a previously scheduled timeout before scheduling a new one', () => {
128 const removable = new RemovableTest()
129 removable.callUpdateGcTime(1000)
130β
131 removable.callScheduleGc()
132 removable.callScheduleGc()
133β
134 // The second schedule clears the first timer before setting a new one.
135 expect(provider.clearTimeout).toHaveBeenCalledWith(123)
136 expect(provider.setTimeout).toHaveBeenCalledTimes(2)
137 })
138 })
139β
140 describe('clearGcTimeout', () => {
141 it('should clear a scheduled timeout', () => {
142 const removable = new RemovableTest()
143 removable.callUpdateGcTime(1000)
144 removable.callScheduleGc()
145β
146 removable.callClearGcTimeout()
147β
148 expect(provider.clearTimeout).toHaveBeenCalledWith(123)
149 })
150β
151 it('should do nothing when no timeout is scheduled', () => {
152 const removable = new RemovableTest()
153β
154 removable.callClearGcTimeout()
155β
156 expect(provider.clearTimeout).not.toHaveBeenCalled()
157 })
158β
159 it('should not clear the same timeout twice', () => {
160 const removable = new RemovableTest()
161 removable.callUpdateGcTime(1000)
162 removable.callScheduleGc()
163β
164 removable.callClearGcTimeout()
165 removable.callClearGcTimeout()
166β
167 expect(provider.clearTimeout).toHaveBeenCalledTimes(1)
168 })
169 })
170β
171 describe('destroy', () => {
172 it('should clear the scheduled gc timeout', () => {
173 const removable = new RemovableTest()
174 removable.callUpdateGcTime(1000)
175 removable.callScheduleGc()
176β
177 removable.destroy()
178β
179 expect(provider.clearTimeout).toHaveBeenCalledWith(123)
180 })
181 })
182})