@@ -15,7 +15,7 @@ def test_is_enabled(sentry_init, capture_events, uninstall_integration):
15
15
uninstall_integration (UnleashIntegration .identifier )
16
16
17
17
with mock_unleash_client ():
18
- client = UnleashClient ()
18
+ client = UnleashClient () # type: ignore[arg-type]
19
19
sentry_init (integrations = [UnleashIntegration ()])
20
20
client .is_enabled ("hello" )
21
21
client .is_enabled ("world" )
@@ -34,41 +34,12 @@ def test_is_enabled(sentry_init, capture_events, uninstall_integration):
34
34
}
35
35
36
36
37
- def test_get_variant (sentry_init , capture_events , uninstall_integration ):
38
- uninstall_integration (UnleashIntegration .identifier )
39
-
40
- with mock_unleash_client ():
41
- client = UnleashClient ()
42
- sentry_init (integrations = [UnleashIntegration ()]) # type: ignore
43
- client .get_variant ("no_payload_feature" )
44
- client .get_variant ("string_feature" )
45
- client .get_variant ("json_feature" )
46
- client .get_variant ("csv_feature" )
47
- client .get_variant ("number_feature" )
48
- client .get_variant ("unknown_feature" )
49
-
50
- events = capture_events ()
51
- sentry_sdk .capture_exception (Exception ("something wrong!" ))
52
-
53
- assert len (events ) == 1
54
- assert events [0 ]["contexts" ]["flags" ] == {
55
- "values" : [
56
- {"flag" : "no_payload_feature" , "result" : True },
57
- {"flag" : "string_feature" , "result" : True },
58
- {"flag" : "json_feature" , "result" : True },
59
- {"flag" : "csv_feature" , "result" : True },
60
- {"flag" : "number_feature" , "result" : True },
61
- {"flag" : "unknown_feature" , "result" : False },
62
- ]
63
- }
64
-
65
-
66
37
def test_is_enabled_threaded (sentry_init , capture_events , uninstall_integration ):
67
38
uninstall_integration (UnleashIntegration .identifier )
68
39
69
40
with mock_unleash_client ():
70
- client = UnleashClient ()
71
- sentry_init (integrations = [UnleashIntegration ()]) # type: ignore
41
+ client = UnleashClient () # type: ignore[arg-type]
42
+ sentry_init (integrations = [UnleashIntegration ()])
72
43
events = capture_events ()
73
44
74
45
def task (flag_key ):
@@ -112,63 +83,14 @@ def task(flag_key):
112
83
}
113
84
114
85
115
- def test_get_variant_threaded (sentry_init , capture_events , uninstall_integration ):
116
- uninstall_integration (UnleashIntegration .identifier )
117
-
118
- with mock_unleash_client ():
119
- client = UnleashClient ()
120
- sentry_init (integrations = [UnleashIntegration ()]) # type: ignore
121
- events = capture_events ()
122
-
123
- def task (flag_key ):
124
- # Creates a new isolation scope for the thread.
125
- # This means the evaluations in each task are captured separately.
126
- with sentry_sdk .isolation_scope ():
127
- client .get_variant (flag_key )
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!" ))
131
-
132
- # Capture an eval before we split isolation scopes.
133
- client .get_variant ("hello" )
134
-
135
- with cf .ThreadPoolExecutor (max_workers = 2 ) as pool :
136
- pool .map (task , ["no_payload_feature" , "other" ])
137
-
138
- # Capture error in original scope
139
- sentry_sdk .set_tag ("task_id" , "0" )
140
- sentry_sdk .capture_exception (Exception ("something wrong!" ))
141
-
142
- assert len (events ) == 3
143
- events .sort (key = lambda e : e ["tags" ]["task_id" ])
144
-
145
- assert events [0 ]["contexts" ]["flags" ] == {
146
- "values" : [
147
- {"flag" : "hello" , "result" : False },
148
- ]
149
- }
150
- assert events [1 ]["contexts" ]["flags" ] == {
151
- "values" : [
152
- {"flag" : "hello" , "result" : False },
153
- {"flag" : "no_payload_feature" , "result" : True },
154
- ]
155
- }
156
- assert events [2 ]["contexts" ]["flags" ] == {
157
- "values" : [
158
- {"flag" : "hello" , "result" : False },
159
- {"flag" : "other" , "result" : False },
160
- ]
161
- }
162
-
163
-
164
86
@pytest .mark .skipif (sys .version_info < (3 , 7 ), reason = "requires python3.7 or higher" )
165
87
def test_is_enabled_asyncio (sentry_init , capture_events , uninstall_integration ):
166
88
asyncio = pytest .importorskip ("asyncio" )
167
89
uninstall_integration (UnleashIntegration .identifier )
168
90
169
91
with mock_unleash_client ():
170
- client = UnleashClient ()
171
- sentry_init (integrations = [UnleashIntegration ()]) # type: ignore
92
+ client = UnleashClient () # type: ignore[arg-type]
93
+ sentry_init (integrations = [UnleashIntegration ()])
172
94
events = capture_events ()
173
95
174
96
async def task (flag_key ):
@@ -212,66 +134,12 @@ async def runner():
212
134
}
213
135
214
136
215
- @pytest .mark .skipif (sys .version_info < (3 , 7 ), reason = "requires python3.7 or higher" )
216
- def test_get_variant_asyncio (sentry_init , capture_events , uninstall_integration ):
217
- asyncio = pytest .importorskip ("asyncio" )
218
-
219
- uninstall_integration (UnleashIntegration .identifier )
220
-
221
- with mock_unleash_client ():
222
- client = UnleashClient ()
223
- sentry_init (integrations = [UnleashIntegration ()]) # type: ignore
224
- events = capture_events ()
225
-
226
- async def task (flag_key ):
227
- with sentry_sdk .isolation_scope ():
228
- client .get_variant (flag_key )
229
- # use a tag to identify to identify events later on
230
- sentry_sdk .set_tag ("task_id" , flag_key )
231
- sentry_sdk .capture_exception (Exception ("something wrong!" ))
232
-
233
- async def runner ():
234
- return asyncio .gather (task ("no_payload_feature" ), task ("other" ))
235
-
236
- # Capture an eval before we split isolation scopes.
237
- client .get_variant ("hello" )
238
-
239
- asyncio .run (runner ())
240
-
241
- # Capture error in original scope
242
- sentry_sdk .set_tag ("task_id" , "0" )
243
- sentry_sdk .capture_exception (Exception ("something wrong!" ))
244
-
245
- assert len (events ) == 3
246
- events .sort (key = lambda e : e ["tags" ]["task_id" ])
247
-
248
- assert events [0 ]["contexts" ]["flags" ] == {
249
- "values" : [
250
- {"flag" : "hello" , "result" : False },
251
- ]
252
- }
253
- assert events [1 ]["contexts" ]["flags" ] == {
254
- "values" : [
255
- {"flag" : "hello" , "result" : False },
256
- {"flag" : "no_payload_feature" , "result" : True },
257
- ]
258
- }
259
- assert events [2 ]["contexts" ]["flags" ] == {
260
- "values" : [
261
- {"flag" : "hello" , "result" : False },
262
- {"flag" : "other" , "result" : False },
263
- ]
264
- }
265
-
266
-
267
137
def test_wraps_original (sentry_init , uninstall_integration ):
268
138
with mock_unleash_client ():
269
- client = UnleashClient ()
139
+ client = UnleashClient () # type: ignore[arg-type]
270
140
271
141
mock_is_enabled = mock .Mock (return_value = random () < 0.5 )
272
- mock_get_variant = mock .Mock (return_value = {"enabled" : random () < 0.5 })
273
142
client .is_enabled = mock_is_enabled
274
- client .get_variant = mock_get_variant
275
143
276
144
uninstall_integration (UnleashIntegration .identifier )
277
145
sentry_init (integrations = [UnleashIntegration ()]) # type: ignore
@@ -283,26 +151,16 @@ def test_wraps_original(sentry_init, uninstall_integration):
283
151
{"kwarg" : 1 },
284
152
)
285
153
286
- res = client .get_variant ("test-flag" , "arg" , kwarg = 1 )
287
- assert res == mock_get_variant .return_value
288
- assert mock_get_variant .call_args == (
289
- ("test-flag" , "arg" ),
290
- {"kwarg" : 1 },
291
- )
292
-
293
154
294
155
def test_wrapper_attributes (sentry_init , uninstall_integration ):
295
156
with mock_unleash_client ():
296
- client = UnleashClient () # <- Returns a MockUnleashClient
157
+ client = UnleashClient () # type: ignore[arg-type]
297
158
298
159
original_is_enabled = client .is_enabled
299
- original_get_variant = client .get_variant
300
160
301
161
uninstall_integration (UnleashIntegration .identifier )
302
162
sentry_init (integrations = [UnleashIntegration ()]) # type: ignore
303
163
304
164
# Mock clients methods have not lost their qualified names after decoration.
305
165
assert client .is_enabled .__name__ == "is_enabled"
306
166
assert client .is_enabled .__qualname__ == original_is_enabled .__qualname__
307
- assert client .get_variant .__name__ == "get_variant"
308
- assert client .get_variant .__qualname__ == original_get_variant .__qualname__
0 commit comments