44import ldclient
55
66import sentry_sdk
7+ import pytest
78
9+ from ldclient import LDClient
810from ldclient .config import Config
911from ldclient .context import Context
1012from ldclient .integrations .test_data import TestData
1113
14+ from sentry_sdk .integrations import DidNotEnable
1215from sentry_sdk .integrations .launchdarkly import LaunchDarklyIntegration
1316
1417
15- def test_launchdarkly_integration (sentry_init ):
18+ @pytest .mark .parametrize (
19+ "use_global_client" ,
20+ (False , True ),
21+ )
22+ def test_launchdarkly_integration (sentry_init , use_global_client ):
1623 td = TestData .data_source ()
1724 config = Config ("sdk-key" , update_processor_class = td )
18- ldclient .set_config (config )
19- client = ldclient .get ()
20- sentry_init (integrations = [LaunchDarklyIntegration ()])
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 )])
2132
2233 # Set test values
2334 td .update (td .flag ("hello" ).variation_for_all (True ))
@@ -39,8 +50,8 @@ def test_launchdarkly_integration(sentry_init):
3950
4051def test_launchdarkly_integration_threaded (sentry_init ):
4152 td = TestData .data_source ()
42- client = ldclient . get ( )
43- sentry_init (integrations = [LaunchDarklyIntegration ()])
53+ client = LDClient ( config = Config ( "sdk-key" , update_processor_class = td ) )
54+ sentry_init (integrations = [LaunchDarklyIntegration (ld_client = client )])
4455 context = Context .create ("user1" )
4556
4657 def task (flag_key ):
@@ -67,8 +78,8 @@ def task(flag_key):
6778def test_launchdarkly_integration_asyncio (sentry_init ):
6879 """Assert concurrently evaluated flags do not pollute one another."""
6980 td = TestData .data_source ()
70- client = ldclient . get ( )
71- sentry_init (integrations = [LaunchDarklyIntegration ()])
81+ client = LDClient ( config = Config ( "sdk-key" , update_processor_class = td ) )
82+ sentry_init (integrations = [LaunchDarklyIntegration (ld_client = client )])
7283 context = Context .create ("user1" )
7384
7485 async def task (flag_key ):
@@ -87,4 +98,23 @@ async def runner():
8798 assert results [0 ] == ["hello" , "world" ]
8899 assert results [1 ] == ["hello" , "other" ]
89100
101+
102+ def test_launchdarkly_integration_did_not_enable (monkeypatch ):
103+ # Client is not passed in and set_config wasn't called.
104+ # TODO: Bad practice to access internals like this. We can skip this test, or remove this
105+ # case entirely (force user to pass in a client instance).
90106 ldclient ._reset_client ()
107+ try :
108+ ldclient .__lock .lock ()
109+ ldclient .__config = None
110+ finally :
111+ ldclient .__lock .unlock ()
112+
113+ with pytest .raises (DidNotEnable ):
114+ LaunchDarklyIntegration ()
115+
116+ # Client not initialized.
117+ client = LDClient (config = Config ("sdk-key" ))
118+ monkeypatch .setattr (client , "is_initialized" , lambda : False )
119+ with pytest .raises (DidNotEnable ):
120+ LaunchDarklyIntegration (ld_client = client )
0 commit comments