Skip to content

Commit 349217a

Browse files
committed
LD client isolation test
1 parent 58e412e commit 349217a

File tree

1 file changed

+44
-18
lines changed

1 file changed

+44
-18
lines changed

tests/integrations/launchdarkly/test_launchdarkly.py

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,31 @@
1414
from sentry_sdk.integrations.launchdarkly import LaunchDarklyIntegration
1515

1616

17+
@pytest.fixture
18+
def reset_launchdarkly(uninstall_integration):
19+
yield
20+
21+
# Teardown. We're using ldclient internals here, so this might break if their implementation
22+
# changes.
23+
uninstall_integration(LaunchDarklyIntegration.identifier)
24+
ldclient._reset_client()
25+
try:
26+
ldclient.__lock.lock()
27+
ldclient.__config = None
28+
finally:
29+
ldclient.__lock.unlock()
30+
31+
1732
@pytest.mark.parametrize(
1833
"use_global_client",
1934
(False, True),
2035
)
2136
def test_launchdarkly_integration(
22-
sentry_init, use_global_client, capture_events, uninstall_integration
37+
sentry_init, use_global_client, capture_events, reset_launchdarkly
2338
):
2439
td = TestData.data_source()
2540
config = Config("sdk-key", update_processor_class=td)
2641

27-
uninstall_integration(LaunchDarklyIntegration.identifier)
2842
if use_global_client:
2943
ldclient.set_config(config)
3044
sentry_init(integrations=[LaunchDarklyIntegration()])
@@ -56,13 +70,12 @@ def test_launchdarkly_integration(
5670

5771

5872
def test_launchdarkly_integration_threaded(
59-
sentry_init, capture_events, uninstall_integration
73+
sentry_init, capture_events, reset_launchdarkly
6074
):
6175
td = TestData.data_source()
6276
client = LDClient(config=Config("sdk-key", update_processor_class=td))
6377
context = Context.create("user1")
6478

65-
uninstall_integration(LaunchDarklyIntegration.identifier)
6679
sentry_init(integrations=[LaunchDarklyIntegration(ld_client=client)])
6780
events = capture_events()
6881

@@ -111,7 +124,7 @@ def task(flag_key):
111124

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

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

124-
uninstall_integration(LaunchDarklyIntegration.identifier)
125137
sentry_init(integrations=[LaunchDarklyIntegration(ld_client=client)])
126138
events = capture_events()
127139

@@ -168,21 +180,35 @@ async def runner():
168180
}
169181

170182

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

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

186-
uninstall_integration(LaunchDarklyIntegration.identifier)
202+
events = capture_events()
203+
sentry_sdk.capture_exception(Exception("something wrong!"))
204+
205+
assert len(events) == 1
206+
assert events[0]["contexts"]["flags"] == {"values": []}
207+
208+
209+
def test_launchdarkly_integration_did_not_enable(sentry_init, reset_launchdarkly):
210+
"""
211+
Setup should fail when using global client and ldclient.set_config wasn't called.
212+
"""
187213
with pytest.raises(DidNotEnable):
188214
sentry_init(integrations=[LaunchDarklyIntegration()])

0 commit comments

Comments
 (0)