perf: remove repeated project and log filesystem work without weakening checks

This commit is contained in:
阿白叔 committed 2026-10-19 17:15:21 +08:00
1 parent bbf9bffe32
commit e6ec267a26
10 files changed
+210 -6

No files matched your search

+74
View File
@@ -0,0 +1,74 @@
"""Bound repeated filesystem work without caching mutable file identities."""
from pathlib import Path
from types import SimpleNamespace
import errno
import pytest
from dx_productivity.trial_task_center import legacy_paths, logging_setup, quick_run, retention
def test_project_access_resolves_each_distinct_input_once_per_preparation(tmp_path,monkeypatch):
project=tmp_path/'board.prj';project.write_text('fixture')
(tmp_path/'command.tsv').write_text('project_path\n'+(str(project)+'\n')*1000,encoding='utf8')
original=Path.resolve;calls=[]
def resolve(path,*args,**kwargs):
if path==project:calls.append(path)
return original(path,*args,**kwargs)
monkeypatch.setattr(Path,'resolve',resolve)
legacy_paths._prepare_project_access(tmp_path)
assert len(calls)==1
assert not (tmp_path/'project-access.tsv').exists()
legacy_paths._prepare_project_access(tmp_path)
assert len(calls)==2 # No lifetime cache of filesystem trust.
def test_below_threshold_log_rotation_needs_only_one_live_stat(tmp_path,monkeypatch):
log=tmp_path/'events.jsonl';log.write_text('record')
original=Path.stat;calls=[]
def stat(path,*args,**kwargs):
if path==log:calls.append(path)
return original(path,*args,**kwargs)
monkeypatch.setattr(Path,'stat',stat)
assert not retention.rotate_text_log(log)
assert len(calls)==1
def test_existing_log_directories_avoid_mkdir_and_recreate_after_removal(tmp_path,monkeypatch):
context=SimpleNamespace(root=tmp_path)
directory=tmp_path/'tasks/task';directory.mkdir(parents=True)
original=Path.mkdir;calls=[]
def mkdir(path,*args,**kwargs):
if path==directory:calls.append(path)
return original(path,*args,**kwargs)
monkeypatch.setattr(Path,'mkdir',mkdir)
assert logging_setup._task_directory(context,'task')==directory
log=directory/'worker.log'
quick_run._append_log(log,'first')
assert calls==[]
log.unlink();directory.rmdir()
quick_run._append_log(log,'second')
assert calls==[directory]
assert log.read_text()=='second\n'
@pytest.mark.parametrize('error',[FileNotFoundError(errno.ENOENT,'missing'),
NotADirectoryError(errno.ENOTDIR,'not a directory'),OSError(errno.EBADF,'bad handle'),
ValueError('embedded null character')])
def test_log_rotation_keeps_path_exists_absence_semantics(tmp_path,monkeypatch,error):
log=tmp_path/'missing.jsonl'
original=Path.stat
def stat(path,*args,**kwargs):
if path==log:raise error
return original(path,*args,**kwargs)
monkeypatch.setattr(Path,'stat',stat)
assert retention.rotate_text_log(log) is False
def test_log_rotation_still_reports_permission_errors(tmp_path,monkeypatch):
log=tmp_path/'denied.jsonl'
original=Path.stat
def stat(path,*args,**kwargs):
if path==log:raise PermissionError(errno.EACCES,'denied')
return original(path,*args,**kwargs)
monkeypatch.setattr(Path,'stat',stat)
with pytest.raises(PermissionError):retention.rotate_text_log(log)