Skip to content

Commit

Permalink
Propagate correct message on memcached connector error (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
argaen authored Jul 22, 2017
1 parent 195f996 commit 9495ec7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
14 changes: 7 additions & 7 deletions aiocache/backends/memcached.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ async def _set(self, key, value, ttl=0, _cas_token=None, _conn=None):
return await self._cas(key, value, _cas_token, ttl=ttl, _conn=_conn)
try:
return await self.client.set(key, value, exptime=ttl or 0)
except aiomcache.exceptions.ValidationException:
raise TypeError("memcached doesn't support float ttl")
except aiomcache.exceptions.ValidationException as e:
raise TypeError('aiomcache error: {}'.format(str(e)))

async def _cas(self, key, value, token, ttl=None, _conn=None):
return await self.client.cas(key, value, token, exptime=ttl or 0)
Expand All @@ -58,17 +58,17 @@ async def _multi_set(self, pairs, ttl=0, _conn=None):

try:
await asyncio.gather(*tasks)
except aiomcache.exceptions.ValidationException:
raise TypeError("memcached doesn't support float ttl")
except aiomcache.exceptions.ValidationException as e:
raise TypeError('aiomcache error: {}'.format(str(e)))

return True

async def _add(self, key, value, ttl=0, _conn=None):
value = str.encode(value) if isinstance(value, str) else value
try:
ret = await self.client.add(key, value, exptime=ttl or 0)
except aiomcache.exceptions.ValidationException:
raise TypeError("memcached doesn't support float ttl")
except aiomcache.exceptions.ValidationException as e:
raise TypeError('aiomcache error: {}'.format(str(e)))
if not ret:
raise ValueError(
"Key {} already exists, use .set to update the value".format(key))
Expand All @@ -89,7 +89,7 @@ async def _increment(self, key, delta, _conn=None):
if "NOT_FOUND" in str(e):
await self._set(key, str(delta).encode())
else:
raise TypeError("Value is not an integer") from None
raise TypeError('aiomcache error: {}'.format(str(e)))

return incremented or delta

Expand Down
16 changes: 12 additions & 4 deletions tests/acceptance/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,24 @@ async def test_accept_explicit_args(self):
with pytest.raises(TypeError):
MemcachedCache(random_attr="wtf")

@pytest.mark.asyncio
async def test_set_too_long_key(self, memcached_cache):
with pytest.raises(TypeError) as exc_info:
await memcached_cache.set('a' * 2000, 'value')
assert str(exc_info.value).startswith('aiomcache error: invalid key')

@pytest.mark.asyncio
async def test_set_float_ttl_fails(self, memcached_cache):
with pytest.raises(TypeError):
await memcached_cache.set(pytest.KEY, "value", ttl=0.1)
with pytest.raises(TypeError) as exc_info:
await memcached_cache.set(pytest.KEY, 'value', ttl=0.1)
assert str(exc_info.value) == 'aiomcache error: exptime not int: 0.1'

@pytest.mark.asyncio
async def test_multi_set_float_ttl(self, memcached_cache):
with pytest.raises(TypeError):
pairs = [(pytest.KEY, b"value"), [pytest.KEY_1, b"random_value"]]
with pytest.raises(TypeError) as exc_info:
pairs = [(pytest.KEY, b'value'), [pytest.KEY_1, b'random_value']]
assert await memcached_cache.multi_set(pairs, ttl=0.1) is True
assert str(exc_info.value) == 'aiomcache error: exptime not int: 0.1'

@pytest.mark.asyncio
async def test_raw(self, memcached_cache):
Expand Down
16 changes: 10 additions & 6 deletions tests/ut/backends/test_memcached.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ async def test_set(self, memcached):
@pytest.mark.asyncio
async def test_set_float_ttl(self, memcached):
memcached.client.set.side_effect = aiomcache.exceptions.ValidationException("msg")
with pytest.raises(TypeError):
await memcached._set(pytest.KEY, "value", ttl=0.1)
with pytest.raises(TypeError) as exc_info:
await memcached._set(pytest.KEY, 'value', ttl=0.1)
assert str(exc_info.value) == 'aiomcache error: msg'

@pytest.mark.asyncio
async def test_set_cas_token(self, mocker, memcached):
Expand Down Expand Up @@ -132,8 +133,9 @@ async def test_multi_set(self, memcached):
@pytest.mark.asyncio
async def test_multi_set_float_ttl(self, memcached):
memcached.client.set.side_effect = aiomcache.exceptions.ValidationException("msg")
with pytest.raises(TypeError):
with pytest.raises(TypeError) as exc_info:
await memcached._multi_set([(pytest.KEY, "value"), (pytest.KEY_1, "random")], ttl=0.1)
assert str(exc_info.value) == 'aiomcache error: msg'

@pytest.mark.asyncio
async def test_add(self, memcached):
Expand All @@ -152,8 +154,9 @@ async def test_add_existing(self, memcached):
@pytest.mark.asyncio
async def test_add_float_ttl(self, memcached):
memcached.client.add.side_effect = aiomcache.exceptions.ValidationException("msg")
with pytest.raises(TypeError):
with pytest.raises(TypeError) as exc_info:
await memcached._add(pytest.KEY, "value", 0.1)
assert str(exc_info.value) == 'aiomcache error: msg'

@pytest.mark.asyncio
async def test_exists(self, memcached):
Expand Down Expand Up @@ -186,9 +189,10 @@ async def test_increment_missing_negative(self, memcached):

@pytest.mark.asyncio
async def test_increment_typerror(self, memcached):
memcached.client.incr.side_effect = aiomcache.exceptions.ClientException("")
with pytest.raises(TypeError):
memcached.client.incr.side_effect = aiomcache.exceptions.ClientException('msg')
with pytest.raises(TypeError) as exc_info:
await memcached._increment(pytest.KEY, 2)
assert str(exc_info.value) == 'aiomcache error: msg'

@pytest.mark.asyncio
async def test_expire(self, memcached):
Expand Down
5 changes: 0 additions & 5 deletions tests/ut/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ def reset_caches():
})


@pytest.fixture(autouse=True)
def disable_logs(mocker):
mocker.patch("logging.getLogger")


class MockCache(BaseCache):

def __init__(self):
Expand Down

0 comments on commit 9495ec7

Please sign in to comment.