Skip to content

Commit

Permalink
Move free_port fixture to libs
Browse files Browse the repository at this point in the history
  • Loading branch information
palango committed Jun 8, 2020
1 parent 54a19cc commit fa9d85a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 21 deletions.
2 changes: 2 additions & 0 deletions tests/libs/fixtures/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,4 +10,5 @@
"keystore_file",
"mockchain",
"default_cli_args",
"free_port",
]
12 changes: 12 additions & 0 deletions tests/libs/fixtures/network.py
Original file line number Diff line number Diff line change
@@ -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
12 changes: 1 addition & 11 deletions tests/monitoring/fixtures/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# pylint: disable=redefined-outer-name
import socket
from typing import Generator, Iterator
from unittest.mock import Mock

Expand All @@ -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,
Expand All @@ -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)
Expand Down
10 changes: 0 additions & 10 deletions tests/pathfinding/fixtures/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# pylint: disable=redefined-outer-name
import socket
from typing import Iterator

import pytest
Expand All @@ -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)
Expand Down

0 comments on commit fa9d85a

Please sign in to comment.