66from pathlib import Path
77from unittest .mock import AsyncMock , MagicMock , Mock , patch
88
9+ import httpx
910import psutil
1011import pytest
1112from fastapi import FastAPI
@@ -332,10 +333,14 @@ async def test_handle_event(self, temp_build_dir):
332333 # The event should be queued for broadcasting
333334 assert not server .websocket_manager ._broadcast_queue .empty ()
334335
335- def test_create_app_factory (self , temp_build_dir ):
336+ @pytest .mark .asyncio
337+ async def test_create_app_factory (self , temp_build_dir ):
336338 """Test the create_app factory function."""
337- app = create_app (build_dir = str (temp_build_dir ))
338- assert isinstance (app , FastAPI )
339+ with patch ("eval_protocol.utils.logs_server.LogsServer.start_loops" ) as mock_start_loops :
340+ app = create_app (build_dir = str (temp_build_dir ))
341+ assert isinstance (app , FastAPI )
342+ # Verify that start_loops was called
343+ mock_start_loops .assert_called_once ()
339344
340345 def test_serve_logs_convenience_function (self , temp_build_dir ):
341346 """Test the serve_logs convenience function."""
@@ -475,13 +480,11 @@ def test_health_endpoint(self, temp_build_dir_with_files):
475480 assert data ["status" ] == "ok"
476481
477482 @pytest .mark .asyncio
478- async def test_server_runs_on_specific_port (self , temp_build_dir_with_files ):
479- """Integration test: verify that LogsServer actually runs on the specified port (async requests)."""
483+ async def test_server_runs_on_specific_port (self ):
484+ """Integration test: verify that LogsServer runs on specified port and handles port parameters correctly."""
485+ import multiprocessing
480486 import socket
481487
482- import httpx
483-
484- # Find an available port for testing
485488 def find_free_port ():
486489 with socket .socket (socket .AF_INET , socket .SOCK_STREAM ) as s :
487490 s .bind (("" , 0 ))
@@ -491,54 +494,34 @@ def find_free_port():
491494
492495 test_port = find_free_port ()
493496
494- # Create and start server in background
495- server = LogsServer (build_dir = str (temp_build_dir_with_files ), port = test_port )
496-
497- # Start server in background task
498- server_task = asyncio .create_task (server .run_async ())
499-
500- try :
501- # Wait longer for server to start and be ready
502- await asyncio .sleep (3 )
503-
504- async with httpx .AsyncClient () as client :
505- # Test that we can actually connect to the server on the specified port
506- response = await client .get (f"http://localhost:{ test_port } /" , timeout = 10 )
507- assert response .status_code == 200
508- assert "Test" in response .text
497+ # Start server with dynamic port and build_dir
498+ server_process = multiprocessing .Process (target = serve_logs , kwargs = {"port" : test_port }, daemon = True )
499+ server_process .start ()
509500
510- # Test the health endpoint
511- response = await client .get (f"http://localhost:{ test_port } /health" , timeout = 10 )
512- assert response .status_code == 200
513- data = response .json ()
514- assert data ["status" ] == "ok"
515-
516- finally :
517- # Clean up
518- server_task .cancel ()
501+ # Wait for server to be ready
502+ for _ in range (20 ):
519503 try :
520- await server_task
521- except asyncio .CancelledError :
504+ response = httpx .get (f"http://localhost:{ test_port } /health" , timeout = 1 )
505+ if response .status_code == 200 :
506+ break
507+ except httpx .RequestError :
522508 pass
523-
524- def test_serve_logs_port_parameter_integration (self , temp_build_dir_with_files ):
525- """Integration test: verify that serve_logs function actually works with port parameter."""
526- # This test verifies that serve_logs creates LogsServer with the correct port
527- # without actually starting the server
528- test_port = 9999
529-
530- # Use a different approach - mock the LogsServer class and verify the port parameter
531- with patch ("eval_protocol.utils.logs_server.LogsServer" ) as mock_logs_server_class :
532- mock_server_instance = Mock ()
533- mock_logs_server_class .return_value = mock_server_instance
534-
535- # Call serve_logs with specific port
536- serve_logs (port = test_port )
537-
538- # Verify that LogsServer was created with the correct port
539- mock_logs_server_class .assert_called_once_with (port = test_port )
540- # Verify that the run method was called on the instance
541- mock_server_instance .run .assert_called_once ()
509+ await asyncio .sleep (1 )
510+
511+ async with httpx .AsyncClient () as client :
512+ # Test health endpoint
513+ response = await client .get (f"http://localhost:{ test_port } /health" , timeout = 10 )
514+ assert response .status_code == 200
515+ data = response .json ()
516+ assert data ["status" ] == "ok"
517+
518+ # Clean up server
519+ if server_process .is_alive ():
520+ server_process .terminate ()
521+ server_process .join (timeout = 2 )
522+ if server_process .is_alive ():
523+ server_process .kill ()
524+ server_process .join (timeout = 1 )
542525
543526
544527@pytest .mark .asyncio
0 commit comments