-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Federico Bond <[email protected]>
- Loading branch information
1 parent
34ac91c
commit 11d580d
Showing
4 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import typing | ||
from contextvars import ContextVar | ||
|
||
from openfeature.evaluation_context import EvaluationContext | ||
|
||
__all__ = [ | ||
"TransactionContextPropagator", | ||
"NoopTransactionContextPropagator", | ||
"ContextVarTransactionContextPropagator", | ||
] | ||
|
||
|
||
class TransactionContextPropagator(typing.Protocol): | ||
def get_transaction_context(self) -> EvaluationContext: ... | ||
|
||
def set_transaction_context(self, context: EvaluationContext) -> None: ... | ||
|
||
|
||
class NoopTransactionContextPropagator(TransactionContextPropagator): | ||
def get_transaction_context(self) -> EvaluationContext: | ||
return EvaluationContext() | ||
|
||
def set_transaction_context(self, context: EvaluationContext) -> None: | ||
pass | ||
|
||
|
||
class ContextVarTransactionContextPropagator(TransactionContextPropagator): | ||
def __init__(self) -> None: | ||
self._contextvar = ContextVar( | ||
"transaction_context", default=EvaluationContext() | ||
) | ||
|
||
def get_transaction_context(self) -> EvaluationContext: | ||
return self._contextvar.get() | ||
|
||
def set_transaction_context(self, context: EvaluationContext) -> None: | ||
self._contextvar.set(context) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from concurrent.futures import ThreadPoolExecutor | ||
|
||
from openfeature.evaluation_context import EvaluationContext | ||
from openfeature.transaction_context import ContextVarTransactionContextPropagator | ||
|
||
|
||
def test_contextvar_transaction_context_propagator(): | ||
propagator = ContextVarTransactionContextPropagator() | ||
|
||
context = propagator.get_transaction_context() | ||
assert isinstance(context, EvaluationContext) | ||
|
||
context = EvaluationContext(targeting_key="foo", attributes={"key": "value"}) | ||
propagator.set_transaction_context(context) | ||
transaction_context = propagator.get_transaction_context() | ||
|
||
assert transaction_context.targeting_key == "foo" | ||
assert transaction_context.attributes == {"key": "value"} | ||
|
||
def thread_fn(): | ||
thread_context = propagator.get_transaction_context() | ||
assert thread_context.targeting_key is None | ||
assert thread_context.attributes == {} | ||
|
||
with ThreadPoolExecutor() as executor: | ||
future = executor.submit(thread_fn) | ||
|
||
future.result() |