Skip to content

Commit ddff610

Browse files
committed
Get rid of _bytes_read. Add test with seek(0)
1 parent 9865eb5 commit ddff610

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/fastapi_cloud_cli/utils/progress_file.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@ def __init__(
1616
self._progress_callback = progress_callback
1717
self._update_interval = update_interval
1818
self._last_update_time = 0.0
19-
self._bytes_read = 0
2019

2120
def read(self, n: int = -1) -> bytes:
2221
data = self._file.read(n)
23-
self._bytes_read += len(data)
2422
now_ = datetime.now().timestamp()
2523
is_eof = (len(data) == 0) or (n > 0 and len(data) < n)
2624
if (now_ - self._last_update_time >= self._update_interval) or is_eof:
27-
self._progress_callback(self._bytes_read)
25+
self._progress_callback(self._file.tell())
2826
self._last_update_time = now_
2927
return data
3028

tests/test_progress_file.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,23 @@ def test_seek_and_tell() -> None:
105105
assert pf.tell() == 0
106106

107107

108+
def test_seek_resets_bytes_read() -> None:
109+
file = _make_file(b"abcde")
110+
mock_callback = Mock()
111+
112+
pf = ProgressFile(file, progress_callback=mock_callback)
113+
114+
pf.read(3)
115+
116+
# Imitate retrying
117+
pf.seek(0)
118+
pf.read(3)
119+
120+
pf.read(3)
121+
122+
mock_callback.assert_has_calls([call(3), call(5)])
123+
124+
108125
def test_iter_delegates() -> None:
109126
file = _make_file(b"line1\nline2\n")
110127
pf = ProgressFile(file, progress_callback=lambda _: None)

0 commit comments

Comments
 (0)