From dc0111f2f3940372e7343d3dfe87e31db037a6b8 Mon Sep 17 00:00:00 2001 From: Rodrigo Tobar Date: Mon, 23 Sep 2024 19:59:42 +0800 Subject: [PATCH] Add tests for sub-interpreter support These are flaky as they use internal CPython packages, but that will do until the "interpreters" module is made eventually official. Signed-off-by: Rodrigo Tobar --- test/test_subinterpreter.py | 47 +++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 test/test_subinterpreter.py diff --git a/test/test_subinterpreter.py b/test/test_subinterpreter.py new file mode 100644 index 0000000..6073758 --- /dev/null +++ b/test/test_subinterpreter.py @@ -0,0 +1,47 @@ +import contextlib +from typing import Iterable, Tuple, no_type_check + +import pytest + +import crc32c + +try: + from test.support.interpreters import Interpreter # type: ignore + from test.support.interpreters import create, queues +except ImportError: + pytest.skip("No sub-interpreter support", allow_module_level=True) + + +@pytest.fixture(name="interpreter") +def interpreter_fixture() -> Iterable[Interpreter]: + interpreter = create() + with contextlib.closing(interpreter): + yield interpreter + + +def test_crc32c_can_be_loaded(interpreter: Interpreter) -> None: + interpreter.exec("import crc32c") + # all good, no exception raised + + +@pytest.mark.calculates_crc32c +def test_crc32c_can_run(interpreter: Interpreter) -> None: + + queue = queues.create() + VALUE = b"test" + interpreter.prepare_main(queue_id=queue.id, value_in=VALUE) + + @no_type_check + def crc32c_in_subinterpreter() -> None: + from test.support.interpreters.queues import Queue # type: ignore + + import crc32c + + queue = Queue(queue_id) + value_out = crc32c.crc32c(value_in) + queue.put(value_out) + + thread = interpreter.call_in_thread(crc32c_in_subinterpreter) + result = queue.get(timeout=5) + thread.join() + assert result == crc32c.crc32c(VALUE)