Skip to content

Commit

Permalink
Merge pull request raiden-network#260 from hackaugusto/geth_port
Browse files Browse the repository at this point in the history
tests: unique port numbers for each test
  • Loading branch information
LefterisJP committed Nov 16, 2016
2 parents 7fb4a38 + d617686 commit 0d94d8c
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 24 deletions.
8 changes: 6 additions & 2 deletions raiden/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@
blockchain_number_of_nodes,
blockchain_key_seed,
blockchain_private_keys,
blockchain_p2p_base_port,
port_generator,
blockchain_rpc_ports,
blockchain_p2p_ports,
)

__all__ = (
Expand Down Expand Up @@ -106,7 +108,9 @@
'blockchain_number_of_nodes',
'blockchain_key_seed',
'blockchain_private_keys',
'blockchain_p2p_base_port',
'port_generator',
'blockchain_rpc_ports',
'blockchain_p2p_ports',

'pytest_addoption',
'logging_level',
Expand Down
8 changes: 6 additions & 2 deletions raiden/tests/fixtures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
blockchain_number_of_nodes,
blockchain_key_seed,
blockchain_private_keys,
blockchain_p2p_base_port,
port_generator,
blockchain_rpc_ports,
blockchain_p2p_ports,
)

__all__ = (
Expand Down Expand Up @@ -105,5 +107,7 @@
'blockchain_number_of_nodes',
'blockchain_key_seed',
'blockchain_private_keys',
'blockchain_p2p_base_port',
'port_generator',
'blockchain_rpc_ports',
'blockchain_p2p_ports',
)
21 changes: 16 additions & 5 deletions raiden/tests/fixtures/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ def blockchain_services(
poll_timeout,
blockchain_backend, # This fixture is required because it will start
# the geth subprocesses
blockchain_rpc_ports,
blockchain_type,
tester_blockgas_limit,
cached_genesis):
Expand All @@ -232,6 +233,7 @@ def blockchain_services(
private_keys,
verbose,
poll_timeout,
blockchain_rpc_ports[0],
registry_address,
)

Expand All @@ -258,7 +260,8 @@ def blockchain_backend(
deploy_key,
private_keys,
blockchain_private_keys,
blockchain_p2p_base_port,
blockchain_p2p_ports,
blockchain_rpc_ports,
tmpdir,
blockchain_type,
cached_genesis):
Expand All @@ -277,7 +280,8 @@ def blockchain_backend(
deploy_key,
private_keys,
blockchain_private_keys,
blockchain_p2p_base_port,
blockchain_p2p_ports,
blockchain_rpc_ports,
tmpdir,
genesis_path,
)
Expand All @@ -297,7 +301,8 @@ def _geth_blockchain(
deploy_key,
private_keys,
cluster_private_keys,
p2p_base_port,
blockchain_p2p_ports,
blockchain_rpc_ports,
tmpdir,
genesis_path):

Expand All @@ -308,7 +313,8 @@ def _geth_blockchain(
deploy_key,
private_keys,
cluster_private_keys,
p2p_base_port,
blockchain_rpc_ports,
blockchain_p2p_ports,
str(tmpdir),
verbosity,
genesis_path,
Expand All @@ -329,10 +335,15 @@ def _jsonrpc_services(
private_keys,
verbose,
poll_timeout,
rpc_port,
registry_address=None):

host = '0.0.0.0'
deploy_client = JSONRPCClient(host=host, privkey=deploy_key)
deploy_client = JSONRPCClient(
host=host,
port=rpc_port,
privkey=deploy_key,
)

# we cannot instantiate BlockChainService without a registry, so first
# deploy it directly with a JSONRPCClient
Expand Down
31 changes: 26 additions & 5 deletions raiden/tests/fixtures/variables.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import os
from itertools import count

import pytest
from ethereum.utils import sha3
Expand Down Expand Up @@ -158,9 +159,29 @@ def blockchain_private_keys(blockchain_number_of_nodes, blockchain_key_seed):
]


