|
| 1 | +"""Reusable test fixtures for ``jupyter_server_proxy``.""" |
| 2 | +import socket |
| 3 | +import sys |
| 4 | +import os |
| 5 | +from pytest import fixture |
| 6 | +from subprocess import Popen |
| 7 | +from typing import Generator, Any, Tuple |
| 8 | +from pathlib import Path |
| 9 | +from uuid import uuid4 |
| 10 | +from urllib.error import URLError |
| 11 | +import time |
| 12 | +from urllib.request import urlopen |
| 13 | +import shutil |
| 14 | + |
| 15 | +HERE = Path(__file__).parent |
| 16 | +RESOURCES = HERE / "resources" |
| 17 | + |
| 18 | +@fixture |
| 19 | +def a_token() -> str: |
| 20 | + """Get a random UUID to use for a token.""" |
| 21 | + return str(uuid4()) |
| 22 | + |
| 23 | + |
| 24 | +@fixture |
| 25 | +def an_unused_port() -> int: |
| 26 | + """Get a random unused port.""" |
| 27 | + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 28 | + s.bind(("127.0.0.1", 0)) |
| 29 | + s.listen(1) |
| 30 | + port = s.getsockname()[1] |
| 31 | + s.close() |
| 32 | + return port |
| 33 | + |
| 34 | + |
| 35 | +@fixture(params=["notebook", "lab"]) |
| 36 | +def a_server_cmd(request: Any) -> str: |
| 37 | + """Get a viable name for a command.""" |
| 38 | + return request.param |
| 39 | + |
| 40 | + |
| 41 | +@fixture |
| 42 | +def a_server( |
| 43 | + a_server_cmd: str, |
| 44 | + tmp_path: Path, |
| 45 | + an_unused_port: int, |
| 46 | + a_token: str, |
| 47 | +) -> Generator[str, None, None]: |
| 48 | + """Get a running server.""" |
| 49 | + # get a copy of the resources |
| 50 | + tests = tmp_path / "tests" |
| 51 | + tests.mkdir() |
| 52 | + shutil.copytree(RESOURCES, tests / "resources") |
| 53 | + args = [ |
| 54 | + sys.executable, |
| 55 | + "-m", |
| 56 | + "jupyter", |
| 57 | + a_server_cmd, |
| 58 | + f"--port={an_unused_port}", |
| 59 | + "--no-browser", |
| 60 | + "--config=./tests/resources/jupyter_server_config.py", |
| 61 | + "--debug", |
| 62 | + ] |
| 63 | + |
| 64 | + # prepare an env |
| 65 | + env = dict(os.environ) |
| 66 | + env.update(JUPYTER_TOKEN=a_token) |
| 67 | + |
| 68 | + # start the process |
| 69 | + server_proc = Popen(args, cwd=str(tmp_path), env=env) |
| 70 | + |
| 71 | + # prepare some URLss |
| 72 | + url = f"http://127.0.0.1:{an_unused_port}/" |
| 73 | + canary_url = f"{url}favicon.ico" |
| 74 | + shutdown_url = f"{url}api/shutdown?token={a_token}" |
| 75 | + |
| 76 | + retries = 10 |
| 77 | + |
| 78 | + while retries: |
| 79 | + try: |
| 80 | + urlopen(canary_url) |
| 81 | + break |
| 82 | + except URLError as err: |
| 83 | + if "Connection refused" in str(err): |
| 84 | + print( |
| 85 | + f"{a_server_cmd} not ready, will try again in 0.5s [{retries} retries]", |
| 86 | + flush=True, |
| 87 | + ) |
| 88 | + time.sleep(0.5) |
| 89 | + retries -= 1 |
| 90 | + continue |
| 91 | + raise err |
| 92 | + |
| 93 | + print(f"{a_server_cmd} is ready...", flush=True) |
| 94 | + |
| 95 | + yield url |
| 96 | + |
| 97 | + # clean up after server is no longer needed |
| 98 | + print(f"{a_server_cmd} shutting down...", flush=True) |
| 99 | + urlopen(shutdown_url, data=[]) |
| 100 | + server_proc.wait() |
| 101 | + print(f"{a_server_cmd} is stopped", flush=True) |
| 102 | + |
| 103 | + |
| 104 | +@fixture |
| 105 | +def a_server_port_and_token( |
| 106 | + a_server: str, # noqa |
| 107 | + an_unused_port: int, |
| 108 | + a_token: str, |
| 109 | +) -> Tuple[int, str]: |
| 110 | + """Get the port and token for a running server.""" |
| 111 | + return an_unused_port, a_token |
0 commit comments