wait for uvicorn server to bind before running tests (#607)

* wait for uvicorn server to bind before running tests

Previously we had a simple `time.sleep(1)` call after `server.start()`
which was present to give the Mint's HTTP server time to spin up during
test runs. This meant that if the server took longer than 1s to start
on a dev's machine for any reason (even intermittently) then tests would
fail due to connection errors.

The fix is to use a simple repeated polling check which allows
the test runner to start only once the server is confirmed listening.

* fix linter errors

* prevent infinite loop

* specifically except httpx.ConnectError

Co-authored-by: Pavol Rusnak <pavol@rusnak.io>

---------

Co-authored-by: Pavol Rusnak <pavol@rusnak.io>
This commit is contained in:
conduition
2024-10-19 19:01:32 -04:00
committed by GitHub
parent d2e96162f8
commit d12a8d1bde

View File

@@ -6,6 +6,7 @@ import shutil
import time import time
from pathlib import Path from pathlib import Path
import httpx
import pytest import pytest
import pytest_asyncio import pytest_asyncio
import uvicorn import uvicorn
@@ -27,7 +28,6 @@ settings.log_level = "TRACE"
settings.cashu_dir = "./test_data/" settings.cashu_dir = "./test_data/"
settings.mint_host = "localhost" settings.mint_host = "localhost"
settings.mint_port = SERVER_PORT settings.mint_port = SERVER_PORT
settings.mint_host = "0.0.0.0"
settings.mint_listen_port = SERVER_PORT settings.mint_listen_port = SERVER_PORT
settings.mint_url = SERVER_ENDPOINT settings.mint_url = SERVER_ENDPOINT
settings.tor = False settings.tor = False
@@ -139,6 +139,16 @@ def mint():
server = UvicornServer(config=config) server = UvicornServer(config=config)
server.start() server.start()
time.sleep(1)
# Wait until the server has bound to the localhost socket. Max out after 10s.
tries = 0
while tries < 100:
try:
httpx.get(settings.mint_url)
break
except httpx.ConnectError:
tries += 1
time.sleep(0.1)
yield server yield server
server.stop() server.stop()