-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathtest_aioredis_py36plus.py
More file actions
180 lines (145 loc) · 5.54 KB
/
test_aioredis_py36plus.py
File metadata and controls
180 lines (145 loc) · 5.54 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
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
# coding=utf-8
from __future__ import absolute_import, division, print_function, unicode_literals
import logging
import os
import aioredis
import pytest
from scout_apm.instruments.aioredis import ensure_installed
from tests.compat import mock
from tests.tools import async_test
async def get_redis_conn():
ensure_installed()
# e.g. export REDIS_URL="redis://localhost:6379/0"
if "REDIS_URL" not in os.environ:
raise pytest.skip("Redis isn't available")
conn = await aioredis.create_connection(os.environ["REDIS_URL"])
return aioredis.Redis(conn)
def test_ensure_installed_twice(caplog):
ensure_installed()
ensure_installed()
assert caplog.record_tuples == 2 * [
(
"scout_apm.instruments.aioredis",
logging.DEBUG,
"Instrumenting aioredis.",
)
]
def test_ensure_installed_fail_no_redis_execute(caplog):
mock_not_patched = mock.patch(
"scout_apm.instruments.aioredis.have_patched_redis_execute", new=False
)
mock_redis = mock.patch("scout_apm.instruments.aioredis.Redis")
with mock_not_patched, mock_redis as mocked_redis:
del mocked_redis.execute
ensure_installed()
assert len(caplog.record_tuples) == 2
assert caplog.record_tuples[0] == (
"scout_apm.instruments.aioredis",
logging.DEBUG,
"Instrumenting aioredis.",
)
logger, level, message = caplog.record_tuples[1]
assert logger == "scout_apm.instruments.aioredis"
assert level == logging.WARNING
assert message.startswith(
"Failed to instrument aioredis.Redis.execute: AttributeError"
)
def test_ensure_installed_fail_no_wrapped_redis_execute(caplog):
mock_not_patched = mock.patch(
"scout_apm.instruments.aioredis.have_patched_redis_execute", new=False
)
mock_wrapped_redis_execute = mock.patch(
"scout_apm.instruments.aioredis.wrapped_redis_execute", new=None
)
with mock_not_patched, mock_wrapped_redis_execute:
ensure_installed()
assert len(caplog.record_tuples) == 2
assert caplog.record_tuples[0] == (
"scout_apm.instruments.aioredis",
logging.DEBUG,
"Instrumenting aioredis.",
)
assert caplog.record_tuples[1] == (
"scout_apm.instruments.aioredis",
logging.DEBUG,
(
"Couldn't import scout_apm.async_.instruments.aioredis - probably"
+ " using Python < 3.6."
),
)
def test_ensure_installed_fail_no_pipeline_execute(caplog):
mock_not_patched = mock.patch(
"scout_apm.instruments.aioredis.have_patched_pipeline_execute", new=False
)
mock_pipeline = mock.patch("scout_apm.instruments.aioredis.Pipeline")
with mock_not_patched, mock_pipeline as mocked_pipeline:
del mocked_pipeline.execute
ensure_installed()
assert len(caplog.record_tuples) == 2
assert caplog.record_tuples[0] == (
"scout_apm.instruments.aioredis",
logging.DEBUG,
"Instrumenting aioredis.",
)
logger, level, message = caplog.record_tuples[1]
assert logger == "scout_apm.instruments.aioredis"
assert level == logging.WARNING
assert message.startswith(
"Failed to instrument aioredis.commands.Pipeline.execute: AttributeError"
)
def test_ensure_installed_fail_no_wrapped_pipeline_execute(caplog):
mock_not_patched = mock.patch(
"scout_apm.instruments.aioredis.have_patched_pipeline_execute", new=False
)
mock_wrapped_pipeline_execute = mock.patch(
"scout_apm.instruments.aioredis.wrapped_pipeline_execute", new=None
)
with mock_not_patched, mock_wrapped_pipeline_execute:
ensure_installed()
assert len(caplog.record_tuples) == 2
assert caplog.record_tuples[0] == (
"scout_apm.instruments.aioredis",
logging.DEBUG,
"Instrumenting aioredis.",
)
assert caplog.record_tuples[1] == (
"scout_apm.instruments.aioredis",
logging.DEBUG,
(
"Couldn't import scout_apm.async_.instruments.aioredis -"
+ " probably using Python < 3.6."
),
)
@async_test
async def test_echo(tracked_request):
redis_conn = await get_redis_conn()
await redis_conn.echo("Hello World!")
assert len(tracked_request.complete_spans) == 1
assert tracked_request.complete_spans[0].operation == "Redis/ECHO"
@async_test
async def test_pipeline_echo(tracked_request):
redis_conn = await get_redis_conn()
with redis_conn.pipeline() as p:
p.echo("Hello World!")
p.execute()
assert len(tracked_request.complete_spans) == 1
assert tracked_request.complete_spans[0].operation == "Redis/MULTI"
@async_test
async def test_execute_command_missing_argument(tracked_request):
redis_conn = await get_redis_conn()
# Redis instrumentation doesn't crash if op is missing.
# This raises a TypeError (Python 3) or IndexError (Python 2)
# when calling the original method.
with pytest.raises(IndexError):
redis_conn.execute_command()
assert len(tracked_request.complete_spans) == 1
assert tracked_request.complete_spans[0].operation == "Redis/Unknown"
@async_test
async def test_perform_request_bad_url(tracked_request):
redis_conn = await get_redis_conn()
with pytest.raises(TypeError):
# Redis instrumentation doesn't crash if op has the wrong type.
# This raises a TypeError when calling the original method.
redis_conn.execute_command(None)
assert len(tracked_request.complete_spans) == 1
assert tracked_request.complete_spans[0].operation == "Redis/None"