Path: blob/master/src/packages/jupyter/stateless-api/stateless-api-kernel.test.ts
1447 views
/*1Test the Jupyer stateless api kernel functionality.2*/34import { getPythonKernelName } from "../kernel/kernel-data";5import Kernel, { DEFAULT_POOL_SIZE } from "./kernel";67describe("create a jupyter stateless-api kernel and test basic functionality", () => {8let kernel;9it("gets a kernel", async () => {10const kernelName = await getPythonKernelName();11// @ts-ignore12expect(Kernel.getPool(kernelName).length).toBe(0);13kernel = await Kernel.getFromPool(kernelName);14// @ts-ignore15expect(Kernel.getPool(kernelName).length).toBe(DEFAULT_POOL_SIZE);16});1718it("confirms it is 'running'", () => {19expect(kernel.kernel.get_state()).toBe("running");20});2122it("compute 2+3", async () => {23const output = await kernel.execute("2+3");24expect(output[0].data["text/plain"]).toBe("5");25});2627it("exec something with two distinct output messages", async () => {28const output = await kernel.execute(29"import sys; sys.stdout.write('1'); sys.stdout.flush(); print('2')",30);31expect(output.length).toBe(2);32expect(output).toEqual([33{ name: "stdout", text: "1" },34{ name: "stdout", text: "2\n" },35]);36});3738it("exec something that throws an error", async () => {39const output = await kernel.execute("1/0");40expect(output[0].traceback.join("")).toContain("division by zero");41});4243it("chdir to /tmp and confirm that", async () => {44await kernel.chdir("/tmp");45const output = await kernel.execute(46"import os; os.path.abspath(os.curdir)",47);48expect(output).toEqual([{ data: { "text/plain": "'/tmp'" } }]);49});5051it("get another kernel and confirm pool is maintained", async () => {52const kernelName = await getPythonKernelName();53const kernel2 = await Kernel.getFromPool(kernelName);54// @ts-ignore55expect(Kernel.getPool(kernelName).length).toBe(DEFAULT_POOL_SIZE);56kernel2.close();57});5859it("cleans up", () => {60kernel.close();61Kernel.closeAll();62});63});6465describe("test timeout - this is how long until pool starts getting trimmed", () => {66let kernel;67it("gets a kernel with a 1s timeout", async () => {68const kernelName = await getPythonKernelName();69kernel = await Kernel.getFromPool(kernelName, { timeout_s: 1 });70});7172it("quick eval works", async () => {73const output = await kernel.execute("389+11");74expect(output[0].data["text/plain"]).toBe("400");75});7677it("something that takes more than a second", async () => {78await kernel.execute("print('hi'); import time; time.sleep(1.2)");79});8081it("now check that the pool started shrinking", async () => {82const kernelName = await getPythonKernelName();83// @ts-ignore84expect(Kernel.getPool(kernelName).length).toBeLessThan(DEFAULT_POOL_SIZE);85});86});87888990