Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_clear_timeout(cache):
transact = mock.Mock()
transact.side_effect = dc.Timeout
with mock.patch.object(cache, '_transact', transact):
with pytest.raises(dc.Timeout):
cache.clear()
def test_delete_timeout(cache):
shards = mock.Mock()
shard = mock.Mock()
delete_func = mock.Mock()
shards.__getitem__ = mock.Mock(side_effect=lambda key: shard)
shard.delete = delete_func
delete_func.side_effect = dc.Timeout
with mock.patch.object(cache, '_shards', shards):
assert not cache.delete(0)
def test_clear_timeout(cache):
transact = mock.Mock()
transact.side_effect = dc.Timeout
with mock.patch.object(cache, '_transact', transact):
with pytest.raises(dc.Timeout):
cache.clear()
def test_remove_timeout(cache):
shard = mock.Mock()
clear = mock.Mock()
shard.clear = clear
clear.side_effect = [dc.Timeout(2), 3]
with mock.patch.object(cache, '_shards', [shard]):
assert cache.clear() == 5
def test_add_timeout(cache):
local = mock.Mock()
con = mock.Mock()
execute = mock.Mock()
local.pid = os.getpid()
local.con = con
con.execute = execute
execute.side_effect = sqlite3.OperationalError
with pytest.raises(dc.Timeout):
try:
with mock.patch.object(cache, '_local', local):
cache.add(0, 0)
finally:
cache.check()
def worker(queue, eviction_policy, processes, threads):
timings = co.defaultdict(list)
cache = Cache('tmp', eviction_policy=eviction_policy)
for index, (action, key, value) in enumerate(iter(queue.get, None)):
start = time.time()
try:
if action == 'set':
cache.set(key, value, expire=EXPIRE)
elif action == 'get':
result = cache.get(key)
else:
assert action == 'delete'
cache.delete(key)
except Timeout:
miss = True
else:
miss = False
stop = time.time()
if action == 'get' and processes == 1 and threads == 1 and EXPIRE is None:
assert result == value
if index > WARMUP:
delta = stop - start
timings[action].append(delta)
if miss:
timings[action + '-miss'].append(delta)
queue.put(timings)