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':
fp.write('debugexclude=libevent\n')
fp.write('zmqpubsmsg=tcp://127.0.0.1:{}\n'.format(settings['zmqport']))
fp.write('spentindex=1')
fp.write('txindex=1')
fp.write('staking=0')
fp.write('spentindex=1\n')
fp.write('txindex=1\n')
fp.write('staking=0\n')
if particl_mnemonic == 'none':
fp.write('createdefaultmasterkey=1')

View file

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

View file

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