|
| 1 | +"""Test APIRemoteWorkspace timeout configuration.""" |
| 2 | + |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | +import httpx |
| 6 | + |
| 7 | + |
| 8 | +def test_api_timeout_is_used_in_client(): |
| 9 | + """Test that api_timeout parameter is used for the HTTP client timeout.""" |
| 10 | + from openhands.workspace import APIRemoteWorkspace |
| 11 | + |
| 12 | + # Mock the entire initialization process |
| 13 | + with patch.object(APIRemoteWorkspace, "_start_or_attach_to_runtime") as mock_init: |
| 14 | + mock_init.return_value = None |
| 15 | + |
| 16 | + # Create a workspace with custom api_timeout |
| 17 | + custom_timeout = 300.0 |
| 18 | + workspace = APIRemoteWorkspace( |
| 19 | + runtime_api_url="https://example.com", |
| 20 | + runtime_api_key="test-key", |
| 21 | + server_image="test-image", |
| 22 | + api_timeout=custom_timeout, |
| 23 | + ) |
| 24 | + |
| 25 | + # The runtime properties need to be set for client initialization |
| 26 | + workspace._runtime_id = "test-runtime-id" |
| 27 | + workspace._runtime_url = "https://test-runtime.com" |
| 28 | + workspace._session_api_key = "test-session-key" |
| 29 | + workspace.host = workspace._runtime_url |
| 30 | + |
| 31 | + # Access the client property to trigger initialization |
| 32 | + client = workspace.client |
| 33 | + |
| 34 | + # Verify that the client's timeout uses the custom api_timeout |
| 35 | + assert isinstance(client, httpx.Client) |
| 36 | + assert client.timeout.read == custom_timeout |
| 37 | + assert client.timeout.connect == 10.0 |
| 38 | + assert client.timeout.write == 10.0 |
| 39 | + assert client.timeout.pool == 10.0 |
| 40 | + |
| 41 | + # Clean up |
| 42 | + workspace._runtime_id = None # Prevent cleanup from trying to stop runtime |
| 43 | + workspace.cleanup() |
| 44 | + |
| 45 | + |
| 46 | +def test_api_timeout_default_value(): |
| 47 | + """Test that the default api_timeout is 60 seconds.""" |
| 48 | + from openhands.workspace import APIRemoteWorkspace |
| 49 | + |
| 50 | + with patch.object(APIRemoteWorkspace, "_start_or_attach_to_runtime") as mock_init: |
| 51 | + mock_init.return_value = None |
| 52 | + |
| 53 | + workspace = APIRemoteWorkspace( |
| 54 | + runtime_api_url="https://example.com", |
| 55 | + runtime_api_key="test-key", |
| 56 | + server_image="test-image", |
| 57 | + ) |
| 58 | + |
| 59 | + # The runtime properties need to be set for client initialization |
| 60 | + workspace._runtime_id = "test-runtime-id" |
| 61 | + workspace._runtime_url = "https://test-runtime.com" |
| 62 | + workspace._session_api_key = "test-session-key" |
| 63 | + workspace.host = workspace._runtime_url |
| 64 | + |
| 65 | + # Access the client property to trigger initialization |
| 66 | + client = workspace.client |
| 67 | + |
| 68 | + # Verify default timeout is 60 seconds |
| 69 | + assert client.timeout.read == 60.0 |
| 70 | + |
| 71 | + # Clean up |
| 72 | + workspace._runtime_id = None |
| 73 | + workspace.cleanup() |
| 74 | + |
| 75 | + |
| 76 | +def test_different_timeout_values(): |
| 77 | + """Test that different api_timeout values are correctly applied.""" |
| 78 | + from openhands.workspace import APIRemoteWorkspace |
| 79 | + |
| 80 | + test_timeouts = [30.0, 120.0, 600.0] |
| 81 | + |
| 82 | + for timeout_value in test_timeouts: |
| 83 | + with patch.object( |
| 84 | + APIRemoteWorkspace, "_start_or_attach_to_runtime" |
| 85 | + ) as mock_init: |
| 86 | + mock_init.return_value = None |
| 87 | + |
| 88 | + workspace = APIRemoteWorkspace( |
| 89 | + runtime_api_url="https://example.com", |
| 90 | + runtime_api_key="test-key", |
| 91 | + server_image="test-image", |
| 92 | + api_timeout=timeout_value, |
| 93 | + ) |
| 94 | + |
| 95 | + workspace._runtime_id = "test-runtime-id" |
| 96 | + workspace._runtime_url = "https://test-runtime.com" |
| 97 | + workspace._session_api_key = "test-session-key" |
| 98 | + workspace.host = workspace._runtime_url |
| 99 | + |
| 100 | + client = workspace.client |
| 101 | + |
| 102 | + assert client.timeout.read == timeout_value, ( |
| 103 | + f"Expected timeout {timeout_value}, got {client.timeout.read}" |
| 104 | + ) |
| 105 | + |
| 106 | + workspace._runtime_id = None |
| 107 | + workspace.cleanup() |
0 commit comments