Files
douyi_guaji_main/tests/test_read_with_retries_resilience.py

94 lines
2.8 KiB
Python

from __future__ import annotations
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
def _mk_analyser():
a = SimpleNamespace()
a._fudai_detail_verified = False
a.fudai_detail_initial_wait_seconds = 0.0
a.fudai_detail_retry_delay_seconds = 0.0
a.fudai_detail_max_attempts = 1
a.operation = SimpleNamespace(delay=lambda _seconds: None)
a._log_kv = MagicMock(return_value=None)
return a
def test_status_ok_with_invalid_tuple_does_not_crash():
from douyin.fudai.core.fudai_detail_orchestrator_pkg.read_with_retries import (
read_fudai_detail_with_retries,
)
analyser = _mk_analyser()
with patch(
"douyin.fudai.core.fudai_detail_orchestrator_pkg.read_with_retries.read_fudai_detail_once",
return_value=("ok", None, {}),
):
result = read_fudai_detail_with_retries(
analyser,
fudai_x=0,
read_once_callback=None,
close_detail_callback=lambda *_a, **_k: None,
ensure_open_callback=lambda *_a, **_k: None,
)
assert isinstance(result, tuple)
assert len(result) == 6
def test_read_once_callback_is_used_when_provided():
from douyin.fudai.core.fudai_detail_orchestrator_pkg.read_with_retries import (
read_fudai_detail_with_retries,
)
analyser = _mk_analyser()
def custom_read_once(_analyser):
return ("watch_task", ("", "", 0, 0, 0.0, (0.0, 60.0)), {"source": "custom"})
result = read_fudai_detail_with_retries(
analyser,
fudai_x=0,
read_once_callback=custom_read_once,
close_detail_callback=lambda *_a, **_k: None,
ensure_open_callback=lambda *_a, **_k: None,
)
assert isinstance(result, tuple)
assert len(result) == 6
assert getattr(analyser, "_last_fudai_detail_status", "") == "watch_task"
def test_task_count_zero_reason_from_read_once_triggers_reopen_retry():
from douyin.fudai.core.fudai_detail_orchestrator_pkg.read_with_retries import (
read_fudai_detail_with_retries,
)
analyser = _mk_analyser()
analyser.fudai_detail_max_attempts = 2
reopen_calls = []
seq = [
("error", ("", "", 0, 0, 0.0, (0.0, 60.0)), {"reason": "not_in_fudai_detail", "task_count": 0}),
("watch_task", ("", "", 0, 0, 0.0, (0.0, 60.0)), {}),
]
with patch(
"douyin.fudai.core.fudai_detail_orchestrator_pkg.read_with_retries.read_fudai_detail_once",
side_effect=seq,
):
result = read_fudai_detail_with_retries(
analyser,
fudai_x=321,
read_once_callback=None,
close_detail_callback=lambda *_a, **_k: None,
ensure_open_callback=lambda _a, x: reopen_calls.append(x),
)
assert isinstance(result, tuple)
assert len(result) == 6
assert reopen_calls == [321]