|
| 1 | +import concurrent.futures as cf |
| 2 | +import sys |
| 3 | +from contextlib import contextmanager |
| 4 | +from statsig import statsig |
| 5 | +from statsig.statsig_user import StatsigUser |
| 6 | +from random import random |
| 7 | +from unittest.mock import Mock |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +import sentry_sdk |
| 12 | +from sentry_sdk.integrations.statsig import StatsigIntegration |
| 13 | + |
| 14 | + |
| 15 | +@contextmanager |
| 16 | +def mock_statsig(gate_dict): |
| 17 | + old_check_gate = statsig.check_gate |
| 18 | + |
| 19 | + def mock_check_gate(user, gate, *args, **kwargs): |
| 20 | + return gate_dict.get(gate, False) |
| 21 | + |
| 22 | + statsig.check_gate = Mock(side_effect=mock_check_gate) |
| 23 | + |
| 24 | + yield |
| 25 | + |
| 26 | + statsig.check_gate = old_check_gate |
| 27 | + |
| 28 | + |
| 29 | +def test_check_gate(sentry_init, capture_events, uninstall_integration): |
| 30 | + uninstall_integration(StatsigIntegration.identifier) |
| 31 | + |
| 32 | + with mock_statsig({"hello": True, "world": False}): |
| 33 | + sentry_init(integrations=[StatsigIntegration()]) |
| 34 | + events = capture_events() |
| 35 | + user = StatsigUser(user_id="user-id") |
| 36 | + |
| 37 | + statsig.check_gate(user, "hello") |
| 38 | + statsig.check_gate(user, "world") |
| 39 | + statsig.check_gate(user, "other") # unknown gates default to False. |
| 40 | + |
| 41 | + sentry_sdk.capture_exception(Exception("something wrong!")) |
| 42 | + |
| 43 | + assert len(events) == 1 |
| 44 | + assert events[0]["contexts"]["flags"] == { |
| 45 | + "values": [ |
| 46 | + {"flag": "hello", "result": True}, |
| 47 | + {"flag": "world", "result": False}, |
| 48 | + {"flag": "other", "result": False}, |
| 49 | + ] |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | +def test_check_gate_threaded(sentry_init, capture_events, uninstall_integration): |
| 54 | + uninstall_integration(StatsigIntegration.identifier) |
| 55 | + |
| 56 | + with mock_statsig({"hello": True, "world": False}): |
| 57 | + sentry_init(integrations=[StatsigIntegration()]) |
| 58 | + events = capture_events() |
| 59 | + user = StatsigUser(user_id="user-id") |
| 60 | + |
| 61 | + # Capture an eval before we split isolation scopes. |
| 62 | + statsig.check_gate(user, "hello") |
| 63 | + |
| 64 | + def task(flag_key): |
| 65 | + # Creates a new isolation scope for the thread. |
| 66 | + # This means the evaluations in each task are captured separately. |
| 67 | + with sentry_sdk.isolation_scope(): |
| 68 | + statsig.check_gate(user, flag_key) |
| 69 | + # use a tag to identify to identify events later on |
| 70 | + sentry_sdk.set_tag("task_id", flag_key) |
| 71 | + sentry_sdk.capture_exception(Exception("something wrong!")) |
| 72 | + |
| 73 | + with cf.ThreadPoolExecutor(max_workers=2) as pool: |
| 74 | + pool.map(task, ["world", "other"]) |
| 75 | + |
| 76 | + # Capture error in original scope |
| 77 | + sentry_sdk.set_tag("task_id", "0") |
| 78 | + sentry_sdk.capture_exception(Exception("something wrong!")) |
| 79 | + |
| 80 | + assert len(events) == 3 |
| 81 | + events.sort(key=lambda e: e["tags"]["task_id"]) |
| 82 | + |
| 83 | + assert events[0]["contexts"]["flags"] == { |
| 84 | + "values": [ |
| 85 | + {"flag": "hello", "result": True}, |
| 86 | + ] |
| 87 | + } |
| 88 | + assert events[1]["contexts"]["flags"] == { |
| 89 | + "values": [ |
| 90 | + {"flag": "hello", "result": True}, |
| 91 | + {"flag": "other", "result": False}, |
| 92 | + ] |
| 93 | + } |
| 94 | + assert events[2]["contexts"]["flags"] == { |
| 95 | + "values": [ |
| 96 | + {"flag": "hello", "result": True}, |
| 97 | + {"flag": "world", "result": False}, |
| 98 | + ] |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher") |
| 103 | +def test_check_gate_asyncio(sentry_init, capture_events, uninstall_integration): |
| 104 | + asyncio = pytest.importorskip("asyncio") |
| 105 | + uninstall_integration(StatsigIntegration.identifier) |
| 106 | + |
| 107 | + with mock_statsig({"hello": True, "world": False}): |
| 108 | + sentry_init(integrations=[StatsigIntegration()]) |
| 109 | + events = capture_events() |
| 110 | + user = StatsigUser(user_id="user-id") |
| 111 | + |
| 112 | + # Capture an eval before we split isolation scopes. |
| 113 | + statsig.check_gate(user, "hello") |
| 114 | + |
| 115 | + async def task(flag_key): |
| 116 | + with sentry_sdk.isolation_scope(): |
| 117 | + statsig.check_gate(user, flag_key) |
| 118 | + # use a tag to identify to identify events later on |
| 119 | + sentry_sdk.set_tag("task_id", flag_key) |
| 120 | + sentry_sdk.capture_exception(Exception("something wrong!")) |
| 121 | + |
| 122 | + async def runner(): |
| 123 | + return asyncio.gather(task("world"), task("other")) |
| 124 | + |
| 125 | + asyncio.run(runner()) |
| 126 | + |
| 127 | + # Capture error in original scope |
| 128 | + sentry_sdk.set_tag("task_id", "0") |
| 129 | + sentry_sdk.capture_exception(Exception("something wrong!")) |
| 130 | + |
| 131 | + assert len(events) == 3 |
| 132 | + events.sort(key=lambda e: e["tags"]["task_id"]) |
| 133 | + |
| 134 | + assert events[0]["contexts"]["flags"] == { |
| 135 | + "values": [ |
| 136 | + {"flag": "hello", "result": True}, |
| 137 | + ] |
| 138 | + } |
| 139 | + assert events[1]["contexts"]["flags"] == { |
| 140 | + "values": [ |
| 141 | + {"flag": "hello", "result": True}, |
| 142 | + {"flag": "other", "result": False}, |
| 143 | + ] |
| 144 | + } |
| 145 | + assert events[2]["contexts"]["flags"] == { |
| 146 | + "values": [ |
| 147 | + {"flag": "hello", "result": True}, |
| 148 | + {"flag": "world", "result": False}, |
| 149 | + ] |
| 150 | + } |
| 151 | + |
| 152 | + |
| 153 | +def test_wraps_original(sentry_init, uninstall_integration): |
| 154 | + uninstall_integration(StatsigIntegration.identifier) |
| 155 | + flag_value = random() < 0.5 |
| 156 | + |
| 157 | + with mock_statsig( |
| 158 | + {"test-flag": flag_value} |
| 159 | + ): # patches check_gate with a Mock object. |
| 160 | + mock_check_gate = statsig.check_gate |
| 161 | + sentry_init(integrations=[StatsigIntegration()]) # wraps check_gate. |
| 162 | + user = StatsigUser(user_id="user-id") |
| 163 | + |
| 164 | + res = statsig.check_gate(user, "test-flag", "extra-arg", kwarg=1) # type: ignore[arg-type] |
| 165 | + |
| 166 | + assert res == flag_value |
| 167 | + assert mock_check_gate.call_args == ( # type: ignore[attr-defined] |
| 168 | + (user, "test-flag", "extra-arg"), |
| 169 | + {"kwarg": 1}, |
| 170 | + ) |
| 171 | + |
| 172 | + |
| 173 | +def test_wrapper_attributes(sentry_init, uninstall_integration): |
| 174 | + uninstall_integration(StatsigIntegration.identifier) |
| 175 | + original_check_gate = statsig.check_gate |
| 176 | + sentry_init(integrations=[StatsigIntegration()]) |
| 177 | + |
| 178 | + # Methods have not lost their qualified names after decoration. |
| 179 | + assert statsig.check_gate.__name__ == "check_gate" |
| 180 | + assert statsig.check_gate.__qualname__ == original_check_gate.__qualname__ |
| 181 | + |
| 182 | + # Clean up |
| 183 | + statsig.check_gate = original_check_gate |
0 commit comments