This is an automated email from the ASF dual-hosted git repository.

eamonford pushed a commit to branch tests
in repository https://gitbox.apache.org/repos/asf/incubator-sdap-ingester.git


The following commit(s) were added to refs/heads/tests by this push:
     new 1f97c44  unit tests
1f97c44 is described below

commit 1f97c44adc62b3d2b128a975daf31105f73732e0
Author: Eamon Ford <[email protected]>
AuthorDate: Fri Jun 12 15:50:20 2020 -0700

    unit tests
---
 .../services/CollectionWatcher.py                  |  9 +++++
 .../tests/services/test_CollectionWatcher.py       | 46 +++++++++++++++++-----
 2 files changed, 45 insertions(+), 10 deletions(-)

diff --git 
a/collection_manager/collection_manager/services/CollectionWatcher.py 
b/collection_manager/collection_manager/services/CollectionWatcher.py
index b3c4af3..8b10df4 100644
--- a/collection_manager/collection_manager/services/CollectionWatcher.py
+++ b/collection_manager/collection_manager/services/CollectionWatcher.py
@@ -139,3 +139,12 @@ class _GranuleEventHandler(FileSystemEventHandler):
         for collection in self._collections_for_dir:
             if collection.owns_file(event.src_path):
                 self._callback(event.src_path, collection)
+
+    def on_modified(self, event):
+        super().on_modified(event)
+        if os.path.isdir(event.src_path):
+            return
+
+        for collection in self._collections_for_dir:
+            if collection.owns_file(event.src_path):
+                self._callback(event.src_path, collection)
diff --git a/collection_manager/tests/services/test_CollectionWatcher.py 
b/collection_manager/tests/services/test_CollectionWatcher.py
index 5a5b073..9a1f50c 100644
--- a/collection_manager/tests/services/test_CollectionWatcher.py
+++ b/collection_manager/tests/services/test_CollectionWatcher.py
@@ -100,7 +100,7 @@ collections:
             """
             collections_config.write(collections_str.encode("utf-8"))
 
-            assert_called_within_timeout(collection_callback, call_count=2)
+            self.assert_called_within_timeout(collection_callback, 
call_count=2)
             granule_dir.cleanup()
 
     def test_granule_callback_is_called_on_new_file(self):
@@ -121,17 +121,43 @@ collections:
             collection_watcher.start_watching()
 
             new_granule = open(os.path.join(granule_dir.name, 'test.nc'), "w+")
-            new_granule.close()
 
-            assert_called_within_timeout(granule_callback)
+            self.assert_called_within_timeout(granule_callback)
+
+            new_granule.close()
             granule_dir.cleanup()
 
+    def test_granule_callback_is_called_on_modified_file(self):
+        with tempfile.NamedTemporaryFile("w+b", buffering=0) as 
collections_config:
+            granule_dir = tempfile.TemporaryDirectory()
+            collections_str = f"""
+collections:
+- id: TELLUS_GRACE_MASCON_CRI_GRID_RL05_V2_LAND
+  path: {granule_dir.name}
+  variable: lwe_thickness
+  priority: 1
+  forward-processing-priority: 5
+            """
+            collections_config.write(collections_str.encode("utf-8"))
+            new_granule = open(os.path.join(granule_dir.name, 'test.nc'), "w+")
+
+            granule_callback = Mock()
+            collection_watcher = CollectionWatcher(collections_config.name, 
Mock(), granule_callback)
+            collection_watcher.start_watching()
+
+            new_granule.write("hello world")
+            new_granule.close()
+
+            self.assert_called_within_timeout(granule_callback)
+
+            granule_dir.cleanup()
 
-def assert_called_within_timeout(mock_func, timeout_sec=1.0, call_count=1):
-    start = datetime.now()
+    @staticmethod
+    def assert_called_within_timeout(mock_func, timeout_sec=1.0, call_count=1):
+        start = datetime.now()
 
-    while (datetime.now() - start).total_seconds() < timeout_sec:
-        time.sleep(0.01)
-        if mock_func.call_count >= call_count:
-            return
-    raise AssertionError(f"{mock_func} did not reach {call_count} calls called 
within {timeout_sec} sec")
+        while (datetime.now() - start).total_seconds() < timeout_sec:
+            time.sleep(0.01)
+            if mock_func.call_count >= call_count:
+                return
+        raise AssertionError(f"{mock_func} did not reach {call_count} calls 
called within {timeout_sec} sec")

Reply via email to