|
| 1 | +import asyncio |
| 2 | +import concurrent.futures as cf |
| 3 | + |
| 4 | +import ldclient |
| 5 | + |
1 | 6 | import sentry_sdk |
| 7 | +import pytest |
2 | 8 |
|
3 | 9 | from ldclient import LDClient |
4 | 10 | from ldclient.config import Config |
|
8 | 14 | from sentry_sdk.integrations.launchdarkly import LaunchDarklyIntegration |
9 | 15 |
|
10 | 16 | # Ref: https://docs.launchdarkly.com/sdk/features/test-data-sources#python |
| 17 | +# https://launchdarkly-python-sdk.readthedocs.io/en/latest/api-testing.html#ldclient.integrations.test_data.TestData |
11 | 18 |
|
12 | 19 |
|
13 | | -def test_launchdarkly_integration(sentry_init): |
| 20 | +@pytest.mark.parametrize( |
| 21 | + "use_global_client", |
| 22 | + (False, True), |
| 23 | +) |
| 24 | +def test_launchdarkly_integration(sentry_init, use_global_client): |
14 | 25 | td = TestData.data_source() |
15 | | - client = LDClient(config=Config("sdk-key", update_processor_class=td)) |
16 | | - sentry_init(integrations=[LaunchDarklyIntegration(client=client)]) |
| 26 | + config = Config("sdk-key", update_processor_class=td) |
| 27 | + if use_global_client: |
| 28 | + ldclient.set_config(config) |
| 29 | + sentry_init(integrations=[LaunchDarklyIntegration()]) |
| 30 | + client = ldclient.get() |
| 31 | + else: |
| 32 | + client = LDClient(config=config) |
| 33 | + sentry_init(integrations=[LaunchDarklyIntegration(client=client)]) |
17 | 34 |
|
18 | 35 | # Set test values |
19 | 36 | td.update(td.flag("hello").variation_for_all(True)) |
20 | 37 | td.update(td.flag("world").variation_for_all(True)) |
21 | | - td.update(td.flag("goodbye").variation_for_all(False)) |
22 | 38 |
|
23 | 39 | # Evaluate |
24 | 40 | client.variation("hello", Context.create("my-org", "organization"), False) |
25 | 41 | client.variation("world", Context.create("user1", "user"), False) |
26 | | - client.variation("goodbye", Context.create("user2", "user"), False) |
| 42 | + client.variation("other", Context.create("user2", "user"), False) |
27 | 43 |
|
28 | 44 | assert sentry_sdk.get_current_scope().flags.get() == [ |
29 | 45 | {"flag": "hello", "result": True}, |
30 | 46 | {"flag": "world", "result": True}, |
31 | | - {"flag": "goodbye", "result": False}, |
| 47 | + {"flag": "other", "result": False}, |
32 | 48 | ] |
| 49 | + |
| 50 | + |
| 51 | +def test_launchdarkly_integration_threaded(sentry_init): |
| 52 | + td = TestData.data_source() |
| 53 | + client = LDClient(config=Config("sdk-key", update_processor_class=td)) |
| 54 | + sentry_init(integrations=[LaunchDarklyIntegration(client=client)]) |
| 55 | + context = Context.create("user1") |
| 56 | + |
| 57 | + def task(flag_key): |
| 58 | + # Create a new isolation scope for the thread. This means the evaluations in each task are captured separately. |
| 59 | + with sentry_sdk.isolation_scope(): |
| 60 | + client.variation(flag_key, context, False) |
| 61 | + return [f["flag"] for f in sentry_sdk.get_current_scope().flags.get()] |
| 62 | + |
| 63 | + td.update(td.flag("hello").variation_for_all(True)) |
| 64 | + td.update(td.flag("world").variation_for_all(False)) |
| 65 | + client.variation( |
| 66 | + "hello", context, False |
| 67 | + ) # Captured before splitting isolation scopes. |
| 68 | + |
| 69 | + with cf.ThreadPoolExecutor(max_workers=2) as pool: |
| 70 | + results = list(pool.map(task, ["world", "other"])) |
| 71 | + |
| 72 | + assert results[0] == ["hello", "world"] |
| 73 | + assert results[1] == ["hello", "other"] |
| 74 | + |
| 75 | + |
| 76 | +def test_launchdarkly_integration_asyncio(sentry_init): |
| 77 | + """Assert concurrently evaluated flags do not pollute one another.""" |
| 78 | + td = TestData.data_source() |
| 79 | + client = LDClient(config=Config("sdk-key", update_processor_class=td)) |
| 80 | + sentry_init(integrations=[LaunchDarklyIntegration(client=client)]) |
| 81 | + context = Context.create("user1") |
| 82 | + |
| 83 | + async def task(flag_key): |
| 84 | + with sentry_sdk.isolation_scope(): |
| 85 | + client.variation(flag_key, context, False) |
| 86 | + return [f["flag"] for f in sentry_sdk.get_current_scope().flags.get()] |
| 87 | + |
| 88 | + async def runner(): |
| 89 | + return asyncio.gather(task("world"), task("other")) |
| 90 | + |
| 91 | + td.update(td.flag("hello").variation_for_all(True)) |
| 92 | + td.update(td.flag("world").variation_for_all(False)) |
| 93 | + client.variation("hello", context, False) |
| 94 | + |
| 95 | + results = asyncio.run(runner()).result() |
| 96 | + assert results[0] == ["hello", "world"] |
| 97 | + assert results[1] == ["hello", "other"] |
0 commit comments