|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import sys |
| 5 | +import threading |
| 6 | +import types |
| 7 | +from contextlib import contextmanager |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +_TEST_HELPERS_ROOT = Path(__file__).resolve().parents[3] / "cuda_python_test_helpers" |
| 13 | +sys.path.insert(0, str(_TEST_HELPERS_ROOT)) |
| 14 | + |
| 15 | +from cuda_python_test_helpers.pytest_run_parallel import ( |
| 16 | + install_run_parallel_worker_context_patch, |
| 17 | + mark_item_for_worker_context, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +def _install_fake_pytest_run_parallel(monkeypatch): |
| 22 | + package = types.ModuleType("pytest_run_parallel") |
| 23 | + package.__path__ = [] |
| 24 | + plugin = types.ModuleType("pytest_run_parallel.plugin") |
| 25 | + |
| 26 | + def wrap_function_parallel(fn, n_workers, n_iterations): |
| 27 | + raise AssertionError("unpatched fake wrapper should not be called") |
| 28 | + |
| 29 | + plugin.wrap_function_parallel = wrap_function_parallel |
| 30 | + monkeypatch.setitem(sys.modules, "pytest_run_parallel", package) |
| 31 | + monkeypatch.setitem(sys.modules, "pytest_run_parallel.plugin", plugin) |
| 32 | + return plugin |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.agent_authored(model="gpt-5") |
| 36 | +def test_install_run_parallel_worker_context_patch_is_idempotent(monkeypatch): |
| 37 | + plugin = _install_fake_pytest_run_parallel(monkeypatch) |
| 38 | + |
| 39 | + assert install_run_parallel_worker_context_patch() is True |
| 40 | + patched = plugin.wrap_function_parallel |
| 41 | + assert patched._cuda_python_patched_run_parallel_worker_context |
| 42 | + |
| 43 | + assert install_run_parallel_worker_context_patch() is True |
| 44 | + assert plugin.wrap_function_parallel is patched |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.agent_authored(model="gpt-5") |
| 48 | +def test_patched_wrapper_runs_context_with_isolated_kwargs(monkeypatch): |
| 49 | + plugin = _install_fake_pytest_run_parallel(monkeypatch) |
| 50 | + install_run_parallel_worker_context_patch() |
| 51 | + |
| 52 | + lock = threading.Lock() |
| 53 | + context_events = [] |
| 54 | + calls = [] |
| 55 | + |
| 56 | + @contextmanager |
| 57 | + def worker_context(*, thread_index, iteration_index, kwargs): |
| 58 | + token = object() |
| 59 | + kwargs["token"] = (thread_index, iteration_index, id(token)) |
| 60 | + kwargs["context_kwargs_id"] = id(kwargs) |
| 61 | + with lock: |
| 62 | + context_events.append(("enter", thread_index, iteration_index, id(kwargs))) |
| 63 | + try: |
| 64 | + yield |
| 65 | + finally: |
| 66 | + with lock: |
| 67 | + context_events.append(("exit", thread_index, iteration_index, id(kwargs))) |
| 68 | + |
| 69 | + def test_body(*, thread_index, iteration_index, token, context_kwargs_id, static_value): |
| 70 | + with lock: |
| 71 | + calls.append( |
| 72 | + { |
| 73 | + "thread_index": thread_index, |
| 74 | + "iteration_index": iteration_index, |
| 75 | + "token": token, |
| 76 | + "context_kwargs_id": context_kwargs_id, |
| 77 | + "static_value": static_value, |
| 78 | + } |
| 79 | + ) |
| 80 | + |
| 81 | + item = types.SimpleNamespace(obj=test_body) |
| 82 | + assert mark_item_for_worker_context(item, worker_context) is True |
| 83 | + |
| 84 | + wrapped = plugin.wrap_function_parallel(item.obj, n_workers=3, n_iterations=2) |
| 85 | + wrapped(thread_index=-1, iteration_index=-1, static_value="fixture-value") |
| 86 | + |
| 87 | + expected_pairs = {(thread_index, iteration_index) for thread_index in range(3) for iteration_index in range(2)} |
| 88 | + actual_pairs = {(call["thread_index"], call["iteration_index"]) for call in calls} |
| 89 | + assert actual_pairs == expected_pairs |
| 90 | + assert {call["token"][:2] for call in calls} == expected_pairs |
| 91 | + assert {call["static_value"] for call in calls} == {"fixture-value"} |
| 92 | + |
| 93 | + kwargs_ids = {call["context_kwargs_id"] for call in calls} |
| 94 | + assert len(kwargs_ids) == 6 |
| 95 | + assert len(context_events) == 12 |
| 96 | + assert {event[3] for event in context_events} == kwargs_ids |
| 97 | + |
| 98 | + |
| 99 | +@pytest.mark.agent_authored(model="gpt-5") |
| 100 | +def test_mark_item_for_worker_context_wraps_callables_without_attrs(): |
| 101 | + class CallableWithoutAttrs: |
| 102 | + __slots__ = ("calls",) |
| 103 | + |
| 104 | + def __init__(self): |
| 105 | + self.calls = [] |
| 106 | + |
| 107 | + def __call__(self, **kwargs): |
| 108 | + self.calls.append(kwargs) |
| 109 | + |
| 110 | + @contextmanager |
| 111 | + def worker_context(*, thread_index, iteration_index, kwargs): |
| 112 | + kwargs["patched"] = True |
| 113 | + yield |
| 114 | + |
| 115 | + original = CallableWithoutAttrs() |
| 116 | + item = types.SimpleNamespace(obj=original) |
| 117 | + |
| 118 | + assert mark_item_for_worker_context(item, worker_context) is True |
| 119 | + assert item.obj is not original |
| 120 | + item.obj() |
| 121 | + assert original.calls == [{}] |
0 commit comments