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