Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/core/posix-node/src/spawn.test.ts
1067 views
1
import posix from "./index";
2
3
test("posix_spawn /bin/sleep and wait for it to finish using waitpid and confirm the time", () => {
4
const t0 = new Date().valueOf();
5
const pid = posix.posix_spawn?.(
6
"/bin/sleep",
7
null,
8
null,
9
["/bin/sleep", "0.5"],
10
{}
11
);
12
if (pid == null) {
13
throw Error("pid must be a positive integer");
14
}
15
const x = posix.waitpid?.(pid, 0);
16
if (x == null) {
17
throw Error("waitpid must return something");
18
}
19
const { ret, wstatus } = x;
20
expect(ret).toBe(pid);
21
expect(wstatus).toBe(0);
22
const tm = new Date().valueOf() - t0;
23
expect(tm > 400 && tm < 1000).toBe(true);
24
});
25
26
test("posix_spawnp sleep and wait for it to finish using waitpid and confirm the time", () => {
27
const t0 = new Date().valueOf();
28
const pid = posix.posix_spawnp?.("sleep", null, null, ["sleep", "0.5"], {});
29
if (pid == null) {
30
throw Error("pid must be a positive integer");
31
}
32
const x = posix.waitpid?.(pid, 0);
33
if (x == null) {
34
throw Error("waitpid must return something");
35
}
36
const { ret, wstatus } = x;
37
expect(ret).toBe(pid);
38
expect(wstatus).toBe(0);
39
const tm = new Date().valueOf() - t0;
40
expect(tm > 400 && tm < 1000).toBe(true);
41
});
42
43
test("calling posix_spawnp with invalid fd arg to addclose to throw", () => {
44
expect(() =>
45
posix.posix_spawnp?.("sleep", [["addclose", -1]], {}, ["sleep", "3"], {})
46
).toThrow("posix_spawn_file_actions_addclose failed");
47
});
48
49
test("calling posix_spawnp with invalid new_fd arg to adddup2 to throw", () => {
50
expect(() =>
51
posix.posix_spawnp?.(
52
"sleep",
53
[["adddup2", 1, -100]],
54
{},
55
["sleep", "3"],
56
{}
57
)
58
).toThrow("posix_spawn_file_actions_adddup2 failed");
59
});
60
61
test("calling posix_spawnp with invalid fd arg to addopen to throw", () => {
62
expect(() =>
63
posix.posix_spawnp?.(
64
"sleep",
65
[["addopen", -1, "/tmp/a", 0, 0]],
66
{},
67
["sleep", "3"],
68
{}
69
)
70
).toThrow("posix_spawn_file_actions_addopen failed");
71
});
72
73