Skip to content

Commit

Permalink
LD client isolation test
Browse files Browse the repository at this point in the history
  • Loading branch information
aliu39 committed Dec 27, 2024
1 parent 58e412e commit 349217a
Showing 1 changed file with 44 additions and 18 deletions.
62 changes: 44 additions & 18 deletions tests/integrations/launchdarkly/test_launchdarkly.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,31 @@
from sentry_sdk.integrations.launchdarkly import LaunchDarklyIntegration


@pytest.fixture
def reset_launchdarkly(uninstall_integration):
yield

# Teardown. We're using ldclient internals here, so this might break if their implementation
# changes.
uninstall_integration(LaunchDarklyIntegration.identifier)
ldclient._reset_client()
try:
ldclient.__lock.lock()
ldclient.__config = None
finally:
ldclient.__lock.unlock()


@pytest.mark.parametrize(
"use_global_client",
(False, True),
)
def test_launchdarkly_integration(
sentry_init, use_global_client, capture_events, uninstall_integration
sentry_init, use_global_client, capture_events, reset_launchdarkly
):
td = TestData.data_source()
config = Config("sdk-key", update_processor_class=td)

uninstall_integration(LaunchDarklyIntegration.identifier)
if use_global_client:
ldclient.set_config(config)
sentry_init(integrations=[LaunchDarklyIntegration()])
Expand Down Expand Up @@ -56,13 +70,12 @@ def test_launchdarkly_integration(


def test_launchdarkly_integration_threaded(
sentry_init, capture_events, uninstall_integration
sentry_init, capture_events, reset_launchdarkly
):
td = TestData.data_source()
client = LDClient(config=Config("sdk-key", update_processor_class=td))
context = Context.create("user1")

uninstall_integration(LaunchDarklyIntegration.identifier)
sentry_init(integrations=[LaunchDarklyIntegration(ld_client=client)])
events = capture_events()

Expand Down Expand Up @@ -111,7 +124,7 @@ def task(flag_key):

@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher")
def test_launchdarkly_integration_asyncio(
sentry_init, capture_events, uninstall_integration
sentry_init, capture_events, reset_launchdarkly
):
"""Assert concurrently evaluated flags do not pollute one another."""

Expand All @@ -121,7 +134,6 @@ def test_launchdarkly_integration_asyncio(
client = LDClient(config=Config("sdk-key", update_processor_class=td))
context = Context.create("user1")

uninstall_integration(LaunchDarklyIntegration.identifier)
sentry_init(integrations=[LaunchDarklyIntegration(ld_client=client)])
events = capture_events()

Expand Down Expand Up @@ -168,21 +180,35 @@ async def runner():
}


def test_launchdarkly_integration_did_not_enable(sentry_init, uninstall_integration):
def test_launchdarkly_integration_client_isolation(
sentry_init, capture_events, reset_launchdarkly
):
"""
Setup should fail when using global client and ldclient.set_config wasn't called.
We're accessing ldclient internals to set up this test, so it might break if launchdarkly's
implementation changes.
If the integration is tracking a single client, evaluations from other clients should not be
captured.
"""
td = TestData.data_source()
td.update(td.flag("hello").variation_for_all(True))
td.update(td.flag("world").variation_for_all(True))
client = LDClient(config=Config("sdk-key", update_processor_class=td))
sentry_init(integrations=[LaunchDarklyIntegration(ld_client=client)])

ldclient._reset_client()
try:
ldclient.__lock.lock()
ldclient.__config = None
finally:
ldclient.__lock.unlock()
# For isolation you must use a new Config object, but data source can be the same.
other_client = LDClient(Config("sdk-key", update_processor_class=td))
other_client.variation("hello", Context.create("my-org", "organization"), False)
other_client.variation("world", Context.create("user1", "user"), False)
other_client.variation("other", Context.create("user2", "user"), False)

uninstall_integration(LaunchDarklyIntegration.identifier)
events = capture_events()
sentry_sdk.capture_exception(Exception("something wrong!"))

assert len(events) == 1
assert events[0]["contexts"]["flags"] == {"values": []}


def test_launchdarkly_integration_did_not_enable(sentry_init, reset_launchdarkly):
"""
Setup should fail when using global client and ldclient.set_config wasn't called.
"""
with pytest.raises(DidNotEnable):
sentry_init(integrations=[LaunchDarklyIntegration()])

0 comments on commit 349217a

Please sign in to comment.