/usr/lib/python3/dist-packages/pymemcache/test/utils.py is in python3-pymemcache 1.2.4-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | """
Useful testing utilities.
This module is considered public API.
"""
import time
import six
from pymemcache.client import MemcacheIllegalInputError
class MockMemcacheClient(object):
"""
A (partial) in-memory mock for Clients.
"""
def __init__(self,
server=None,
serializer=None,
deserializer=None,
connect_timeout=None,
timeout=None,
no_delay=False,
ignore_exc=False):
self._contents = {}
self.serializer = serializer
self.deserializer = deserializer
# Unused, but present for interface compatibility
self.server = server
self.connect_timeout = connect_timeout
self.timeout = timeout
self.no_delay = no_delay
self.ignore_exc = ignore_exc
def get(self, key):
if isinstance(key, six.text_type):
raise MemcacheIllegalInputError(key)
if key not in self._contents:
return None
expire, value, was_serialized = self._contents[key]
if expire and expire < time.time():
del self._contents[key]
return None
if self.deserializer:
return self.deserializer(key, value, 2 if was_serialized else 1)
return value
def get_many(self, keys):
out = {}
for key in keys:
value = self.get(key)
if value is not None:
out[key] = value
return out
def set(self, key, value, expire=0, noreply=True):
if isinstance(key, six.text_type):
raise MemcacheIllegalInputError(key)
if isinstance(value, six.text_type):
raise MemcacheIllegalInputError(value)
was_serialized = False
if self.serializer:
value = self.serializer(key, value)
if expire:
expire += time.time()
self._contents[key] = expire, value, was_serialized
return True
def set_many(self, values, expire=None, noreply=True):
for key, value in six.iteritems(values):
self.set(key, value, expire, noreply)
return True
def incr(self, key, value, noreply=False):
current = self.get(key)
present = current is not None
if present:
self.set(key, current + value, noreply=noreply)
return None if noreply or not present else current + value
def decr(self, key, value, noreply=False):
current = self.get(key)
if current is None:
return
self.set(key, current - value, noreply=noreply)
return current - value
def add(self, key, value, expire=None, noreply=True):
current = self.get(key)
present = current is not None
if not present:
self.set(key, value, expire, noreply)
return not present
def delete(self, key, noreply=True):
current = self._contents.pop(key, None)
present = current is not None
return noreply or present
def stats(self):
# I make no claim that these values make any sense, but the format
# of the output is the same as for pymemcache.client.Client.stats()
return {
"version": "MockMemcacheClient",
"rusage_user": 1.0,
"rusage_system": 1.0,
"hash_is_expanding": False,
"slab_reassign_running": False,
"inter": "in-memory",
"evictions": False,
"growth_factor": 1.0,
"stat_key_prefix": "",
"umask": 0o644,
"detail_enabled": False,
"cas_enabled": False,
"auth_enabled_sasl": False,
"maxconns_fast": False,
"slab_reassign": False,
"slab_automove": False,
}
|