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((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((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) + }) +})