fix: 下载文件改由任务自己的线程读写,消除并发竞争和漏下载
This commit is contained in:
@@ -16,7 +16,9 @@ from app.features.download.journal import CsvJournal
|
||||
from app.features.download.router import active_downloads, router
|
||||
from app.features.download.rows import build_rows
|
||||
from app.features.download.spool import IdSpool
|
||||
from app.features.download import jobs as job_module
|
||||
from app.features.download import spool as spool_module
|
||||
from app.shared import files as files_module
|
||||
from app.platform.client import (
|
||||
MIN_TILE_WIDTH,
|
||||
NqiAuthError,
|
||||
@@ -326,10 +328,10 @@ async def _server_error_is_retried(monkeypatch):
|
||||
|
||||
def test_spool_keeps_downloading_when_meta_replace_is_denied(monkeypatch, tmp_path: Path):
|
||||
sleeps: list[float] = []
|
||||
monkeypatch.setattr(spool_module.time, "sleep", lambda seconds: sleeps.append(seconds))
|
||||
monkeypatch.setattr(files_module.time, "sleep", lambda seconds: sleeps.append(seconds))
|
||||
spool = IdSpool(tmp_path)
|
||||
spool.open()
|
||||
real = spool_module.os.replace
|
||||
real = files_module.os.replace
|
||||
calls = {"n": 0}
|
||||
|
||||
def flaky(src, dst):
|
||||
@@ -338,13 +340,39 @@ def test_spool_keeps_downloading_when_meta_replace_is_denied(monkeypatch, tmp_pa
|
||||
raise PermissionError(13, "Access is denied", str(src), 5)
|
||||
real(src, dst)
|
||||
|
||||
monkeypatch.setattr(spool_module.os, "replace", flaky)
|
||||
monkeypatch.setattr(files_module.os, "replace", flaky)
|
||||
assert spool.add("10") is True
|
||||
spool.flush()
|
||||
assert spool.committed == 1
|
||||
assert IdSpool._peek(tmp_path)["committed"] == 1
|
||||
assert IdSpool.read_meta(tmp_path)["committed"] == 1
|
||||
assert calls["n"] == 2
|
||||
assert sleeps
|
||||
spool.close()
|
||||
|
||||
|
||||
def test_spool_flush_failure_keeps_ids_and_trims_tail(monkeypatch, tmp_path: Path):
|
||||
spool = IdSpool(tmp_path)
|
||||
spool.open()
|
||||
real = spool_module.os.fsync
|
||||
failed = {"n": 0}
|
||||
|
||||
def fail_once(fd):
|
||||
if fd == spool._file.fileno() and failed["n"] == 0:
|
||||
failed["n"] += 1
|
||||
raise OSError(28, "No space left on device")
|
||||
return real(fd)
|
||||
|
||||
monkeypatch.setattr(spool_module.os, "fsync", fail_once)
|
||||
assert spool.add("7") and spool.add("8")
|
||||
with pytest.raises(OSError):
|
||||
spool.flush()
|
||||
assert spool.committed == 0
|
||||
assert (tmp_path / "ids.spool").stat().st_size == 0
|
||||
spool.flush()
|
||||
assert spool.committed == 2
|
||||
spool.mark_complete()
|
||||
assert spool.pull() == ["7", "8"]
|
||||
spool.close()
|
||||
|
||||
|
||||
def test_spool_writer_serves_flush_and_read_together(tmp_path: Path):
|
||||
@@ -359,7 +387,6 @@ def test_spool_writer_serves_flush_and_read_together(tmp_path: Path):
|
||||
spool.add(str(index))
|
||||
if index % 40 == 0:
|
||||
spool.flush()
|
||||
IdSpool.listing_finished(tmp_path)
|
||||
spool.mark_complete()
|
||||
except BaseException as exc:
|
||||
errors.append(exc)
|
||||
@@ -704,15 +731,18 @@ async def _auth_failure_pauses_without_counting(tmp_path: Path):
|
||||
assert "good-layer" in used and "good-grid" in used
|
||||
|
||||
|
||||
def test_unknown_download_error_is_generic(tmp_path: Path, caplog):
|
||||
def test_unknown_download_error_is_generic(tmp_path: Path, caplog, monkeypatch):
|
||||
monkeypatch.setattr(job_module, "PASS_RETRY_WAITS", (0, 0, 0))
|
||||
asyncio.run(_unknown_download_error_is_generic(tmp_path, caplog))
|
||||
|
||||
|
||||
async def _unknown_download_error_is_generic(tmp_path: Path, caplog):
|
||||
job = _job(tmp_path, grid_ids=["5"])
|
||||
calls = {"n": 0}
|
||||
|
||||
class Boom:
|
||||
async def layer_code(self, **_kwargs):
|
||||
calls["n"] += 1
|
||||
raise RuntimeError("socket exploded")
|
||||
|
||||
async def aclose(self):
|
||||
@@ -725,6 +755,7 @@ async def _unknown_download_error_is_generic(tmp_path: Path, caplog):
|
||||
await run_download(job)
|
||||
finally:
|
||||
_restore(saved)
|
||||
assert calls["n"] == 4
|
||||
assert job.status == "error"
|
||||
assert job.message == "下载中断,已下载的内容还在,点继续即可"
|
||||
assert "socket exploded" in caplog.text
|
||||
|
||||
Reference in New Issue
Block a user