1+ # Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
2+ # SPDX-License-Identifier: Apache-2.0
3+
14import logging
25import os
36import tempfile
47import threading
58from collections .abc import Iterable
69from concurrent import futures
7- from typing import Callable , Generator , Literal
10+ from typing import Callable , Dict , Generator , Literal
811
912import openai
1013import pytest
@@ -40,15 +43,18 @@ def Export(self, request, context):
4043 return ExportTraceServiceResponse ()
4144
4245
43- @pytest .fixture (scope = "function" )
46+ # The trace service binds a free port at runtime and exposes its address via the fixture.
47+ @pytest .fixture (scope = "module" )
4448def trace_service () -> Generator [FakeTraceService , None , None ]:
4549 """Fixture to set up a fake gRPC trace service"""
4650 executor = futures .ThreadPoolExecutor (max_workers = 1 )
4751 import grpc
4852 server = grpc .server (executor )
4953 service = FakeTraceService ()
5054 add_TraceServiceServicer_to_server (service , server )
51- server .add_insecure_port (FAKE_TRACE_SERVER_ADDRESS )
55+ # Bind to an ephemeral port to avoid conflicts with local collectors.
56+ port = server .add_insecure_port ("localhost:0" )
57+ service .address = f"localhost:{ port } "
5258 server .start ()
5359
5460 yield service
@@ -97,14 +103,14 @@ def temp_extra_llm_api_options_file(request):
97103
98104@pytest .fixture (scope = "module" )
99105def server (model_name : str , backend : str , temp_extra_llm_api_options_file : str ,
100- num_postprocess_workers : int ):
106+ num_postprocess_workers : int , trace_service : FakeTraceService ):
101107 model_path = get_model_path (model_name )
102108 args = ["--backend" , f"{ backend } " ]
103109 if backend == "trt" :
104110 args .extend (["--max_beam_width" , "4" ])
105111 args .extend (["--extra_llm_api_options" , temp_extra_llm_api_options_file ])
106112 args .extend (["--num_postprocess_workers" , f"{ num_postprocess_workers } " ])
107- args .extend (["--otlp_traces_endpoint" , FAKE_TRACE_SERVER_ADDRESS ])
113+ args .extend (["--otlp_traces_endpoint" , trace_service . address ])
108114
109115 os .environ [OTEL_EXPORTER_OTLP_TRACES_INSECURE ] = "true"
110116
@@ -117,7 +123,7 @@ def server(model_name: str, backend: str, temp_extra_llm_api_options_file: str,
117123
118124
119125def decode_value (value : AnyValue ):
120- field_decoders : dict [FieldName , Callable ] = {
126+ field_decoders : Dict [FieldName , Callable [[ AnyValue ], object ] ] = {
121127 "bool_value" : (lambda v : v .bool_value ),
122128 "string_value" : (lambda v : v .string_value ),
123129 "int_value" : (lambda v : v .int_value ),
0 commit comments