1919 "use_global_client" ,
2020 (False , True ),
2121)
22- def test_launchdarkly_integration (sentry_init , use_global_client ):
22+ def test_launchdarkly_integration (
23+ sentry_init , use_global_client , capture_events , uninstall_integration
24+ ):
2325 td = TestData .data_source ()
2426 config = Config ("sdk-key" , update_processor_class = td )
27+
28+ uninstall_integration (LaunchDarklyIntegration .identifier )
2529 if use_global_client :
2630 ldclient .set_config (config )
2731 sentry_init (integrations = [LaunchDarklyIntegration ()])
@@ -39,72 +43,126 @@ def test_launchdarkly_integration(sentry_init, use_global_client):
3943 client .variation ("world" , Context .create ("user1" , "user" ), False )
4044 client .variation ("other" , Context .create ("user2" , "user" ), False )
4145
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- ]
46+ events = capture_events ()
47+ sentry_sdk .capture_exception (Exception ("something wrong!" ))
48+
49+ assert len (events ) == 1
50+ assert events [0 ]["contexts" ]["flags" ] == {
51+ "values" : [
52+ {"flag" : "hello" , "result" : True },
53+ {"flag" : "world" , "result" : True },
54+ {"flag" : "other" , "result" : False },
55+ ]
56+ }
4757
4858
49- def test_launchdarkly_integration_threaded (sentry_init ):
59+ def test_launchdarkly_integration_threaded (
60+ sentry_init , capture_events , uninstall_integration
61+ ):
5062 td = TestData .data_source ()
5163 client = LDClient (config = Config ("sdk-key" , update_processor_class = td ))
52- sentry_init (integrations = [LaunchDarklyIntegration (ld_client = client )])
5364 context = Context .create ("user1" )
5465
66+ uninstall_integration (LaunchDarklyIntegration .identifier )
67+ sentry_init (integrations = [LaunchDarklyIntegration (ld_client = client )])
68+ events = capture_events ()
69+
5570 def task (flag_key ):
5671 # Creates a new isolation scope for the thread.
5772 # This means the evaluations in each task are captured separately.
5873 with sentry_sdk .isolation_scope ():
5974 client .variation (flag_key , context , False )
60- return sentry_sdk .get_current_scope ().flags .get ()
75+ # use a tag to identify to identify events later on
76+ sentry_sdk .set_tag ("task_id" , flag_key )
77+ sentry_sdk .capture_exception (Exception ("something wrong!" ))
6178
6279 td .update (td .flag ("hello" ).variation_for_all (True ))
6380 td .update (td .flag ("world" ).variation_for_all (False ))
6481 # Capture an eval before we split isolation scopes.
6582 client .variation ("hello" , context , False )
6683
6784 with cf .ThreadPoolExecutor (max_workers = 2 ) as pool :
68- results = list (pool .map (task , ["world" , "other" ]))
69-
70- assert results [0 ] == [
71- {"flag" : "hello" , "result" : True },
72- {"flag" : "world" , "result" : False },
73- ]
74- assert results [1 ] == [
75- {"flag" : "hello" , "result" : True },
76- {"flag" : "other" , "result" : False },
77- ]
78-
79-
80- def test_launchdarkly_integration_asyncio (sentry_init ):
85+ pool .map (task , ["world" , "other" ])
86+
87+ # Capture error in original scope
88+ sentry_sdk .set_tag ("task_id" , "0" )
89+ sentry_sdk .capture_exception (Exception ("something wrong!" ))
90+
91+ assert len (events ) == 3
92+ events .sort (key = lambda e : e ["tags" ]["task_id" ])
93+
94+ assert events [0 ]["contexts" ]["flags" ] == {
95+ "values" : [
96+ {"flag" : "hello" , "result" : True },
97+ ]
98+ }
99+ assert events [1 ]["contexts" ]["flags" ] == {
100+ "values" : [
101+ {"flag" : "hello" , "result" : True },
102+ {"flag" : "other" , "result" : False },
103+ ]
104+ }
105+ assert events [2 ]["contexts" ]["flags" ] == {
106+ "values" : [
107+ {"flag" : "hello" , "result" : True },
108+ {"flag" : "world" , "result" : False },
109+ ]
110+ }
111+
112+
113+ def test_launchdarkly_integration_asyncio (
114+ sentry_init , capture_events , uninstall_integration
115+ ):
81116 """Assert concurrently evaluated flags do not pollute one another."""
82117 td = TestData .data_source ()
83118 client = LDClient (config = Config ("sdk-key" , update_processor_class = td ))
84- sentry_init (integrations = [LaunchDarklyIntegration (ld_client = client )])
85119 context = Context .create ("user1" )
86120
121+ uninstall_integration (LaunchDarklyIntegration .identifier )
122+ sentry_init (integrations = [LaunchDarklyIntegration (ld_client = client )])
123+ events = capture_events ()
124+
87125 async def task (flag_key ):
88126 with sentry_sdk .isolation_scope ():
89127 client .variation (flag_key , context , False )
90- return sentry_sdk .get_current_scope ().flags .get ()
128+ # use a tag to identify to identify events later on
129+ sentry_sdk .set_tag ("task_id" , flag_key )
130+ sentry_sdk .capture_exception (Exception ("something wrong!" ))
91131
92132 async def runner ():
93133 return asyncio .gather (task ("world" ), task ("other" ))
94134
95135 td .update (td .flag ("hello" ).variation_for_all (True ))
96136 td .update (td .flag ("world" ).variation_for_all (False ))
137+ # Capture an eval before we split isolation scopes.
97138 client .variation ("hello" , context , False )
98139
99- results = asyncio .run (runner ()).result ()
100- assert results [0 ] == [
101- {"flag" : "hello" , "result" : True },
102- {"flag" : "world" , "result" : False },
103- ]
104- assert results [1 ] == [
105- {"flag" : "hello" , "result" : True },
106- {"flag" : "other" , "result" : False },
107- ]
140+ asyncio .run (runner ())
141+
142+ # Capture error in original scope
143+ sentry_sdk .set_tag ("task_id" , "0" )
144+ sentry_sdk .capture_exception (Exception ("something wrong!" ))
145+
146+ assert len (events ) == 3
147+ events .sort (key = lambda e : e ["tags" ]["task_id" ])
148+
149+ assert events [0 ]["contexts" ]["flags" ] == {
150+ "values" : [
151+ {"flag" : "hello" , "result" : True },
152+ ]
153+ }
154+ assert events [1 ]["contexts" ]["flags" ] == {
155+ "values" : [
156+ {"flag" : "hello" , "result" : True },
157+ {"flag" : "other" , "result" : False },
158+ ]
159+ }
160+ assert events [2 ]["contexts" ]["flags" ] == {
161+ "values" : [
162+ {"flag" : "hello" , "result" : True },
163+ {"flag" : "world" , "result" : False },
164+ ]
165+ }
108166
109167
110168def test_launchdarkly_integration_did_not_enable (monkeypatch ):
0 commit comments