|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from types import SimpleNamespace |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +import provider_hub.registry as registry |
| 7 | +from provider_hub.registry import HubProxyProvider |
| 8 | + |
| 9 | + |
| 10 | +WHISPER_CONFIG = { |
| 11 | + "endpoint": "http://127.0.0.1:9000", |
| 12 | + "ffmpeg_path": "ffmpeg", |
| 13 | + "pass_video_name": False, |
| 14 | + "response_timeout_seconds": 600, |
| 15 | + "transcription_timeout_seconds": 3600, |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +def _pin_global(monkeypatch, value): |
| 20 | + monkeypatch.setattr(registry, "_global_worker_timeout", lambda: float(value)) |
| 21 | + |
| 22 | + |
| 23 | +def test_worker_timeout_validator_default(): |
| 24 | + from app.config import settings |
| 25 | + |
| 26 | + assert int(settings.general.provider_hub_worker_timeout) == 120 |
| 27 | + |
| 28 | + |
| 29 | +def test_declared_transcription_timeout_drives_deadline(monkeypatch): |
| 30 | + _pin_global(monkeypatch, 120) |
| 31 | + provider = HubProxyProvider(**WHISPER_CONFIG) |
| 32 | + # 3600 declared + 30 margin, floored by 120, under the 86400 cap. |
| 33 | + assert provider._request_timeout() == 3630.0 |
| 34 | + |
| 35 | + |
| 36 | +def test_no_declared_timeout_uses_global_default(monkeypatch): |
| 37 | + _pin_global(monkeypatch, 120) |
| 38 | + provider = HubProxyProvider(endpoint="http://x", api_key="secret") |
| 39 | + assert provider._request_timeout() == 120.0 |
| 40 | + |
| 41 | + |
| 42 | +def test_global_default_is_a_floor(monkeypatch): |
| 43 | + _pin_global(monkeypatch, 5000) |
| 44 | + provider = HubProxyProvider(**WHISPER_CONFIG) |
| 45 | + # derived 3630 < global default 5000 -> floor wins. |
| 46 | + assert provider._request_timeout() == 5000.0 |
| 47 | + |
| 48 | + |
| 49 | +def test_declared_timeout_clamped_to_cap(monkeypatch): |
| 50 | + _pin_global(monkeypatch, 120) |
| 51 | + # A plugin timeout above the cap is clamped to _MAX_WORKER_REQUEST_TIMEOUT |
| 52 | + # before the margin is added, so the host wall is cap + margin. Defensive |
| 53 | + # only: the setting validator and plugin manifests already bound configured |
| 54 | + # values to the cap, so a value this high should not reach here in practice. |
| 55 | + provider = HubProxyProvider(endpoint="http://x", transcription_timeout_seconds=90000) |
| 56 | + assert provider._request_timeout() == registry._MAX_WORKER_REQUEST_TIMEOUT + 30.0 |
| 57 | + |
| 58 | + |
| 59 | +def test_margin_preserved_at_the_cap(monkeypatch): |
| 60 | + _pin_global(monkeypatch, 120) |
| 61 | + # Exactly at the manifest maximum, the +30 host margin must not be swallowed |
| 62 | + # by the cap (regression guard: host deadline > worker's own timeout). |
| 63 | + provider = HubProxyProvider(endpoint="http://x", transcription_timeout_seconds=86400) |
| 64 | + assert provider._request_timeout() == 86430.0 |
| 65 | + |
| 66 | + |
| 67 | +def test_explicit_constructor_timeout_override(monkeypatch): |
| 68 | + _pin_global(monkeypatch, 120) |
| 69 | + # An explicit timeout= above the floor drives the deadline (this is the only |
| 70 | + # path exercising the self.timeout contribution to `declared`). |
| 71 | + provider = HubProxyProvider(endpoint="http://x", timeout=5000) |
| 72 | + assert provider._request_timeout() == 5030.0 |
| 73 | + |
| 74 | + |
| 75 | +def test_legacy_worker_timeout_key_still_honored(monkeypatch): |
| 76 | + _pin_global(monkeypatch, 120) |
| 77 | + provider = HubProxyProvider(endpoint="http://x", worker_timeout=900) |
| 78 | + assert provider._request_timeout() == 930.0 |
| 79 | + |
| 80 | + |
| 81 | +def test_legacy_and_suffix_keys_take_the_larger(monkeypatch): |
| 82 | + _pin_global(monkeypatch, 120) |
| 83 | + # Both timeout categories present with the legacy key larger, so a refactor |
| 84 | + # that let the *_timeout_seconds loop overwrite (rather than extend) the |
| 85 | + # declared list would drop to 630 and fail here. |
| 86 | + provider = HubProxyProvider( |
| 87 | + endpoint="http://x", worker_timeout=3600, transcription_timeout_seconds=600 |
| 88 | + ) |
| 89 | + assert provider._request_timeout() == 3630.0 |
| 90 | + |
| 91 | + |
| 92 | +def test_registry_cap_covers_the_validator_maximum(): |
| 93 | + # The host cap must accommodate the largest value the setting/manifest allow |
| 94 | + # (validator lte and whisper manifest maximum are both 86400); otherwise a |
| 95 | + # user's configured timeout would be silently clipped below what they set. |
| 96 | + assert registry._MAX_WORKER_REQUEST_TIMEOUT >= 86400 |
| 97 | + |
| 98 | + |
| 99 | +def test_global_worker_timeout_reads_setting(monkeypatch): |
| 100 | + monkeypatch.setattr( |
| 101 | + "app.config.settings", |
| 102 | + SimpleNamespace(general=SimpleNamespace(provider_hub_worker_timeout=500)), |
| 103 | + raising=False, |
| 104 | + ) |
| 105 | + assert registry._global_worker_timeout() == 500.0 |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.parametrize("bad_value", [None, 0, -5, "oops"]) |
| 109 | +def test_global_worker_timeout_falls_back(monkeypatch, bad_value): |
| 110 | + # Missing, zero, negative, and non-numeric all fall back to the default via |
| 111 | + # _coerce_timeout; the docstring promises this for "missing/invalid". |
| 112 | + general = SimpleNamespace() if bad_value is None else SimpleNamespace( |
| 113 | + provider_hub_worker_timeout=bad_value |
| 114 | + ) |
| 115 | + monkeypatch.setattr( |
| 116 | + "app.config.settings", |
| 117 | + SimpleNamespace(general=general), |
| 118 | + raising=False, |
| 119 | + ) |
| 120 | + assert registry._global_worker_timeout() == 120.0 |
0 commit comments