fix tests

This commit is contained in:
Duncan Tourolle 2025-11-08 12:42:17 +01:00
parent 39622c7dd7
commit 5c0b22569a

View File

@ -348,6 +348,9 @@ class TestImagePIL(unittest.TestCase):
@classmethod @classmethod
def _start_flask_server(cls): def _start_flask_server(cls):
"""Start a Flask server for URL testing.""" """Start a Flask server for URL testing."""
import urllib.request
import urllib.error
cls.flask_app = Flask(__name__) cls.flask_app = Flask(__name__)
cls.flask_port = 5555 # Use a specific port for testing cls.flask_port = 5555 # Use a specific port for testing
cls.flask_server_running = True cls.flask_server_running = True
@ -356,6 +359,10 @@ class TestImagePIL(unittest.TestCase):
def serve_test_image(): def serve_test_image():
return send_file(cls.jpg_path, mimetype='image/jpeg') return send_file(cls.jpg_path, mimetype='image/jpeg')
@cls.flask_app.route('/health')
def health_check():
return 'OK', 200
def run_flask(): def run_flask():
cls.flask_app.run(host='127.0.0.1', port=cls.flask_port, debug=False, cls.flask_app.run(host='127.0.0.1', port=cls.flask_port, debug=False,
use_reloader=False, threaded=True) use_reloader=False, threaded=True)
@ -363,8 +370,20 @@ class TestImagePIL(unittest.TestCase):
cls.flask_thread = threading.Thread(target=run_flask, daemon=True) cls.flask_thread = threading.Thread(target=run_flask, daemon=True)
cls.flask_thread.start() cls.flask_thread.start()
# Wait for server to start # Wait for server to be ready with health check
time.sleep(1) max_wait = 5 # Maximum 5 seconds
wait_interval = 0.1 # Check every 100ms
elapsed = 0
while elapsed < max_wait:
try:
with urllib.request.urlopen(f'http://127.0.0.1:{cls.flask_port}/health', timeout=1) as response:
if response.status == 200:
break
except (urllib.error.URLError, ConnectionRefusedError, OSError):
pass
time.sleep(wait_interval)
elapsed += wait_interval
def test_image_url_detection(self): def test_image_url_detection(self):
"""Test URL detection functionality.""" """Test URL detection functionality."""