Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/jupyter/stateless-api/kernel-ulimit.test.ts
1447 views
1
/*
2
Testing that ulimit is set on the kernels.
3
*/
4
5
import { getPythonKernelName } from "../kernel/kernel-data";
6
import Kernel from "./kernel";
7
import { until } from "@cocalc/util/async-utils";
8
9
const SECONDS = 2;
10
11
jest.setTimeout(10000);
12
describe("ulimit is set on the stateless api kernels (and can be configured)", () => {
13
let kernel;
14
let kernelName;
15
16
it(`modifies the pool params so the ulimit is ${SECONDS} second of CPU usage`, async () => {
17
kernelName = await getPythonKernelName();
18
Kernel.setUlimit(kernelName, `-t ${SECONDS}`);
19
});
20
21
it("gets a kernel", async () => {
22
// repeat because in rare cases the kernel already in the pool may
23
// get the ulimit from starting up python (1s of cpu time is short!)
24
await until(
25
async () => {
26
try {
27
kernel = await Kernel.getFromPool(kernelName);
28
return true;
29
} catch {
30
return false;
31
}
32
},
33
{ start: 1000 },
34
);
35
});
36
37
it("quick eval works", async () => {
38
const output = await kernel.execute("389+11");
39
expect(output[0].data["text/plain"]).toBe("400");
40
});
41
42
it(`something that takes infinite CPU time gets killed in at most ${SECONDS} seconds`, async () => {
43
const start = Date.now();
44
try {
45
await kernel.execute("while True: sum(range(10**8))");
46
} catch (err) {
47
expect(`${err}`).toContain("Kernel last exited with code 137.");
48
}
49
expect(Date.now() - start).toBeLessThan(SECONDS * 1500);
50
});
51
52
it("cleans up", () => {
53
kernel.close();
54
});
55
});
56
57