From a698c9a73308aa06859583d4cb265d773bd6380d Mon Sep 17 00:00:00 2001 From: Jake Teton-Landis Date: Sat, 10 Feb 2024 15:20:43 -0500 Subject: [PATCH] add test for shouldInterruptAfterDeadline (this test never failed) --- .../quickjs-emscripten/src/quickjs.test.ts | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/packages/quickjs-emscripten/src/quickjs.test.ts b/packages/quickjs-emscripten/src/quickjs.test.ts index 4ddc0934..a95c6107 100644 --- a/packages/quickjs-emscripten/src/quickjs.test.ts +++ b/packages/quickjs-emscripten/src/quickjs.test.ts @@ -31,6 +31,7 @@ import { RELEASE_SYNC, RELEASE_ASYNC, newVariant, + shouldInterruptAfterDeadline, } from "." const TEST_NO_ASYNC = Boolean(process.env.TEST_NO_ASYNC) @@ -641,6 +642,27 @@ function contextTests(getContext: GetTestContext, isDebug = false) { assert.fail("Should have returned an interrupt error") } }) + + it("shouldInterruptAfterDeadline: interrupts", () => { + const deadline = Date.now() + 100 + vm.runtime.setInterruptHandler(shouldInterruptAfterDeadline(deadline)) + + const result = vm.evalCode("i = 0; while (1) { i++ }") + try { + const interruptedAfterIterations = vm.getProp(vm.global, "i").consume(vm.dump) + assert(interruptedAfterIterations > 0, "Allowed at least 1 iteration") + + if (result.error) { + const errorJson = vm.dump(result.error) + assert.equal(errorJson.name, "InternalError") + assert.equal(errorJson.message, "interrupted") + } else { + assert.fail("Should have returned an interrupt error") + } + } finally { + result.error ? result.error.dispose() : result.value.dispose() + } + }) }) describe(".computeMemoryUsage", () => {