Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions pymemcache/test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,35 @@ def test_incr_decr():
assert client.get(b"k") == 4


@pytest.mark.unit()
def test_default_noreply():
client = MockMemcacheClient(default_noreply=False)

assert client.add(b"k", 2) is True
assert client.add(b"k", 25) is False
assert client.delete(b"k") is True
assert client.delete(b"k") is False


@pytest.mark.unit()
def test_default_noreply_true():
client = MockMemcacheClient(default_noreply=True)

assert client.add(b"k", 2) is True
assert client.add(b"k", 25) is True
assert client.delete(b"k") is True
assert client.delete(b"missing") is True


@pytest.mark.unit()
def test_noreply_arg_overrides_default():
client = MockMemcacheClient(default_noreply=True)

assert client.add(b"k", 2, noreply=False) is True
assert client.add(b"k", 25, noreply=False) is False
assert client.delete(b"missing", noreply=False) is False


@pytest.mark.unit()
def test_prepand_append():
client = MockMemcacheClient()
Expand Down
37 changes: 28 additions & 9 deletions pymemcache/test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def _serializer(key, value):

self.serde = serde or LegacyWrappingSerde(serializer, deserializer)
self.allow_unicode_keys = allow_unicode_keys
self.default_noreply = default_noreply

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

get_multi = get_many

def set(self, key, value, expire=0, noreply=True, flags=None):
def set(self, key, value, expire=0, noreply=None, flags=None):
if noreply is None:
noreply = self.default_noreply
key = self.check_key(key)
if isinstance(value, str) and not isinstance(value, bytes):
try:
Expand All @@ -108,7 +111,9 @@ def set(self, key, value, expire=0, noreply=True, flags=None):
self._contents[key] = expire, value, flags
return True

def set_many(self, values, expire=0, noreply=True, flags=None):
def set_many(self, values, expire=0, noreply=None, flags=None):
if noreply is None:
noreply = self.default_noreply
result = []
for key, value in values.items():
ret = self.set(key, value, expire, noreply, flags=flags)
Expand All @@ -132,25 +137,33 @@ def decr(self, key, value, noreply=False):
self.set(key, current - value, noreply=noreply)
return None if noreply or not present else current - value

def add(self, key, value, expire=0, noreply=True, flags=None):
def add(self, key, value, expire=0, noreply=None, flags=None):
if noreply is None:
noreply = self.default_noreply
current = self.get(key)
present = current is not None
if not present:
self.set(key, value, expire, noreply, flags=flags)
return noreply or not present

def delete(self, key, noreply=True):
def delete(self, key, noreply=None):
if noreply is None:
noreply = self.default_noreply
key = self.check_key(key)
current = self._contents.pop(key, None)
present = current is not None
return noreply or present

def delete_many(self, keys, noreply=True):
def delete_many(self, keys, noreply=None):
if noreply is None:
noreply = self.default_noreply
for key in keys:
self.delete(key, noreply)
return True

def prepend(self, key, value, expire=0, noreply=True, flags=None):
def prepend(self, key, value, expire=0, noreply=None, flags=None):
if noreply is None:
noreply = self.default_noreply
current = self.get(key)
if current is not None:
if isinstance(value, str) and not isinstance(value, bytes):
Expand All @@ -161,7 +174,9 @@ def prepend(self, key, value, expire=0, noreply=True, flags=None):
self.set(key, value + current, expire, noreply, flags=flags)
return True

def append(self, key, value, expire=0, noreply=True, flags=None):
def append(self, key, value, expire=0, noreply=None, flags=None):
if noreply is None:
noreply = self.default_noreply
current = self.get(key)
if current is not None:
if isinstance(value, str) and not isinstance(value, bytes):
Expand Down Expand Up @@ -206,7 +221,9 @@ def replace(self, key, value, expire=0, noreply=True, flags=None):
def cas(self, key, value, cas, expire=0, noreply=False, flags=None):
raise MemcacheClientError("CAS is not enabled for this instance")

def touch(self, key, expire=0, noreply=True):
def touch(self, key, expire=0, noreply=None):
if noreply is None:
noreply = self.default_noreply
current = self.get(key)
present = current is not None
if present:
Expand All @@ -219,7 +236,9 @@ def cache_memlimit(self, memlimit):
def version(self):
return "MockMemcacheClient"

def flush_all(self, delay=0, noreply=True):
def flush_all(self, delay=0, noreply=None):
if noreply is None:
noreply = self.default_noreply
self.clear()

return noreply or self._contents == {}
Expand Down
Loading