Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/jupyter/stateless-api/stateless-api-kernel.test.ts
1447 views
1
/*
2
Test the Jupyer stateless api kernel functionality.
3
*/
4
5
import { getPythonKernelName } from "../kernel/kernel-data";
6
import Kernel, { DEFAULT_POOL_SIZE } from "./kernel";
7
8
describe("create a jupyter stateless-api kernel and test basic functionality", () => {
9
let kernel;
10
it("gets a kernel", async () => {
11
const kernelName = await getPythonKernelName();
12
// @ts-ignore
13
expect(Kernel.getPool(kernelName).length).toBe(0);
14
kernel = await Kernel.getFromPool(kernelName);
15
// @ts-ignore
16
expect(Kernel.getPool(kernelName).length).toBe(DEFAULT_POOL_SIZE);
17
});
18
19
it("confirms it is 'running'", () => {
20
expect(kernel.kernel.get_state()).toBe("running");
21
});
22
23
it("compute 2+3", async () => {
24
const output = await kernel.execute("2+3");
25
expect(output[0].data["text/plain"]).toBe("5");
26
});
27
28
it("exec something with two distinct output messages", async () => {
29
const output = await kernel.execute(
30
"import sys; sys.stdout.write('1'); sys.stdout.flush(); print('2')",
31
);
32
expect(output.length).toBe(2);
33
expect(output).toEqual([
34
{ name: "stdout", text: "1" },
35
{ name: "stdout", text: "2\n" },
36
]);
37
});
38
39
it("exec something that throws an error", async () => {
40
const output = await kernel.execute("1/0");
41
expect(output[0].traceback.join("")).toContain("division by zero");
42
});
43
44
it("chdir to /tmp and confirm that", async () => {
45
await kernel.chdir("/tmp");
46
const output = await kernel.execute(
47
"import os; os.path.abspath(os.curdir)",
48
);
49
expect(output).toEqual([{ data: { "text/plain": "'/tmp'" } }]);
50
});
51
52
it("get another kernel and confirm pool is maintained", async () => {
53
const kernelName = await getPythonKernelName();
54
const kernel2 = await Kernel.getFromPool(kernelName);
55
// @ts-ignore
56
expect(Kernel.getPool(kernelName).length).toBe(DEFAULT_POOL_SIZE);
57
kernel2.close();
58
});
59
60
it("cleans up", () => {
61
kernel.close();
62
Kernel.closeAll();
63
});
64
});
65
66
describe("test timeout - this is how long until pool starts getting trimmed", () => {
67
let kernel;
68
it("gets a kernel with a 1s timeout", async () => {
69
const kernelName = await getPythonKernelName();
70
kernel = await Kernel.getFromPool(kernelName, { timeout_s: 1 });
71
});
72
73
it("quick eval works", async () => {
74
const output = await kernel.execute("389+11");
75
expect(output[0].data["text/plain"]).toBe("400");
76
});
77
78
it("something that takes more than a second", async () => {
79
await kernel.execute("print('hi'); import time; time.sleep(1.2)");
80
});
81
82
it("now check that the pool started shrinking", async () => {
83
const kernelName = await getPythonKernelName();
84
// @ts-ignore
85
expect(Kernel.getPool(kernelName).length).toBeLessThan(DEFAULT_POOL_SIZE);
86
});
87
});
88
89
90