|
| 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 os |
| 16 | + |
| 17 | +import aiomcache |
| 18 | +from testing_support.db_settings import memcached_settings |
| 19 | +from testing_support.validators.validate_transaction_metrics import ( |
| 20 | + validate_transaction_metrics, |
| 21 | +) |
| 22 | + |
| 23 | +from newrelic.api.background_task import background_task |
| 24 | +from newrelic.api.transaction import set_background_task |
| 25 | + |
| 26 | +from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics |
| 27 | +from testing_support.fixture.event_loop import event_loop as loop |
| 28 | + |
| 29 | +DB_SETTINGS = memcached_settings()[0] |
| 30 | + |
| 31 | +MEMCACHED_HOST = DB_SETTINGS["host"] |
| 32 | +MEMCACHED_PORT = DB_SETTINGS["port"] |
| 33 | +MEMCACHED_NAMESPACE = str(os.getpid()) |
| 34 | + |
| 35 | +_test_bt_set_get_delete_scoped_metrics = [ |
| 36 | + ("Datastore/operation/Memcached/set", 1), |
| 37 | + ("Datastore/operation/Memcached/get", 1), |
| 38 | + ("Datastore/operation/Memcached/delete", 1), |
| 39 | +] |
| 40 | + |
| 41 | +_test_bt_set_get_delete_rollup_metrics = [ |
| 42 | + ("Datastore/all", 3), |
| 43 | + ("Datastore/allOther", 3), |
| 44 | + ("Datastore/Memcached/all", 3), |
| 45 | + ("Datastore/Memcached/allOther", 3), |
| 46 | + ("Datastore/operation/Memcached/set", 1), |
| 47 | + ("Datastore/operation/Memcached/get", 1), |
| 48 | + ("Datastore/operation/Memcached/delete", 1), |
| 49 | +] |
| 50 | + |
| 51 | + |
| 52 | +@validate_transaction_metrics( |
| 53 | + "test_aiomcache:test_bt_set_get_delete", |
| 54 | + scoped_metrics=_test_bt_set_get_delete_scoped_metrics, |
| 55 | + rollup_metrics=_test_bt_set_get_delete_rollup_metrics, |
| 56 | + background_task=True, |
| 57 | +) |
| 58 | +@background_task() |
| 59 | +def test_bt_set_get_delete(loop): |
| 60 | + set_background_task(True) |
| 61 | + client = aiomcache.Client(host=MEMCACHED_HOST, port=MEMCACHED_PORT) |
| 62 | + |
| 63 | + key = (MEMCACHED_NAMESPACE + "key").encode() |
| 64 | + data = "value".encode() |
| 65 | + |
| 66 | + loop.run_until_complete(client.set(key, data)) |
| 67 | + value = loop.run_until_complete(client.get(key)) |
| 68 | + loop.run_until_complete(client.delete(key)) |
| 69 | + |
| 70 | + assert value == data |
| 71 | + |
| 72 | + |
| 73 | +_test_wt_set_get_delete_scoped_metrics = [ |
| 74 | + ("Datastore/operation/Memcached/set", 1), |
| 75 | + ("Datastore/operation/Memcached/get", 1), |
| 76 | + ("Datastore/operation/Memcached/delete", 1), |
| 77 | +] |
| 78 | + |
| 79 | +_test_wt_set_get_delete_rollup_metrics = [ |
| 80 | + ("Datastore/all", 3), |
| 81 | + ("Datastore/allWeb", 3), |
| 82 | + ("Datastore/Memcached/all", 3), |
| 83 | + ("Datastore/Memcached/allWeb", 3), |
| 84 | + ("Datastore/operation/Memcached/set", 1), |
| 85 | + ("Datastore/operation/Memcached/get", 1), |
| 86 | + ("Datastore/operation/Memcached/delete", 1), |
| 87 | +] |
| 88 | + |
| 89 | + |
| 90 | +@validate_transaction_metrics( |
| 91 | + "test_aiomcache:test_wt_set_get_delete", |
| 92 | + scoped_metrics=_test_wt_set_get_delete_scoped_metrics, |
| 93 | + rollup_metrics=_test_wt_set_get_delete_rollup_metrics, |
| 94 | + background_task=False, |
| 95 | +) |
| 96 | +@background_task() |
| 97 | +def test_wt_set_get_delete(loop): |
| 98 | + set_background_task(False) |
| 99 | + client = aiomcache.Client(host=MEMCACHED_HOST, port=MEMCACHED_PORT) |
| 100 | + |
| 101 | + key = (MEMCACHED_NAMESPACE + "key").encode() |
| 102 | + data = "value".encode() |
| 103 | + |
| 104 | + loop.run_until_complete(client.set(key, data)) |
| 105 | + value = loop.run_until_complete(client.get(key)) |
| 106 | + loop.run_until_complete(client.delete(key)) |
| 107 | + |
| 108 | + assert value == data |
0 commit comments