#4 added failing test for cache pruning

This commit is contained in:
Alok Saldanha
2019-09-19 12:02:24 -04:00
parent c4828d8be5
commit f6d10946c6
2 changed files with 40 additions and 11 deletions
+14 -11
View File
@@ -21,17 +21,20 @@ class PruneProcessCache:
def __call__(self):
while True:
time.sleep(60)
self.prune()
timestamp = current_time_stamp()
def prune(self):
timestamp = current_time_stamp()
processes_to_delete = []
for p in self.cache.entry_list:
if timestamp - p.timestamp > (3600 if ttl is None else ttl):
processes_to_delete.append(p)
processes_to_delete
processes_to_delete = []
for p in self.cache.entry_list:
if timestamp - p.timestamp > (3600 if ttl is None else ttl):
processes_to_delete.append(p)
processes_to_delete
for process in processes_to_delete:
try:
cache.prune(process)
except Exception:
logging.getLogger("werkzeug").exception("failed to prune process")
for process in processes_to_delete:
try:
cache.prune(process)
except Exception:
logging.getLogger("werkzeug").exception("failed to prune process")
+26
View File
@@ -0,0 +1,26 @@
import unittest
from unittest.mock import MagicMock, patch
from cellxgene_gateway.cache_entry import CacheEntry
from cellxgene_gateway.backend_cache import BackendCache
class TestPruneProcessCache(unittest.TestCase):
@patch('cellxgene_gateway.util.current_time_stamp', new=lambda:0)
@patch('cellxgene_gateway.env.ttl', new='10')
@patch('cellxgene_gateway.cache_entry.CacheEntry')
@patch('cellxgene_gateway.cache_entry.CacheEntry')
def test_GIVEN_one_old_one_new_THEN_prune_old(self, old, new):
from cellxgene_gateway.prune_process_cache import PruneProcessCache
cache = BackendCache()
old.timestamp = -100
cache.entry_list.append(old)
new.timestamp = -5
cache.entry_list.append(new)
self.assertEqual(len(cache.entry_list), 2)
ppc = PruneProcessCache(cache)
ppc.prune()
self.assertEqual(len(cache.entry_list), 1)
self.assertEqual(cache.entry_list[0], new)
if __name__ == '__main__':
unittest.main()