|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +from concurrent.futures import ThreadPoolExecutor |
| 6 | +from contextvars import ContextVar, copy_context |
5 | 7 | from dataclasses import FrozenInstanceError |
6 | 8 |
|
7 | 9 | import pytest |
@@ -34,4 +36,79 @@ def test_context_is_immutable() -> None: |
34 | 36 | context = GuardrailsContext(guardrail_llm=_StubClient()) |
35 | 37 |
|
36 | 38 | with pytest.raises(FrozenInstanceError): |
37 | | - context.guardrail_llm = None # type: ignore[misc] |
| 39 | + context.guardrail_llm = None |
| 40 | + |
| 41 | + |
| 42 | +def test_contextvar_propagates_with_copy_context() -> None: |
| 43 | + test_var: ContextVar[str | None] = ContextVar("test_var", default=None) |
| 44 | + test_var.set("test_value") |
| 45 | + |
| 46 | + def get_contextvar(): |
| 47 | + return test_var.get() |
| 48 | + |
| 49 | + ctx = copy_context() |
| 50 | + result = ctx.run(get_contextvar) |
| 51 | + assert result == "test_value" # noqa: S101 |
| 52 | + |
| 53 | + |
| 54 | +def test_contextvar_propagates_with_threadpool() -> None: |
| 55 | + test_var: ContextVar[str | None] = ContextVar("test_var", default=None) |
| 56 | + test_var.set("thread_test") |
| 57 | + |
| 58 | + def get_contextvar(): |
| 59 | + return test_var.get() |
| 60 | + |
| 61 | + ctx = copy_context() |
| 62 | + with ThreadPoolExecutor(max_workers=1) as executor: |
| 63 | + future = executor.submit(ctx.run, get_contextvar) |
| 64 | + result = future.result() |
| 65 | + |
| 66 | + assert result == "thread_test" # noqa: S101 |
| 67 | + |
| 68 | + |
| 69 | +def test_guardrails_context_propagates_with_copy_context() -> None: |
| 70 | + context = GuardrailsContext(guardrail_llm=_StubClient()) |
| 71 | + set_context(context) |
| 72 | + |
| 73 | + def get_guardrails_context(): |
| 74 | + return get_context() |
| 75 | + |
| 76 | + ctx = copy_context() |
| 77 | + result = ctx.run(get_guardrails_context) |
| 78 | + assert result is context # noqa: S101 |
| 79 | + |
| 80 | + clear_context() |
| 81 | + |
| 82 | + |
| 83 | +def test_guardrails_context_propagates_with_threadpool() -> None: |
| 84 | + context = GuardrailsContext(guardrail_llm=_StubClient()) |
| 85 | + set_context(context) |
| 86 | + |
| 87 | + def get_guardrails_context(): |
| 88 | + return get_context() |
| 89 | + |
| 90 | + ctx = copy_context() |
| 91 | + with ThreadPoolExecutor(max_workers=1) as executor: |
| 92 | + future = executor.submit(ctx.run, get_guardrails_context) |
| 93 | + result = future.result() |
| 94 | + |
| 95 | + assert result is context # noqa: S101 |
| 96 | + |
| 97 | + clear_context() |
| 98 | + |
| 99 | + |
| 100 | +def test_multiple_contextvars_propagate_with_threadpool() -> None: |
| 101 | + var1: ContextVar[str | None] = ContextVar("var1", default=None) |
| 102 | + var2: ContextVar[int | None] = ContextVar("var2", default=None) |
| 103 | + var1.set("value1") |
| 104 | + var2.set(42) |
| 105 | + |
| 106 | + def get_multiple_contextvars(): |
| 107 | + return (var1.get(), var2.get()) |
| 108 | + |
| 109 | + ctx = copy_context() |
| 110 | + with ThreadPoolExecutor(max_workers=1) as executor: |
| 111 | + future = executor.submit(ctx.run, get_multiple_contextvars) |
| 112 | + result = future.result() |
| 113 | + |
| 114 | + assert result == ("value1", 42) # noqa: S101 |
0 commit comments