From 6746607a578eca3383ade7fe7a88c39612f4d6e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Mon, 27 Aug 2018 11:21:18 +0200 Subject: Stop when portfolio worker is down and data is not fetched --- store.py | 9 +++++++-- tests/test_store.py | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/store.py b/store.py index add3df6..425f08d 100644 --- 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: diff --git a/tests/test_store.py b/tests/test_store.py index 4ab9bdf..6f220c8 100644 --- a/tests/test_store.py +++ b/tests/test_store.py @@ -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: -- cgit v1.2.3