diff --git a/tests/libs/fixtures/__init__.py b/tests/libs/fixtures/__init__.py index 235e05f6c..1286cdff8 100644 --- a/tests/libs/fixtures/__init__.py +++ b/tests/libs/fixtures/__init__.py @@ -1,4 +1,5 @@ from .cli import default_cli_args +from .network import free_port from .web3 import contracts_manager, keystore_file, mockchain, wait_for_blocks # Without declaring `__all__`, the names `cli` and `web3` would get imported @@ -9,4 +10,5 @@ "keystore_file", "mockchain", "default_cli_args", + "free_port", ] diff --git a/tests/libs/fixtures/network.py b/tests/libs/fixtures/network.py new file mode 100644 index 000000000..7995f21d8 --- /dev/null +++ b/tests/libs/fixtures/network.py @@ -0,0 +1,12 @@ +import socket + +import pytest + + +@pytest.fixture(scope="session") +def free_port() -> int: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.bind(("localhost", 0)) # binding to port 0 will choose a free socket + port = sock.getsockname()[1] + sock.close() + return port diff --git a/tests/monitoring/fixtures/api.py b/tests/monitoring/fixtures/api.py index 3a1d105dc..cdd619831 100644 --- a/tests/monitoring/fixtures/api.py +++ b/tests/monitoring/fixtures/api.py @@ -1,5 +1,4 @@ # pylint: disable=redefined-outer-name -import socket from typing import Generator, Iterator from unittest.mock import Mock @@ -9,8 +8,8 @@ from tests.libs.mocks.web3 import Web3Mock from monitoring_service.api import MsApi +from monitoring_service.constants import API_PATH from monitoring_service.service import MonitoringService -from pathfinding_service.constants import API_PATH from raiden.utils.typing import BlockTimeout from raiden_contracts.constants import ( CONTRACT_MONITORING_SERVICE, @@ -21,15 +20,6 @@ from raiden_libs.constants import DEFAULT_API_HOST -@pytest.fixture(scope="session") -def free_port() -> int: - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.bind(("localhost", 0)) # binding to port 0 will choose a free socket - port = sock.getsockname()[1] - sock.close() - return port - - @pytest.fixture(scope="session") def api_url(free_port: int) -> str: return "http://localhost:{}{}".format(free_port, API_PATH) diff --git a/tests/pathfinding/fixtures/api.py b/tests/pathfinding/fixtures/api.py index fe1782505..e06c1a1dd 100644 --- a/tests/pathfinding/fixtures/api.py +++ b/tests/pathfinding/fixtures/api.py @@ -1,5 +1,4 @@ # pylint: disable=redefined-outer-name -import socket from typing import Iterator import pytest @@ -13,15 +12,6 @@ from ..utils import SimpleReachabilityContainer -@pytest.fixture(scope="session") -def free_port() -> int: - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.bind(("localhost", 0)) # binding to port 0 will choose a free socket - port = sock.getsockname()[1] - sock.close() - return port - - @pytest.fixture(scope="session") def api_url(free_port: int) -> str: return "http://localhost:{}{}".format(free_port, API_PATH)