Skip to content

Commit 0881909

Browse files
michael.yakmichaelyaakoby
authored andcommitted
Make sure test servers bind on unique ports - hopefully this will solve the random test failures
1 parent 55998e7 commit 0881909

File tree

5 files changed

+39
-15
lines changed

5 files changed

+39
-15
lines changed

tests/aiohttp_test_server.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,26 @@
88
from pyctuator.pyctuator import Pyctuator
99
from tests.conftest import PyctuatorServer
1010

11+
bind_port = 6000
12+
1113

1214
# mypy: ignore_errors
1315
# pylint: disable=unused-variable
1416
class AiohttpPyctuatorServer(PyctuatorServer):
1517

1618
def __init__(self) -> None:
19+
global bind_port
20+
self.port = bind_port
21+
bind_port += 1
22+
1723
self.app = web.Application()
1824
self.routes = web.RouteTableDef()
1925

2026
self.pyctuator = Pyctuator(
2127
self.app,
2228
"AIOHTTP Pyctuator",
23-
"http://localhost:8888",
24-
"http://localhost:8888/pyctuator",
29+
f"http://localhost:{self.port}",
30+
f"http://localhost:{self.port}/pyctuator",
2531
"http://localhost:8001/register",
2632
registration_interval_sec=1,
2733
metadata=self.metadata,
@@ -61,7 +67,7 @@ async def _run_server(self) -> None:
6167
await runner.setup()
6268

6369
logging.info("Starting aiohttp server")
64-
site = web.TCPSite(runner, port=8888)
70+
site = web.TCPSite(runner, port=self.port)
6571
await site.start()
6672
self.server_started = True
6773
logging.info("aiohttp server started")

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def deregister(registration_id: str) -> None:
114114
registration_tracker.deregistration_time = datetime.now(timezone.utc)
115115

116116
# Start the mock boot-admin server that is needed to test pyctuator's registration
117-
boot_admin_config = Config(app=boot_admin_app, port=8001, loop="asyncio", lifespan="off", log_level="info")
117+
boot_admin_config = Config(app=boot_admin_app, port=8001, lifespan="off", log_level="info")
118118
boot_admin_server = CustomServer(config=boot_admin_config)
119119
boot_admin_thread = threading.Thread(target=boot_admin_server.run)
120120
boot_admin_thread.start()

tests/fast_api_test_server.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@
1111
from pyctuator.pyctuator import Pyctuator
1212
from tests.conftest import PyctuatorServer, CustomServer
1313

14+
bind_port = 7000
15+
1416

1517
class FastApiPyctuatorServer(PyctuatorServer):
1618
def __init__(self) -> None:
19+
global bind_port
20+
self.port = bind_port
21+
bind_port += 1
22+
1723
self.app = FastAPI(
1824
title="FastAPI Example Server",
1925
description="Demonstrate Spring Boot Admin Integration with FastAPI",
@@ -23,8 +29,8 @@ def __init__(self) -> None:
2329
self.pyctuator = Pyctuator(
2430
self.app,
2531
"FastAPI Pyctuator",
26-
"http://localhost:8000",
27-
"http://localhost:8000/pyctuator",
32+
f"http://localhost:{self.port}",
33+
f"http://localhost:{self.port}/pyctuator",
2834
"http://localhost:8001/register",
2935
registration_interval_sec=1,
3036
metadata=self.metadata,
@@ -37,7 +43,7 @@ def logfile_test_repeater(repeated_string: str) -> str:
3743
logging.error(repeated_string)
3844
return repeated_string
3945

40-
self.server = CustomServer(config=(Config(app=self.app, loop="asyncio", lifespan="off", log_level="info")))
46+
self.server = CustomServer(config=(Config(app=self.app, port=self.port, lifespan="off", log_level="info")))
4147
self.thread = threading.Thread(target=self.server.run)
4248

4349
@self.app.get("/httptrace_test_url")

tests/flask_test_server.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@
1111

1212
REQUEST_TIMEOUT = 10
1313

14+
bind_port = 5000
15+
1416

1517
class FlaskPyctuatorServer(PyctuatorServer):
1618
def __init__(self) -> None:
19+
global bind_port
20+
self.port = bind_port
21+
bind_port += 1
22+
1723
self.app = Flask("Flask Example Server")
18-
self.server = make_server('127.0.0.1', 5000, self.app)
24+
self.server = make_server('127.0.0.1', self.port, self.app)
1925
self.ctx = self.app.app_context()
2026
self.ctx.push()
2127

@@ -24,8 +30,8 @@ def __init__(self) -> None:
2430
self.pyctuator = Pyctuator(
2531
self.app,
2632
"Flask Pyctuator",
27-
"http://localhost:5000",
28-
"http://localhost:5000/pyctuator",
33+
f"http://localhost:{self.port}",
34+
f"http://localhost:{self.port}/pyctuator",
2935
"http://localhost:8001/register",
3036
registration_interval_sec=1,
3137
metadata=self.metadata,
@@ -61,7 +67,7 @@ def start(self) -> None:
6167
while True:
6268
time.sleep(0.5)
6369
try:
64-
requests.get("http://localhost:5000/pyctuator", timeout=REQUEST_TIMEOUT)
70+
requests.get(f"http://localhost:{self.port}/pyctuator", timeout=REQUEST_TIMEOUT)
6571
logging.info("Flask server started")
6672
return
6773
except requests.exceptions.RequestException: # Catches all exceptions that Requests raises!

tests/tornado_test_server.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@
1010
from pyctuator.pyctuator import Pyctuator
1111
from tests.conftest import PyctuatorServer
1212

13+
bind_port = 9000
14+
1315

1416
class TornadoPyctuatorServer(PyctuatorServer):
1517
def __init__(self) -> None:
18+
global bind_port
19+
self.port = bind_port
20+
bind_port += 1
1621

1722
# pylint: disable=abstract-method
1823
class LogfileTestRepeater(RequestHandler):
@@ -43,13 +48,14 @@ def get(self) -> None:
4348
("/logfile_test_repeater", LogfileTestRepeater),
4449
("/httptrace_test_url", GetHttptraceTestUrl)
4550
],
46-
debug=True)
51+
debug=False
52+
)
4753

4854
self.pyctuator = Pyctuator(
4955
self.app,
5056
"Tornado Pyctuator",
51-
app_url="http://localhost:6000",
52-
pyctuator_endpoint_url="http://localhost:6000/pyctuator",
57+
app_url=f"http://localhost:{self.port}",
58+
pyctuator_endpoint_url=f"http://localhost:{self.port}/pyctuator",
5359
registration_url="http://localhost:8001/register",
5460
app_description="Demonstrate Spring Boot Admin Integration with Tornado",
5561
registration_interval_sec=1,
@@ -63,7 +69,7 @@ def get(self) -> None:
6369

6470
def _start_in_thread(self) -> None:
6571
self.io_loop = ioloop.IOLoop()
66-
self.http_server.listen(6000)
72+
self.app.listen(self.port)
6773
self.io_loop.start()
6874

6975
def start(self) -> None:

0 commit comments

Comments
 (0)