elif delta < datetime.timedelta(days=1):
return 60*60
else:
- raise "Too long waiting"
+ raise Exception("Too long waiting")
@classmethod
def start_worker(cls):
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):
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:
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"):
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: