mirror of
https://github.com/Novartis/cellxgene-gateway.git
synced 2026-10-10 20:20:56 +08:00
#4 added failing test for cache pruning
This commit is contained in:
@@ -21,17 +21,20 @@ class PruneProcessCache:
|
|||||||
def __call__(self):
|
def __call__(self):
|
||||||
while True:
|
while True:
|
||||||
time.sleep(60)
|
time.sleep(60)
|
||||||
|
self.prune()
|
||||||
|
|
||||||
timestamp = current_time_stamp()
|
def prune(self):
|
||||||
|
timestamp = current_time_stamp()
|
||||||
|
|
||||||
processes_to_delete = []
|
processes_to_delete = []
|
||||||
for p in self.cache.entry_list:
|
for p in self.cache.entry_list:
|
||||||
if timestamp - p.timestamp > (3600 if ttl is None else ttl):
|
if timestamp - p.timestamp > (3600 if ttl is None else ttl):
|
||||||
processes_to_delete.append(p)
|
processes_to_delete.append(p)
|
||||||
processes_to_delete
|
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")
|
|
||||||
|
|||||||
@@ -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()
|
||||||
Reference in New Issue
Block a user