9
9
from dataclasses import dataclass
10
10
from json .decoder import JSONDecodeError
11
11
from tempfile import NamedTemporaryFile
12
- from typing import Any , Sequence
13
- from unittest .mock import patch , MagicMock
12
+ from typing import Any
13
+ from unittest .mock import MagicMock , patch
14
14
from uuid import uuid4
15
15
16
16
import pytest
@@ -138,16 +138,19 @@ def test_an_error_is_raised_when_custom_logging_config_is_invalid_json():
138
138
139
139
140
140
@patch ("vllm.logger.VLLM_CONFIGURE_LOGGING" , 1 )
141
- @pytest .mark .parametrize ("unexpected_config" , (
142
- "Invalid string" ,
143
- [{
144
- "version" : 1 ,
145
- "loggers" : []
146
- }],
147
- 0 ,
148
- ))
141
+ @pytest .mark .parametrize (
142
+ "unexpected_config" ,
143
+ (
144
+ "Invalid string" ,
145
+ [{
146
+ "version" : 1 ,
147
+ "loggers" : []
148
+ }],
149
+ 0 ,
150
+ ),
151
+ )
149
152
def test_an_error_is_raised_when_custom_logging_config_is_unexpected_json (
150
- unexpected_config : Any ):
153
+ unexpected_config : Any , ):
151
154
"""This test calls _configure_vllm_root_logger again to test custom logging
152
155
config behavior, however it fails before any change in behavior or
153
156
configuration occurs."""
@@ -174,14 +177,16 @@ def test_custom_logging_config_is_parsed_and_used_when_provided():
174
177
"propagate" : False ,
175
178
}
176
179
},
177
- "version" : 1
180
+ "version" : 1 ,
178
181
}
179
182
with NamedTemporaryFile (encoding = "utf-8" , mode = "w" ) as logging_config_file :
180
183
logging_config_file .write (json .dumps (valid_logging_config ))
181
184
logging_config_file .flush ()
182
- with patch ("vllm.logger.VLLM_LOGGING_CONFIG_PATH" ,
183
- logging_config_file .name ), patch (
184
- "vllm.logger.dictConfig" ) as dict_config_mock :
185
+ with (
186
+ patch ("vllm.logger.VLLM_LOGGING_CONFIG_PATH" ,
187
+ logging_config_file .name ),
188
+ patch ("vllm.logger.dictConfig" ) as dict_config_mock ,
189
+ ):
185
190
_configure_vllm_root_logger ()
186
191
dict_config_mock .assert_called_with (valid_logging_config )
187
192
@@ -197,7 +202,7 @@ def test_custom_logging_config_causes_an_error_if_configure_logging_is_off():
197
202
"handlers" : [],
198
203
}
199
204
},
200
- "version" : 1
205
+ "version" : 1 ,
201
206
}
202
207
with NamedTemporaryFile (encoding = "utf-8" , mode = "w" ) as logging_config_file :
203
208
logging_config_file .write (json .dumps (valid_logging_config ))
@@ -223,21 +228,22 @@ def test_custom_logging_config_causes_an_error_if_configure_logging_is_off():
223
228
224
229
225
230
def test_prepare_object_to_dump ():
226
- str_obj = ' str'
231
+ str_obj = " str"
227
232
assert prepare_object_to_dump (str_obj ) == "'str'"
228
233
229
234
list_obj = [1 , 2 , 3 ]
230
- assert prepare_object_to_dump (list_obj ) == ' [1, 2, 3]'
235
+ assert prepare_object_to_dump (list_obj ) == " [1, 2, 3]"
231
236
232
- dict_obj = {'a' : 1 , 'b' : 'b' }
237
+ dict_obj = {"a" : 1 , "b" : "b" }
233
238
assert prepare_object_to_dump (dict_obj ) in [
234
- "{a: 1, b: 'b'}" , "{b: 'b', a: 1}"
239
+ "{a: 1, b: 'b'}" ,
240
+ "{b: 'b', a: 1}" ,
235
241
]
236
242
237
243
set_obj = {1 , 2 , 3 }
238
- assert prepare_object_to_dump (set_obj ) == ' [1, 2, 3]'
244
+ assert prepare_object_to_dump (set_obj ) == " [1, 2, 3]"
239
245
240
- tuple_obj = ('a' , 'b' , 'c' )
246
+ tuple_obj = ("a" , "b" , "c" )
241
247
assert prepare_object_to_dump (tuple_obj ) == "['a', 'b', 'c']"
242
248
243
249
class CustomEnum (enum .Enum ):
@@ -253,114 +259,115 @@ class CustomClass:
253
259
b : str
254
260
255
261
assert (prepare_object_to_dump (CustomClass (
256
- 1 , 'b' )) == "CustomClass(a=1, b='b')" )
262
+ 1 , "b" )) == "CustomClass(a=1, b='b')" )
257
263
258
264
259
265
def test_request_logger_log_outputs ():
260
266
"""Test the new log_outputs functionality."""
261
267
# Create a mock logger to capture log calls
262
268
mock_logger = MagicMock ()
263
-
264
- with patch (' vllm.entrypoints.logger.logger' , mock_logger ):
269
+
270
+ with patch (" vllm.entrypoints.logger.logger" , mock_logger ):
265
271
request_logger = RequestLogger (max_log_len = None )
266
-
272
+
267
273
# Test basic output logging
268
274
request_logger .log_outputs (
269
275
request_id = "test-123" ,
270
276
outputs = "Hello, world!" ,
271
277
output_token_ids = [1 , 2 , 3 , 4 ],
272
278
finish_reason = "stop" ,
273
279
is_streaming = False ,
274
- delta = False
280
+ delta = False ,
275
281
)
276
-
282
+
277
283
mock_logger .info .assert_called_once ()
278
284
call_args = mock_logger .info .call_args [0 ]
279
285
assert "Generated response test-123" in call_args [0 ]
280
286
assert "Hello, world!" in call_args [1 ]
281
- assert [ 1 , 2 , 3 , 4 ] == call_args [ 2 ]
282
- assert "stop" == call_args [3 ]
287
+ assert call_args [ 2 ] == [ 1 , 2 , 3 , 4 ]
288
+ assert call_args [3 ] == "stop"
283
289
284
290
285
291
def test_request_logger_log_outputs_streaming_delta ():
286
292
"""Test log_outputs with streaming delta mode."""
287
293
mock_logger = MagicMock ()
288
-
289
- with patch (' vllm.entrypoints.logger.logger' , mock_logger ):
294
+
295
+ with patch (" vllm.entrypoints.logger.logger" , mock_logger ):
290
296
request_logger = RequestLogger (max_log_len = None )
291
-
297
+
292
298
# Test streaming delta logging
293
299
request_logger .log_outputs (
294
300
request_id = "test-456" ,
295
301
outputs = "Hello" ,
296
302
output_token_ids = [1 ],
297
303
finish_reason = None ,
298
304
is_streaming = True ,
299
- delta = True
305
+ delta = True ,
300
306
)
301
-
307
+
302
308
mock_logger .info .assert_called_once ()
303
309
call_args = mock_logger .info .call_args [0 ]
304
310
assert "Generated response test-456 (streaming delta)" in call_args [0 ]
305
- assert "Hello" == call_args [1 ]
306
- assert [ 1 ] == call_args [ 2 ]
311
+ assert call_args [1 ] == "Hello"
312
+ assert call_args [ 2 ] == [ 1 ]
307
313
assert call_args [3 ] is None
308
314
309
315
310
316
def test_request_logger_log_outputs_streaming_complete ():
311
317
"""Test log_outputs with streaming complete mode."""
312
318
mock_logger = MagicMock ()
313
-
314
- with patch (' vllm.entrypoints.logger.logger' , mock_logger ):
319
+
320
+ with patch (" vllm.entrypoints.logger.logger" , mock_logger ):
315
321
request_logger = RequestLogger (max_log_len = None )
316
-
322
+
317
323
# Test streaming complete logging
318
324
request_logger .log_outputs (
319
325
request_id = "test-789" ,
320
326
outputs = "Complete response" ,
321
327
output_token_ids = [1 , 2 , 3 ],
322
328
finish_reason = "length" ,
323
329
is_streaming = True ,
324
- delta = False
330
+ delta = False ,
325
331
)
326
-
332
+
327
333
mock_logger .info .assert_called_once ()
328
334
call_args = mock_logger .info .call_args [0 ]
329
- assert "Generated response test-789 (streaming complete)" in call_args [0 ]
330
- assert "Complete response" == call_args [1 ]
331
- assert [1 , 2 , 3 ] == call_args [2 ]
332
- assert "length" == call_args [3 ]
335
+ assert ("Generated response test-789 (streaming complete)"
336
+ in call_args [0 ])
337
+ assert call_args [1 ] == "Complete response"
338
+ assert call_args [2 ] == [1 , 2 , 3 ]
339
+ assert call_args [3 ] == "length"
333
340
334
341
335
342
def test_request_logger_log_outputs_with_truncation ():
336
343
"""Test log_outputs respects max_log_len setting."""
337
344
mock_logger = MagicMock ()
338
-
339
- with patch (' vllm.entrypoints.logger.logger' , mock_logger ):
345
+
346
+ with patch (" vllm.entrypoints.logger.logger" , mock_logger ):
340
347
# Set max_log_len to 10
341
348
request_logger = RequestLogger (max_log_len = 10 )
342
-
349
+
343
350
# Test output truncation
344
351
long_output = "This is a very long output that should be truncated"
345
352
long_token_ids = list (range (20 )) # 20 tokens
346
-
353
+
347
354
request_logger .log_outputs (
348
355
request_id = "test-truncate" ,
349
356
outputs = long_output ,
350
357
output_token_ids = long_token_ids ,
351
358
finish_reason = "stop" ,
352
359
is_streaming = False ,
353
- delta = False
360
+ delta = False ,
354
361
)
355
-
362
+
356
363
mock_logger .info .assert_called_once ()
357
364
call_args = mock_logger .info .call_args
358
-
365
+
359
366
# Check that output was truncated to first 10 characters
360
367
logged_output = call_args [0 ][1 ]
361
368
assert logged_output == "This is a "
362
369
assert len (logged_output ) == 10
363
-
370
+
364
371
# Check that token IDs were truncated to first 10 tokens
365
372
logged_token_ids = call_args [0 ][2 ]
366
373
assert logged_token_ids == list (range (10 ))
@@ -370,60 +377,60 @@ def test_request_logger_log_outputs_with_truncation():
370
377
def test_request_logger_log_outputs_none_values ():
371
378
"""Test log_outputs handles None values correctly."""
372
379
mock_logger = MagicMock ()
373
-
374
- with patch (' vllm.entrypoints.logger.logger' , mock_logger ):
380
+
381
+ with patch (" vllm.entrypoints.logger.logger" , mock_logger ):
375
382
request_logger = RequestLogger (max_log_len = None )
376
-
383
+
377
384
# Test with None output_token_ids
378
385
request_logger .log_outputs (
379
386
request_id = "test-none" ,
380
387
outputs = "Test output" ,
381
388
output_token_ids = None ,
382
389
finish_reason = "stop" ,
383
390
is_streaming = False ,
384
- delta = False
391
+ delta = False ,
385
392
)
386
-
393
+
387
394
mock_logger .info .assert_called_once ()
388
395
call_args = mock_logger .info .call_args [0 ]
389
396
assert "Generated response test-none" in call_args [0 ]
390
- assert "Test output" == call_args [ 1 ]
397
+ assert call_args [ 1 ] == "Test output"
391
398
assert call_args [2 ] is None
392
- assert "stop" == call_args [3 ]
399
+ assert call_args [3 ] == "stop"
393
400
394
401
395
402
def test_request_logger_log_outputs_empty_output ():
396
403
"""Test log_outputs handles empty output correctly."""
397
404
mock_logger = MagicMock ()
398
-
399
- with patch (' vllm.entrypoints.logger.logger' , mock_logger ):
405
+
406
+ with patch (" vllm.entrypoints.logger.logger" , mock_logger ):
400
407
request_logger = RequestLogger (max_log_len = 5 )
401
-
408
+
402
409
# Test with empty output
403
410
request_logger .log_outputs (
404
411
request_id = "test-empty" ,
405
412
outputs = "" ,
406
413
output_token_ids = [],
407
414
finish_reason = "stop" ,
408
415
is_streaming = False ,
409
- delta = False
416
+ delta = False ,
410
417
)
411
-
418
+
412
419
mock_logger .info .assert_called_once ()
413
420
call_args = mock_logger .info .call_args [0 ]
414
421
assert "Generated response test-empty" in call_args [0 ]
415
- assert "" == call_args [1 ]
416
- assert [ ] == call_args [ 2 ]
417
- assert "stop" == call_args [3 ]
422
+ assert call_args [1 ] == ""
423
+ assert call_args [ 2 ] == [ ]
424
+ assert call_args [3 ] == "stop"
418
425
419
426
420
427
def test_request_logger_log_outputs_integration ():
421
428
"""Test that log_outputs can be called alongside log_inputs."""
422
429
mock_logger = MagicMock ()
423
-
424
- with patch (' vllm.entrypoints.logger.logger' , mock_logger ):
430
+
431
+ with patch (" vllm.entrypoints.logger.logger" , mock_logger ):
425
432
request_logger = RequestLogger (max_log_len = None )
426
-
433
+
427
434
# Test that both methods can be called without interference
428
435
request_logger .log_inputs (
429
436
request_id = "test-integration" ,
@@ -432,24 +439,24 @@ def test_request_logger_log_outputs_integration():
432
439
prompt_embeds = None ,
433
440
params = None ,
434
441
lora_request = None ,
435
- prompt_adapter_request = None
442
+ prompt_adapter_request = None ,
436
443
)
437
-
444
+
438
445
request_logger .log_outputs (
439
446
request_id = "test-integration" ,
440
447
outputs = "Test output" ,
441
448
output_token_ids = [4 , 5 , 6 ],
442
449
finish_reason = "stop" ,
443
450
is_streaming = False ,
444
- delta = False
451
+ delta = False ,
445
452
)
446
-
453
+
447
454
# Should have been called twice - once for inputs, once for outputs
448
455
assert mock_logger .info .call_count == 2
449
-
456
+
450
457
# Check that the calls were made with correct patterns
451
458
input_call = mock_logger .info .call_args_list [0 ][0 ]
452
459
output_call = mock_logger .info .call_args_list [1 ][0 ]
453
-
460
+
454
461
assert "Received request test-integration" in input_call [0 ]
455
462
assert "Generated response test-integration" in output_call [0 ]
0 commit comments