Files
VoiceStudio/tests/test_segmented_download_default.py
T
marreiradigital 66f7ef8cfe fix(download): reentra no acelerador na proxima tentativa apos queda
Achados do CodeRabbit no PR #1942.

O mais grave: com o erro classificado como transitorio, o codigo mantinha o
acelerador ligado mas caia direto no `snapshot_download` na MESMA tentativa. Se
esse download desse certo, o laco terminava e o manifesto do `.part` nunca era
reusado — exatamente o recomeco-do-zero que a correcao existe para impedir.

Agora o erro transitorio e propagado para o retry externo, cuja proxima
tentativa reentra no `_segmented_snapshot` e retoma do manifesto. A decisao
virou o helper puro `_segmented_retry_plan`, testavel direto (o laco mora dentro
de `install_model`, uma rota de ~200 linhas). A ultima tentativa fica reservada
para o caminho simples, entao o acelerador continua sem poder ser o motivo de um
install falhar de vez.

Tambem deste round de revisao:

- `Invoke-CimMethod ... Terminate` tinha o retorno descartado com `$null =`. O
  Win32_Process.Terminate reporta falha pelo ReturnValue, nao lancando: um kill
  negado por permissao era reportado como sucesso e a porta seguia presa. Agora
  o ReturnValue e validado, com exit 4 proprio e a mensagem carregando o codigo.
- O teste de concorrencia era vazio: o handler sincrono do MockTransport retorna
  antes de qualquer outra task rodar, entao `peak` nunca passava de 1 e a
  asserção `peak <= 4` passava sem exercitar o semaforo. Passou a segurar as
  requisicoes abertas com um asyncio.Event e a exigir `peak == 4` (verificado:
  com o semaforo afrouxado para 1000, o teste acusa 31).
- A doc dizia que OMNIVOICE_DOWNLOAD_MAX_WORKERS limita as faixas e que origem
  sem Range cai no snapshot_download. Nenhum dos dois: `_segmented_snapshot` nao
  passa `num_connections` (usa as 8 padrao) e origem sem Range vira stream unico
  dentro do proprio acelerador.
- Entradas de Highlights do CHANGELOG sem o `(#NNNN)` exigido.
2026-09-08 23:12:14 -04:00

70 lines
2.7 KiB
Python

"""FDL-09: the segmented (multi-connection) downloader is ON by default.
The app forces the legacy-LFS path (HF_HUB_DISABLE_XET=1) for clear progress,
which is single-stream and slow. The segmented accelerator restores parallel
byte-range speed with a safe fallback to snapshot_download — so it ships ON by
default for fast first-run downloads. This pins the default so it can't silently
regress to opt-in, and that the env override still disables it.
"""
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "backend"))
from api.routers.setup.download import _segmented_enabled # noqa: E402
def test_segmented_is_on_by_default(monkeypatch):
monkeypatch.delenv("OMNIVOICE_SEGMENTED_DOWNLOAD", raising=False)
assert _segmented_enabled() is True
def test_env_override_can_disable(monkeypatch):
monkeypatch.setenv("OMNIVOICE_SEGMENTED_DOWNLOAD", "0")
assert _segmented_enabled() is False
def test_env_override_truthy_keeps_it_on(monkeypatch):
for val in ("1", "true", "on", "yes"):
monkeypatch.setenv("OMNIVOICE_SEGMENTED_DOWNLOAD", val)
assert _segmented_enabled() is True
# The accelerator must be re-entered on the NEXT attempt after a dropped
# connection, so it resumes from its .part manifest. Falling straight through to
# snapshot_download in the same attempt finishes the install from a separate
# .incomplete file and strands the manifest — restart-from-zero all over again.
import httpx # noqa: E402
from api.routers.setup.download import _segmented_retry_plan # noqa: E402
_MAX = 5
def _dropped():
return httpx.RemoteProtocolError(
"peer closed connection without sending complete message body"
)
def test_dropped_connection_reraises_so_the_next_attempt_resumes():
for attempt in (1, 2, 3):
disable, reraise = _segmented_retry_plan(_dropped(), attempt, _MAX)
assert reraise is True, f"attempt {attempt} must reach the outer retry"
assert disable is False, f"attempt {attempt} must keep the accelerator"
def test_final_attempt_is_reserved_for_the_plain_path():
"""The accelerator can never be the reason an install fails outright."""
disable, reraise = _segmented_retry_plan(_dropped(), _MAX - 1, _MAX)
assert (disable, reraise) == (True, False)
disable, reraise = _segmented_retry_plan(_dropped(), _MAX, _MAX)
assert (disable, reraise) == (True, False)
def test_a_non_network_failure_disables_the_accelerator_at_once():
"""An accelerator that cannot work here must not burn every retry."""
disable, reraise = _segmented_retry_plan(ValueError("sha256 mismatch"), 1, _MAX)
assert (disable, reraise) == (True, False)