|
| 1 | +import socket |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | + |
| 4 | + |
| 5 | +def test_create_aiohttp_transport_sets_socket_factory_when_enabled(monkeypatch): |
| 6 | + from litellm.llms.custom_httpx import http_handler as http_handler_module |
| 7 | + |
| 8 | + connector_mock = MagicMock(name="connector") |
| 9 | + session_mock = MagicMock(name="session") |
| 10 | + monkeypatch.setattr(http_handler_module, "AIOHTTP_TCP_KEEPALIVE", True) |
| 11 | + monkeypatch.setattr( |
| 12 | + http_handler_module, "_AIOHTTP_SUPPORTS_SOCKET_FACTORY", True |
| 13 | + ) |
| 14 | + |
| 15 | + with patch.object( |
| 16 | + http_handler_module, "TCPConnector", return_value=connector_mock |
| 17 | + ) as mock_tcp_connector: |
| 18 | + with patch.object( |
| 19 | + http_handler_module, "ClientSession", return_value=session_mock |
| 20 | + ): |
| 21 | + transport = http_handler_module.AsyncHTTPHandler._create_aiohttp_transport( |
| 22 | + shared_session=None |
| 23 | + ) |
| 24 | + transport._get_valid_client_session() |
| 25 | + |
| 26 | + assert ( |
| 27 | + mock_tcp_connector.call_args.kwargs["socket_factory"] |
| 28 | + is http_handler_module._tcp_keepalive_socket_factory |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +def test_create_aiohttp_transport_omits_socket_factory_when_disabled(monkeypatch): |
| 33 | + from litellm.llms.custom_httpx import http_handler as http_handler_module |
| 34 | + |
| 35 | + connector_mock = MagicMock(name="connector") |
| 36 | + session_mock = MagicMock(name="session") |
| 37 | + monkeypatch.setattr(http_handler_module, "AIOHTTP_TCP_KEEPALIVE", False) |
| 38 | + monkeypatch.setattr( |
| 39 | + http_handler_module, "_AIOHTTP_SUPPORTS_SOCKET_FACTORY", True |
| 40 | + ) |
| 41 | + |
| 42 | + with patch.object( |
| 43 | + http_handler_module, "TCPConnector", return_value=connector_mock |
| 44 | + ) as mock_tcp_connector: |
| 45 | + with patch.object( |
| 46 | + http_handler_module, "ClientSession", return_value=session_mock |
| 47 | + ): |
| 48 | + transport = http_handler_module.AsyncHTTPHandler._create_aiohttp_transport( |
| 49 | + shared_session=None |
| 50 | + ) |
| 51 | + transport._get_valid_client_session() |
| 52 | + |
| 53 | + assert "socket_factory" not in mock_tcp_connector.call_args.kwargs |
| 54 | + |
| 55 | + |
| 56 | +def test_create_aiohttp_transport_omits_socket_factory_when_unsupported(monkeypatch): |
| 57 | + """Older aiohttp without socket_factory must not receive the kwarg (would TypeError).""" |
| 58 | + from litellm.llms.custom_httpx import http_handler as http_handler_module |
| 59 | + |
| 60 | + connector_mock = MagicMock(name="connector") |
| 61 | + session_mock = MagicMock(name="session") |
| 62 | + monkeypatch.setattr(http_handler_module, "AIOHTTP_TCP_KEEPALIVE", True) |
| 63 | + monkeypatch.setattr( |
| 64 | + http_handler_module, "_AIOHTTP_SUPPORTS_SOCKET_FACTORY", False |
| 65 | + ) |
| 66 | + |
| 67 | + with patch.object( |
| 68 | + http_handler_module, "TCPConnector", return_value=connector_mock |
| 69 | + ) as mock_tcp_connector: |
| 70 | + with patch.object( |
| 71 | + http_handler_module, "ClientSession", return_value=session_mock |
| 72 | + ): |
| 73 | + transport = http_handler_module.AsyncHTTPHandler._create_aiohttp_transport( |
| 74 | + shared_session=None |
| 75 | + ) |
| 76 | + transport._get_valid_client_session() |
| 77 | + |
| 78 | + assert "socket_factory" not in mock_tcp_connector.call_args.kwargs |
| 79 | + |
| 80 | + |
| 81 | +def test_tcp_keepalive_socket_factory_sets_socket_options(): |
| 82 | + from litellm.llms.custom_httpx import http_handler as http_handler_module |
| 83 | + |
| 84 | + addr_info = socket.getaddrinfo( |
| 85 | + "127.0.0.1", 80, socket.AF_INET, socket.SOCK_STREAM |
| 86 | + )[0] |
| 87 | + sock = http_handler_module._tcp_keepalive_socket_factory(addr_info) |
| 88 | + try: |
| 89 | + assert sock.getsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE) == 1 |
| 90 | + assert sock.getsockopt( |
| 91 | + socket.IPPROTO_TCP, socket.TCP_KEEPIDLE |
| 92 | + ) == http_handler_module.AIOHTTP_TCP_KEEPALIVE_IDLE |
| 93 | + assert sock.getsockopt( |
| 94 | + socket.IPPROTO_TCP, socket.TCP_KEEPINTVL |
| 95 | + ) == http_handler_module.AIOHTTP_TCP_KEEPALIVE_INTVL |
| 96 | + assert sock.getsockopt( |
| 97 | + socket.IPPROTO_TCP, socket.TCP_KEEPCNT |
| 98 | + ) == http_handler_module.AIOHTTP_TCP_KEEPALIVE_CNT |
| 99 | + finally: |
| 100 | + sock.close() |
0 commit comments