|
| 1 | +import asyncio |
| 2 | +import concurrent.futures as cf |
| 3 | + |
| 4 | +import ldclient |
| 5 | + |
| 6 | +import sentry_sdk |
| 7 | +import pytest |
| 8 | + |
| 9 | +from ldclient import LDClient |
| 10 | +from ldclient.config import Config |
| 11 | +from ldclient.context import Context |
| 12 | +from ldclient.integrations.test_data import TestData |
| 13 | + |
| 14 | +from sentry_sdk.integrations import DidNotEnable |
| 15 | +from sentry_sdk.integrations.launchdarkly import LaunchDarklyIntegration |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.parametrize( |
| 19 | + "use_global_client", |
| 20 | + (False, True), |
| 21 | +) |
| 22 | +def test_launchdarkly_integration(sentry_init, use_global_client): |
| 23 | + td = TestData.data_source() |
| 24 | + config = Config("sdk-key", update_processor_class=td) |
| 25 | + if use_global_client: |
| 26 | + ldclient.set_config(config) |
| 27 | + sentry_init(integrations=[LaunchDarklyIntegration()]) |
| 28 | + client = ldclient.get() |
| 29 | + else: |
| 30 | + client = LDClient(config=config) |
| 31 | + sentry_init(integrations=[LaunchDarklyIntegration(ld_client=client)]) |
| 32 | + |
| 33 | + # Set test values |
| 34 | + td.update(td.flag("hello").variation_for_all(True)) |
| 35 | + td.update(td.flag("world").variation_for_all(True)) |
| 36 | + |
| 37 | + # Evaluate |
| 38 | + client.variation("hello", Context.create("my-org", "organization"), False) |
| 39 | + client.variation("world", Context.create("user1", "user"), False) |
| 40 | + client.variation("other", Context.create("user2", "user"), False) |
| 41 | + |
| 42 | + assert sentry_sdk.get_current_scope().flags.get() == [ |
| 43 | + {"flag": "hello", "result": True}, |
| 44 | + {"flag": "world", "result": True}, |
| 45 | + {"flag": "other", "result": False}, |
| 46 | + ] |
| 47 | + |
| 48 | + |
| 49 | +def test_launchdarkly_integration_threaded(sentry_init): |
| 50 | + td = TestData.data_source() |
| 51 | + client = LDClient(config=Config("sdk-key", update_processor_class=td)) |
| 52 | + sentry_init(integrations=[LaunchDarklyIntegration(ld_client=client)]) |
| 53 | + context = Context.create("user1") |
| 54 | + |
| 55 | + def task(flag_key): |
| 56 | + # Creates a new isolation scope for the thread. |
| 57 | + # This means the evaluations in each task are captured separately. |
| 58 | + with sentry_sdk.isolation_scope(): |
| 59 | + client.variation(flag_key, context, False) |
| 60 | + return [f["flag"] for f in sentry_sdk.get_current_scope().flags.get()] |
| 61 | + |
| 62 | + td.update(td.flag("hello").variation_for_all(True)) |
| 63 | + td.update(td.flag("world").variation_for_all(False)) |
| 64 | + # Capture an eval before we split isolation scopes. |
| 65 | + client.variation("hello", context, False) |
| 66 | + |
| 67 | + with cf.ThreadPoolExecutor(max_workers=2) as pool: |
| 68 | + results = list(pool.map(task, ["world", "other"])) |
| 69 | + |
| 70 | + assert results[0] == ["hello", "world"] |
| 71 | + assert results[1] == ["hello", "other"] |
| 72 | + |
| 73 | + |
| 74 | +def test_launchdarkly_integration_asyncio(sentry_init): |
| 75 | + """Assert concurrently evaluated flags do not pollute one another.""" |
| 76 | + td = TestData.data_source() |
| 77 | + client = LDClient(config=Config("sdk-key", update_processor_class=td)) |
| 78 | + sentry_init(integrations=[LaunchDarklyIntegration(ld_client=client)]) |
| 79 | + context = Context.create("user1") |
| 80 | + |
| 81 | + async def task(flag_key): |
| 82 | + with sentry_sdk.isolation_scope(): |
| 83 | + client.variation(flag_key, context, False) |
| 84 | + return [f["flag"] for f in sentry_sdk.get_current_scope().flags.get()] |
| 85 | + |
| 86 | + async def runner(): |
| 87 | + return asyncio.gather(task("world"), task("other")) |
| 88 | + |
| 89 | + td.update(td.flag("hello").variation_for_all(True)) |
| 90 | + td.update(td.flag("world").variation_for_all(False)) |
| 91 | + client.variation("hello", context, False) |
| 92 | + |
| 93 | + results = asyncio.run(runner()).result() |
| 94 | + assert results[0] == ["hello", "world"] |
| 95 | + assert results[1] == ["hello", "other"] |
| 96 | + |
| 97 | + |
| 98 | +def test_launchdarkly_integration_did_not_enable(monkeypatch): |
| 99 | + # Client is not passed in and set_config wasn't called. |
| 100 | + # TODO: Bad practice to access internals like this. We can skip this test, or remove this |
| 101 | + # case entirely (force user to pass in a client instance). |
| 102 | + ldclient._reset_client() |
| 103 | + try: |
| 104 | + ldclient.__lock.lock() |
| 105 | + ldclient.__config = None |
| 106 | + finally: |
| 107 | + ldclient.__lock.unlock() |
| 108 | + |
| 109 | + with pytest.raises(DidNotEnable): |
| 110 | + LaunchDarklyIntegration() |
| 111 | + |
| 112 | + # Client not initialized. |
| 113 | + client = LDClient(config=Config("sdk-key")) |
| 114 | + monkeypatch.setattr(client, "is_initialized", lambda: False) |
| 115 | + with pytest.raises(DidNotEnable): |
| 116 | + LaunchDarklyIntegration(ld_client=client) |
0 commit comments