aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2018-08-27 11:21:18 +0200
committerIsmaël Bouya <ismael.bouya@normalesup.org>2018-08-27 11:21:18 +0200
commit6746607a578eca3383ade7fe7a88c39612f4d6e8 (patch)
treefa17f9b0e771f40584d8247f1cfa71e751d93dda
parent8e648fd5b8d10c8f68b30ee0c30f02d5b60a1b77 (diff)
downloadTrader-6746607a578eca3383ade7fe7a88c39612f4d6e8.tar.gz
Trader-6746607a578eca3383ade7fe7a88c39612f4d6e8.tar.zst
Trader-6746607a578eca3383ade7fe7a88c39612f4d6e8.zip
Stop when portfolio worker is down and data is not fetched
-rw-r--r--store.py9
-rw-r--r--tests/test_store.py24
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:
517 elif delta < datetime.timedelta(days=1): 517 elif delta < datetime.timedelta(days=1):
518 return 60*60 518 return 60*60
519 else: 519 else:
520 raise "Too long waiting" 520 raise Exception("Too long waiting")
521 521
522 @classmethod 522 @classmethod
523 def start_worker(cls): 523 def start_worker(cls):
@@ -555,7 +555,10 @@ class Portfolio:
555 cls.report.print_log("[Worker] Fetching cryptoportfolio") 555 cls.report.print_log("[Worker] Fetching cryptoportfolio")
556 cls.get_cryptoportfolio(refetch=True) 556 cls.get_cryptoportfolio(refetch=True)
557 cls.callback.set() 557 cls.callback.set()
558 time.sleep(cls.next_wait_time()) 558 try:
559 time.sleep(cls.next_wait_time())
560 except Exception:
561 cls.stop_worker()
559 562
560 @classmethod 563 @classmethod
561 def stop_worker(cls): 564 def stop_worker(cls):
@@ -591,6 +594,8 @@ class Portfolio:
591 if cls.data.get() is not None and not refetch: 594 if cls.data.get() is not None and not refetch:
592 return 595 return
593 if cls.worker is not None and not cls.is_worker_thread(): 596 if cls.worker is not None and not cls.is_worker_thread():
597 if not cls.worker_started:
598 raise Exception("Portfolio worker is down and no usable data is present")
594 cls.notify_and_wait() 599 cls.notify_and_wait()
595 return 600 return
596 try: 601 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):
1379 with self.subTest(worker=False): 1379 with self.subTest(worker=False):
1380 market.Portfolio.data = store.LockedVar(None) 1380 market.Portfolio.data = store.LockedVar(None)
1381 market.Portfolio.worker = mock.Mock() 1381 market.Portfolio.worker = mock.Mock()
1382 market.Portfolio.worker_started = True
1382 is_worker.return_value = False 1383 is_worker.return_value = False
1383 market.Portfolio.get_cryptoportfolio() 1384 market.Portfolio.get_cryptoportfolio()
1384 notify.assert_called_once_with() 1385 notify.assert_called_once_with()
1385 parse_cryptoportfolio.assert_not_called() 1386 parse_cryptoportfolio.assert_not_called()
1386 store_cryptoportfolio.assert_not_called() 1387 store_cryptoportfolio.assert_not_called()
1388 with self.subTest(worker_started=False):
1389 market.Portfolio.data = store.LockedVar(None)
1390 market.Portfolio.worker = mock.Mock()
1391 market.Portfolio.worker_started = False
1392 is_worker.return_value = False
1393 with self.assertRaises(Exception):
1394 market.Portfolio.get_cryptoportfolio()
1387 1395
1388 def test_parse_cryptoportfolio(self): 1396 def test_parse_cryptoportfolio(self):
1389 with self.subTest(description="Normal case"): 1397 with self.subTest(description="Normal case"):
@@ -1649,6 +1657,22 @@ class PortfolioTest(WebMockTestCase):
1649 store.Portfolio.worker.join() 1657 store.Portfolio.worker.join()
1650 self.assertFalse(store.Portfolio.worker.is_alive()) 1658 self.assertFalse(store.Portfolio.worker.is_alive())
1651 1659
1660 with self.subTest("overdue"),\
1661 mock.patch.object(store.Portfolio, "get_cryptoportfolio") as get,\
1662 mock.patch.object(store.Portfolio, "report") as report,\
1663 mock.patch.object(store.Portfolio, "next_wait_time") as wait,\
1664 mock.patch.object(store.time, "sleep") as sleep:
1665 wait.side_effect = Exception("Time over")
1666 store.Portfolio.start_worker()
1667
1668 store.Portfolio.worker_notify.set()
1669
1670 store.Portfolio.callback.wait()
1671
1672 report.print_log.assert_called_once_with("[Worker] Fetching cryptoportfolio")
1673 get.assert_called_once_with(refetch=True)
1674 self.assertFalse(store.Portfolio.worker.is_alive())
1675
1652 def test_notify_and_wait(self): 1676 def test_notify_and_wait(self):
1653 with mock.patch.object(store.Portfolio, "callback") as callback,\ 1677 with mock.patch.object(store.Portfolio, "callback") as callback,\
1654 mock.patch.object(store.Portfolio, "worker_notify") as worker_notify: 1678 mock.patch.object(store.Portfolio, "worker_notify") as worker_notify: