Debian Codegraph @

tests/test_kernelapp.py

raw
1 import os
2 import shutil
3 import sys
4 import time
5 from subprocess import PIPE, Popen
6 from tempfile import mkdtemp
7
8
9 def _launch(extra_env):
10 env = os.environ.copy()
11 env.update(extra_env)
12 return Popen(
13 [sys.executable, "-c", "from jupyter_client.kernelapp import main; main()"],
14 env=env,
15 stderr=PIPE,
16 )
17
18
19 WAIT_TIME = 10
20 POLL_FREQ = 10
21
22
23 def test_kernelapp_lifecycle():
24 # Check that 'jupyter kernel' starts and terminates OK.
25 runtime_dir = mkdtemp()
26 startup_dir = mkdtemp()
27 started = os.path.join(startup_dir, "started")
28 try:
29 p = _launch(
30 {
31 "JUPYTER_RUNTIME_DIR": runtime_dir,
32 "JUPYTER_CLIENT_TEST_RECORD_STARTUP_PRIVATE": started,
33 }
34 )
35 # Wait for start
36 for _ in range(WAIT_TIME * POLL_FREQ):
37 if os.path.isfile(started):
38 break
39 time.sleep(1 / POLL_FREQ)
40 else:
41 raise AssertionError(f"No started file created in {WAIT_TIME} seconds")
42
43 # Connection file should be there by now
44 for _ in range(WAIT_TIME * POLL_FREQ):
45 files = os.listdir(runtime_dir)
46 if files:
47 break
48 time.sleep(1 / POLL_FREQ)
49 else:
50 raise AssertionError(f"No connection file created in {WAIT_TIME} seconds")
51 assert len(files) == 1
52 cf = files[0]
53 assert cf.startswith("kernel")
54 assert cf.endswith(".json")
55
56 # Send SIGTERM to shut down
57 time.sleep(1)
58 p.terminate()
59 _, stderr = p.communicate(timeout=WAIT_TIME)
60 assert cf in stderr.decode("utf-8", "replace")
61 finally:
62 shutil.rmtree(runtime_dir)
63 shutil.rmtree(startup_dir)