setup: a download that gets no data for two minutes says so with the percentage and the one next step, instead of a line that never moves (#3567); test

This commit is contained in:
Niko1221 committed 2026-10-24 13:35:43 +02:00
1 parent aef053e353
commit ed0c8939aa
2 files changed
+60

No files matched your search

+26
View File
@@ -2543,6 +2543,27 @@ def _piece_worker(src: _Source, st: _Pieces, todo: list, part: Path):
st.stop.set()
class StallNote:
"""A download that gets no byte for a while says so (#3567): "stalled at 69%" instead of a silent line that never moves. `check` is called
often with the bytes so far; it answers a sentence at most once a minute while nothing arrives, and None otherwise."""
AFTER = 120.0
def __init__(self, now: float = 0.0):
self.have, self.since, self.said = -1, now, 0.0
def check(self, have: int, total: int, now: float) -> str | None:
if have != self.have:
self.have, self.since, self.said = have, now, 0.0
return None
idle = now - self.since
if idle < self.AFTER or now - self.said < 60:
return None
self.said = now
return (f"no data for {int(idle // 60)} min at {100 * have // max(1, total)}%: the connection is stuck. It keeps trying; to stop, press Ctrl+C, "
"and run the same command again: the download continues where it stopped")
def parallel_download(url: str, part: Path, total: int, label: str, conns: int, name: str) -> bool:
"""Fill `part` (total bytes) over `conns` ranged connections. True when every byte is there; False when the server
does not do ranges (the caller reads the file in one stream)."""
@@ -2562,6 +2583,7 @@ def parallel_download(url: str, part: Path, total: int, label: str, conns: int,
if not part.with_name(part.name + ".chunks").is_file() and not _save_pieces(part, total, done):
return False # no piece list can be kept here: the pieces would not be told from holes
last_print = last_save = 0.0
stall = StallNote(time.time())
def report(force=False):
nonlocal last_print, last_save
@@ -2570,6 +2592,10 @@ def parallel_download(url: str, part: Path, total: int, label: str, conns: int,
last_print = now
have = st.have()
print(f"\r {label}: {have / 1e9:6.2f} / {total / 1e9:.2f} GB ({100 * have / total:.0f}%) ", end="", flush=True)
note = stall.check(have, total, now)
if note:
print()
warn(note)
if now - last_save > DL_SAVE_EVERY:
last_save = now
_save_pieces(part, total, st.snapshot())
+34
View File
@@ -0,0 +1,34 @@
"""A download with no data for two minutes says so (#3567).
python -m unittest tools.test_setup_stall_note
"""
import sys
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
import setup # noqa: E402
class Stall(unittest.TestCase):
def test_silent_while_bytes_arrive_and_for_the_first_two_minutes(self):
s = setup.StallNote(0.0)
self.assertIsNone(s.check(10, 100, 1.0))
self.assertIsNone(s.check(20, 100, 200.0)) # moving
self.assertIsNone(s.check(20, 100, 300.0)) # 100 s idle
def test_says_it_once_a_minute_and_stops_when_data_comes(self):
s = setup.StallNote(0.0)
s.check(69, 100, 1.0)
said = s.check(69, 100, 130.0)
self.assertIn("no data for 2 min at 69%", said)
self.assertIn("run the same command again", said)
self.assertIsNone(s.check(69, 100, 150.0))
self.assertIn("min at 69%", s.check(69, 100, 200.0))
self.assertIsNone(s.check(70, 100, 201.0))
self.assertIsNone(s.check(70, 100, 250.0))
if __name__ == "__main__":
unittest.main()