Skip to content

Commit c821354

Browse files
umaannamalaiTimPansinolrafeei
committed
Add aredis testing. (#489)
Co-authored-by: Tim Pansino <[email protected]> Co-authored-by: Lalleh Rafeei <[email protected]> Co-authored-by: Tim Pansino <[email protected]> Co-authored-by: Lalleh Rafeei <[email protected]>
1 parent ec02b06 commit c821354

7 files changed

+789
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# Copyright 2010 New Relic, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
''' The purpose of these tests is to confirm that using a non-standard
16+
connection pool that does not have a `connection_kwargs` attribute
17+
will not result in an error.
18+
'''
19+
20+
import pytest
21+
import aredis
22+
23+
from newrelic.api.background_task import background_task
24+
25+
from testing_support.fixture.event_loop import event_loop as loop
26+
from testing_support.fixtures import (validate_transaction_metrics,
27+
override_application_settings)
28+
from testing_support.db_settings import redis_settings
29+
from testing_support.util import instance_hostname
30+
31+
DB_SETTINGS = redis_settings()[0]
32+
REDIS_PY_VERSION = aredis.VERSION
33+
34+
35+
class FakeConnectionPool(object):
36+
"""Connection Pool without connection_kwargs attribute."""
37+
38+
def __init__(self, connection):
39+
self.connection = connection
40+
41+
def get_connection(self, name=None, *keys, **options):
42+
return self.connection
43+
44+
def release(self, connection):
45+
self.connection.disconnect()
46+
47+
# Settings
48+
49+
_enable_instance_settings = {
50+
'datastore_tracer.instance_reporting.enabled': True,
51+
}
52+
_disable_instance_settings = {
53+
'datastore_tracer.instance_reporting.enabled': False,
54+
}
55+
56+
# Metrics
57+
58+
# We don't record instance metrics when using redis blaster,
59+
# so we just check for base metrics.
60+
61+
_base_scoped_metrics = (
62+
('Datastore/operation/Redis/get', 1),
63+
('Datastore/operation/Redis/set', 1),
64+
('Datastore/operation/Redis/client_list', 1),
65+
)
66+
67+
_base_rollup_metrics = (
68+
('Datastore/all', 3),
69+
('Datastore/allOther', 3),
70+
('Datastore/Redis/all', 3),
71+
('Datastore/Redis/allOther', 3),
72+
('Datastore/operation/Redis/get', 1),
73+
('Datastore/operation/Redis/set', 1),
74+
('Datastore/operation/Redis/client_list', 1),
75+
)
76+
77+
_disable_scoped_metrics = list(_base_scoped_metrics)
78+
_disable_rollup_metrics = list(_base_rollup_metrics)
79+
80+
_enable_scoped_metrics = list(_base_scoped_metrics)
81+
_enable_rollup_metrics = list(_base_rollup_metrics)
82+
83+
_host = instance_hostname(DB_SETTINGS['host'])
84+
_port = DB_SETTINGS['port']
85+
86+
_instance_metric_name = 'Datastore/instance/Redis/%s/%s' % (_host, _port)
87+
88+
_enable_rollup_metrics.append(
89+
(_instance_metric_name, 3)
90+
)
91+
92+
_disable_rollup_metrics.append(
93+
(_instance_metric_name, None)
94+
)
95+
96+
# Operations
97+
98+
async def exercise_redis(client):
99+
await client.set('key', 'value')
100+
await client.get('key')
101+
await client.execute_command('CLIENT', 'LIST', parse='LIST')
102+
103+
# Tests
104+
105+
@override_application_settings(_enable_instance_settings)
106+
@validate_transaction_metrics(
107+
'test_custom_conn_pool:test_fake_conn_pool_enable_instance',
108+
scoped_metrics=_enable_scoped_metrics,
109+
rollup_metrics=_enable_rollup_metrics,
110+
background_task=True)
111+
@background_task()
112+
def test_fake_conn_pool_enable_instance(loop):
113+
client = aredis.StrictRedis(host=DB_SETTINGS['host'],
114+
port=DB_SETTINGS['port'], db=0)
115+
116+
# Get a real connection
117+
118+
conn = client.connection_pool.get_connection('GET')
119+
120+
# Replace the original connection pool with one that doesn't
121+
# have the `connection_kwargs` attribute.
122+
123+
fake_pool = FakeConnectionPool(conn)
124+
client.connection_pool = fake_pool
125+
assert not hasattr(client.connection_pool, 'connection_kwargs')
126+
127+
loop.run_until_complete(exercise_redis(client))
128+
129+
@override_application_settings(_disable_instance_settings)
130+
@validate_transaction_metrics(
131+
'test_custom_conn_pool:test_fake_conn_pool_disable_instance',
132+
scoped_metrics=_disable_scoped_metrics,
133+
rollup_metrics=_disable_rollup_metrics,
134+
background_task=True)
135+
@background_task()
136+
def test_fake_conn_pool_disable_instance(loop):
137+
client = aredis.StrictRedis(host=DB_SETTINGS['host'],
138+
port=DB_SETTINGS['port'], db=0)
139+
140+
# Get a real connection
141+
142+
conn = client.connection_pool.get_connection('GET')
143+
144+
# Replace the original connection pool with one that doesn't
145+
# have the `connection_kwargs` attribute.
146+
147+
fake_pool = FakeConnectionPool(conn)
148+
client.connection_pool = fake_pool
149+
assert not hasattr(client.connection_pool, 'connection_kwargs')
150+
151+
loop.run_until_complete(exercise_redis(client))

tests/datastore_aredis/test_execute_command.py

+37-1
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,40 @@ def test_strict_redis_execute_command_two_args_enable(loop):
8686
r = aredis.StrictRedis(host=DB_SETTINGS['host'],
8787
port=DB_SETTINGS['port'], db=0)
8888
loop.run_until_complete(exercise_redis_multi_args(r))
89-
89+
90+
@override_application_settings(_disable_instance_settings)
91+
@validate_transaction_metrics(
92+
'test_execute_command:test_strict_redis_execute_command_two_args_disabled',
93+
scoped_metrics=_disable_scoped_metrics,
94+
rollup_metrics=_disable_rollup_metrics,
95+
background_task=True)
96+
@background_task()
97+
def test_strict_redis_execute_command_two_args_disabled(loop):
98+
r = aredis.StrictRedis(host=DB_SETTINGS['host'],
99+
port=DB_SETTINGS['port'], db=0)
100+
loop.run_until_complete(exercise_redis_multi_args(r))
101+
102+
103+
@override_application_settings(_enable_instance_settings)
104+
@validate_transaction_metrics(
105+
'test_execute_command:test_strict_redis_execute_command_as_one_arg_enable',
106+
scoped_metrics=_enable_scoped_metrics,
107+
rollup_metrics=_enable_rollup_metrics,
108+
background_task=True)
109+
@background_task()
110+
def test_strict_redis_execute_command_as_one_arg_enable(loop):
111+
r = aredis.StrictRedis(host=DB_SETTINGS['host'],
112+
port=DB_SETTINGS['port'], db=0)
113+
loop.run_until_complete(exercise_redis_single_arg(r))
114+
115+
@override_application_settings(_disable_instance_settings)
116+
@validate_transaction_metrics(
117+
'test_execute_command:test_strict_redis_execute_command_as_one_arg_disabled',
118+
scoped_metrics=_disable_scoped_metrics,
119+
rollup_metrics=_disable_rollup_metrics,
120+
background_task=True)
121+
@background_task()
122+
def test_strict_redis_execute_command_as_one_arg_disabled(loop):
123+
r = aredis.StrictRedis(host=DB_SETTINGS['host'],
124+
port=DB_SETTINGS['port'], db=0)
125+
loop.run_until_complete(exercise_redis_single_arg(r))
+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Copyright 2010 New Relic, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import aredis
16+
17+
from newrelic.api.background_task import background_task
18+
19+
from testing_support.fixture.event_loop import event_loop as loop
20+
from testing_support.fixtures import (validate_transaction_metrics,
21+
override_application_settings)
22+
from testing_support.db_settings import redis_settings
23+
from testing_support.util import instance_hostname
24+
25+
DB_SETTINGS = redis_settings()[0]
26+
27+
# Settings
28+
29+
_enable_instance_settings = {
30+
'datastore_tracer.instance_reporting.enabled': True,
31+
}
32+
_disable_instance_settings = {
33+
'datastore_tracer.instance_reporting.enabled': False,
34+
}
35+
36+
# Metrics
37+
38+
_base_scoped_metrics = (
39+
('Datastore/operation/Redis/get', 1),
40+
('Datastore/operation/Redis/set', 1),
41+
)
42+
43+
_base_rollup_metrics = (
44+
('Datastore/all', 2),
45+
('Datastore/allOther', 2),
46+
('Datastore/Redis/all', 2),
47+
('Datastore/Redis/allOther', 2),
48+
('Datastore/operation/Redis/get', 1),
49+
('Datastore/operation/Redis/set', 1),
50+
)
51+
52+
_disable_scoped_metrics = list(_base_scoped_metrics)
53+
_disable_rollup_metrics = list(_base_rollup_metrics)
54+
55+
_enable_scoped_metrics = list(_base_scoped_metrics)
56+
_enable_rollup_metrics = list(_base_rollup_metrics)
57+
58+
_host = instance_hostname(DB_SETTINGS['host'])
59+
_port = DB_SETTINGS['port']
60+
61+
_instance_metric_name = 'Datastore/instance/Redis/%s/%s' % (_host, _port)
62+
63+
_enable_rollup_metrics.append(
64+
(_instance_metric_name, 2)
65+
)
66+
67+
_disable_rollup_metrics.append(
68+
(_instance_metric_name, None)
69+
)
70+
71+
# Operations
72+
73+
async def exercise_redis(client):
74+
await client.set('key', 'value')
75+
await client.get('key')
76+
77+
# Tests
78+
79+
@override_application_settings(_enable_instance_settings)
80+
@validate_transaction_metrics(
81+
'test_get_and_set:test_strict_redis_operation_enable_instance',
82+
scoped_metrics=_enable_scoped_metrics,
83+
rollup_metrics=_enable_rollup_metrics,
84+
background_task=True)
85+
@background_task()
86+
def test_strict_redis_operation_enable_instance(loop):
87+
client = aredis.StrictRedis(host=DB_SETTINGS['host'],
88+
port=DB_SETTINGS['port'], db=0)
89+
loop.run_until_complete(exercise_redis(client))
90+
91+
@override_application_settings(_disable_instance_settings)
92+
@validate_transaction_metrics(
93+
'test_get_and_set:test_strict_redis_operation_disable_instance',
94+
scoped_metrics=_disable_scoped_metrics,
95+
rollup_metrics=_disable_rollup_metrics,
96+
background_task=True)
97+
@background_task()
98+
def test_strict_redis_operation_disable_instance(loop):
99+
client = aredis.StrictRedis(host=DB_SETTINGS['host'],
100+
port=DB_SETTINGS['port'], db=0)
101+
loop.run_until_complete(exercise_redis(client))

0 commit comments

Comments
 (0)