From 325a846f478e5b772b6b376ec53d71db1e82ad44 Mon Sep 17 00:00:00 2001 From: tecnovert Date: Sun, 13 Dec 2020 17:11:18 +0200 Subject: [PATCH] Split test update thread. --- tests/basicswap/test_network.py | 10 ++++++---- tests/basicswap/test_nmc.py | 30 ++++++++++++++++++++++++++---- tests/basicswap/test_run.py | 20 ++++++++++++++++---- tests/basicswap/test_xmr.py | 14 +++++++------- 4 files changed, 55 insertions(+), 19 deletions(-) diff --git a/tests/basicswap/test_network.py b/tests/basicswap/test_network.py index 755ed14..27c6518 100644 --- a/tests/basicswap/test_network.py +++ b/tests/basicswap/test_network.py @@ -141,15 +141,17 @@ def callnoderpc(node_id, method, params=[], wallet=None, base_rpc_port=BASE_RPC_ def run_coins_loop(cls): - global stop_test while not stop_test: - if cls.btc_addr is not None: - btcRpc('generatetoaddress 1 {}'.format(cls.btc_addr)) time.sleep(1.0) + try: + if cls.btc_addr is not None: + btcRpc('generatetoaddress 1 {}'.format(cls.btc_addr)) + except Exception as e: + logging.warning('run_coins_loop ' + str(e)) + def run_loop(cls): - global stop_test while not stop_test: for c in cls.swap_clients: c.update() diff --git a/tests/basicswap/test_nmc.py b/tests/basicswap/test_nmc.py index 6c8c0f6..a5589a1 100644 --- a/tests/basicswap/test_nmc.py +++ b/tests/basicswap/test_nmc.py @@ -204,13 +204,21 @@ def signal_handler(sig, frame): delay_event.set() +def run_coins_loop(cls): + while not stop_test: + time.sleep(1.0) + try: + nmcRpc('generatetoaddress 1 {}'.format(cls.nmc_addr)) + btcRpc('generatetoaddress 1 {}'.format(cls.btc_addr)) + except Exception as e: + logging.warning('run_coins_loop ' + str(e)) + + def run_loop(self): while not stop_test: time.sleep(1) for c in self.swap_clients: c.update() - nmcRpc('generatetoaddress 1 {}'.format(self.nmc_addr)) - btcRpc('generatetoaddress 1 {}'.format(self.btc_addr)) def make_part_cli_rpc_func(node_id): @@ -260,6 +268,7 @@ class Test(unittest.TestCase): cls.daemons.append(startDaemon(os.path.join(cfg.TEST_DATADIRS, str(i)), cfg.PARTICL_BINDIR, cfg.PARTICLD)) logging.info('Started %s %d', cfg.PARTICLD, cls.daemons[-1].pid) + for i in range(NUM_NODES): rpc = make_part_cli_rpc_func(i) waitForRPC(rpc) if i == 0: @@ -271,8 +280,6 @@ class Test(unittest.TestCase): else: rpc('extkeyimportmaster', [rpc('mnemonic', ['new'])['master']]) - time.sleep(1) - for i in range(NUM_NODES): basicswap_dir = os.path.join(os.path.join(cfg.TEST_DATADIRS, str(i)), 'basicswap') settings_path = os.path.join(basicswap_dir, cfg.CONFIG_FILENAME) with open(settings_path) as fs: @@ -319,12 +326,27 @@ class Test(unittest.TestCase): cls.update_thread = threading.Thread(target=run_loop, args=(cls,)) cls.update_thread.start() + cls.coins_update_thread = threading.Thread(target=run_coins_loop, args=(cls,)) + cls.coins_update_thread.start() + + # Wait for height, or sequencelock is thrown off by genesis blocktime + num_blocks = 3 + logging.info('Waiting for Particl chain height %d', num_blocks) + for i in range(60): + particl_blocks = cls.swap_clients[0].callrpc('getblockchaininfo')['blocks'] + print('particl_blocks', particl_blocks) + if particl_blocks >= num_blocks: + break + delay_event.wait(1) + assert(particl_blocks >= num_blocks) + @classmethod def tearDownClass(cls): global stop_test logging.info('Finalising') stop_test = True cls.update_thread.join() + cls.coins_update_thread.join() for t in cls.http_threads: t.stop() t.join() diff --git a/tests/basicswap/test_run.py b/tests/basicswap/test_run.py index 7196e2b..8e9048d 100644 --- a/tests/basicswap/test_run.py +++ b/tests/basicswap/test_run.py @@ -212,13 +212,21 @@ def signal_handler(sig, frame): delay_event.set() -def run_loop(self): +def run_coins_loop(cls): + while not stop_test: + try: + time.sleep(1.0) + ltcRpc('generatetoaddress 1 {}'.format(cls.ltc_addr)) + btcRpc('generatetoaddress 1 {}'.format(cls.btc_addr)) + except Exception as e: + logging.warning('run_coins_loop ' + str(e)) + + +def run_loop(cls): while not stop_test: time.sleep(1) - for c in self.swap_clients: + for c in cls.swap_clients: c.update() - ltcRpc('generatetoaddress 1 {}'.format(self.ltc_addr)) - btcRpc('generatetoaddress 1 {}'.format(self.btc_addr)) def make_part_cli_rpc_func(node_id): @@ -328,6 +336,9 @@ class Test(unittest.TestCase): cls.update_thread = threading.Thread(target=run_loop, args=(cls,)) cls.update_thread.start() + cls.coins_update_thread = threading.Thread(target=run_coins_loop, args=(cls,)) + cls.coins_update_thread.start() + # Wait for height, or sequencelock is thrown off by genesis blocktime num_blocks = 3 logging.info('Waiting for Particl chain height %d', num_blocks) @@ -345,6 +356,7 @@ class Test(unittest.TestCase): logging.info('Finalising') stop_test = True cls.update_thread.join() + cls.coins_update_thread.join() for t in cls.http_threads: t.stop() t.join() diff --git a/tests/basicswap/test_xmr.py b/tests/basicswap/test_xmr.py index 3dbdce7..f6464e6 100644 --- a/tests/basicswap/test_xmr.py +++ b/tests/basicswap/test_xmr.py @@ -237,18 +237,18 @@ def callnoderpc(node_id, method, params=[], wallet=None, base_rpc_port=BASE_RPC_ def run_coins_loop(cls): - global stop_test while not stop_test: - if cls.btc_addr is not None: - btcRpc('generatetoaddress 1 {}'.format(cls.btc_addr)) - - if cls.xmr_addr is not None: - callrpc_xmr_na(XMR_BASE_RPC_PORT + 1, 'generateblocks', {'wallet_address': cls.xmr_addr, 'amount_of_blocks': 1}) time.sleep(1.0) + try: + if cls.btc_addr is not None: + btcRpc('generatetoaddress 1 {}'.format(cls.btc_addr)) + if cls.xmr_addr is not None: + callrpc_xmr_na(XMR_BASE_RPC_PORT + 1, 'generateblocks', {'wallet_address': cls.xmr_addr, 'amount_of_blocks': 1}) + except Exception as e: + logging.warning('run_coins_loop ' + str(e)) def run_loop(cls): - global stop_test while not stop_test: for c in cls.swap_clients: c.update()