Test if return value is an exception when downloading historic data

This commit is contained in:
Matthias 2020-10-23 07:45:11 +02:00
parent 2e7367d647
commit b8c12f6576
2 changed files with 21 additions and 5 deletions

View File

@ -687,6 +687,9 @@ class Exchange:
async def _async_get_historic_ohlcv(self, pair: str, async def _async_get_historic_ohlcv(self, pair: str,
timeframe: str, timeframe: str,
since_ms: int) -> List: since_ms: int) -> List:
"""
Download historic ohlcv
"""
one_call = timeframe_to_msecs(timeframe) * self._ohlcv_candle_limit one_call = timeframe_to_msecs(timeframe) * self._ohlcv_candle_limit
logger.debug( logger.debug(
@ -702,9 +705,14 @@ class Exchange:
# Combine gathered results # Combine gathered results
data: List = [] data: List = []
for p, timeframe, res in results: for res in results:
if isinstance(res, Exception):
logger.warning("Async code raised an exception: %s", res.__class__.__name__)
continue
# Deconstruct tuple if it's not an exception
p, _, new_data = res
if p == pair: if p == pair:
data.extend(res) data.extend(new_data)
# Sort data again after extending the result - above calls return in "async order" # Sort data again after extending the result - above calls return in "async order"
data = sorted(data, key=lambda x: x[0]) data = sorted(data, key=lambda x: x[0])
logger.info("Downloaded data for %s with length %s.", pair, len(data)) logger.info("Downloaded data for %s with length %s.", pair, len(data))
@ -741,9 +749,8 @@ class Exchange:
if isinstance(res, Exception): if isinstance(res, Exception):
logger.warning("Async code raised an exception: %s", res.__class__.__name__) logger.warning("Async code raised an exception: %s", res.__class__.__name__)
continue continue
pair = res[0] # Deconstruct tuple (has 3 elements)
timeframe = res[1] pair, timeframe, ticks = res
ticks = res[2]
# keeping last candle time as last refreshed time of the pair # keeping last candle time as last refreshed time of the pair
if ticks: if ticks:
self._pairs_last_refresh_time[(pair, timeframe)] = ticks[-1][0] // 1000 self._pairs_last_refresh_time[(pair, timeframe)] = ticks[-1][0] // 1000

View File

@ -1295,6 +1295,15 @@ def test_get_historic_ohlcv(default_conf, mocker, caplog, exchange_name):
# Returns twice the above OHLCV data # Returns twice the above OHLCV data
assert len(ret) == 2 assert len(ret) == 2
caplog.clear()
async def mock_get_candle_hist_error(pair, *args, **kwargs):
raise TimeoutError()
exchange._async_get_candle_history = MagicMock(side_effect=mock_get_candle_hist_error)
ret = exchange.get_historic_ohlcv(pair, "5m", int((arrow.utcnow().timestamp - since) * 1000))
assert log_has_re(r"Async code raised an exception: .*", caplog)
def test_refresh_latest_ohlcv(mocker, default_conf, caplog) -> None: def test_refresh_latest_ohlcv(mocker, default_conf, caplog) -> None:
ohlcv = [ ohlcv = [