Skip to content

Commit 50101e7

Browse files
committed
MockMemcacheClient: add missing noreply, default_noreply support
add noreply support to many methods, and add default_noreply throughout
1 parent 3ef28ef commit 50101e7

2 files changed

Lines changed: 57 additions & 9 deletions

File tree

pymemcache/test/test_utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,35 @@ def test_incr_decr():
124124
assert client.get(b"k") == 4
125125

126126

127+
@pytest.mark.unit()
128+
def test_default_noreply():
129+
client = MockMemcacheClient(default_noreply=False)
130+
131+
assert client.add(b"k", 2) is True
132+
assert client.add(b"k", 25) is False
133+
assert client.delete(b"k") is True
134+
assert client.delete(b"k") is False
135+
136+
137+
@pytest.mark.unit()
138+
def test_default_noreply_true():
139+
client = MockMemcacheClient(default_noreply=True)
140+
141+
assert client.add(b"k", 2) is True
142+
assert client.add(b"k", 25) is True
143+
assert client.delete(b"k") is True
144+
assert client.delete(b"missing") is True
145+
146+
147+
@pytest.mark.unit()
148+
def test_noreply_arg_overrides_default():
149+
client = MockMemcacheClient(default_noreply=True)
150+
151+
assert client.add(b"k", 2, noreply=False) is True
152+
assert client.add(b"k", 25, noreply=False) is False
153+
assert client.delete(b"missing", noreply=False) is False
154+
155+
127156
@pytest.mark.unit()
128157
def test_prepand_append():
129158
client = MockMemcacheClient()

pymemcache/test/utils.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def _serializer(key, value):
4949

5050
self.serde = serde or LegacyWrappingSerde(serializer, deserializer)
5151
self.allow_unicode_keys = allow_unicode_keys
52+
self.default_noreply = default_noreply
5253

5354
# Unused, but present for interface compatibility
5455
self.server = server
@@ -92,7 +93,9 @@ def get_many(self, keys):
9293

9394
get_multi = get_many
9495

95-
def set(self, key, value, expire=0, noreply=True, flags=None):
96+
def set(self, key, value, expire=0, noreply=None, flags=None):
97+
if noreply is None:
98+
noreply = self.default_noreply
9699
key = self.check_key(key)
97100
if isinstance(value, str) and not isinstance(value, bytes):
98101
try:
@@ -108,7 +111,9 @@ def set(self, key, value, expire=0, noreply=True, flags=None):
108111
self._contents[key] = expire, value, flags
109112
return True
110113

111-
def set_many(self, values, expire=0, noreply=True, flags=None):
114+
def set_many(self, values, expire=0, noreply=None, flags=None):
115+
if noreply is None:
116+
noreply = self.default_noreply
112117
result = []
113118
for key, value in values.items():
114119
ret = self.set(key, value, expire, noreply, flags=flags)
@@ -132,25 +137,33 @@ def decr(self, key, value, noreply=False):
132137
self.set(key, current - value, noreply=noreply)
133138
return None if noreply or not present else current - value
134139

135-
def add(self, key, value, expire=0, noreply=True, flags=None):
140+
def add(self, key, value, expire=0, noreply=None, flags=None):
141+
if noreply is None:
142+
noreply = self.default_noreply
136143
current = self.get(key)
137144
present = current is not None
138145
if not present:
139146
self.set(key, value, expire, noreply, flags=flags)
140147
return noreply or not present
141148

142-
def delete(self, key, noreply=True):
149+
def delete(self, key, noreply=None):
150+
if noreply is None:
151+
noreply = self.default_noreply
143152
key = self.check_key(key)
144153
current = self._contents.pop(key, None)
145154
present = current is not None
146155
return noreply or present
147156

148-
def delete_many(self, keys, noreply=True):
157+
def delete_many(self, keys, noreply=None):
158+
if noreply is None:
159+
noreply = self.default_noreply
149160
for key in keys:
150161
self.delete(key, noreply)
151162
return True
152163

153-
def prepend(self, key, value, expire=0, noreply=True, flags=None):
164+
def prepend(self, key, value, expire=0, noreply=None, flags=None):
165+
if noreply is None:
166+
noreply = self.default_noreply
154167
current = self.get(key)
155168
if current is not None:
156169
if isinstance(value, str) and not isinstance(value, bytes):
@@ -161,7 +174,9 @@ def prepend(self, key, value, expire=0, noreply=True, flags=None):
161174
self.set(key, value + current, expire, noreply, flags=flags)
162175
return True
163176

164-
def append(self, key, value, expire=0, noreply=True, flags=None):
177+
def append(self, key, value, expire=0, noreply=None, flags=None):
178+
if noreply is None:
179+
noreply = self.default_noreply
165180
current = self.get(key)
166181
if current is not None:
167182
if isinstance(value, str) and not isinstance(value, bytes):
@@ -206,7 +221,9 @@ def replace(self, key, value, expire=0, noreply=True, flags=None):
206221
def cas(self, key, value, cas, expire=0, noreply=False, flags=None):
207222
raise MemcacheClientError("CAS is not enabled for this instance")
208223

209-
def touch(self, key, expire=0, noreply=True):
224+
def touch(self, key, expire=0, noreply=None):
225+
if noreply is None:
226+
noreply = self.default_noreply
210227
current = self.get(key)
211228
present = current is not None
212229
if present:
@@ -219,7 +236,9 @@ def cache_memlimit(self, memlimit):
219236
def version(self):
220237
return "MockMemcacheClient"
221238

222-
def flush_all(self, delay=0, noreply=True):
239+
def flush_all(self, delay=0, noreply=None):
240+
if noreply is None:
241+
noreply = self.default_noreply
223242
self.clear()
224243

225244
return noreply or self._contents == {}

0 commit comments

Comments
 (0)