Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/jupyter/kernel/test/kernel.test.ts
1450 views
1
/*
2
I'm a little hesistant about testing this since we'll need to make sure that a kernel is
3
installed, e.g., to test on Github actions.
4
Probably, the way to go would be to install https://www.npmjs.com/package/ijavascript
5
and just test that a lot, since it would be the minimal dependency.
6
7
There are a lot of ideas for tests in this bitrotted place:
8
9
https://github.com/sagemathinc/cocalc/tree/master/src/packages/project/jupyter/test
10
*/
11
12
import { getPythonKernel, closeKernels } from "./util";
13
import { kernel } from "../kernel";
14
15
describe("test trying to use a kernel that doesn't exist", () => {
16
it("fails to start", async () => {
17
const k = kernel({ name: "no-such-kernel", path: "none.ipynb" });
18
const errors: any[] = [];
19
k.on("kernel_error", (err) => {
20
errors.push(err);
21
});
22
23
await expect(
24
async () => await k.execute_code_now({ code: "2+3" }),
25
).rejects.toThrow("No spec available for kernel");
26
27
expect(errors[0]).toContain("No spec available for kernel");
28
});
29
});
30
31
describe("create and close python kernel", () => {
32
let k;
33
it("get a python kernel", async () => {
34
k = await getPythonKernel("python-0.ipynb");
35
});
36
37
it("cleans up", () => {
38
k.close();
39
});
40
});
41
42
describe("spawn and close python kernel", () => {
43
let k;
44
it("get a python kernel", async () => {
45
k = await getPythonKernel("python-1.ipynb");
46
});
47
48
it("spawns the kernel", async () => {
49
await k.spawn();
50
});
51
52
it("cleans up", () => {
53
k.close();
54
});
55
});
56
57
describe("compute 2+3 using a python kernel", () => {
58
let k;
59
it("get a python kernel", async () => {
60
k = await getPythonKernel("python-2.ipynb");
61
});
62
63
it("spawn the kernel", async () => {
64
await k.spawn();
65
});
66
67
it("evaluate 2+3, confirming the result", async () => {
68
const output = k.execute_code({ code: "2+3" });
69
const iter = output.iter();
70
const v: any[] = [];
71
for await (const x of iter) {
72
v.push(x);
73
}
74
expect(v[0].content).toEqual({ execution_state: "busy" });
75
expect(v[1].content).toEqual({ code: "2+3", execution_count: 1 });
76
expect(v[2].content.data).toEqual({ "text/plain": "5" });
77
});
78
79
it("define a variable in one call, then use it in another", async () => {
80
const output = k.execute_code({ code: "a=5" });
81
await output.waitUntilDone();
82
output.close();
83
});
84
85
it("uses that variable in another call", async () => {
86
const output = k.execute_code({ code: "a + a" });
87
const iter = output.iter();
88
await output.waitUntilDone();
89
for await (const x of iter) {
90
if (x.content?.data) {
91
expect(x.content?.data).toEqual({ "text/plain": "10" });
92
break;
93
}
94
}
95
});
96
97
it("cleans up", () => {
98
k.close();
99
});
100
});
101
102
describe("start computation then immediately close the kernel should not crash", () => {
103
let k;
104
it("get and spawn a python kernel", async () => {
105
k = await getPythonKernel("python-4.ipynb");
106
await k.spawn();
107
});
108
109
it("start something running, then immediately close and see error event is called", async () => {
110
const output = k.execute_code({ code: "sleep 10000" });
111
for await (const _ of output.iter()) {
112
// it's ack'd as running:
113
break;
114
}
115
});
116
117
it("closes during computation", () => {
118
k.close();
119
});
120
121
it("starts and closes another kernel with the same path", async () => {
122
const k2 = await getPythonKernel("python-4.ipynb", true);
123
k2.close();
124
});
125
});
126
127
afterAll(() => {
128
closeKernels();
129
});
130
131