File size: 4,264 Bytes
b2cad4f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | diff --git a/packages/next/src/server/web/sandbox/timer-resource-lifecycle.test.ts b/packages/next/src/server/web/sandbox/timer-resource-lifecycle.test.ts
new file mode 100644
index 0000000000..1557cc681e
--- /dev/null
+++ b/packages/next/src/server/web/sandbox/timer-resource-lifecycle.test.ts
@@ -0,0 +1,126 @@
+import { clearAllModuleContexts, getModuleContext } from './context'
+
+async function createSandbox() {
+ return getModuleContext({
+ moduleName: `timer-lifecycle-${Math.random()}`,
+ onError: () => {},
+ onWarning: () => {},
+ useCache: false,
+ distDir: '/tmp',
+ edgeFunctionEntry: {
+ assets: [],
+ wasm: [],
+ env: {},
+ },
+ })
+}
+
+describe('sandbox timer resource lifecycle', () => {
+ let edgeGlobal: any
+
+ beforeEach(async () => {
+ edgeGlobal = (await createSandbox()).runtime.context
+ })
+
+ afterEach(async () => {
+ await clearAllModuleContexts()
+ jest.restoreAllMocks()
+ jest.useRealTimers()
+ })
+
+ it('releases completed one-shot timers across repeated rounds', async () => {
+ const clearTimeoutSpy = jest.spyOn(global, 'clearTimeout')
+ const callbackArg = () => 'callback value'
+ const rounds = 4
+ const timersPerRound = 8
+
+ for (let round = 0; round < rounds; round++) {
+ await Promise.all(
+ Array.from(
+ { length: timersPerRound },
+ () =>
+ new Promise<void>((resolve, reject) => {
+ edgeGlobal.setTimeout(
+ function (this: unknown, label: string, arg: () => string) {
+ try {
+ expect(this).toBe(edgeGlobal)
+ expect(label).toBe('round')
+ expect(arg).toBe(callbackArg)
+ expect(arg()).toBe('callback value')
+ resolve()
+ } catch (error) {
+ reject(error)
+ }
+ },
+ 0,
+ 'round',
+ callbackArg
+ )
+ })
+ )
+ )
+ }
+
+ // Each completed Node timer is cleared once by the sandbox workaround.
+ expect(clearTimeoutSpy).toHaveBeenCalledTimes(rounds * timersPerRound)
+
+ await clearAllModuleContexts()
+
+ // Tearing down the still-live sandbox must not revisit timers that have
+ // already completed naturally.
+ expect(clearTimeoutSpy).toHaveBeenCalledTimes(rounds * timersPerRound)
+ })
+
+ it('stops tracking a one-shot timer even when its callback throws', async () => {
+ jest.useFakeTimers()
+ const clearTimeoutSpy = jest.spyOn(global, 'clearTimeout')
+
+ edgeGlobal.setTimeout(() => {
+ throw new Error('timer callback failed')
+ }, 0)
+
+ expect(() => jest.runOnlyPendingTimers()).toThrow('timer callback failed')
+ expect(clearTimeoutSpy).toHaveBeenCalledTimes(1)
+
+ await clearAllModuleContexts()
+ expect(clearTimeoutSpy).toHaveBeenCalledTimes(1)
+ })
+
+ it('cancels an explicitly cleared one-shot timer', async () => {
+ const clearTimeoutSpy = jest.spyOn(global, 'clearTimeout')
+ const callback = jest.fn()
+ const id = edgeGlobal.setTimeout(callback, 30)
+
+ edgeGlobal.clearTimeout(id)
+
+ expect(clearTimeoutSpy).toHaveBeenCalledWith(id)
+ await new Promise((resolve) => setTimeout(resolve, 60))
+ expect(callback).not.toHaveBeenCalled()
+
+ const callsAfterClear = clearTimeoutSpy.mock.calls.length
+ await clearAllModuleContexts()
+ expect(clearTimeoutSpy).toHaveBeenCalledTimes(callsAfterClear)
+ })
+
+ it('keeps repeating intervals tracked until sandbox teardown', async () => {
+ const clearIntervalSpy = jest.spyOn(global, 'clearInterval')
+ let ticks = 0
+ let resolveThirdTick: () => void
+ const thirdTick = new Promise<void>((resolve) => {
+ resolveThirdTick = resolve
+ })
+
+ const id = edgeGlobal.setInterval(() => {
+ ticks++
+ if (ticks === 3) resolveThirdTick()
+ }, 5)
+
+ await thirdTick
+ expect(ticks).toBeGreaterThanOrEqual(3)
+ expect(clearIntervalSpy).not.toHaveBeenCalled()
+
+ await clearAllModuleContexts()
+ expect(clearIntervalSpy).toHaveBeenCalledTimes(1)
+ expect(clearIntervalSpy).toHaveBeenCalledWith(id)
+ })
+})
|