# TODO: return a base port that is not random and guaranteed to be used
# only once (avoid that a badly cleaned test interfere with the next).
@pytest.fixture(scope='session')
def port_generator():
""" count generator used to get a unique port number. """
return count(29870)


@pytest.fixture
def blockchain_p2p_base_port():
""" Default P2P base port. """
return 29870
def blockchain_rpc_ports(blockchain_number_of_nodes, port_generator):
""" A list of unique port numbers to be used by the blockchain nodes for
the json-rpc interface.
"""
return [
next(port_generator)
for _ in range(blockchain_number_of_nodes)
]


@pytest.fixture
def blockchain_p2p_ports(blockchain_number_of_nodes, port_generator):
""" A list of unique port numbers to be used by the blockchain nodes for
the p2p protocol.
"""
return [
next(port_generator)
for _ in range(blockchain_number_of_nodes)
]
2 changes: 2 additions & 0 deletions raiden/tests/integration/test_blockchainservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def test_new_netting_contract(raiden_network, asset_amount, settle_timeout):
def test_blockchain(
blockchain_type,
blockchain_backend, # required to start the geth backend
blockchain_rpc_ports,
private_keys,
poll_timeout):
# pylint: disable=too-many-locals
Expand All @@ -169,6 +170,7 @@ def test_blockchain(
total_asset = 100

jsonrpc_client = JSONRPCClient(
port=blockchain_rpc_ports[0],
privkey=privatekey,
print_communication=False,
)
Expand Down
20 changes: 10 additions & 10 deletions raiden/tests/utils/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,15 @@ def geth_init_datadir(datadir, genesis_path):
subprocess.call(['geth', '--datadir', datadir, 'init', genesis_path])


def geth_wait_and_check(privatekeys):
def geth_wait_and_check(privatekeys, rpc_ports):
""" Wait until the geth cluster is ready. """
address = address_encoder(privatekey_to_address(privatekeys[0]))
jsonrpc_running = False
tries = 5
rpc_port = rpc_ports[0]
jsonrpc_client = JSONRPCClient(
host='0.0.0.0',
port=rpc_port,
privkey=privatekeys[0],
print_communication=False,
)
Expand All @@ -183,6 +185,7 @@ def geth_wait_and_check(privatekeys):
address = address_encoder(privatekey_to_address(key))
jsonrpc_client = JSONRPCClient(
host='0.0.0.0',
port=rpc_port,
privkey=key,
print_communication=False,
)
Expand All @@ -202,18 +205,15 @@ def geth_create_blockchain(
deploy_key,
private_keys,
geth_private_keys,
p2p_base_port,
rpc_ports,
p2p_ports,
base_datadir,
verbosity,
genesis_path=None):
# pylint: disable=too-many-locals,too-many-statements,too-many-arguments

# TODO: handle better the errors cases:
# - cant bind, port in use
start_rpcport = 4000

nodes_configuration = []
for pos, key in enumerate(geth_private_keys):
for pos, (key, p2p_port, rpc_port) in enumerate(zip(geth_private_keys, p2p_ports, rpc_ports)):
config = dict()

# make the first node miner
Expand All @@ -225,8 +225,8 @@ def geth_create_blockchain(
config['nodekeyhex'] = encode_hex(key)
config['pub'] = encode_hex(privtopub(key))
config['address'] = privatekey_to_address(key)
config['port'] = p2p_base_port + pos
config['rpcport'] = start_rpcport + pos
config['port'] = p2p_port
config['rpcport'] = rpc_port
config['enode'] = 'enode://{pub}@127.0.0.1:{port}'.format(
pub=config['pub'],
port=config['port'],
Expand Down Expand Up @@ -276,7 +276,7 @@ def geth_create_blockchain(
processes_list.append(process)
assert process.returncode is None

geth_wait_and_check(private_keys)
geth_wait_and_check(private_keys, rpc_ports)

# reenter echo mode (disabled by geth pasphrase prompt)
if isinstance(sys.stdin, file):
Expand Down

0 comments on commit 0d94d8c

Please sign in to comment.