|
1 | | -# import asyncio |
2 | 1 | import concurrent.futures as cf |
3 | 2 |
|
4 | 3 | import sentry_sdk |
|
7 | 6 | add_feature_flag, |
8 | 7 | ) |
9 | 8 |
|
10 | | -# import pytest |
11 | | -# import sys |
| 9 | +import pytest |
12 | 10 |
|
13 | 11 |
|
14 | 12 | def test_featureflags_integration(sentry_init, capture_events, uninstall_integration): |
@@ -81,56 +79,53 @@ def task(flag_key): |
81 | 79 | } |
82 | 80 |
|
83 | 81 |
|
84 | | -# |
85 | | -# |
86 | | -# @pytest.mark.skipif( |
87 | | -# sys.version_info < (3, 7), reason="Test requires Python 3.7 or higher" |
88 | | -# ) |
89 | | -# def test_featureflags_integration_asyncio( |
90 | | -# sentry_init, capture_events, uninstall_integration |
91 | | -# ): |
92 | | -# uninstall_integration(FeatureFlagsIntegration.identifier) |
93 | | -# sentry_init(integrations=[FeatureFlagsIntegration()]) |
94 | | -# events = capture_events() |
95 | | -# |
96 | | -# # Capture an eval before we split isolation scopes. |
97 | | -# add_feature_flag("hello", False) |
98 | | -# |
99 | | -# async def task(flag_key): |
100 | | -# # Creates a new isolation scope for the thread. |
101 | | -# # This means the evaluations in each task are captured separately. |
102 | | -# with sentry_sdk.isolation_scope(): |
103 | | -# add_feature_flag(flag_key, False) |
104 | | -# # use a tag to identify to identify events later on |
105 | | -# sentry_sdk.set_tag("task_id", flag_key) |
106 | | -# sentry_sdk.capture_exception(Exception("something wrong!")) |
107 | | -# |
108 | | -# async def runner(): |
109 | | -# return asyncio.gather(task("world"), task("other")) |
110 | | -# |
111 | | -# asyncio.run(runner()) |
112 | | -# |
113 | | -# # Capture error in original scope |
114 | | -# sentry_sdk.set_tag("task_id", "0") |
115 | | -# sentry_sdk.capture_exception(Exception("something wrong!")) |
116 | | -# |
117 | | -# assert len(events) == 3 |
118 | | -# events.sort(key=lambda e: e["tags"]["task_id"]) |
119 | | -# |
120 | | -# assert events[0]["contexts"]["flags"] == { |
121 | | -# "values": [ |
122 | | -# {"flag": "hello", "result": False}, |
123 | | -# ] |
124 | | -# } |
125 | | -# assert events[1]["contexts"]["flags"] == { |
126 | | -# "values": [ |
127 | | -# {"flag": "hello", "result": False}, |
128 | | -# {"flag": "other", "result": False}, |
129 | | -# ] |
130 | | -# } |
131 | | -# assert events[2]["contexts"]["flags"] == { |
132 | | -# "values": [ |
133 | | -# {"flag": "hello", "result": False}, |
134 | | -# {"flag": "world", "result": False}, |
135 | | -# ] |
136 | | -# } |
| 82 | +def test_featureflags_integration_asyncio( |
| 83 | + sentry_init, capture_events, uninstall_integration |
| 84 | +): |
| 85 | + asyncio = pytest.importorskip("asyncio") # Only available in Python 3.7+. |
| 86 | + |
| 87 | + uninstall_integration(FeatureFlagsIntegration.identifier) |
| 88 | + sentry_init(integrations=[FeatureFlagsIntegration()]) |
| 89 | + events = capture_events() |
| 90 | + |
| 91 | + # Capture an eval before we split isolation scopes. |
| 92 | + add_feature_flag("hello", False) |
| 93 | + |
| 94 | + async def task(flag_key): |
| 95 | + # Creates a new isolation scope for the thread. |
| 96 | + # This means the evaluations in each task are captured separately. |
| 97 | + with sentry_sdk.isolation_scope(): |
| 98 | + add_feature_flag(flag_key, False) |
| 99 | + # use a tag to identify to identify events later on |
| 100 | + sentry_sdk.set_tag("task_id", flag_key) |
| 101 | + sentry_sdk.capture_exception(Exception("something wrong!")) |
| 102 | + |
| 103 | + async def runner(): |
| 104 | + return asyncio.gather(task("world"), task("other")) |
| 105 | + |
| 106 | + asyncio.run(runner()) |
| 107 | + |
| 108 | + # Capture error in original scope |
| 109 | + sentry_sdk.set_tag("task_id", "0") |
| 110 | + sentry_sdk.capture_exception(Exception("something wrong!")) |
| 111 | + |
| 112 | + assert len(events) == 3 |
| 113 | + events.sort(key=lambda e: e["tags"]["task_id"]) |
| 114 | + |
| 115 | + assert events[0]["contexts"]["flags"] == { |
| 116 | + "values": [ |
| 117 | + {"flag": "hello", "result": False}, |
| 118 | + ] |
| 119 | + } |
| 120 | + assert events[1]["contexts"]["flags"] == { |
| 121 | + "values": [ |
| 122 | + {"flag": "hello", "result": False}, |
| 123 | + {"flag": "other", "result": False}, |
| 124 | + ] |
| 125 | + } |
| 126 | + assert events[2]["contexts"]["flags"] == { |
| 127 | + "values": [ |
| 128 | + {"flag": "hello", "result": False}, |
| 129 | + {"flag": "world", "result": False}, |
| 130 | + ] |
| 131 | + } |
0 commit comments