tests: Use multiprocess.

This commit is contained in:
tecnovert 2019-08-16 00:31:39 +02:00
parent b5216e1d96
commit f906b1aebb
No known key found for this signature in database
GPG key ID: 8ED6D8750C4E3F93
3 changed files with 28 additions and 21 deletions

View file

@ -217,9 +217,9 @@ def prepareDataDir(coin, settings, data_dir, chain, particl_mnemonic):
if coin == 'particl': if coin == 'particl':
fp.write('debugexclude=libevent\n') fp.write('debugexclude=libevent\n')
fp.write('zmqpubsmsg=tcp://127.0.0.1:{}\n'.format(settings['zmqport'])) fp.write('zmqpubsmsg=tcp://127.0.0.1:{}\n'.format(settings['zmqport']))
fp.write('spentindex=1') fp.write('spentindex=1\n')
fp.write('txindex=1') fp.write('txindex=1\n')
fp.write('staking=0') fp.write('staking=0\n')
if particl_mnemonic == 'none': if particl_mnemonic == 'none':
fp.write('createdefaultmasterkey=1') fp.write('createdefaultmasterkey=1')

View file

@ -34,6 +34,7 @@ swap_client = None
def signal_handler(sig, frame): def signal_handler(sig, frame):
global swap_client
logger.info('Signal %d detected, ending program.' % (sig)) logger.info('Signal %d detected, ending program.' % (sig))
if swap_client is not None: if swap_client is not None:
swap_client.stopRunning() swap_client.stopRunning()
@ -47,7 +48,7 @@ def startDaemon(node_dir, bin_dir, daemon_bin, opts=[]):
return subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) return subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def runClient(fp, data_dir, chain, test_mode): def runClient(fp, data_dir, chain):
global swap_client global swap_client
settings_path = os.path.join(data_dir, 'basicswap.json') settings_path = os.path.join(data_dir, 'basicswap.json')
pids_path = os.path.join(data_dir, '.pids') pids_path = os.path.join(data_dir, '.pids')
@ -90,10 +91,8 @@ def runClient(fp, data_dir, chain, test_mode):
for p in pids: for p in pids:
fd.write('{}:{}\n'.format(*p)) fd.write('{}:{}\n'.format(*p))
if not test_mode: signal.signal(signal.SIGINT, signal_handler)
# Signal only works in main thread signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
swap_client.start() swap_client.start()
if 'htmlhost' in settings: if 'htmlhost' in settings:
@ -163,7 +162,6 @@ def printHelp():
def main(): def main():
data_dir = None data_dir = None
chain = 'mainnet' chain = 'mainnet'
test_mode = False
for v in sys.argv[1:]: for v in sys.argv[1:]:
if len(v) < 2 or v[0] != '-': if len(v) < 2 or v[0] != '-':
@ -184,9 +182,6 @@ def main():
printHelp() printHelp()
return 0 return 0
if name == 'testmode':
test_mode = True
continue
if name == 'testnet': if name == 'testnet':
chain = 'testnet' chain = 'testnet'
continue continue
@ -212,7 +207,7 @@ def main():
with open(os.path.join(data_dir, 'basicswap.log'), 'a') as fp: with open(os.path.join(data_dir, 'basicswap.log'), 'a') as fp:
logger.info(os.path.basename(sys.argv[0]) + ', version: ' + __version__ + '\n\n') logger.info(os.path.basename(sys.argv[0]) + ', version: ' + __version__ + '\n\n')
runClient(fp, data_dir, chain, test_mode) runClient(fp, data_dir, chain)
logger.info('Done.') logger.info('Done.')
return swap_client.fail_code if swap_client is not None else 0 return swap_client.fail_code if swap_client is not None else 0

View file

@ -20,9 +20,9 @@ import time
import unittest import unittest
import logging import logging
import shutil import shutil
import threading
import json import json
import traceback import traceback
import multiprocessing
from unittest.mock import patch from unittest.mock import patch
from urllib.request import urlopen from urllib.request import urlopen
from urllib import parse from urllib import parse
@ -31,6 +31,9 @@ from urllib import parse
import bin.basicswap_prepare as prepareSystem import bin.basicswap_prepare as prepareSystem
import bin.basicswap_run as runSystem import bin.basicswap_run as runSystem
test_path = os.path.expanduser('~/test_basicswap1') test_path = os.path.expanduser('~/test_basicswap1')
PARTICL_PORT_BASE = 11938
BITCOIN_PORT_BASE = 10938
logger = logging.getLogger() logger = logging.getLogger()
logger.level = logging.DEBUG logger.level = logging.DEBUG
@ -66,7 +69,8 @@ class Test(unittest.TestCase):
shutil.rmtree(client_path) shutil.rmtree(client_path)
except Exception as ex: except Exception as ex:
logger.warning('setUpClass %s', str(ex)) logger.warning('setUpClass %s', str(ex))
testargs = ['basicswap-prepare', testargs = [
'basicswap-prepare',
'-datadir="{}"'.format(client_path), '-datadir="{}"'.format(client_path),
'-bindir="{}"'.format(test_path + '/bin'), '-bindir="{}"'.format(test_path + '/bin'),
'-portoffset={}'.format(i), '-portoffset={}'.format(i),
@ -75,18 +79,25 @@ class Test(unittest.TestCase):
with patch.object(sys, 'argv', testargs): with patch.object(sys, 'argv', testargs):
prepareSystem.main() prepareSystem.main()
with open(os.path.join(client_path, 'particl', 'particl.conf'), 'a') as fp:
fp.write('port={}\n'.format(PARTICL_PORT_BASE + i))
with open(os.path.join(client_path, 'bitcoin', 'bitcoin.conf'), 'a') as fp:
fp.write('port={}\n'.format(BITCOIN_PORT_BASE + i))
assert(os.path.exists(config_path)) assert(os.path.exists(config_path))
def run_thread(self, client_id): def run_thread(self, client_id):
client_path = os.path.join(test_path, 'client{}'.format(client_id)) client_path = os.path.join(test_path, 'client{}'.format(client_id))
testargs = ['basicswap-run', '-datadir=' + client_path, '-regtest', '-testmode'] testargs = ['basicswap-run', '-datadir=' + client_path, '-regtest']
with patch.object(sys, 'argv', testargs): with patch.object(sys, 'argv', testargs):
runSystem.main() runSystem.main()
def test_reload(self): def test_reload(self):
processes = []
thread0 = threading.Thread(target=self.run_thread, args=(0,)) for i in range(3):
thread0.start() processes.append(multiprocessing.Process(target=self.run_thread, args=(i,)))
processes[-1].start()
try: try:
waitForServer() waitForServer()
@ -107,9 +118,10 @@ class Test(unittest.TestCase):
logger.warning('TODO') logger.warning('TODO')
time.sleep(5) time.sleep(5)
runSystem.swap_client.stopRunning() for p in processes:
p.terminate()
thread0.join() for p in processes:
p.join()
if __name__ == '__main__': if __name__ == '__main__':