]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/commitdiff
Stop when portfolio worker is down and data is not fetched
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Mon, 27 Aug 2018 09:21:18 +0000 (11:21 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Mon, 27 Aug 2018 09:21:18 +0000 (11:21 +0200)
store.py
tests/test_store.py

index add3df6349dfa40d2231682b8117b7e82053ed53..425f08d7ce85f322addb44219ed7ab0341926132 100644 (file)
--- a/store.py
+++ b/store.py
@@ -517,7 +517,7 @@ class Portfolio:
         elif delta < datetime.timedelta(days=1):
             return 60*60
         else:
-            raise "Too long waiting"
+            raise Exception("Too long waiting")
 
     @classmethod
     def start_worker(cls):
@@ -555,7 +555,10 @@ class Portfolio:
                 cls.report.print_log("[Worker] Fetching cryptoportfolio")
                 cls.get_cryptoportfolio(refetch=True)
                 cls.callback.set()
-                time.sleep(cls.next_wait_time())
+                try:
+                    time.sleep(cls.next_wait_time())
+                except Exception:
+                    cls.stop_worker()
 
     @classmethod
     def stop_worker(cls):
@@ -591,6 +594,8 @@ class Portfolio:
         if cls.data.get() is not None and not refetch:
             return
         if cls.worker is not None and not cls.is_worker_thread():
+            if not cls.worker_started:
+                raise Exception("Portfolio worker is down and no usable data is present")
             cls.notify_and_wait()
             return
         try:
index 4ab9bdf2f1587b199b146fbf203edd6a6bde9246..6f220c8bfd5f01f6bb90bd010ddd3875b0ea5ec7 100644 (file)
@@ -1379,11 +1379,19 @@ class PortfolioTest(WebMockTestCase):
                 with self.subTest(worker=False):
                     market.Portfolio.data = store.LockedVar(None)
                     market.Portfolio.worker = mock.Mock()
+                    market.Portfolio.worker_started = True
                     is_worker.return_value = False
                     market.Portfolio.get_cryptoportfolio()
                     notify.assert_called_once_with()
                     parse_cryptoportfolio.assert_not_called()
                     store_cryptoportfolio.assert_not_called()
+                with self.subTest(worker_started=False):
+                    market.Portfolio.data = store.LockedVar(None)
+                    market.Portfolio.worker = mock.Mock()
+                    market.Portfolio.worker_started = False
+                    is_worker.return_value = False
+                    with self.assertRaises(Exception):
+                        market.Portfolio.get_cryptoportfolio()
 
     def test_parse_cryptoportfolio(self):
         with self.subTest(description="Normal case"):
@@ -1649,6 +1657,22 @@ class PortfolioTest(WebMockTestCase):
             store.Portfolio.worker.join()
             self.assertFalse(store.Portfolio.worker.is_alive())
 
+        with self.subTest("overdue"),\
+                mock.patch.object(store.Portfolio, "get_cryptoportfolio") as get,\
+                mock.patch.object(store.Portfolio, "report") as report,\
+                mock.patch.object(store.Portfolio, "next_wait_time") as wait,\
+                mock.patch.object(store.time, "sleep") as sleep:
+            wait.side_effect = Exception("Time over")
+            store.Portfolio.start_worker()
+
+            store.Portfolio.worker_notify.set()
+
+            store.Portfolio.callback.wait()
+
+            report.print_log.assert_called_once_with("[Worker] Fetching cryptoportfolio")
+            get.assert_called_once_with(refetch=True)
+            self.assertFalse(store.Portfolio.worker.is_alive())
+
     def test_notify_and_wait(self):
         with mock.patch.object(store.Portfolio, "callback") as callback,\
                 mock.patch.object(store.Portfolio, "worker_notify") as worker_notify: