From 520069a825d950593c7cce0f9cb1497e934e0a1b Mon Sep 17 00:00:00 2001 From: Alok Saldanha Date: Mon, 12 Jul 2021 19:10:35 -0400 Subject: [PATCH 1/2] #48 added failing test for cache pruning --- tests/test_prune_process_cache.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/tests/test_prune_process_cache.py b/tests/test_prune_process_cache.py index 3609e3a..4320c4c 100644 --- a/tests/test_prune_process_cache.py +++ b/tests/test_prune_process_cache.py @@ -1,11 +1,20 @@ import unittest -from unittest.mock import MagicMock, patch +from unittest.mock import patch, seal from cellxgene_gateway.backend_cache import BackendCache -from cellxgene_gateway.cache_entry import CacheEntry +from cellxgene_gateway.cache_key import CacheKey +from cellxgene_gateway.items.file.fileitem import FileItem +from cellxgene_gateway.items.file.fileitem_source import FileItemSource +from cellxgene_gateway.items.item import ItemType + +key = CacheKey( + FileItem("/czi/", name="pbmc3k.h5ad", type=ItemType.h5ad), + FileItemSource("/tmp", "local"), +) class TestPruneProcessCache(unittest.TestCase): + @unittest.skip("skipping until #39 fixed") @patch("cellxgene_gateway.util.current_time_stamp", new=lambda: 0) @patch("cellxgene_gateway.env.ttl", new="10") @patch("cellxgene_gateway.cache_entry.CacheEntry") @@ -15,14 +24,21 @@ class TestPruneProcessCache(unittest.TestCase): cache = BackendCache() old.timestamp = -100 + old.foo = 12 + old.pid = 1 + old.key = key + seal(old) + new.key = key cache.entry_list.append(old) new.timestamp = -5 + seal(new) 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) + self.assertEqual(cache.entry_list[0], new) if __name__ == "__main__": From 264a324946996cf754fce3f236bb15943396b4ee Mon Sep 17 00:00:00 2001 From: Alok Saldanha Date: Mon, 12 Jul 2021 19:19:29 -0400 Subject: [PATCH 2/2] #48 fix bug in cache pruning --- cellxgene_gateway/prune_process_cache.py | 4 ++-- tests/test_prune_process_cache.py | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/cellxgene_gateway/prune_process_cache.py b/cellxgene_gateway/prune_process_cache.py index d4b2f2e..29eafb4 100644 --- a/cellxgene_gateway/prune_process_cache.py +++ b/cellxgene_gateway/prune_process_cache.py @@ -39,9 +39,9 @@ class PruneProcessCache: for process in processes_to_delete: try: - logger.info(f"pruning process {process.pid} ({process.key.dataset})") + logger.info(f"pruning process {process.pid} ({process.key.descriptor})") self.cache.prune(process) except Exception: logger.exception( - "failed to prune process {process.pid} ({process.dataset})" + "failed to prune process {process.pid} ({process.key.descriptor})" ) diff --git a/tests/test_prune_process_cache.py b/tests/test_prune_process_cache.py index 4320c4c..b500007 100644 --- a/tests/test_prune_process_cache.py +++ b/tests/test_prune_process_cache.py @@ -14,7 +14,6 @@ key = CacheKey( class TestPruneProcessCache(unittest.TestCase): - @unittest.skip("skipping until #39 fixed") @patch("cellxgene_gateway.util.current_time_stamp", new=lambda: 0) @patch("cellxgene_gateway.env.ttl", new="10") @patch("cellxgene_gateway.cache_entry.CacheEntry") @@ -27,6 +26,7 @@ class TestPruneProcessCache(unittest.TestCase): old.foo = 12 old.pid = 1 old.key = key + old.terminate.return_value = None seal(old) new.key = key cache.entry_list.append(old) @@ -39,6 +39,7 @@ class TestPruneProcessCache(unittest.TestCase): self.assertEqual(len(cache.entry_list), 1) self.assertEqual(cache.entry_list[0], new) self.assertEqual(cache.entry_list[0], new) + self.assertTrue(old.terminate.called) if __name__ == "__main__":