forked from eval-protocol/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_logs_server.py
More file actions
596 lines (482 loc) · 22.2 KB
/
Copy pathtest_logs_server.py
File metadata and controls
596 lines (482 loc) · 22.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
import asyncio
import json
import tempfile
import threading
import time
from pathlib import Path
from unittest.mock import AsyncMock, MagicMock, Mock, patch
import httpx
import psutil
import pytest
from fastapi import FastAPI
from fastapi.routing import APIWebSocketRoute
from fastapi.testclient import TestClient
from eval_protocol.dataset_logger import default_logger
from eval_protocol.dataset_logger.dataset_logger import LOG_EVENT_TYPE
from eval_protocol.event_bus import event_bus
from eval_protocol.models import EvalMetadata, EvaluationRow, InputMetadata, Message, Status
from eval_protocol.utils.logs_server import (
EvaluationWatcher,
LogsServer,
WebSocketManager,
create_app,
serve_logs,
)
class TestWebSocketManager:
"""Test WebSocketManager class."""
def test_initialization(self):
"""Test WebSocketManager initialization."""
manager = WebSocketManager()
assert len(manager.active_connections) == 0
assert manager._broadcast_queue is not None
assert manager._broadcast_task is None
@pytest.mark.asyncio
async def test_connect_disconnect(self):
"""Test WebSocket connection and disconnection."""
manager = WebSocketManager()
mock_websocket = AsyncMock()
# Test connection
with patch.object(default_logger, "read", return_value=[]):
await manager.connect(mock_websocket)
assert len(manager.active_connections) == 1
assert mock_websocket in manager.active_connections
mock_websocket.accept.assert_called_once()
# Test disconnection
manager.disconnect(mock_websocket)
assert len(manager.active_connections) == 0
assert mock_websocket not in manager.active_connections
@pytest.mark.asyncio
async def test_connect_sends_initial_logs(self):
"""Test that connecting sends initial logs."""
manager = WebSocketManager()
mock_websocket = AsyncMock()
# Mock default_logger.read()
mock_logs = [
EvaluationRow(
messages=[Message(role="user", content="test")],
input_metadata=InputMetadata(row_id="test-123"),
)
]
with patch.object(default_logger, "read", return_value=mock_logs):
await manager.connect(mock_websocket)
# Verify that initial logs were sent
mock_websocket.send_text.assert_called_once()
sent_data = json.loads(mock_websocket.send_text.call_args[0][0])
assert sent_data["type"] == "initialize_logs"
assert len(sent_data["logs"]) == 1
def test_broadcast_row_upserted(self):
"""Test broadcasting row upsert events."""
manager = WebSocketManager()
test_row = EvaluationRow(
messages=[Message(role="user", content="test")],
input_metadata=InputMetadata(row_id="test-123"),
)
# Test that broadcast doesn't fail when no connections
manager.broadcast_row_upserted(test_row)
# Test that message is queued
assert not manager._broadcast_queue.empty()
queued_message = manager._broadcast_queue.get_nowait()
data = json.loads(queued_message)
assert data["type"] == "log"
assert "row" in data
assert data["row"]["messages"][0]["content"] == "test"
assert data["row"]["input_metadata"]["row_id"] == "test-123"
@pytest.mark.asyncio
async def test_broadcast_loop(self):
"""Test the broadcast loop functionality."""
manager = WebSocketManager()
mock_websocket = AsyncMock()
await manager.connect(mock_websocket)
# Test that broadcast loop can be started and stopped
manager.start_broadcast_loop()
assert manager._broadcast_task is not None
# Stop broadcast loop
manager.stop_broadcast_loop()
assert manager._broadcast_task is None
@pytest.mark.asyncio
async def test_send_text_to_all_connections(self):
"""Test sending text to all connections."""
manager = WebSocketManager()
mock_websocket1 = AsyncMock()
mock_websocket2 = AsyncMock()
# Mock default_logger.read() to return empty logs
with patch.object(default_logger, "read", return_value=[]):
await manager.connect(mock_websocket1)
await manager.connect(mock_websocket2)
test_message = "test message"
await manager._send_text_to_all_connections(test_message)
# Check that the test message was sent to both websockets
mock_websocket1.send_text.assert_any_call(test_message)
mock_websocket2.send_text.assert_any_call(test_message)
@pytest.mark.asyncio
async def test_send_text_handles_failed_connections(self):
"""Test that failed connections are handled gracefully."""
manager = WebSocketManager()
mock_websocket1 = AsyncMock()
mock_websocket2 = AsyncMock()
# Mock default_logger.read() to return empty logs
with patch.object(default_logger, "read", return_value=[]):
await manager.connect(mock_websocket1)
await manager.connect(mock_websocket2)
# Make the second websocket fail AFTER connection is established
# We need to make send_text raise an exception when awaited
async def failing_send_text(text):
raise Exception("Connection failed")
mock_websocket2.send_text = failing_send_text
test_message = "test message"
await manager._send_text_to_all_connections(test_message)
# First websocket should receive the message
mock_websocket1.send_text.assert_any_call(test_message)
# Second websocket should have been removed due to failure
assert len(manager.active_connections) == 1
assert mock_websocket1 in manager.active_connections
class TestEvaluationWatcher:
"""Test EvaluationWatcher class."""
def test_initialization(self):
"""Test EvaluationWatcher initialization."""
mock_manager = Mock()
watcher = EvaluationWatcher(mock_manager)
assert watcher.websocket_manager == mock_manager
assert watcher._thread is None
assert watcher._stop_event is not None
def test_start_stop(self):
"""Test starting and stopping the watcher."""
mock_manager = Mock()
watcher = EvaluationWatcher(mock_manager)
# Test start
watcher.start()
assert watcher._thread is not None
assert watcher._thread.is_alive()
# Test stop
watcher.stop()
assert watcher._stop_event.is_set()
if watcher._thread:
watcher._thread.join(timeout=1.0)
@patch("psutil.Process")
def test_should_update_status_running_process(self, mock_process):
"""Test status update for running process."""
mock_manager = Mock()
watcher = EvaluationWatcher(mock_manager)
# Mock a running process
mock_process_instance = Mock()
mock_process_instance.is_running.return_value = True
mock_process.return_value = mock_process_instance
test_row = EvaluationRow(
messages=[Message(role="user", content="test")],
input_metadata=InputMetadata(row_id="test-123"),
eval_metadata=EvalMetadata(
name="test_eval", num_runs=1, aggregation_method="mean", status=Status.rollout_running()
),
pid=12345,
)
# Process is running, should not update
assert watcher._should_update_status(test_row) is False
@patch("psutil.Process")
def test_should_update_status_stopped_process(self, mock_process):
"""Test status update for stopped process."""
mock_manager = Mock()
watcher = EvaluationWatcher(mock_manager)
# Mock a stopped process
mock_process_instance = Mock()
mock_process_instance.is_running.return_value = False
mock_process.return_value = mock_process_instance
test_row = EvaluationRow(
messages=[Message(role="user", content="test")],
input_metadata=InputMetadata(row_id="test-123"),
eval_metadata=EvalMetadata(
name="test_eval", num_runs=1, aggregation_method="mean", status=Status.rollout_running()
),
pid=12345,
)
# Process is stopped, should update
assert watcher._should_update_status(test_row) is True
@patch("psutil.Process")
def test_should_update_status_no_such_process(self, mock_process):
"""Test status update for non-existent process."""
mock_manager = Mock()
watcher = EvaluationWatcher(mock_manager)
# Mock process not found
mock_process.side_effect = psutil.NoSuchProcess(pid=999)
test_row = EvaluationRow(
messages=[Message(role="user", content="test")],
input_metadata=InputMetadata(row_id="test-123"),
eval_metadata=EvalMetadata(
name="test_eval", num_runs=1, aggregation_method="mean", status=Status.rollout_running()
),
pid=999,
)
# Process doesn't exist, should update
assert watcher._should_update_status(test_row) is True
def test_should_update_status_not_running(self):
"""Test status update for non-running evaluation."""
mock_manager = Mock()
watcher = EvaluationWatcher(mock_manager)
test_row = EvaluationRow(
messages=[Message(role="user", content="test")],
input_metadata=InputMetadata(row_id="test-123"),
eval_metadata=EvalMetadata(
name="test_eval", num_runs=1, aggregation_method="mean", status=Status.rollout_finished()
),
rollout_status=Status.rollout_finished(),
pid=12345,
)
# Not running status, should not update
assert watcher._should_update_status(test_row) is False
def test_should_update_status_no_pid(self):
"""Test status update for evaluation without PID."""
mock_manager = Mock()
watcher = EvaluationWatcher(mock_manager)
test_row = EvaluationRow(
messages=[Message(role="user", content="test")],
input_metadata=InputMetadata(row_id="test-123"),
eval_metadata=EvalMetadata(
name="test_eval", num_runs=1, aggregation_method="mean", status=Status.rollout_running()
),
pid=None,
)
# No PID, should not update
assert watcher._should_update_status(test_row) is False
class TestLogsServer:
"""Test LogsServer class."""
@pytest.fixture
def temp_build_dir(self):
"""Create a temporary build directory for testing."""
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
# Create a minimal index.html file
(temp_path / "index.html").write_text("<html><body>Test</body></html>")
# Create assets directory (required by ViteServer)
(temp_path / "assets").mkdir(exist_ok=True)
yield temp_path
def test_initialization(self, temp_build_dir: Path):
"""Test LogsServer initialization."""
server = LogsServer(build_dir=str(temp_build_dir))
assert server.build_dir == temp_build_dir
assert server.websocket_manager is not None
assert server.evaluation_watcher is not None
def test_initialization_invalid_build_dir(self):
"""Test LogsServer initialization with invalid build directory."""
with pytest.raises(FileNotFoundError, match="Build directory '/nonexistent/path' does not exist"):
LogsServer(build_dir="/nonexistent/path")
def test_websocket_routes(self, temp_build_dir):
"""Test that WebSocket routes are properly set up."""
server = LogsServer(build_dir=str(temp_build_dir))
# Check that the WebSocket endpoint exists
if not server.app.routes:
raise ValueError("No routes found")
for route in server.app.routes:
if isinstance(route, APIWebSocketRoute) and route.path == "/ws":
break
else:
raise ValueError("WebSocket route not found")
@pytest.mark.asyncio
async def test_handle_event(self, temp_build_dir):
"""Test event handling."""
server = LogsServer(build_dir=str(temp_build_dir))
# Test handling a log event
test_row = {
"messages": [{"role": "user", "content": "test"}],
"input_metadata": {"row_id": "test-123"},
}
server._handle_event(LOG_EVENT_TYPE, test_row)
# The event should be queued for broadcasting
assert not server.websocket_manager._broadcast_queue.empty()
@pytest.mark.asyncio
async def test_create_app_factory(self, temp_build_dir):
"""Test the create_app factory function."""
with patch("eval_protocol.utils.logs_server.LogsServer.start_loops") as mock_start_loops:
app = create_app(build_dir=str(temp_build_dir))
assert isinstance(app, FastAPI)
# Verify that start_loops was called
mock_start_loops.assert_called_once()
def test_serve_logs_convenience_function(self, temp_build_dir):
"""Test the serve_logs convenience function."""
# Mock the LogsServer.run method to avoid actually starting a server
with patch("eval_protocol.utils.logs_server.LogsServer.run") as mock_run:
# This should not raise an error
serve_logs(port=8001)
# Verify that the run method was called
mock_run.assert_called_once()
def test_serve_logs_port_parameter(self, temp_build_dir):
"""Test that serve_logs properly passes the port parameter to LogsServer."""
with patch("eval_protocol.utils.logs_server.LogsServer") as mock_logs_server_class:
mock_server_instance = Mock()
mock_logs_server_class.return_value = mock_server_instance
# Call serve_logs with a specific port
test_port = 9000
serve_logs(port=test_port)
# Verify that LogsServer was created with the correct port
mock_logs_server_class.assert_called_once_with(port=test_port, elasticsearch_config=None, debug=False)
# Verify that the run method was called on the instance
mock_server_instance.run.assert_called_once()
def test_serve_logs_default_port(self, temp_build_dir):
"""Test that serve_logs uses default port when none is specified."""
with patch("eval_protocol.utils.logs_server.LogsServer") as mock_logs_server_class:
mock_server_instance = Mock()
mock_logs_server_class.return_value = mock_server_instance
# Call serve_logs without specifying a port
serve_logs()
# Verify that LogsServer was created with None port (which will use LogsServer's default of 8000)
mock_logs_server_class.assert_called_once_with(port=None, elasticsearch_config=None, debug=False)
# Verify that the run method was called on the instance
mock_server_instance.run.assert_called_once()
@pytest.mark.asyncio
async def test_run_async_lifecycle(self, temp_build_dir):
"""Test the async lifecycle of the server."""
server = LogsServer(build_dir=str(temp_build_dir))
# Mock the uvicorn.Server to avoid actually starting a server
with patch("uvicorn.Server") as mock_uvicorn_server:
mock_server = Mock()
mock_server.serve = AsyncMock()
mock_uvicorn_server.return_value = mock_server
# Start the server
start_task = asyncio.create_task(server.run_async())
# Wait a bit for it to start
await asyncio.sleep(0.1)
# Cancel the task instead of calling non-existent stop method
start_task.cancel()
# Wait for the task to complete
try:
await start_task
except asyncio.CancelledError:
pass
class TestLogsServerIntegration:
"""Integration tests for LogsServer."""
@pytest.fixture
def temp_build_dir_with_files(self):
"""Create a temporary build directory with index.html and assets/ directory."""
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
# Create index.html
(temp_path / "index.html").write_text("<html><body>Test</body></html>")
# Create assets directory and some files inside it
assets_dir = temp_path / "assets"
assets_dir.mkdir()
(assets_dir / "app.js").write_text("console.log('test');")
(assets_dir / "style.css").write_text("body { color: black; }")
# Optionally, create a nested directory inside assets
(assets_dir / "nested").mkdir()
(assets_dir / "nested" / "file.txt").write_text("nested content")
yield temp_path
def test_static_file_serving(self, temp_build_dir_with_files):
"""Test that static files are served correctly."""
server = LogsServer(build_dir=str(temp_build_dir_with_files))
client = TestClient(server.app)
# Test serving index.html
response = client.get("/")
assert response.status_code == 200
assert "Test" in response.text
# Test serving static files
response = client.get("/assets/app.js")
assert response.status_code == 200
assert "console.log('test')" in response.text
response = client.get("/assets/style.css")
assert response.status_code == 200
assert "color: black" in response.text
def test_spa_routing(self, temp_build_dir_with_files):
"""Test SPA routing fallback."""
server = LogsServer(build_dir=str(temp_build_dir_with_files))
client = TestClient(server.app)
# Test that non-existent routes fall back to index.html
response = client.get("/some/nonexistent/route")
assert response.status_code == 200
assert "Test" in response.text
def test_root_endpoint(self, temp_build_dir_with_files):
"""Test the root endpoint."""
server = LogsServer(build_dir=str(temp_build_dir_with_files))
client = TestClient(server.app)
response = client.get("/")
assert response.status_code == 200
assert "Test" in response.text
def test_health_endpoint(self, temp_build_dir_with_files):
"""Test the health endpoint."""
server = LogsServer(build_dir=str(temp_build_dir_with_files))
client = TestClient(server.app)
response = client.get("/health")
assert response.status_code == 200
data = response.json()
assert data["status"] == "ok"
@pytest.mark.asyncio
async def test_server_runs_on_specific_port(self):
"""Integration test: verify that LogsServer runs on specified port and handles port parameters correctly."""
import multiprocessing
import socket
def find_free_port():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("", 0))
s.listen(1)
port = s.getsockname()[1]
return port
test_port = find_free_port()
# Start server with dynamic port and build_dir
server_process = multiprocessing.Process(target=serve_logs, kwargs={"port": test_port}, daemon=True)
server_process.start()
# Wait for server to be ready
for _ in range(30):
try:
response = httpx.get(f"http://localhost:{test_port}/health", timeout=1)
if response.status_code == 200:
break
except httpx.RequestError:
pass
await asyncio.sleep(1)
async with httpx.AsyncClient() as client:
# Test health endpoint
response = await client.get(f"http://localhost:{test_port}/health", timeout=10)
assert response.status_code == 200
data = response.json()
assert data["status"] == "ok"
# Clean up server
if server_process.is_alive():
server_process.terminate()
server_process.join(timeout=2)
if server_process.is_alive():
server_process.kill()
server_process.join(timeout=1)
@pytest.mark.asyncio
class TestAsyncWebSocketOperations:
"""Test async WebSocket operations."""
async def test_websocket_connection_lifecycle(self):
"""Test complete WebSocket connection lifecycle."""
manager = WebSocketManager()
# Create mock WebSocket
mock_websocket = AsyncMock()
# Test connection
with patch.object(default_logger, "read", return_value=[]):
await manager.connect(mock_websocket)
assert len(manager.active_connections) == 1
# Test broadcasting without starting the loop
test_row = EvaluationRow(
messages=[Message(role="user", content="test")],
input_metadata=InputMetadata(row_id="test-123"),
)
manager.broadcast_row_upserted(test_row)
# Verify message was queued
assert not manager._broadcast_queue.empty()
# Test disconnection
manager.disconnect(mock_websocket)
assert len(manager.active_connections) == 0
async def test_multiple_websocket_connections(self):
"""Test handling multiple WebSocket connections."""
manager = WebSocketManager()
# Create multiple mock WebSockets
mock_websocket1 = AsyncMock()
mock_websocket2 = AsyncMock()
mock_websocket3 = AsyncMock()
# Connect all
with patch.object(default_logger, "read", return_value=[]):
await manager.connect(mock_websocket1)
await manager.connect(mock_websocket2)
await manager.connect(mock_websocket3)
assert len(manager.active_connections) == 3
# Test broadcasting to all without starting the loop
test_row = EvaluationRow(
messages=[Message(role="user", content="test")],
input_metadata=InputMetadata(row_id="test-123"),
)
manager.broadcast_row_upserted(test_row)
# Verify message was queued
assert not manager._broadcast_queue.empty()
# Disconnect one
manager.disconnect(mock_websocket2)
assert len(manager.active_connections) == 2