|
| 1 | +import sys |
| 2 | +import time |
| 3 | +import pytest |
| 4 | +import platform |
| 5 | +import textwrap |
| 6 | +from queue import Empty |
| 7 | + |
| 8 | +from .testutils import check_deterministic_pickle |
| 9 | + |
| 10 | +if sys.platform == "win32": |
| 11 | + if sys.version_info < (3, 11): |
| 12 | + pytest.skip( |
| 13 | + "ipykernel requires Python 3.11 or later", |
| 14 | + allow_module_level=True |
| 15 | + ) |
| 16 | +ipykernel = pytest.importorskip("ipykernel") |
| 17 | + |
| 18 | + |
| 19 | +def run_in_notebook(code, timeout=10): |
| 20 | + |
| 21 | + km = ipykernel.connect.jupyter_client.KernelManager() |
| 22 | + km.start_kernel() |
| 23 | + kc = km.client() |
| 24 | + kc.start_channels() |
| 25 | + status, output, err = "kernel_started", None, None |
| 26 | + try: |
| 27 | + assert km.is_alive() and kc.is_alive() |
| 28 | + kc.wait_for_ready() |
| 29 | + idx = kc.execute(code) |
| 30 | + running = True |
| 31 | + while running: |
| 32 | + try: |
| 33 | + res = kc.iopub_channel.get_msg(timeout=timeout) |
| 34 | + except Empty: |
| 35 | + status = "timeout" |
| 36 | + break |
| 37 | + if res['parent_header'].get('msg_id') != idx: |
| 38 | + continue |
| 39 | + content = res['content'] |
| 40 | + if content.get("name", "state") == "stdout": |
| 41 | + output = content['text'] |
| 42 | + if "traceback" in content: |
| 43 | + err = "\n".join(content['traceback']) |
| 44 | + status = "error" |
| 45 | + running = res['content'].get('execution_state', None) != "idle" |
| 46 | + finally: |
| 47 | + kc.shutdown() |
| 48 | + kc.stop_channels() |
| 49 | + km.shutdown_kernel(now=True, restart=False) |
| 50 | + assert not km.is_alive() |
| 51 | + if status not in ["error", "timeout"]: |
| 52 | + status = "ok" if not running else "exec_error" |
| 53 | + return status, output, err |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.skipif( |
| 57 | + platform.python_implementation() == "PyPy", |
| 58 | + reason="Skip PyPy because tests are too slow", |
| 59 | +) |
| 60 | +@pytest.mark.parametrize("code, expected", [ |
| 61 | + ("1 + 1", "ok"), |
| 62 | + ("raise ValueError('This is a test error')", "error"), |
| 63 | + ("import time; time.sleep(100)", "timeout") |
| 64 | +
|
| 65 | +]) |
| 66 | +def test_run_in_notebook(code, expected): |
| 67 | + code = textwrap.dedent(code) |
| 68 | + |
| 69 | + t_start = time.time() |
| 70 | + status, output, err = run_in_notebook(code, timeout=1) |
| 71 | + duration = time.time() - t_start |
| 72 | + assert status == expected, ( |
| 73 | + f"Unexpected status: {status}, output: {output}, err: {err}, duration: {duration}" |
| 74 | + ) |
| 75 | + assert duration < 10, "Timeout not enforced properly" |
| 76 | + if expected == "error": |
| 77 | + assert "This is a test error" in err |
| 78 | + |
| 79 | + |
| 80 | +def test_deterministic_payload_for_dynamic_func_in_notebook(): |
| 81 | + code = textwrap.dedent(""" |
| 82 | + import cloudpickle |
| 83 | +
|
| 84 | + MY_PI = 3.1415 |
| 85 | +
|
| 86 | + def get_pi(): |
| 87 | + return MY_PI |
| 88 | +
|
| 89 | + print(cloudpickle.dumps(get_pi)) |
| 90 | + """) |
| 91 | + |
| 92 | + status, output, err = run_in_notebook(code) |
| 93 | + assert status == "ok" |
| 94 | + payload = eval(output.strip(), {}) |
| 95 | + |
| 96 | + status, output, err = run_in_notebook(code) |
| 97 | + assert status == "ok" |
| 98 | + payload2 = eval(output.strip(), {}) |
| 99 | + |
| 100 | + check_deterministic_pickle(payload, payload2) |
0 commit comments