|
18 | 18 | from io import StringIO |
19 | 19 | import json |
20 | 20 | import sys |
| 21 | +import builtins |
21 | 22 | from unittest.mock import ANY |
22 | 23 | from unittest.mock import AsyncMock |
23 | 24 | from unittest.mock import Mock |
24 | 25 | from unittest.mock import patch |
25 | 26 |
|
26 | 27 | from google.adk.platform import thread as platform_thread |
| 28 | +from google.adk.tools.mcp_tool.mcp_session_manager import create_mcp_http_client |
27 | 29 | from google.adk.tools.mcp_tool.mcp_session_manager import MCPSessionManager |
28 | 30 | from google.adk.tools.mcp_tool.mcp_session_manager import retry_on_errors |
29 | 31 | from google.adk.tools.mcp_tool.mcp_session_manager import SseConnectionParams |
@@ -191,6 +193,48 @@ def test_init_with_streamable_http_default_httpx_factory( |
191 | 193 | ].get_default(), |
192 | 194 | ) |
193 | 195 |
|
| 196 | + @patch("google.adk.tools.mcp_tool.mcp_session_manager._create_mcp_http_client") |
| 197 | + def test_default_httpx_factory_instruments_client_when_available( |
| 198 | + self, mock_base_factory |
| 199 | + ): |
| 200 | + """Test default MCP HTTP factory instruments HTTPX client when available.""" |
| 201 | + client = Mock() |
| 202 | + mock_base_factory.return_value = client |
| 203 | + |
| 204 | + mock_instrumentor = Mock() |
| 205 | + with patch.dict( |
| 206 | + sys.modules, |
| 207 | + { |
| 208 | + "opentelemetry.instrumentation.httpx": Mock( |
| 209 | + HTTPXClientInstrumentor=mock_instrumentor |
| 210 | + ) |
| 211 | + }, |
| 212 | + ): |
| 213 | + result = create_mcp_http_client() |
| 214 | + |
| 215 | + assert result is client |
| 216 | + mock_instrumentor.instrument_client.assert_called_once_with(client) |
| 217 | + |
| 218 | + @patch("google.adk.tools.mcp_tool.mcp_session_manager._create_mcp_http_client") |
| 219 | + def test_default_httpx_factory_handles_missing_opentelemetry( |
| 220 | + self, mock_base_factory |
| 221 | + ): |
| 222 | + """Test default MCP HTTP factory works without OTel instrumentation.""" |
| 223 | + client = Mock() |
| 224 | + mock_base_factory.return_value = client |
| 225 | + |
| 226 | + original_import = builtins.__import__ |
| 227 | + |
| 228 | + def import_with_missing_otel(name, *args, **kwargs): |
| 229 | + if name == "opentelemetry.instrumentation.httpx": |
| 230 | + raise ImportError("missing test dependency") |
| 231 | + return original_import(name, *args, **kwargs) |
| 232 | + |
| 233 | + with patch("builtins.__import__", side_effect=import_with_missing_otel): |
| 234 | + result = create_mcp_http_client() |
| 235 | + |
| 236 | + assert result is client |
| 237 | + |
194 | 238 | def test_generate_session_key_stdio(self): |
195 | 239 | """Test session key generation for stdio connections.""" |
196 | 240 | manager = MCPSessionManager(self.mock_stdio_connection_params) |
|
0 commit comments