Tighten memcached tests

Check flags set and key used; stop relying on dict.values()

Change-Id: Ibf9228dabd66ae98fb3b64050ccd46f5032d0df9
This commit is contained in:
Tim Burke 2016-06-23 12:22:02 -07:00 committed by Thiago da Silva
parent 143b0eec92
commit b6be925cf5
2 changed files with 43 additions and 20 deletions

View File

@ -388,7 +388,7 @@ class MemcacheRing(object):
Sets multiple key/value pairs in memcache. Sets multiple key/value pairs in memcache.
:param mapping: dictionary of keys and values to be set in memcache :param mapping: dictionary of keys and values to be set in memcache
:param servery_key: key to use in determining which server in the ring :param server_key: key to use in determining which server in the ring
is used is used
:param serialize: if True, value is serialized with JSON before sending :param serialize: if True, value is serialized with JSON before sending
to memcache, or with pickle if configured to use to memcache, or with pickle if configured to use

View File

@ -17,6 +17,7 @@
"""Tests for swift.common.utils""" """Tests for swift.common.utils"""
from collections import defaultdict from collections import defaultdict
from hashlib import md5
import logging import logging
import socket import socket
import time import time
@ -293,30 +294,38 @@ class TestMemcached(unittest.TestCase):
finally: finally:
sock.close() sock.close()
def test_set_get(self): def test_set_get_json(self):
memcache_client = memcached.MemcacheRing(['1.2.3.4:11211']) memcache_client = memcached.MemcacheRing(['1.2.3.4:11211'])
mock = MockMemcached() mock = MockMemcached()
memcache_client._client_cache['1.2.3.4:11211'] = MockedMemcachePool( memcache_client._client_cache['1.2.3.4:11211'] = MockedMemcachePool(
[(mock, mock)] * 2) [(mock, mock)] * 2)
cache_key = md5('some_key').hexdigest()
memcache_client.set('some_key', [1, 2, 3]) memcache_client.set('some_key', [1, 2, 3])
self.assertEqual(memcache_client.get('some_key'), [1, 2, 3]) self.assertEqual(memcache_client.get('some_key'), [1, 2, 3])
self.assertEqual(mock.cache.values()[0][1], '0') # See JSON_FLAG
self.assertEqual(mock.cache, {cache_key: ('2', '0', '[1, 2, 3]')})
memcache_client.set('some_key', [4, 5, 6]) memcache_client.set('some_key', [4, 5, 6])
self.assertEqual(memcache_client.get('some_key'), [4, 5, 6]) self.assertEqual(memcache_client.get('some_key'), [4, 5, 6])
self.assertEqual(mock.cache, {cache_key: ('2', '0', '[4, 5, 6]')})
memcache_client.set('some_key', ['simple str', 'utf8 str éà']) memcache_client.set('some_key', ['simple str', 'utf8 str éà'])
# As per http://wiki.openstack.org/encoding, # As per http://wiki.openstack.org/encoding,
# we should expect to have unicode # we should expect to have unicode
self.assertEqual( self.assertEqual(
memcache_client.get('some_key'), ['simple str', u'utf8 str éà']) memcache_client.get('some_key'), ['simple str', u'utf8 str éà'])
self.assertTrue(float(mock.cache.values()[0][1]) == 0) self.assertEqual(mock.cache, {cache_key: (
'2', '0', '["simple str", "utf8 str \\u00e9\\u00e0"]')})
memcache_client.set('some_key', [1, 2, 3], time=20) memcache_client.set('some_key', [1, 2, 3], time=20)
self.assertEqual(mock.cache.values()[0][1], '20') self.assertEqual(mock.cache, {cache_key: ('2', '20', '[1, 2, 3]')})
sixtydays = 60 * 24 * 60 * 60 sixtydays = 60 * 24 * 60 * 60
esttimeout = time.time() + sixtydays esttimeout = time.time() + sixtydays
memcache_client.set('some_key', [1, 2, 3], time=sixtydays) memcache_client.set('some_key', [1, 2, 3], time=sixtydays)
self.assertTrue( _junk, cache_timeout, _junk = mock.cache[cache_key]
-1 <= float(mock.cache.values()[0][1]) - esttimeout <= 1) self.assertAlmostEqual(float(cache_timeout), esttimeout, delta=1)
def test_incr(self): def test_incr(self):
memcache_client = memcached.MemcacheRing(['1.2.3.4:11211']) memcache_client = memcached.MemcacheRing(['1.2.3.4:11211'])
@ -343,25 +352,32 @@ class TestMemcached(unittest.TestCase):
mock = MockMemcached() mock = MockMemcached()
memcache_client._client_cache['1.2.3.4:11211'] = MockedMemcachePool( memcache_client._client_cache['1.2.3.4:11211'] = MockedMemcachePool(
[(mock, mock)] * 2) [(mock, mock)] * 2)
cache_key = md5('some_key').hexdigest()
memcache_client.incr('some_key', delta=5, time=55) memcache_client.incr('some_key', delta=5, time=55)
self.assertEqual(memcache_client.get('some_key'), '5') self.assertEqual(memcache_client.get('some_key'), '5')
self.assertEqual(mock.cache.values()[0][1], '55') self.assertEqual(mock.cache, {cache_key: ('0', '55', '5')})
memcache_client.delete('some_key') memcache_client.delete('some_key')
self.assertEqual(memcache_client.get('some_key'), None) self.assertEqual(memcache_client.get('some_key'), None)
fiftydays = 50 * 24 * 60 * 60 fiftydays = 50 * 24 * 60 * 60
esttimeout = time.time() + fiftydays esttimeout = time.time() + fiftydays
memcache_client.incr('some_key', delta=5, time=fiftydays) memcache_client.incr('some_key', delta=5, time=fiftydays)
self.assertEqual(memcache_client.get('some_key'), '5') self.assertEqual(memcache_client.get('some_key'), '5')
self.assertTrue( _junk, cache_timeout, _junk = mock.cache[cache_key]
-1 <= float(mock.cache.values()[0][1]) - esttimeout <= 1) self.assertAlmostEqual(float(cache_timeout), esttimeout, delta=1)
memcache_client.delete('some_key') memcache_client.delete('some_key')
self.assertEqual(memcache_client.get('some_key'), None) self.assertEqual(memcache_client.get('some_key'), None)
memcache_client.incr('some_key', delta=5) memcache_client.incr('some_key', delta=5)
self.assertEqual(memcache_client.get('some_key'), '5') self.assertEqual(memcache_client.get('some_key'), '5')
self.assertEqual(mock.cache.values()[0][1], '0') self.assertEqual(mock.cache, {cache_key: ('0', '0', '5')})
memcache_client.incr('some_key', delta=5, time=55) memcache_client.incr('some_key', delta=5, time=55)
self.assertEqual(memcache_client.get('some_key'), '10') self.assertEqual(memcache_client.get('some_key'), '10')
self.assertEqual(mock.cache.values()[0][1], '0') self.assertEqual(mock.cache, {cache_key: ('0', '0', '10')})
def test_decr(self): def test_decr(self):
memcache_client = memcached.MemcacheRing(['1.2.3.4:11211']) memcache_client = memcached.MemcacheRing(['1.2.3.4:11211'])
@ -409,28 +425,35 @@ class TestMemcached(unittest.TestCase):
mock = MockMemcached() mock = MockMemcached()
memcache_client._client_cache['1.2.3.4:11211'] = MockedMemcachePool( memcache_client._client_cache['1.2.3.4:11211'] = MockedMemcachePool(
[(mock, mock)] * 2) [(mock, mock)] * 2)
memcache_client.set_multi( memcache_client.set_multi(
{'some_key1': [1, 2, 3], 'some_key2': [4, 5, 6]}, 'multi_key') {'some_key1': [1, 2, 3], 'some_key2': [4, 5, 6]}, 'multi_key')
self.assertEqual( self.assertEqual(
memcache_client.get_multi(('some_key2', 'some_key1'), 'multi_key'), memcache_client.get_multi(('some_key2', 'some_key1'), 'multi_key'),
[[4, 5, 6], [1, 2, 3]]) [[4, 5, 6], [1, 2, 3]])
self.assertEqual(mock.cache.values()[0][1], '0') for key in ('some_key1', 'some_key2'):
self.assertEqual(mock.cache.values()[1][1], '0') key = md5(key).hexdigest()
self.assertIn(key, mock.cache)
_junk, cache_timeout, _junk = mock.cache[key]
self.assertEqual(cache_timeout, '0')
memcache_client.set_multi( memcache_client.set_multi(
{'some_key1': [1, 2, 3], 'some_key2': [4, 5, 6]}, 'multi_key', {'some_key1': [1, 2, 3], 'some_key2': [4, 5, 6]}, 'multi_key',
time=20) time=20)
self.assertEqual(mock.cache.values()[0][1], '20') for key in ('some_key1', 'some_key2'):
self.assertEqual(mock.cache.values()[1][1], '20') key = md5(key).hexdigest()
_junk, cache_timeout, _junk = mock.cache[key]
self.assertEqual(cache_timeout, '20')
fortydays = 50 * 24 * 60 * 60 fortydays = 50 * 24 * 60 * 60
esttimeout = time.time() + fortydays esttimeout = time.time() + fortydays
memcache_client.set_multi( memcache_client.set_multi(
{'some_key1': [1, 2, 3], 'some_key2': [4, 5, 6]}, 'multi_key', {'some_key1': [1, 2, 3], 'some_key2': [4, 5, 6]}, 'multi_key',
time=fortydays) time=fortydays)
self.assertTrue( for key in ('some_key1', 'some_key2'):
-1 <= float(mock.cache.values()[0][1]) - esttimeout <= 1) key = md5(key).hexdigest()
self.assertTrue( _junk, cache_timeout, _junk = mock.cache[key]
-1 <= float(mock.cache.values()[1][1]) - esttimeout <= 1) self.assertAlmostEqual(float(cache_timeout), esttimeout, delta=1)
self.assertEqual(memcache_client.get_multi( self.assertEqual(memcache_client.get_multi(
('some_key2', 'some_key1', 'not_exists'), 'multi_key'), ('some_key2', 'some_key1', 'not_exists'), 'multi_key'),
[[4, 5, 6], [1, 2, 3], None]) [[4, 5, 6], [1, 2, 3], None])