|
| 1 | +import concurrent.futures as cf |
| 2 | +import sys |
| 3 | +from random import random |
| 4 | +from unittest import mock |
| 5 | +from UnleashClient import UnleashClient |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +import sentry_sdk |
| 10 | +from sentry_sdk.integrations.unleash import UnleashIntegration |
| 11 | +from tests.integrations.unleash.testutils import mock_unleash_client |
| 12 | + |
| 13 | + |
| 14 | +def test_is_enabled(sentry_init, capture_events, uninstall_integration): |
| 15 | + uninstall_integration(UnleashIntegration.identifier) |
| 16 | + |
| 17 | + with mock_unleash_client(): |
| 18 | + client = UnleashClient() |
| 19 | + sentry_init(integrations=[UnleashIntegration()]) |
| 20 | + client.is_enabled("hello") |
| 21 | + client.is_enabled("world") |
| 22 | + client.is_enabled("other") |
| 23 | + |
| 24 | + events = capture_events() |
| 25 | + sentry_sdk.capture_exception(Exception("something wrong!")) |
| 26 | + |
| 27 | + assert len(events) == 1 |
| 28 | + assert events[0]["contexts"]["flags"] == { |
| 29 | + "values": [ |
| 30 | + {"flag": "hello", "result": True}, |
| 31 | + {"flag": "world", "result": False}, |
| 32 | + {"flag": "other", "result": False}, |
| 33 | + ] |
| 34 | + } |
| 35 | + |
| 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 | +def test_is_enabled_threaded(sentry_init, capture_events, uninstall_integration): |
| 67 | + uninstall_integration(UnleashIntegration.identifier) |
| 68 | + |
| 69 | + with mock_unleash_client(): |
| 70 | + client = UnleashClient() |
| 71 | + sentry_init(integrations=[UnleashIntegration()]) # type: ignore |
| 72 | + events = capture_events() |
| 73 | + |
| 74 | + def task(flag_key): |
| 75 | + # Creates a new isolation scope for the thread. |
| 76 | + # This means the evaluations in each task are captured separately. |
| 77 | + with sentry_sdk.isolation_scope(): |
| 78 | + client.is_enabled(flag_key) |
| 79 | + # use a tag to identify to identify events later on |
| 80 | + sentry_sdk.set_tag("task_id", flag_key) |
| 81 | + sentry_sdk.capture_exception(Exception("something wrong!")) |
| 82 | + |
| 83 | + # Capture an eval before we split isolation scopes. |
| 84 | + client.is_enabled("hello") |
| 85 | + |
| 86 | + with cf.ThreadPoolExecutor(max_workers=2) as pool: |
| 87 | + pool.map(task, ["world", "other"]) |
| 88 | + |
| 89 | + # Capture error in original scope |
| 90 | + sentry_sdk.set_tag("task_id", "0") |
| 91 | + sentry_sdk.capture_exception(Exception("something wrong!")) |
| 92 | + |
| 93 | + assert len(events) == 3 |
| 94 | + events.sort(key=lambda e: e["tags"]["task_id"]) |
| 95 | + |
| 96 | + assert events[0]["contexts"]["flags"] == { |
| 97 | + "values": [ |
| 98 | + {"flag": "hello", "result": True}, |
| 99 | + ] |
| 100 | + } |
| 101 | + assert events[1]["contexts"]["flags"] == { |
| 102 | + "values": [ |
| 103 | + {"flag": "hello", "result": True}, |
| 104 | + {"flag": "other", "result": False}, |
| 105 | + ] |
| 106 | + } |
| 107 | + assert events[2]["contexts"]["flags"] == { |
| 108 | + "values": [ |
| 109 | + {"flag": "hello", "result": True}, |
| 110 | + {"flag": "world", "result": False}, |
| 111 | + ] |
| 112 | + } |
| 113 | + |
| 114 | + |
| 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 | +@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher") |
| 165 | +def test_is_enabled_asyncio(sentry_init, capture_events, uninstall_integration): |
| 166 | + asyncio = pytest.importorskip("asyncio") |
| 167 | + uninstall_integration(UnleashIntegration.identifier) |
| 168 | + |
| 169 | + with mock_unleash_client(): |
| 170 | + client = UnleashClient() |
| 171 | + sentry_init(integrations=[UnleashIntegration()]) # type: ignore |
| 172 | + events = capture_events() |
| 173 | + |
| 174 | + async def task(flag_key): |
| 175 | + with sentry_sdk.isolation_scope(): |
| 176 | + client.is_enabled(flag_key) |
| 177 | + # use a tag to identify to identify events later on |
| 178 | + sentry_sdk.set_tag("task_id", flag_key) |
| 179 | + sentry_sdk.capture_exception(Exception("something wrong!")) |
| 180 | + |
| 181 | + async def runner(): |
| 182 | + return asyncio.gather(task("world"), task("other")) |
| 183 | + |
| 184 | + # Capture an eval before we split isolation scopes. |
| 185 | + client.is_enabled("hello") |
| 186 | + |
| 187 | + asyncio.run(runner()) |
| 188 | + |
| 189 | + # Capture error in original scope |
| 190 | + sentry_sdk.set_tag("task_id", "0") |
| 191 | + sentry_sdk.capture_exception(Exception("something wrong!")) |
| 192 | + |
| 193 | + assert len(events) == 3 |
| 194 | + events.sort(key=lambda e: e["tags"]["task_id"]) |
| 195 | + |
| 196 | + assert events[0]["contexts"]["flags"] == { |
| 197 | + "values": [ |
| 198 | + {"flag": "hello", "result": True}, |
| 199 | + ] |
| 200 | + } |
| 201 | + assert events[1]["contexts"]["flags"] == { |
| 202 | + "values": [ |
| 203 | + {"flag": "hello", "result": True}, |
| 204 | + {"flag": "other", "result": False}, |
| 205 | + ] |
| 206 | + } |
| 207 | + assert events[2]["contexts"]["flags"] == { |
| 208 | + "values": [ |
| 209 | + {"flag": "hello", "result": True}, |
| 210 | + {"flag": "world", "result": False}, |
| 211 | + ] |
| 212 | + } |
| 213 | + |
| 214 | + |
| 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 | +def test_wraps_original(sentry_init, uninstall_integration): |
| 268 | + with mock_unleash_client(): |
| 269 | + client = UnleashClient() |
| 270 | + |
| 271 | + mock_is_enabled = mock.Mock(return_value=random() < 0.5) |
| 272 | + mock_get_variant = mock.Mock(return_value={"enabled": random() < 0.5}) |
| 273 | + client.is_enabled = mock_is_enabled |
| 274 | + client.get_variant = mock_get_variant |
| 275 | + |
| 276 | + uninstall_integration(UnleashIntegration.identifier) |
| 277 | + sentry_init(integrations=[UnleashIntegration()]) # type: ignore |
| 278 | + |
| 279 | + res = client.is_enabled("test-flag", "arg", kwarg=1) |
| 280 | + assert res == mock_is_enabled.return_value |
| 281 | + assert mock_is_enabled.call_args == ( |
| 282 | + ("test-flag", "arg"), |
| 283 | + {"kwarg": 1}, |
| 284 | + ) |
| 285 | + |
| 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 | + |
| 294 | +def test_wrapper_attributes(sentry_init, uninstall_integration): |
| 295 | + with mock_unleash_client(): |
| 296 | + client = UnleashClient() # <- Returns a MockUnleashClient |
| 297 | + |
| 298 | + original_is_enabled = client.is_enabled |
| 299 | + original_get_variant = client.get_variant |
| 300 | + |
| 301 | + uninstall_integration(UnleashIntegration.identifier) |
| 302 | + sentry_init(integrations=[UnleashIntegration()]) # type: ignore |
| 303 | + |
| 304 | + # Mock clients methods have not lost their qualified names after decoration. |
| 305 | + assert client.is_enabled.__name__ == "is_enabled" |
| 306 | + 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