Skip to content

Commit 1befad7

Browse files
authored
Fix moto server port conflict (#292)
- Change moto server port to 5001 - Throw a meaningful exception when port conflict detected
1 parent 94d7821 commit 1befad7

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

tests/conftest.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import os
2929
import re
30+
import socket
3031
import string
3132
import uuid
3233
from datetime import datetime
@@ -1587,7 +1588,7 @@ def fixture_aws_credentials() -> Generator[None, None, None]:
15871588
os.environ.pop("AWS_DEFAULT_REGION")
15881589

15891590

1590-
MOTO_SERVER = ThreadedMotoServer(ip_address="localhost", port=5000)
1591+
MOTO_SERVER = ThreadedMotoServer(ip_address="localhost", port=5001)
15911592

15921593

15931594
def pytest_sessionfinish(
@@ -1600,10 +1601,17 @@ def pytest_sessionfinish(
16001601

16011602
@pytest.fixture(scope="session")
16021603
def moto_server() -> ThreadedMotoServer:
1604+
# this will throw an exception if the port is already in use
1605+
is_port_in_use(MOTO_SERVER._ip_address, MOTO_SERVER._port)
16031606
MOTO_SERVER.start()
16041607
return MOTO_SERVER
16051608

16061609

1610+
def is_port_in_use(ip_address: str, port: int) -> None:
1611+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
1612+
s.bind((ip_address, port))
1613+
1614+
16071615
@pytest.fixture(scope="session")
16081616
def moto_endpoint_url(moto_server: ThreadedMotoServer) -> str:
16091617
_url = f"http://{moto_server._ip_address}:{moto_server._port}"

0 commit comments

Comments
 (0)