Skip to content

Commit 58e412e

Browse files
committed
OF client isolation test
1 parent 16917f7 commit 58e412e

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

sentry_sdk/integrations/openfeature.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def setup_once():
3535
if client:
3636
# Register the hook within the openfeature client.
3737
client.add_hooks(hooks=[OpenFeatureHook()])
38+
print("added hook to", client)
3839
else:
3940
# Register the hook within the global openfeature hooks list.
4041
api.add_hooks(hooks=[OpenFeatureHook()])

tests/integrations/openfeature/test_openfeature.py

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import concurrent.futures as cf
22
import sys
3-
43
import pytest
54

65
from openfeature import api
@@ -10,12 +9,22 @@
109
from sentry_sdk.integrations.openfeature import OpenFeatureIntegration
1110

1211

12+
@pytest.fixture
13+
def reset_openfeature(uninstall_integration):
14+
yield
15+
16+
# Teardown
17+
uninstall_integration(OpenFeatureIntegration.identifier)
18+
api.clear_hooks()
19+
api.shutdown() # provider clean up
20+
21+
1322
@pytest.mark.parametrize(
1423
"use_global_client",
1524
(False, True),
1625
)
1726
def test_openfeature_integration(
18-
sentry_init, use_global_client, capture_events, uninstall_integration
27+
sentry_init, use_global_client, capture_events, reset_openfeature
1928
):
2029
flags = {
2130
"hello": InMemoryFlag("on", {"on": True, "off": False}),
@@ -24,7 +33,6 @@ def test_openfeature_integration(
2433
api.set_provider(InMemoryProvider(flags))
2534
client = api.get_client()
2635

27-
uninstall_integration(OpenFeatureIntegration.identifier)
2836
if use_global_client:
2937
sentry_init(integrations=[OpenFeatureIntegration()])
3038
else:
@@ -48,7 +56,7 @@ def test_openfeature_integration(
4856

4957

5058
def test_openfeature_integration_threaded(
51-
sentry_init, capture_events, uninstall_integration
59+
sentry_init, capture_events, reset_openfeature
5260
):
5361
flags = {
5462
"hello": InMemoryFlag("on", {"on": True, "off": False}),
@@ -57,7 +65,6 @@ def test_openfeature_integration_threaded(
5765
api.set_provider(InMemoryProvider(flags))
5866
client = api.get_client()
5967

60-
uninstall_integration(OpenFeatureIntegration.identifier)
6168
sentry_init(integrations=[OpenFeatureIntegration(client=client)])
6269
events = capture_events()
6370

@@ -104,7 +111,7 @@ def task(flag):
104111

105112
@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher")
106113
def test_openfeature_integration_asyncio(
107-
sentry_init, capture_events, uninstall_integration
114+
sentry_init, capture_events, reset_openfeature
108115
):
109116
"""Assert concurrently evaluated flags do not pollute one another."""
110117

@@ -117,7 +124,6 @@ def test_openfeature_integration_asyncio(
117124
api.set_provider(InMemoryProvider(flags))
118125
client = api.get_client()
119126

120-
uninstall_integration(OpenFeatureIntegration.identifier)
121127
sentry_init(integrations=[OpenFeatureIntegration(client=client)])
122128
events = capture_events()
123129

@@ -160,3 +166,31 @@ async def runner():
160166
{"flag": "world", "result": False},
161167
]
162168
}
169+
170+
171+
def test_openfeature_integration_client_isolation(
172+
sentry_init, capture_events, reset_openfeature
173+
):
174+
"""
175+
If the integration is tracking a single client, evaluations from other clients should not be
176+
captured.
177+
"""
178+
flags = {
179+
"hello": InMemoryFlag("on", {"on": True, "off": False}),
180+
"world": InMemoryFlag("off", {"on": True, "off": False}),
181+
}
182+
api.set_provider(InMemoryProvider(flags))
183+
client = api.get_client()
184+
sentry_init(integrations=[OpenFeatureIntegration(client=client)])
185+
186+
other_client = api.get_client()
187+
other_client.get_boolean_value("hello", default_value=False)
188+
other_client.get_boolean_value("world", default_value=False)
189+
other_client.get_boolean_value("other", default_value=True)
190+
191+
events = capture_events()
192+
sentry_sdk.set_tag("apple", "0")
193+
sentry_sdk.capture_exception(Exception("something wrong!"))
194+
195+
assert len(events) == 1
196+
assert events[0]["contexts"]["flags"] == {"values": []}

0 commit comments

Comments
 (0)