Skip to content

Commit 8588dbe

Browse files
authored
Fix side effects for parallel tests (#1554)
* Fix parallel tests in older sanic versions 0.8 and 18 * Fix rediscluster test side-effect by resetting integrations
1 parent 272af6b commit 8588dbe

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

sentry_sdk/integrations/redis.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ def patch_redis_client(cls, is_cluster):
131131
This function can be used to instrument custom redis client classes or
132132
subclasses.
133133
"""
134-
135134
old_execute_command = cls.execute_command
136135

137136
def sentry_patched_execute_command(self, name, *args, **kwargs):

tests/conftest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from sentry_sdk.transport import Transport
2020
from sentry_sdk.envelope import Envelope
2121
from sentry_sdk.utils import capture_internal_exceptions
22+
from sentry_sdk.integrations import _installed_integrations # noqa: F401
2223

2324
from tests import _warning_recorder, _warning_recorder_mgr
2425

@@ -165,6 +166,17 @@ def inner(event):
165166
return inner
166167

167168

169+
@pytest.fixture
170+
def reset_integrations():
171+
"""
172+
Use with caution, sometimes we really need to start
173+
with a clean slate to ensure monkeypatching works well,
174+
but this also means some other stuff will be monkeypatched twice.
175+
"""
176+
global _installed_integrations
177+
_installed_integrations.clear()
178+
179+
168180
@pytest.fixture
169181
def sentry_init(monkeypatch_test_transport, request):
170182
def inner(*a, **kw):

tests/integrations/rediscluster/test_rediscluster.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
rediscluster_classes.append(rediscluster.StrictRedisCluster)
1212

1313

14-
@pytest.fixture(scope="module", autouse=True)
15-
def monkeypatch_rediscluster_classes():
14+
@pytest.fixture(autouse=True)
15+
def monkeypatch_rediscluster_classes(reset_integrations):
1616

1717
try:
1818
pipeline_cls = rediscluster.ClusterPipeline

tests/integrations/sanic/test_sanic.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import os
12
import sys
2-
33
import random
44
import asyncio
55
from unittest.mock import Mock
@@ -18,6 +18,20 @@
1818

1919
@pytest.fixture
2020
def app():
21+
if SANIC_VERSION < (19,):
22+
"""
23+
Older Sanic versions 0.8 and 18 bind to the same fixed port which
24+
creates problems when we run tests concurrently.
25+
"""
26+
old_test_client = Sanic.test_client.__get__
27+
28+
def new_test_client(self):
29+
client = old_test_client(self, Sanic)
30+
client.port += os.getpid() % 100
31+
return client
32+
33+
Sanic.test_client = property(new_test_client)
34+
2135
if SANIC_VERSION >= (20, 12):
2236
# Build (20.12.0) adds a feature where the instance is stored in an internal class
2337
# registry for later retrieval, and so add register=False to disable that

0 commit comments

Comments
 (0)