Browse Source

pytest: Use random ports for bitcoind and lightningd to allow parallel testing

Adds a new dependency, but totally worth it :-)

Signed-off-by: Christian Decker <decker.christian@gmail.com>
ppa-0.6.1
Christian Decker 7 years ago
committed by Rusty Russell
parent
commit
071ef628db
  1. 2
      tests/fixtures.py
  2. 13
      tests/test_lightningd.py
  3. 6
      tests/utils.py

2
tests/fixtures.py

@ -30,7 +30,7 @@ def test_name(request):
@pytest.fixture
def bitcoind(directory):
bitcoind = utils.BitcoinD(bitcoin_dir=directory, rpcport=28332)
bitcoind = utils.BitcoinD(bitcoin_dir=directory, rpcport=None)
try:
bitcoind.start()
except Exception:

13
tests/test_lightningd.py

@ -1,5 +1,6 @@
from concurrent import futures
from decimal import Decimal
from ephemeral_port_reserve import reserve as reserve_port
from utils import wait_for
import copy
@ -43,7 +44,7 @@ def to_json(arg):
def setupBitcoind(directory):
global bitcoind
bitcoind = utils.BitcoinD(bitcoin_dir=directory, rpcport=28332)
bitcoind = utils.BitcoinD(bitcoin_dir=directory, rpcport=None)
try:
bitcoind.start()
@ -126,7 +127,8 @@ class NodeFactory(object):
return node_opts, cli_opts
def get_next_port(self):
return 16330 + self.next_id
with self.lock:
return reserve_port()
def get_nodes(self, num_nodes, opts=None):
"""Start a number of nodes in parallel, each with its own options
@ -148,9 +150,10 @@ class NodeFactory(object):
return [j.result() for j in jobs]
def get_node(self, disconnect=None, options=None, may_fail=False, may_reconnect=False, random_hsm=False, fake_bitcoin_cli=False):
with self.lock:
node_id = self.next_id
port = self.get_next_port()
self.next_id += 1
port = self.get_next_port()
lightning_dir = os.path.join(
self.directory, "lightning-{}/".format(node_id))
@ -662,9 +665,7 @@ class LightningDTests(BaseLightningDTests):
"""
l1 = self.node_factory.get_node()
l2 = self.node_factory.get_node()
# Force l3 to give its address.
l3port = self.node_factory.get_next_port()
l3 = self.node_factory.get_node(options={"ipaddr": "127.0.0.1:{}".format(l3port)})
l3 = self.node_factory.get_node(options={"ipaddr": "127.0.0.1"})
l2.rpc.connect(l3.info['id'], 'localhost', l3.info['port'])

6
tests/utils.py

@ -8,6 +8,7 @@ import time
from bitcoin.rpc import RawProxy as BitcoinProxy
from decimal import Decimal
from ephemeral_port_reserve import reserve
BITCOIND_CONFIG = {
@ -209,9 +210,12 @@ class SimpleBitcoinProxy:
class BitcoinD(TailableProc):
def __init__(self, bitcoin_dir="/tmp/bitcoind-test", rpcport=18332):
def __init__(self, bitcoin_dir="/tmp/bitcoind-test", rpcport=None):
TailableProc.__init__(self, bitcoin_dir, verbose=False)
if rpcport is None:
rpcport = reserve()
self.bitcoin_dir = bitcoin_dir
self.rpcport = rpcport
self.prefix = 'bitcoind'

Loading…
Cancel
Save