async def test__main_loop__handle_exception_throws(self): algorithm = Mock(spec=AlgorithmBaseSync) error = ValueError('a') algorithm.next_iteration.side_effect = [0, error, 0, 0] algorithm.handle_exception.side_effect = AttributeError('side') use_algorithm = self.algo(algorithm) with self.assertLogs('btrccts') as cm: with self.assertRaises(AttributeError) as e: await main_loop(timeframe=self.timeframe, algorithm=use_algorithm) self.assertEqual(str(e.exception), 'side') self.assertEqual(algorithm.mock_calls, [ call.next_iteration(), call.next_iteration(), call.handle_exception(error), call.exit(reason=ExitReason.EXCEPTION) ]) self.assertEqual(len(cm.output), 5) self.assertEqual(cm.output[0:2], [ 'INFO:btrccts:Starting main_loop', 'ERROR:btrccts:Error occured during next_iteration' ]) self.assertTrue(cm.output[2].startswith( 'ERROR:btrccts:a\nTraceback (most recent call last):\n File')) self.assertEqual( cm.output[3], 'ERROR:btrccts:Exiting because of ' 'exception in handle_exception') self.assertTrue(cm.output[4].startswith( 'ERROR:btrccts:side\nTraceback (most recent call last):\n File'))
async def test__main_loop__successful(self): algorithm = Mock(spec=AlgorithmBaseSync) error = ValueError('a') algorithm.next_iteration.side_effect = [0, 0, error, 0] use_algorithm = self.algo(algorithm) with self.assertLogs('btrccts') as cm: result = await main_loop(timeframe=self.timeframe, algorithm=use_algorithm) self.assertEqual(result, use_algorithm) self.assertEqual(algorithm.mock_calls, [ call.next_iteration(), call.next_iteration(), call.next_iteration(), call.handle_exception(error), call.next_iteration(), call.exit(reason=ExitReason.FINISHED) ]) self.assertEqual(len(cm.output), 4) self.assertEqual(cm.output[0:2], [ 'INFO:btrccts:Starting main_loop', 'ERROR:btrccts:Error occured during next_iteration' ]) self.assertTrue(cm.output[2].startswith( 'ERROR:btrccts:a\nTraceback (most recent call last):\n File')) self.assertEqual(cm.output[3], 'INFO:btrccts:Finished main_loop')
def template__main_loop__exit_exception(self, exception_class, log_str): algorithm = Mock(spec=AlgorithmBase) algorithm.next_iteration.side_effect = [0, exception_class('aa'), 0, 0] with self.assertLogs('btrccts') as cm: result = main_loop(timeframe=self.timeframe, algorithm=algorithm) self.assertEqual(algorithm.mock_calls, [ call.next_iteration(), call.next_iteration(), call.exit(reason=ExitReason.STOPPED) ]) self.assertEqual(cm.output, ['INFO:btrccts:Starting main_loop', log_str]) self.assertEqual(algorithm, result)
def template__main_loop__exit_exception_during_sleep( self, exception_class, log_str, sleep_mock): algorithm = Mock(spec=AlgorithmBase) sleep_mock.side_effect = [exception_class('aa')] # We need to use future dates, because we are in live mode timeframe = Timeframe(pd_start_date=pd_ts('2217-01-01 1:00'), pd_end_date=pd_ts('2217-01-01 1:03'), pd_interval=pandas.Timedelta(minutes=1)) with self.assertLogs('btrccts') as cm: result = main_loop(timeframe=timeframe, algorithm=algorithm, live=True) self.assertEqual( algorithm.mock_calls, [call.next_iteration(), call.exit(reason=ExitReason.STOPPED)]) self.assertEqual(cm.output, ['INFO:btrccts:Starting main_loop', log_str]) self.assertEqual(algorithm, result)