From 6c0d82abe6864e7aa20f05c8029be3cb93e69559 Mon Sep 17 00:00:00 2001 From: tecnovert Date: Wed, 17 Jul 2019 18:24:54 +0200 Subject: [PATCH] test: Wait for rpc. --- basicswap/basicswap.py | 11 ++++------- tests/test_run.py | 13 +++++++++++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index ed6e36d..e3590e4 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -573,17 +573,14 @@ class BasicSwap(): for i in range(21): if not self.is_running: return - if i == 20: - self.log.error('Can\'t connect to daemon RPC, exiting.') - self.stopRunning(1) # systemd will try restart if fail_code != 0 - return try: self.callrpc('getwalletinfo', [], self.wallet) - break + return except Exception as ex: - traceback.print_exc() - self.log.warning('Can\'t connect to daemon RPC, trying again in %d second/s.', (1 + i)) + logging.warning('Can\'t connect to daemon RPC: %s. Trying again in %d second/s.', str(ex), (1 + i)) time.sleep(1 + i) + self.log.error('Can\'t connect to daemon RPC, exiting.') + self.stopRunning(1) # systemd will try restart if fail_code != 0 def loadFromDB(self): self.log.info('Loading data from db') diff --git a/tests/test_run.py b/tests/test_run.py index 2ac55ad..ca5ba6a 100644 --- a/tests/test_run.py +++ b/tests/test_run.py @@ -207,6 +207,17 @@ def run_loop(self): btcRpc('generatetoaddress 1 {}'.format(self.btc_addr)) +def waitForRPC(rpc_func, wallet=None): + for i in range(5): + try: + rpc_func('getwalletinfo') + return + except Exception as ex: + logging.warning('Can\'t connect to daemon RPC: %s. Trying again in %d second/s.', str(ex), (1 + i)) + time.sleep(1 + i) + raise ValueError('waitForRPC failed') + + class Test(unittest.TestCase): @classmethod @@ -253,6 +264,7 @@ class Test(unittest.TestCase): cls.swap_clients[1].callrpc('getnewextaddress', ['lblExtTest']) cls.swap_clients[1].callrpc('rescanblockchain') + waitForRPC(ltcRpc) num_blocks = 500 logging.info('Mining %d litecoin blocks', num_blocks) cls.ltc_addr = ltcRpc('getnewaddress mining_addr legacy') @@ -262,6 +274,7 @@ class Test(unittest.TestCase): assert(ro['bip9_softforks']['csv']['status'] == 'active') assert(ro['bip9_softforks']['segwit']['status'] == 'active') + waitForRPC(btcRpc) cls.btc_addr = btcRpc('getnewaddress mining_addr bech32') logging.info('Mining %d bitcoin blocks to %s', num_blocks, cls.btc_addr) btcRpc('generatetoaddress {} {}'.format(num_blocks, cls.btc_addr))