1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- ''' The purpose of these tests is to confirm that using a non-standard
15+ """ The purpose of these tests is to confirm that using a non-standard
1616connection pool that does not have a `connection_kwargs` attribute
1717will not result in an error.
18- '''
18+ """
1919
2020import pytest
2121import redis
22-
23- from newrelic .api .background_task import background_task
24- from newrelic .common .package_version_utils import get_package_version_tuple
25-
26- from testing_support .fixtures import override_application_settings
27- from testing_support .validators .validate_transaction_metrics import validate_transaction_metrics
2822from testing_support .db_settings import redis_settings
23+ from testing_support .fixtures import override_application_settings
2924from testing_support .util import instance_hostname
25+ from testing_support .validators .validate_transaction_metrics import (
26+ validate_transaction_metrics ,
27+ )
3028
29+ from newrelic .api .background_task import background_task
30+ from newrelic .common .package_version_utils import get_package_version_tuple
3131
3232DB_SETTINGS = redis_settings ()[0 ]
3333REDIS_PY_VERSION = get_package_version_tuple ("redis" )
@@ -45,13 +45,17 @@ def get_connection(self, name, *keys, **options):
4545 def release (self , connection ):
4646 self .connection .disconnect ()
4747
48+ def disconnect (self ):
49+ self .connection .disconnect ()
50+
51+
4852# Settings
4953
5054_enable_instance_settings = {
51- ' datastore_tracer.instance_reporting.enabled' : True ,
55+ " datastore_tracer.instance_reporting.enabled" : True ,
5256}
5357_disable_instance_settings = {
54- ' datastore_tracer.instance_reporting.enabled' : False ,
58+ " datastore_tracer.instance_reporting.enabled" : False ,
5559}
5660
5761# Metrics
@@ -61,98 +65,100 @@ def release(self, connection):
6165datastore_all_metric_count = 5 if REDIS_PY_VERSION >= (5 , 0 ) else 3
6266
6367_base_scoped_metrics = [
64- ( ' Datastore/operation/Redis/get' , 1 ),
65- ( ' Datastore/operation/Redis/set' , 1 ),
66- ( ' Datastore/operation/Redis/client_list' , 1 ),
68+ ( " Datastore/operation/Redis/get" , 1 ),
69+ ( " Datastore/operation/Redis/set" , 1 ),
70+ ( " Datastore/operation/Redis/client_list" , 1 ),
6771]
6872# client_setinfo was introduced in v5.0.0 and assigns info displayed in client_list output
6973if REDIS_PY_VERSION >= (5 , 0 ):
70- _base_scoped_metrics .append (('Datastore/operation/Redis/client_setinfo' , 2 ),)
74+ _base_scoped_metrics .append (
75+ ("Datastore/operation/Redis/client_setinfo" , 2 ),
76+ )
7177
7278_base_rollup_metrics = [
73- ( ' Datastore/all' , datastore_all_metric_count ),
74- ( ' Datastore/allOther' , datastore_all_metric_count ),
75- ( ' Datastore/Redis/all' , datastore_all_metric_count ),
76- ( ' Datastore/Redis/allOther' , datastore_all_metric_count ),
77- ( ' Datastore/operation/Redis/get' , 1 ),
78- ( ' Datastore/operation/Redis/set' , 1 ),
79- ( ' Datastore/operation/Redis/client_list' , 1 ),
79+ ( " Datastore/all" , datastore_all_metric_count ),
80+ ( " Datastore/allOther" , datastore_all_metric_count ),
81+ ( " Datastore/Redis/all" , datastore_all_metric_count ),
82+ ( " Datastore/Redis/allOther" , datastore_all_metric_count ),
83+ ( " Datastore/operation/Redis/get" , 1 ),
84+ ( " Datastore/operation/Redis/set" , 1 ),
85+ ( " Datastore/operation/Redis/client_list" , 1 ),
8086]
8187if REDIS_PY_VERSION >= (5 , 0 ):
82- _base_rollup_metrics .append (('Datastore/operation/Redis/client_setinfo' , 2 ),)
88+ _base_rollup_metrics .append (
89+ ("Datastore/operation/Redis/client_setinfo" , 2 ),
90+ )
8391
84- _host = instance_hostname (DB_SETTINGS [' host' ])
85- _port = DB_SETTINGS [' port' ]
92+ _host = instance_hostname (DB_SETTINGS [" host" ])
93+ _port = DB_SETTINGS [" port" ]
8694
87- _instance_metric_name = ' Datastore/instance/Redis/%s/%s' % (_host , _port )
95+ _instance_metric_name = " Datastore/instance/Redis/%s/%s" % (_host , _port )
8896
8997instance_metric_count = 5 if REDIS_PY_VERSION >= (5 , 0 ) else 3
9098
91- _enable_rollup_metrics = _base_rollup_metrics .append (
92- (_instance_metric_name , instance_metric_count )
93- )
99+ _enable_rollup_metrics = _base_rollup_metrics .append ((_instance_metric_name , instance_metric_count ))
94100
95- _disable_rollup_metrics = _base_rollup_metrics .append (
96- (_instance_metric_name , None )
97- )
101+ _disable_rollup_metrics = _base_rollup_metrics .append ((_instance_metric_name , None ))
98102
99103# Operations
100104
105+
101106def exercise_redis (client ):
102- client .set ('key' , 'value' )
103- client .get ('key' )
104- client .execute_command ('CLIENT' , 'LIST' , parse = 'LIST' )
107+ client .set ("key" , "value" )
108+ client .get ("key" )
109+ client .execute_command ("CLIENT" , "LIST" , parse = "LIST" )
110+
105111
106112# Tests
107113
108- @ pytest . mark . skipif ( REDIS_PY_VERSION < ( 2 , 7 ),
109- reason = ' Client list command introduced in 2.7' )
114+
115+ @ pytest . mark . skipif ( REDIS_PY_VERSION < ( 2 , 7 ), reason = " Client list command introduced in 2.7" )
110116@override_application_settings (_enable_instance_settings )
111117@validate_transaction_metrics (
112- 'test_custom_conn_pool:test_fake_conn_pool_enable_instance' ,
113- scoped_metrics = _base_scoped_metrics ,
114- rollup_metrics = _enable_rollup_metrics ,
115- background_task = True )
118+ "test_custom_conn_pool:test_fake_conn_pool_enable_instance" ,
119+ scoped_metrics = _base_scoped_metrics ,
120+ rollup_metrics = _enable_rollup_metrics ,
121+ background_task = True ,
122+ )
116123@background_task ()
117124def test_fake_conn_pool_enable_instance ():
118- client = redis .StrictRedis (host = DB_SETTINGS ['host' ],
119- port = DB_SETTINGS ['port' ], db = 0 )
125+ client = redis .StrictRedis (host = DB_SETTINGS ["host" ], port = DB_SETTINGS ["port" ], db = 0 )
120126
121127 # Get a real connection
122128
123- conn = client .connection_pool .get_connection (' GET' )
129+ conn = client .connection_pool .get_connection (" GET" )
124130
125131 # Replace the original connection pool with one that doesn't
126132 # have the `connection_kwargs` attribute.
127133
128134 fake_pool = FakeConnectionPool (conn )
129135 client .connection_pool = fake_pool
130- assert not hasattr (client .connection_pool , ' connection_kwargs' )
136+ assert not hasattr (client .connection_pool , " connection_kwargs" )
131137
132138 exercise_redis (client )
133139
134- @ pytest . mark . skipif ( REDIS_PY_VERSION < ( 2 , 7 ),
135- reason = ' Client list command introduced in 2.7' )
140+
141+ @ pytest . mark . skipif ( REDIS_PY_VERSION < ( 2 , 7 ), reason = " Client list command introduced in 2.7" )
136142@override_application_settings (_disable_instance_settings )
137143@validate_transaction_metrics (
138- 'test_custom_conn_pool:test_fake_conn_pool_disable_instance' ,
139- scoped_metrics = _base_scoped_metrics ,
140- rollup_metrics = _disable_rollup_metrics ,
141- background_task = True )
144+ "test_custom_conn_pool:test_fake_conn_pool_disable_instance" ,
145+ scoped_metrics = _base_scoped_metrics ,
146+ rollup_metrics = _disable_rollup_metrics ,
147+ background_task = True ,
148+ )
142149@background_task ()
143150def test_fake_conn_pool_disable_instance ():
144- client = redis .StrictRedis (host = DB_SETTINGS ['host' ],
145- port = DB_SETTINGS ['port' ], db = 0 )
151+ client = redis .StrictRedis (host = DB_SETTINGS ["host" ], port = DB_SETTINGS ["port" ], db = 0 )
146152
147153 # Get a real connection
148154
149- conn = client .connection_pool .get_connection (' GET' )
155+ conn = client .connection_pool .get_connection (" GET" )
150156
151157 # Replace the original connection pool with one that doesn't
152158 # have the `connection_kwargs` attribute.
153159
154160 fake_pool = FakeConnectionPool (conn )
155161 client .connection_pool = fake_pool
156- assert not hasattr (client .connection_pool , ' connection_kwargs' )
162+ assert not hasattr (client .connection_pool , " connection_kwargs" )
157163
158164 exercise_redis (client )
0 commit comments