forked from denosaurs/deno_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_with_gc.ts
More file actions
48 lines (40 loc) · 1.23 KB
/
test_with_gc.ts
File metadata and controls
48 lines (40 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import python, { Callback } from "../mod.ts";
import { assertEquals } from "./asserts.ts";
Deno.test("callbacks are not gc'd while still needed by python", () => {
const pyModule = python.runModule(
`
stored_callback = None
def store_and_call_callback(cb):
global stored_callback
stored_callback = cb
return stored_callback()
def call_stored_callback():
global stored_callback
if stored_callback is None:
return -1
return stored_callback()
`,
"test_gc_module",
);
let callCount = 0;
const callback = () => {
callCount++;
return callCount * 10;
};
// Store the callback in Python and call it
const callbackObj = new Callback(callback);
assertEquals(pyModule.store_and_call_callback(callbackObj).valueOf(), 10);
assertEquals(callCount, 1);
for (let i = 0; i < 10; i++) {
// @ts-ignore:requires: --v8-flags=--expose-gc
gc();
}
// If the callback was incorrectly GC'd, this should segfault
// But it should work because Python holds a reference
assertEquals(pyModule.call_stored_callback().valueOf(), 20);
assertEquals(callCount, 2);
// Call it again to be sure
assertEquals(pyModule.call_stored_callback().valueOf(), 30);
assertEquals(callCount, 3);
callbackObj.destroy();
});