Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/project/jupyter/test/kernel.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import {} from "mocha";6import * as expect from "expect";7import * as common from "./common";8import { endswith } from "@cocalc/util/misc";910describe("compute 2+7 using the python2 kernel -- ", function () {11this.timeout(5000);12const kernel: common.JupyterKernel = common.kernel("test-python2");1314it("evaluate 2+7", async function () {15expect(await common.exec(kernel, "2+7")).toBe('{"text/plain":"9"}');16});1718it("closes the kernel", function () {19kernel.close();20});2122it("verifies that executing code after closing the kernel gives an appropriate error", async function () {23try {24await kernel.execute_code_now({ code: "2+2" });25} catch (err) {26expect(err.toString()).toBe("Error: closed");27}28});29});3031describe("compute 2/3 using a python3 kernel -- ", function () {32this.timeout(15000);33const kernel: common.JupyterKernel = common.kernel("test-python3");3435it("evaluate 2/3", async function () {36expect(await common.exec(kernel, "2/3")).toBe(37'{"text/plain":"0.6666666666666666"}'38);39});4041return it("closes the kernel", function () {42kernel.close();43});44});4546describe("it tries to start a kernel that does not exist -- ", function () {47let kernel: common.JupyterKernel;4849it("creates a foobar kernel", function () {50kernel = common.kernel("foobar");51return expect(kernel.get_state()).toBe("off");52});5354it("then tries to use it, which will fail", async function () {55try {56await kernel.execute_code_now({ code: "2+2" });57} catch (err) {58expect(err.toString()).toBe("Error: No spec available for foobar");59}60});61});6263describe("calling the spawn method -- ", function () {64const kernel = common.kernel("test-python2");65this.timeout(5000);6667it("observes that the state switches to running", function (done) {68kernel.on("state", function (state) {69if (state !== "running") {70return;71}72done();73});74kernel.spawn();75});7677it("observes that the state switches to closed", function (done) {78kernel.on("state", function (state) {79if (state !== "closed") {80return;81}82done();83});84kernel.close();85});86});8788describe("send signals to a kernel -- ", function () {89const kernel = common.kernel("test-python2");90this.timeout(5000);9192it("ensure kernel is running", async function () {93await kernel.spawn();94});9596it("start a long sleep running... and interrupt it", async function () {97// send an interrupt signal to stop the sleep below:98return setTimeout(() => kernel.signal("SIGINT"), 250);99expect(await common.exec(kernel, "import time; time.sleep(1000)")).toBe(100"foo"101);102});103104it("send a kill signal", function (done) {105kernel.on("state", function (state) {106expect(state).toBe("closed");107done();108});109kernel.signal("SIGKILL");110});111});112113describe("start a kernel in a different directory -- ", function () {114let kernel: common.JupyterKernel;115this.timeout(5000);116117it("creates a python2 kernel in current dir", async function () {118kernel = common.kernel("test-python2");119expect(120endswith(121await common.exec(kernel, 'import os; print(os.path.abspath("."))'),122"project/jupyter\n"123)124).toBe(true);125kernel.close();126});127128it("creates a python2 kernel with path test/a.ipynb2", async function () {129kernel = common.kernel("test-python2", "test/a.ipynb2");130expect(131endswith(132await common.exec(kernel, 'import os; print(os.path.abspath("."))'),133"project/jupyter/test\n"134)135).toBe(true);136kernel.close();137});138});139140describe("use the key:value store -- ", function () {141const kernel = common.kernel("test-python2");142this.timeout(5000);143144it("tests setting the store", function () {145kernel.store.set({ a: 5, b: 7 }, { the: "value" });146expect(kernel.store.get({ b: 7, a: 5 })).toEqual({ the: "value" });147});148149it("tests deleting from the store", function () {150kernel.store.delete({ a: 5, b: 7 });151expect(kernel.store.get({ b: 7, a: 5 })).toBe(undefined);152});153});154155156