Skip to content

Commit

Permalink
Add aredis testing. (#489)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
3 people committed Feb 25, 2022
1 parent ec02b06 commit c821354
Show file tree
Hide file tree
Showing 7 changed files with 789 additions and 1 deletion.
151 changes: 151 additions & 0 deletions tests/datastore_aredis/test_custom_conn_pool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# Copyright 2010 New Relic, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

''' The purpose of these tests is to confirm that using a non-standard
connection pool that does not have a `connection_kwargs` attribute
will not result in an error.
'''

import pytest
import aredis

from newrelic.api.background_task import background_task

from testing_support.fixture.event_loop import event_loop as loop
from testing_support.fixtures import (validate_transaction_metrics,
override_application_settings)
from testing_support.db_settings import redis_settings
from testing_support.util import instance_hostname

DB_SETTINGS = redis_settings()[0]
REDIS_PY_VERSION = aredis.VERSION


class FakeConnectionPool(object):
"""Connection Pool without connection_kwargs attribute."""

def __init__(self, connection):
self.connection = connection

def get_connection(self, name=None, *keys, **options):
return self.connection

def release(self, connection):
self.connection.disconnect()

# Settings

_enable_instance_settings = {
'datastore_tracer.instance_reporting.enabled': True,
}
_disable_instance_settings = {
'datastore_tracer.instance_reporting.enabled': False,
}

# Metrics

# We don't record instance metrics when using redis blaster,
# so we just check for base metrics.

_base_scoped_metrics = (
('Datastore/operation/Redis/get', 1),
('Datastore/operation/Redis/set', 1),
('Datastore/operation/Redis/client_list', 1),
)

_base_rollup_metrics = (
('Datastore/all', 3),
('Datastore/allOther', 3),
('Datastore/Redis/all', 3),
('Datastore/Redis/allOther', 3),
('Datastore/operation/Redis/get', 1),
('Datastore/operation/Redis/set', 1),
('Datastore/operation/Redis/client_list', 1),
)

_disable_scoped_metrics = list(_base_scoped_metrics)
_disable_rollup_metrics = list(_base_rollup_metrics)

_enable_scoped_metrics = list(_base_scoped_metrics)
_enable_rollup_metrics = list(_base_rollup_metrics)

_host = instance_hostname(DB_SETTINGS['host'])
_port = DB_SETTINGS['port']

_instance_metric_name = 'Datastore/instance/Redis/%s/%s' % (_host, _port)

_enable_rollup_metrics.append(
(_instance_metric_name, 3)
)

_disable_rollup_metrics.append(
(_instance_metric_name, None)
)

# Operations

async def exercise_redis(client):
await client.set('key', 'value')
await client.get('key')
await client.execute_command('CLIENT', 'LIST', parse='LIST')

# Tests

@override_application_settings(_enable_instance_settings)
@validate_transaction_metrics(
'test_custom_conn_pool:test_fake_conn_pool_enable_instance',
scoped_metrics=_enable_scoped_metrics,
rollup_metrics=_enable_rollup_metrics,
background_task=True)
@background_task()
def test_fake_conn_pool_enable_instance(loop):
client = aredis.StrictRedis(host=DB_SETTINGS['host'],
port=DB_SETTINGS['port'], db=0)

# Get a real connection

conn = client.connection_pool.get_connection('GET')

# Replace the original connection pool with one that doesn't
# have the `connection_kwargs` attribute.

fake_pool = FakeConnectionPool(conn)
client.connection_pool = fake_pool
assert not hasattr(client.connection_pool, 'connection_kwargs')

loop.run_until_complete(exercise_redis(client))

@override_application_settings(_disable_instance_settings)
@validate_transaction_metrics(
'test_custom_conn_pool:test_fake_conn_pool_disable_instance',
scoped_metrics=_disable_scoped_metrics,
rollup_metrics=_disable_rollup_metrics,
background_task=True)
@background_task()
def test_fake_conn_pool_disable_instance(loop):
client = aredis.StrictRedis(host=DB_SETTINGS['host'],
port=DB_SETTINGS['port'], db=0)

# Get a real connection

conn = client.connection_pool.get_connection('GET')

# Replace the original connection pool with one that doesn't
# have the `connection_kwargs` attribute.

fake_pool = FakeConnectionPool(conn)
client.connection_pool = fake_pool
assert not hasattr(client.connection_pool, 'connection_kwargs')

loop.run_until_complete(exercise_redis(client))
38 changes: 37 additions & 1 deletion tests/datastore_aredis/test_execute_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,40 @@ def test_strict_redis_execute_command_two_args_enable(loop):
r = aredis.StrictRedis(host=DB_SETTINGS['host'],
port=DB_SETTINGS['port'], db=0)
loop.run_until_complete(exercise_redis_multi_args(r))


@override_application_settings(_disable_instance_settings)
@validate_transaction_metrics(
'test_execute_command:test_strict_redis_execute_command_two_args_disabled',
scoped_metrics=_disable_scoped_metrics,
rollup_metrics=_disable_rollup_metrics,
background_task=True)
@background_task()
def test_strict_redis_execute_command_two_args_disabled(loop):
r = aredis.StrictRedis(host=DB_SETTINGS['host'],
port=DB_SETTINGS['port'], db=0)
loop.run_until_complete(exercise_redis_multi_args(r))


@override_application_settings(_enable_instance_settings)
@validate_transaction_metrics(
'test_execute_command:test_strict_redis_execute_command_as_one_arg_enable',
scoped_metrics=_enable_scoped_metrics,
rollup_metrics=_enable_rollup_metrics,
background_task=True)
@background_task()
def test_strict_redis_execute_command_as_one_arg_enable(loop):
r = aredis.StrictRedis(host=DB_SETTINGS['host'],
port=DB_SETTINGS['port'], db=0)
loop.run_until_complete(exercise_redis_single_arg(r))

@override_application_settings(_disable_instance_settings)
@validate_transaction_metrics(
'test_execute_command:test_strict_redis_execute_command_as_one_arg_disabled',
scoped_metrics=_disable_scoped_metrics,
rollup_metrics=_disable_rollup_metrics,
background_task=True)
@background_task()
def test_strict_redis_execute_command_as_one_arg_disabled(loop):
r = aredis.StrictRedis(host=DB_SETTINGS['host'],
port=DB_SETTINGS['port'], db=0)
loop.run_until_complete(exercise_redis_single_arg(r))
101 changes: 101 additions & 0 deletions tests/datastore_aredis/test_get_and_set.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Copyright 2010 New Relic, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import aredis

from newrelic.api.background_task import background_task

from testing_support.fixture.event_loop import event_loop as loop
from testing_support.fixtures import (validate_transaction_metrics,
override_application_settings)
from testing_support.db_settings import redis_settings
from testing_support.util import instance_hostname

DB_SETTINGS = redis_settings()[0]

# Settings

_enable_instance_settings = {
'datastore_tracer.instance_reporting.enabled': True,
}
_disable_instance_settings = {
'datastore_tracer.instance_reporting.enabled': False,
}

# Metrics

_base_scoped_metrics = (
('Datastore/operation/Redis/get', 1),
('Datastore/operation/Redis/set', 1),
)

_base_rollup_metrics = (
('Datastore/all', 2),
('Datastore/allOther', 2),
('Datastore/Redis/all', 2),
('Datastore/Redis/allOther', 2),
('Datastore/operation/Redis/get', 1),
('Datastore/operation/Redis/set', 1),
)

_disable_scoped_metrics = list(_base_scoped_metrics)
_disable_rollup_metrics = list(_base_rollup_metrics)

_enable_scoped_metrics = list(_base_scoped_metrics)
_enable_rollup_metrics = list(_base_rollup_metrics)

_host = instance_hostname(DB_SETTINGS['host'])
_port = DB_SETTINGS['port']

_instance_metric_name = 'Datastore/instance/Redis/%s/%s' % (_host, _port)

_enable_rollup_metrics.append(
(_instance_metric_name, 2)
)

_disable_rollup_metrics.append(
(_instance_metric_name, None)
)

# Operations

async def exercise_redis(client):
await client.set('key', 'value')
await client.get('key')

# Tests

@override_application_settings(_enable_instance_settings)
@validate_transaction_metrics(
'test_get_and_set:test_strict_redis_operation_enable_instance',
scoped_metrics=_enable_scoped_metrics,
rollup_metrics=_enable_rollup_metrics,
background_task=True)
@background_task()
def test_strict_redis_operation_enable_instance(loop):
client = aredis.StrictRedis(host=DB_SETTINGS['host'],
port=DB_SETTINGS['port'], db=0)
loop.run_until_complete(exercise_redis(client))

@override_application_settings(_disable_instance_settings)
@validate_transaction_metrics(
'test_get_and_set:test_strict_redis_operation_disable_instance',
scoped_metrics=_disable_scoped_metrics,
rollup_metrics=_disable_rollup_metrics,
background_task=True)
@background_task()
def test_strict_redis_operation_disable_instance(loop):
client = aredis.StrictRedis(host=DB_SETTINGS['host'],
port=DB_SETTINGS['port'], db=0)
loop.run_until_complete(exercise_redis(client))
Loading

0 comments on commit c821354

Please sign in to comment.