forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_threading.py
More file actions
116 lines (84 loc) · 2.74 KB
/
Copy pathtest_threading.py
File metadata and controls
116 lines (84 loc) · 2.74 KB
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
import gc
from threading import Thread
import pytest
from sentry_sdk import configure_scope, capture_message
from sentry_sdk.integrations.threading import ThreadingIntegration
@pytest.mark.forked
@pytest.mark.parametrize("integrations", [[ThreadingIntegration()], []])
def test_handles_exceptions(sentry_init, capture_events, integrations):
sentry_init(default_integrations=False, integrations=integrations)
events = capture_events()
def crash():
1 / 0
t = Thread(target=crash)
t.start()
t.join()
if integrations:
(event,) = events
(exception,) = event["exception"]["values"]
assert exception["type"] == "ZeroDivisionError"
assert exception["mechanism"] == {"type": "threading", "handled": False}
else:
assert not events
@pytest.mark.forked
@pytest.mark.parametrize("propagate_hub", (True, False))
def test_propagates_hub(sentry_init, capture_events, propagate_hub):
sentry_init(
default_integrations=False,
integrations=[ThreadingIntegration(propagate_hub=propagate_hub)],
)
events = capture_events()
def stage1():
with configure_scope() as scope:
scope.set_tag("stage1", "true")
t = Thread(target=stage2)
t.start()
t.join()
def stage2():
1 / 0
t = Thread(target=stage1)
t.start()
t.join()
(event,) = events
(exception,) = event["exception"]["values"]
assert exception["type"] == "ZeroDivisionError"
assert exception["mechanism"] == {"type": "threading", "handled": False}
if propagate_hub:
assert event["tags"]["stage1"] == "true"
else:
assert "stage1" not in event.get("tags", {})
def test_circular_references(sentry_init, request):
sentry_init(default_integrations=False, integrations=[ThreadingIntegration()])
gc.collect()
gc.disable()
request.addfinalizer(gc.enable)
class MyThread(Thread):
def run(self):
pass
t = MyThread()
t.start()
t.join()
del t
assert not gc.collect()
@pytest.mark.forked
def test_double_patching(sentry_init, capture_events):
sentry_init(default_integrations=False, integrations=[ThreadingIntegration()])
events = capture_events()
# XXX: Workaround for race condition in the py library's magic import
# system (py is a dependency of pytest)
capture_message("hi")
del events[:]
class MyThread(Thread):
def run(self):
1 / 0
ts = []
for _ in range(10):
t = MyThread()
t.start()
ts.append(t)
for t in ts:
t.join()
assert len(events) == 10
for event in events:
(exception,) = event["exception"]["values"]
assert exception["type"] == "ZeroDivisionError"