Add Flexpool tests for API failures
Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
parent
0a2bef6192
commit
7f5bd0a1cf
2 changed files with 68 additions and 21 deletions
|
@ -152,30 +152,39 @@ class FlexpoolHandler(Handler):
|
|||
|
||||
@staticmethod
|
||||
def get_blocks(exchange_rate=None, currency=None):
|
||||
remote_blocks = flexpoolapi.pool.last_blocks(count=MAX_BLOCKS_COUNT)
|
||||
# convert to blocks
|
||||
blocks = []
|
||||
if remote_blocks:
|
||||
for remote_block in remote_blocks:
|
||||
block = Block(number=remote_block.number, hash=remote_block.hash, time=remote_block.time,
|
||||
round_time=remote_block.round_time, reward=remote_block.total_rewards,
|
||||
luck=remote_block.luck, exchange_rate=exchange_rate, currency=currency)
|
||||
blocks.append(block)
|
||||
# sort by block number
|
||||
return sorted(blocks)
|
||||
try:
|
||||
remote_blocks = flexpoolapi.pool.last_blocks(count=MAX_BLOCKS_COUNT)
|
||||
# convert to blocks
|
||||
blocks = []
|
||||
if remote_blocks:
|
||||
for remote_block in remote_blocks:
|
||||
block = Block(number=remote_block.number, hash=remote_block.hash, time=remote_block.time,
|
||||
round_time=remote_block.round_time, reward=remote_block.total_rewards,
|
||||
luck=remote_block.luck, exchange_rate=exchange_rate, currency=currency)
|
||||
blocks.append(block)
|
||||
# sort by block number
|
||||
return sorted(blocks)
|
||||
except flexpoolapi.exceptions.APIError as err:
|
||||
logger.warning('failed to get blocks from Flexpool API')
|
||||
logger.debug(err)
|
||||
|
||||
def watch_miner(self, address, last_balance=None, last_transaction=None):
|
||||
logger.debug(f'watching miner {address}')
|
||||
try:
|
||||
miner = Miner(address=address, exchange_rate=self.exchange_rate, currency=self.currency)
|
||||
except Exception as err:
|
||||
logger.debug(miner)
|
||||
|
||||
last_balance = self._watch_miner_balance(miner=miner, last_balance=last_balance)
|
||||
last_transaction = self._watch_miner_payments(miner=miner, last_transaction=last_transaction)
|
||||
|
||||
return last_balance, last_transaction
|
||||
except flexpoolapi.exceptions.InvalidMinerAddress as err:
|
||||
logger.error(f'miner address {address} is invalid')
|
||||
logger.debug(err)
|
||||
except flexpoolapi.exceptions.MinerDoesNotExist as err:
|
||||
logger.error(f'miner {address} not found')
|
||||
logger.exception(err)
|
||||
return
|
||||
|
||||
logger.debug(miner)
|
||||
|
||||
last_balance = self._watch_miner_balance(miner=miner, last_balance=last_balance)
|
||||
last_transaction = self._watch_miner_payments(miner=miner, last_transaction=last_transaction)
|
||||
|
||||
return last_balance, last_transaction
|
||||
logger.debug(err)
|
||||
except flexpoolapi.exceptions.APIError as err:
|
||||
logger.warning('failed to get miner from Flexpool API')
|
||||
logger.debug(err)
|
||||
return None, None
|
||||
|
|
|
@ -34,6 +34,20 @@ class TestFlexpoolHandler:
|
|||
else:
|
||||
notifier.notify_balance.assert_not_called()
|
||||
|
||||
def test_balance_with_api_failure(self, mocker):
|
||||
"""An API failure should not send a balance notification"""
|
||||
notifier = mocker.Mock()
|
||||
notifier.notify_balance = mocker.Mock()
|
||||
handler = FlexpoolHandler(notifier=notifier)
|
||||
request_get = mocker.patch('requests.get')
|
||||
request_get.return_value.status_code = 503
|
||||
mocker.patch('companion.pools.flexpool.FlexpoolHandler._watch_miner_payments')
|
||||
mocker.patch('companion.pools.flexpool.Miner.get_payements')
|
||||
last_balance, last_transaction = handler.watch_miner(address='0000000000000000000000000000000000000001',
|
||||
last_balance=1)
|
||||
assert last_balance is None
|
||||
notifier.notify_balance.assert_not_called()
|
||||
|
||||
@staticmethod
|
||||
def _create_transactions(names):
|
||||
if names:
|
||||
|
@ -66,6 +80,19 @@ class TestFlexpoolHandler:
|
|||
else:
|
||||
notifier.notify_payment.assert_not_called()
|
||||
|
||||
def test_payment_with_api_failure(self, mocker):
|
||||
"""An API failure should not send a payment notification"""
|
||||
notifier = mocker.Mock()
|
||||
notifier.notify_payment = mocker.Mock()
|
||||
handler = FlexpoolHandler(notifier=notifier)
|
||||
request_get = mocker.patch('requests.get')
|
||||
request_get.return_value.status_code = 503
|
||||
mocker.patch('companion.pools.flexpool.FlexpoolHandler._watch_miner_balance')
|
||||
last_balance, last_transaction = handler.watch_miner(address='0000000000000000000000000000000000000001',
|
||||
last_transaction=1)
|
||||
assert last_transaction is None
|
||||
notifier.notify_payment.assert_not_called()
|
||||
|
||||
@staticmethod
|
||||
def _create_blocks(numbers):
|
||||
if numbers:
|
||||
|
@ -102,3 +129,14 @@ class TestFlexpoolHandler:
|
|||
notifier.notify_block.assert_called_once()
|
||||
else:
|
||||
notifier.notify_block.assert_not_called()
|
||||
|
||||
def test_block_with_api_failure(self, mocker):
|
||||
"""An API failure should not send a block notification"""
|
||||
notifier = mocker.Mock()
|
||||
notifier.notify_block = mocker.Mock()
|
||||
handler = FlexpoolHandler(notifier=notifier)
|
||||
request_get = mocker.patch('requests.get')
|
||||
request_get.return_value.status_code = 503
|
||||
block = handler.watch_blocks(last_block=1)
|
||||
assert block is None
|
||||
notifier.notify_block.assert_not_called()
|
||||
|
|
Reference in a new issue