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
12 changes: 8 additions & 4 deletions pymemcache/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1552,13 +1552,17 @@ def gat(self, key: Key, expire: int = 0, default: Optional[Any] = None) -> Any:
else:
raise

def gats(self, key: Key, expire: int = 0, default: Optional[Any] = None) -> Any:
def gats(
self, key: Key, expire: int = 0, default: Any = None, cas_default: Any = None
) -> tuple[Any, Any]:
with self.client_pool.get_and_release(destroy_on_fail=True) as client:
try:
return client.gats(key, expire, default)
return client.gats(
key, expire=expire, default=default, cas_default=cas_default
)
except Exception:
if self.ignore_exc:
return default
return (default, cas_default)
else:
raise

Expand All @@ -1582,7 +1586,7 @@ def gets(
return client.gets(key, default=default, cas_default=cas_default)
except Exception:
if self.ignore_exc:
return (None, None)
return (default, cas_default)
else:
raise

Expand Down
33 changes: 33 additions & 0 deletions pymemcache/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,39 @@ def test_gets_found(self):
result = client.gets(b"key")
assert result == (b"value", b"10")

def test_gats_not_found_defaults(self):
client = self.make_client([b"END\r\n"])
result = client.gats(b"key", default="foo", cas_default="bar")
assert result == ("foo", "bar")

def test_gats_found(self):
client = self.make_client([b"VALUE key 0 5 10\r\nvalue\r\nEND\r\n"])
result = client.gats(b"key", 10)
assert result == (b"value", b"10")

def _make_failing_client(self):
# The pooled clients never ignore errors themselves, so build one
# that raises and let the PooledClient handle ignore_exc.
mock_client = Client("localhost")
mock_client.sock = MockSocket([b"INVALID DATA\r\n"])
client = PooledClient("localhost", ignore_exc=True)
client.client_pool = pool.ObjectPool(lambda: mock_client)
return client

def test_gets_ignore_exc_defaults(self):
client = self._make_failing_client()
assert client.gets(b"key") == (None, None)
client = self._make_failing_client()
result = client.gets(b"key", default="foo", cas_default="bar")
assert result == ("foo", "bar")

def test_gats_ignore_exc_defaults(self):
client = self._make_failing_client()
assert client.gats(b"key") == (None, None)
client = self._make_failing_client()
result = client.gats(b"key", default="foo", cas_default="bar")
assert result == ("foo", "bar")

def test_cas_stored(self):
client = self.make_client([b"STORED\r\n"])
result = client.cas(b"key", b"value", b"123", noreply=False)
Expand Down