#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
+3
View File
@@ -21,7 +21,9 @@ class PruneProcessCache:
def __call__(self): def __call__(self):
while True: while True:
time.sleep(60) time.sleep(60)
self.prune()
def prune(self):
timestamp = current_time_stamp() timestamp = current_time_stamp()
processes_to_delete = [] processes_to_delete = []
@@ -35,3 +37,4 @@ class PruneProcessCache:
cache.prune(process) cache.prune(process)
except Exception: except Exception:
logging.getLogger("werkzeug").exception("failed to prune process") 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()