-
Notifications
You must be signed in to change notification settings - Fork 534
/
Copy pathtest_launchdarkly.py
203 lines (168 loc) · 6.49 KB
/
test_launchdarkly.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import concurrent.futures as cf
import sys
import ldclient
import pytest
from ldclient import LDClient
from ldclient.config import Config
from ldclient.context import Context
from ldclient.integrations.test_data import TestData
import sentry_sdk
from sentry_sdk.integrations import DidNotEnable
from sentry_sdk.integrations.launchdarkly import LaunchDarklyIntegration
@pytest.mark.parametrize(
"use_global_client",
(False, True),
)
def test_launchdarkly_integration(
sentry_init, use_global_client, capture_events, uninstall_integration
):
td = TestData.data_source()
td.update(td.flag("hello").variation_for_all(True))
td.update(td.flag("world").variation_for_all(True))
# Disable background requests as we aren't using a server.
config = Config(
"sdk-key", update_processor_class=td, diagnostic_opt_out=True, send_events=False
)
uninstall_integration(LaunchDarklyIntegration.identifier)
if use_global_client:
ldclient.set_config(config)
sentry_init(integrations=[LaunchDarklyIntegration()])
client = ldclient.get()
else:
client = LDClient(config=config)
sentry_init(integrations=[LaunchDarklyIntegration(ld_client=client)])
# Evaluate
client.variation("hello", Context.create("my-org", "organization"), False)
client.variation("world", Context.create("user1", "user"), False)
client.variation("other", Context.create("user2", "user"), False)
events = capture_events()
sentry_sdk.capture_exception(Exception("something wrong!"))
assert len(events) == 1
assert events[0]["contexts"]["flags"] == {
"values": [
{"flag": "hello", "result": True},
{"flag": "world", "result": True},
{"flag": "other", "result": False},
]
}
def test_launchdarkly_integration_threaded(
sentry_init, capture_events, uninstall_integration
):
td = TestData.data_source()
td.update(td.flag("hello").variation_for_all(True))
td.update(td.flag("world").variation_for_all(True))
client = LDClient(
config=Config(
"sdk-key",
update_processor_class=td,
diagnostic_opt_out=True, # Disable background requests as we aren't using a server.
send_events=False,
)
)
context = Context.create("user1")
uninstall_integration(LaunchDarklyIntegration.identifier)
sentry_init(integrations=[LaunchDarklyIntegration(ld_client=client)])
events = capture_events()
def task(flag_key):
# Creates a new isolation scope for the thread.
# This means the evaluations in each task are captured separately.
with sentry_sdk.isolation_scope():
client.variation(flag_key, context, False)
# use a tag to identify to identify events later on
sentry_sdk.set_tag("task_id", flag_key)
sentry_sdk.capture_exception(Exception("something wrong!"))
# Capture an eval before we split isolation scopes.
client.variation("hello", context, False)
with cf.ThreadPoolExecutor(max_workers=2) as pool:
pool.map(task, ["world", "other"])
# Capture error in original scope
sentry_sdk.set_tag("task_id", "0")
sentry_sdk.capture_exception(Exception("something wrong!"))
assert len(events) == 3
events.sort(key=lambda e: e["tags"]["task_id"])
assert events[0]["contexts"]["flags"] == {
"values": [
{"flag": "hello", "result": True},
]
}
assert events[1]["contexts"]["flags"] == {
"values": [
{"flag": "hello", "result": True},
{"flag": "other", "result": False},
]
}
assert events[2]["contexts"]["flags"] == {
"values": [
{"flag": "hello", "result": True},
{"flag": "world", "result": True},
]
}
@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher")
def test_launchdarkly_integration_asyncio(
sentry_init, capture_events, uninstall_integration
):
"""Assert concurrently evaluated flags do not pollute one another."""
asyncio = pytest.importorskip("asyncio")
td = TestData.data_source()
td.update(td.flag("hello").variation_for_all(True))
td.update(td.flag("world").variation_for_all(True))
client = LDClient(
config=Config(
"sdk-key",
update_processor_class=td,
diagnostic_opt_out=True, # Disable background requests as we aren't using a server.
send_events=False,
)
)
context = Context.create("user1")
uninstall_integration(LaunchDarklyIntegration.identifier)
sentry_init(integrations=[LaunchDarklyIntegration(ld_client=client)])
events = capture_events()
async def task(flag_key):
with sentry_sdk.isolation_scope():
client.variation(flag_key, context, False)
# use a tag to identify to identify events later on
sentry_sdk.set_tag("task_id", flag_key)
sentry_sdk.capture_exception(Exception("something wrong!"))
async def runner():
return asyncio.gather(task("world"), task("other"))
# Capture an eval before we split isolation scopes.
client.variation("hello", context, False)
asyncio.run(runner())
# Capture error in original scope
sentry_sdk.set_tag("task_id", "0")
sentry_sdk.capture_exception(Exception("something wrong!"))
assert len(events) == 3
events.sort(key=lambda e: e["tags"]["task_id"])
assert events[0]["contexts"]["flags"] == {
"values": [
{"flag": "hello", "result": True},
]
}
assert events[1]["contexts"]["flags"] == {
"values": [
{"flag": "hello", "result": True},
{"flag": "other", "result": False},
]
}
assert events[2]["contexts"]["flags"] == {
"values": [
{"flag": "hello", "result": True},
{"flag": "world", "result": True},
]
}
def test_launchdarkly_integration_did_not_enable(sentry_init, uninstall_integration):
"""
Setup should fail when using global client and ldclient.set_config wasn't called.
We're accessing ldclient internals to set up this test, so it might break if launchdarkly's
implementation changes.
"""
ldclient._reset_client()
try:
ldclient.__lock.lock()
ldclient.__config = None
finally:
ldclient.__lock.unlock()
uninstall_integration(LaunchDarklyIntegration.identifier)
with pytest.raises(DidNotEnable):
sentry_init(integrations=[LaunchDarklyIntegration()])