Path: blob/main/python/python-wasm/src/signal.test.ts
1067 views
import { asyncPython } from "./node";1import { delay } from "awaiting";23// This really tests integration with python's sig handling infrastructure.4test("sigint interrupts 'while True: pass' within 250ms", async () => {5const { exec, repr, kernel } = await asyncPython();67// Ensure it is running.8await repr("2+3");9// Launch an infinite loop, where interrupting it won't leave10// an uncaught exception.11let interrupted = false;12(async () => {13try {14await exec("while True: pass # expected to fail!");15} catch (_err) {16interrupted = true;17}18})();19// Now send sigint, which should cause the above function to finish soon and set interrupted to true.20kernel.signal(2);21// maybe it's really fast?22await delay(50);23if (interrupted) {24expect(interrupted).toBe(true);25} else {26// wait a little longer in case computer is loaded.27await delay(200);28//expect(interrupted).toBe(true);29}3031kernel.terminate();32});3334// This checks a completely different aspect of sigint, since the sleep is in the main thread, not the worker.35// This DOESN't work yet. Note that interrupting time.sleep does work in the repl, but there are subtle issues36// with semantics of this via exec.37// test("sigint interrupts 'import time; time.sleep(10000)' within 250ms", async () => {38// // see above test for comments39// await repr("2+3");40// let interrupted = false;41// (async () => {42// try {43// await exec("import time; time.sleep(10000)");44// } catch (_err) {}45// })();46// (wasm as any).sigint();47// await delay(250);48// console.log(await repr('2+3'));49// });505152