Skip to content

Commit

Permalink
add test for shouldInterruptAfterDeadline (this test never failed)
Browse files Browse the repository at this point in the history
  • Loading branch information
justjake committed Feb 10, 2024
1 parent ab6c700 commit a698c9a
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/quickjs-emscripten/src/quickjs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
RELEASE_SYNC,
RELEASE_ASYNC,
newVariant,
shouldInterruptAfterDeadline,
} from "."

const TEST_NO_ASYNC = Boolean(process.env.TEST_NO_ASYNC)
Expand Down Expand Up @@ -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", () => {
Expand Down

0 comments on commit a698c9a

Please sign in to comment.