Tiny add --log-requests-target (#16338)
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import io
|
||||
import json
|
||||
import tempfile
|
||||
import time
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
import requests
|
||||
|
||||
@@ -21,20 +24,26 @@ class BaseTestRequestLogger:
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls._temp_dir_obj = tempfile.TemporaryDirectory()
|
||||
cls.temp_dir = cls._temp_dir_obj.name
|
||||
cls.stdout = io.StringIO()
|
||||
cls.stderr = io.StringIO()
|
||||
other_args = [
|
||||
"--log-requests",
|
||||
"--log-requests-level",
|
||||
"2",
|
||||
"--log-requests-format",
|
||||
cls.log_requests_format,
|
||||
"--skip-server-warmup",
|
||||
"--log-requests-target",
|
||||
"stdout",
|
||||
cls.temp_dir,
|
||||
]
|
||||
cls.process = popen_launch_server(
|
||||
"Qwen/Qwen3-0.6B",
|
||||
DEFAULT_URL_FOR_TEST,
|
||||
timeout=DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH,
|
||||
other_args=[
|
||||
"--log-requests",
|
||||
"--log-requests-level",
|
||||
"2",
|
||||
"--log-requests-format",
|
||||
cls.log_requests_format,
|
||||
"--skip-server-warmup",
|
||||
],
|
||||
other_args=other_args,
|
||||
return_stdout_stderr=(cls.stdout, cls.stderr),
|
||||
)
|
||||
|
||||
@@ -43,8 +52,12 @@ class BaseTestRequestLogger:
|
||||
kill_process_tree(cls.process.pid)
|
||||
cls.stdout.close()
|
||||
cls.stderr.close()
|
||||
cls._temp_dir_obj.cleanup()
|
||||
|
||||
def _send_request(self):
|
||||
def _verify_logs(self, content: str, source_name: str):
|
||||
raise NotImplementedError
|
||||
|
||||
def test_logging(self):
|
||||
response = requests.post(
|
||||
DEFAULT_URL_FOR_TEST + "/generate",
|
||||
json={
|
||||
@@ -54,28 +67,34 @@ class BaseTestRequestLogger:
|
||||
timeout=30,
|
||||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
return self.stdout.getvalue() + self.stderr.getvalue()
|
||||
time.sleep(1)
|
||||
|
||||
stdout_content = self.stdout.getvalue() + self.stderr.getvalue()
|
||||
self._verify_logs(stdout_content, "stdout")
|
||||
|
||||
log_files = list(Path(self.temp_dir).glob("*.log"))
|
||||
self.assertGreater(len(log_files), 0, "No log files found in temp directory")
|
||||
|
||||
file_content = "".join(f.read_text() for f in log_files)
|
||||
self._verify_logs(file_content, "log files")
|
||||
|
||||
|
||||
class TestRequestLoggerText(BaseTestRequestLogger, CustomTestCase):
|
||||
log_requests_format = "text"
|
||||
|
||||
def test_text_format_logging(self):
|
||||
combined_output = self._send_request()
|
||||
self.assertIn("Receive:", combined_output)
|
||||
self.assertIn("Finish:", combined_output)
|
||||
def _verify_logs(self, content: str, source_name: str):
|
||||
self.assertIn("Receive:", content, f"'Receive:' not found in {source_name}")
|
||||
self.assertIn("Finish:", content, f"'Finish:' not found in {source_name}")
|
||||
|
||||
|
||||
class TestRequestLoggerJson(BaseTestRequestLogger, CustomTestCase):
|
||||
log_requests_format = "json"
|
||||
|
||||
def test_json_format_logging(self):
|
||||
combined_output = self._send_request()
|
||||
|
||||
def _verify_logs(self, content: str, source_name: str):
|
||||
received_found = False
|
||||
finished_found = False
|
||||
for line in combined_output.splitlines():
|
||||
if not line.startswith("{"):
|
||||
for line in content.splitlines():
|
||||
if not line.strip() or not line.startswith("{"):
|
||||
continue
|
||||
data = json.loads(line)
|
||||
if data.get("event") == "request.received":
|
||||
@@ -88,8 +107,12 @@ class TestRequestLoggerJson(BaseTestRequestLogger, CustomTestCase):
|
||||
self.assertIn("out", data)
|
||||
finished_found = True
|
||||
|
||||
self.assertTrue(received_found, "request.received event not found in logs")
|
||||
self.assertTrue(finished_found, "request.finished event not found in logs")
|
||||
self.assertTrue(
|
||||
received_found, f"request.received event not found in {source_name}"
|
||||
)
|
||||
self.assertTrue(
|
||||
finished_found, f"request.finished event not found in {source_name}"
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user