|
14 | 14 | get_sync_api_timeout, |
15 | 15 | set_sync_api_timeout, |
16 | 16 | ) |
| 17 | +from lmstudio.sync_api import _DEFAULT_TIMEOUT |
17 | 18 |
|
18 | 19 | from .support import EXPECTED_LLM_ID |
19 | 20 |
|
|
22 | 23 |
|
23 | 24 | @contextmanager |
24 | 25 | def sync_api_timeout(timeout: float | None) -> Generator[float | None, None, None]: |
25 | | - default_timeout = get_sync_api_timeout() |
| 26 | + previous_timeout = get_sync_api_timeout() |
| 27 | + set_sync_api_timeout(timeout) |
26 | 28 | try: |
27 | | - yield default_timeout |
| 29 | + yield previous_timeout |
28 | 30 | finally: |
29 | | - set_sync_api_timeout(default_timeout) |
| 31 | + set_sync_api_timeout(previous_timeout) |
| 32 | + |
| 33 | + |
| 34 | +def test_default_timeout() -> None: |
| 35 | + # Ensure default timeout is defined, but is not excessively short or long |
| 36 | + # (the bounds that are considered reasonable may change over time) |
| 37 | + assert _DEFAULT_TIMEOUT is not None |
| 38 | + assert _DEFAULT_TIMEOUT >= 60 |
| 39 | + assert _DEFAULT_TIMEOUT <= 600 |
30 | 40 |
|
31 | 41 |
|
32 | 42 | @pytest.mark.parametrize("timeout", (None, 0, 1.5, 3600, 3600 * 24 * 7)) |
33 | 43 | def test_timeout_updates_sync(timeout: float | None) -> None: |
34 | 44 | with sync_api_timeout(timeout) as previous_timeout: |
35 | | - assert previous_timeout is not None |
36 | | - assert previous_timeout > 0 |
37 | | - set_sync_api_timeout(timeout) |
| 45 | + assert previous_timeout == _DEFAULT_TIMEOUT |
38 | 46 | assert get_sync_api_timeout() == timeout |
39 | 47 | assert get_sync_api_timeout() == previous_timeout |
40 | 48 |
|
41 | 49 |
|
42 | 50 | @pytest.mark.lmstudio |
43 | | -def test_rpc_timeout_sync(caplog: LogCap) -> None: |
| 51 | +def test_timeout_rpc_sync(caplog: LogCap) -> None: |
44 | 52 | caplog.set_level(logging.DEBUG) |
45 | 53 |
|
46 | 54 | with Client() as client: |
47 | 55 | model = client.llm.model(EXPECTED_LLM_ID) |
48 | 56 | with sync_api_timeout(0): |
| 57 | + assert get_sync_api_timeout() == 0 |
49 | 58 | with pytest.raises(LMStudioTimeoutError): |
50 | | - model.get_info() |
| 59 | + response = model.get_info() |
| 60 | + logging.error(f"Unexpected response: {response}") |
51 | 61 |
|
52 | 62 |
|
53 | 63 | @pytest.mark.lmstudio |
54 | | -def test_channel_timeout_sync(caplog: LogCap) -> None: |
| 64 | +def test_timeout_channel_sync(caplog: LogCap) -> None: |
55 | 65 | caplog.set_level(logging.DEBUG) |
56 | 66 |
|
57 | 67 | with Client() as client: |
58 | 68 | model = client.llm.model(EXPECTED_LLM_ID) |
59 | 69 | with sync_api_timeout(0): |
| 70 | + assert get_sync_api_timeout() == 0 |
60 | 71 | with pytest.raises(LMStudioTimeoutError): |
61 | | - model.respond("This will time out") |
| 72 | + response = model.respond("This will time out") |
| 73 | + logging.error(f"Unexpected response: {response}") |
0 commit comments