|
| 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)) |
0 commit comments