|
| 1 | +import asyncio |
| 2 | +import concurrent.futures as cf |
| 3 | +import sentry_sdk |
| 4 | + |
| 5 | +from openfeature import api |
| 6 | +from openfeature.provider.in_memory_provider import InMemoryFlag, InMemoryProvider |
| 7 | +from sentry_sdk.integrations.openfeature import OpenFeatureIntegration |
| 8 | + |
| 9 | + |
| 10 | +def test_openfeature_integration(sentry_init): |
| 11 | + sentry_init(integrations=[OpenFeatureIntegration()]) |
| 12 | + |
| 13 | + flags = { |
| 14 | + "hello": InMemoryFlag("on", {"on": True, "off": False}), |
| 15 | + "world": InMemoryFlag("off", {"on": True, "off": False}), |
| 16 | + } |
| 17 | + api.set_provider(InMemoryProvider(flags)) |
| 18 | + |
| 19 | + client = api.get_client() |
| 20 | + client.get_boolean_value("hello", default_value=False) |
| 21 | + client.get_boolean_value("world", default_value=False) |
| 22 | + client.get_boolean_value("other", default_value=True) |
| 23 | + |
| 24 | + assert sentry_sdk.get_current_scope().flags.get() == [ |
| 25 | + {"flag": "hello", "result": True}, |
| 26 | + {"flag": "world", "result": False}, |
| 27 | + {"flag": "other", "result": True}, |
| 28 | + ] |
| 29 | + |
| 30 | + |
| 31 | +def test_openfeature_integration_threaded(sentry_init): |
| 32 | + sentry_init(integrations=[OpenFeatureIntegration()]) |
| 33 | + |
| 34 | + flags = { |
| 35 | + "hello": InMemoryFlag("on", {"on": True, "off": False}), |
| 36 | + "world": InMemoryFlag("off", {"on": True, "off": False}), |
| 37 | + } |
| 38 | + api.set_provider(InMemoryProvider(flags)) |
| 39 | + |
| 40 | + client = api.get_client() |
| 41 | + client.get_boolean_value("hello", default_value=False) |
| 42 | + |
| 43 | + def task(flag): |
| 44 | + # Create a new isolation scope for the thread. This means the flags |
| 45 | + with sentry_sdk.isolation_scope(): |
| 46 | + client.get_boolean_value(flag, default_value=False) |
| 47 | + return [f["flag"] for f in sentry_sdk.get_current_scope().flags.get()] |
| 48 | + |
| 49 | + with cf.ThreadPoolExecutor(max_workers=2) as pool: |
| 50 | + results = list(pool.map(task, ["world", "other"])) |
| 51 | + |
| 52 | + assert results[0] == ["hello", "world"] |
| 53 | + assert results[1] == ["hello", "other"] |
| 54 | + |
| 55 | + |
| 56 | +def test_openfeature_integration_asyncio(sentry_init): |
| 57 | + """Assert concurrently evaluated flags do not pollute one another.""" |
| 58 | + |
| 59 | + async def task(flag): |
| 60 | + with sentry_sdk.isolation_scope(): |
| 61 | + client.get_boolean_value(flag, default_value=False) |
| 62 | + return [f["flag"] for f in sentry_sdk.get_current_scope().flags.get()] |
| 63 | + |
| 64 | + async def runner(): |
| 65 | + return asyncio.gather(task("world"), task("other")) |
| 66 | + |
| 67 | + sentry_init(integrations=[OpenFeatureIntegration()]) |
| 68 | + |
| 69 | + flags = { |
| 70 | + "hello": InMemoryFlag("on", {"on": True, "off": False}), |
| 71 | + "world": InMemoryFlag("off", {"on": True, "off": False}), |
| 72 | + } |
| 73 | + api.set_provider(InMemoryProvider(flags)) |
| 74 | + |
| 75 | + client = api.get_client() |
| 76 | + client.get_boolean_value("hello", default_value=False) |
| 77 | + |
| 78 | + results = asyncio.run(runner()).result() |
| 79 | + assert results[0] == ["hello", "world"] |
| 80 | + assert results[1] == ["hello", "other"] |
0 commit comments