CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/project/jupyter/test/kernel.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import {} from "mocha";
7
import * as expect from "expect";
8
import * as common from "./common";
9
import { endswith } from "@cocalc/util/misc";
10
11
describe("compute 2+7 using the python2 kernel -- ", function () {
12
this.timeout(5000);
13
const kernel: common.JupyterKernel = common.kernel("test-python2");
14
15
it("evaluate 2+7", async function () {
16
expect(await common.exec(kernel, "2+7")).toBe('{"text/plain":"9"}');
17
});
18
19
it("closes the kernel", function () {
20
kernel.close();
21
});
22
23
it("verifies that executing code after closing the kernel gives an appropriate error", async function () {
24
try {
25
await kernel.execute_code_now({ code: "2+2" });
26
} catch (err) {
27
expect(err.toString()).toBe("Error: closed");
28
}
29
});
30
});
31
32
describe("compute 2/3 using a python3 kernel -- ", function () {
33
this.timeout(15000);
34
const kernel: common.JupyterKernel = common.kernel("test-python3");
35
36
it("evaluate 2/3", async function () {
37
expect(await common.exec(kernel, "2/3")).toBe(
38
'{"text/plain":"0.6666666666666666"}'
39
);
40
});
41
42
return it("closes the kernel", function () {
43
kernel.close();
44
});
45
});
46
47
describe("it tries to start a kernel that does not exist -- ", function () {
48
let kernel: common.JupyterKernel;
49
50
it("creates a foobar kernel", function () {
51
kernel = common.kernel("foobar");
52
return expect(kernel.get_state()).toBe("off");
53
});
54
55
it("then tries to use it, which will fail", async function () {
56
try {
57
await kernel.execute_code_now({ code: "2+2" });
58
} catch (err) {
59
expect(err.toString()).toBe("Error: No spec available for foobar");
60
}
61
});
62
});
63
64
describe("calling the spawn method -- ", function () {
65
const kernel = common.kernel("test-python2");
66
this.timeout(5000);
67
68
it("observes that the state switches to running", function (done) {
69
kernel.on("state", function (state) {
70
if (state !== "running") {
71
return;
72
}
73
done();
74
});
75
kernel.spawn();
76
});
77
78
it("observes that the state switches to closed", function (done) {
79
kernel.on("state", function (state) {
80
if (state !== "closed") {
81
return;
82
}
83
done();
84
});
85
kernel.close();
86
});
87
});
88
89
describe("send signals to a kernel -- ", function () {
90
const kernel = common.kernel("test-python2");
91
this.timeout(5000);
92
93
it("ensure kernel is running", async function () {
94
await kernel.spawn();
95
});
96
97
it("start a long sleep running... and interrupt it", async function () {
98
// send an interrupt signal to stop the sleep below:
99
return setTimeout(() => kernel.signal("SIGINT"), 250);
100
expect(await common.exec(kernel, "import time; time.sleep(1000)")).toBe(
101
"foo"
102
);
103
});
104
105
it("send a kill signal", function (done) {
106
kernel.on("state", function (state) {
107
expect(state).toBe("closed");
108
done();
109
});
110
kernel.signal("SIGKILL");
111
});
112
});
113
114
describe("start a kernel in a different directory -- ", function () {
115
let kernel: common.JupyterKernel;
116
this.timeout(5000);
117
118
it("creates a python2 kernel in current dir", async function () {
119
kernel = common.kernel("test-python2");
120
expect(
121
endswith(
122
await common.exec(kernel, 'import os; print(os.path.abspath("."))'),
123
"project/jupyter\n"
124
)
125
).toBe(true);
126
kernel.close();
127
});
128
129
it("creates a python2 kernel with path test/a.ipynb2", async function () {
130
kernel = common.kernel("test-python2", "test/a.ipynb2");
131
expect(
132
endswith(
133
await common.exec(kernel, 'import os; print(os.path.abspath("."))'),
134
"project/jupyter/test\n"
135
)
136
).toBe(true);
137
kernel.close();
138
});
139
});
140
141
describe("use the key:value store -- ", function () {
142
const kernel = common.kernel("test-python2");
143
this.timeout(5000);
144
145
it("tests setting the store", function () {
146
kernel.store.set({ a: 5, b: 7 }, { the: "value" });
147
expect(kernel.store.get({ b: 7, a: 5 })).toEqual({ the: "value" });
148
});
149
150
it("tests deleting from the store", function () {
151
kernel.store.delete({ a: 5, b: 7 });
152
expect(kernel.store.get({ b: 7, a: 5 })).toBe(undefined);
153
});
154
});
155
156