forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_rq.py
More file actions
72 lines (50 loc) · 2.03 KB
/
test_rq.py
File metadata and controls
72 lines (50 loc) · 2.03 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
from sentry_sdk.integrations.rq import RqIntegration
import os
import json
from fakeredis import FakeStrictRedis
import rq
from sentry_sdk import Hub
def crashing_job(foo):
1 / 0
def test_basic(sentry_init, capture_events):
sentry_init(integrations=[RqIntegration()])
events = capture_events()
queue = rq.Queue(connection=FakeStrictRedis())
worker = rq.SimpleWorker([queue], connection=queue.connection)
queue.enqueue(crashing_job, foo=42)
worker.work(burst=True)
event, = events
exception, = event["exception"]["values"]
assert exception["type"] == "ZeroDivisionError"
assert exception["mechanism"]["type"] == "rq"
assert exception["stacktrace"]["frames"][-1]["vars"]["foo"] == "42"
assert event["transaction"] == "tests.integrations.rq.test_rq.crashing_job"
assert event["extra"]["rq-job"] == {
"args": [],
"description": "tests.integrations.rq.test_rq.crashing_job(foo=42)",
"func": "tests.integrations.rq.test_rq.crashing_job",
"job_id": event["extra"]["rq-job"]["job_id"],
"kwargs": {"foo": 42},
}
def test_transport_shutdown(sentry_init):
sentry_init(integrations=[RqIntegration()])
events_r, events_w = os.pipe()
events_r = os.fdopen(events_r, "rb", 0)
events_w = os.fdopen(events_w, "wb", 0)
def capture_event(event):
events_w.write(json.dumps(event).encode("utf-8"))
events_w.write(b"\n")
def shutdown(timeout, callback=None):
events_w.write(b"shutdown\n")
Hub.current.client.transport.capture_event = capture_event
Hub.current.client.transport.shutdown = shutdown
queue = rq.Queue(connection=FakeStrictRedis())
worker = rq.Worker([queue], connection=queue.connection)
queue.enqueue(crashing_job, foo=42)
worker.work(burst=True)
event = events_r.readline()
shutdown = events_r.readline()
event = json.loads(event.decode("utf-8"))
assert shutdown == b"shutdown\n"
exception, = event["exception"]["values"]
assert exception["type"] == "ZeroDivisionError"