test(phase-0): 确认基线全绿并预置迭代验收测试契约

This commit is contained in:
2026-10-31 01:14:39 +08:00
parent ffb043c24f
commit 5c283a79fc
7 changed files with 783 additions and 4 deletions
@@ -0,0 +1,134 @@
package com.nanri.aiimage.modules.file.service;
import com.nanri.aiimage.config.StorageProperties;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.scheduling.annotation.Scheduled;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* AC-10(R5):stale 分片会话必须由周期任务回收,不能等人 init 时顺手清。
*
* <p>Pending Phase 4 激活。今天 {@code ChunkedUploadService.cleanupStaleSessions()} 的
* 唯一调用点在 {@code init()} 里(:86),没有任何 {@code @Scheduled};断掉的上传会永久占盘,
* 而 {@code LocalTempCleanupService} 的射程只有 source/result/transientPayload,
* {@code chunk-uploads} 不在其列。</p>
*
* <p>接缝契约(改名必须同步改本文件并登记蓝图 §5):</p>
* <ul>
* <li>回收入口挂在 {@link ChunkedUploadService} 上,方法名不限,但必须有
* {@link Scheduled} 注解——**摘掉注解即判红**(本文件第一条断言就是它);</li>
* <li>「无人 init 也会清」:本用例先造 stale + fresh 两个会话,回拨 stale 的目录时间后
* <b>不再 init</b>,直接反射调用被注解的周期方法;</li>
* <li>近 N 分钟有片写入的会话不得误清(fresh 会话必须原样保留)。</li>
* </ul>
*
* <p>分片会话只有盘上 meta/part,无 DB 行;证据表的 DB 行保留期判据见
* {@code EvidencePhase3ContractTest}(AC-13 配置块)。</p>
*/
@Disabled("Pending Phase 4: 服务端残骸回收补洞(AC-10)")
class ChunkedUploadReclaimContractTest {
@TempDir
Path tempDir;
private ChunkedUploadService service;
@BeforeEach
void setUp() {
StorageProperties properties = new StorageProperties();
properties.setLocalTempDir(tempDir.toString());
properties.setCleanupEnabled(true);
service = new ChunkedUploadService(properties);
}
@Test
void 无人init也必须由周期任务回收stale会话() throws Exception {
List<Method> scheduled = scheduledMethods();
assertFalse(scheduled.isEmpty(),
"AC-10:ChunkedUploadService 上必须挂 @Scheduled 回收方法(摘掉注解即判红)");
ChunkedUploadService.ChunkSession stale = service.init("stale.bin", 2, 10, "evidence");
ChunkedUploadService.ChunkSession fresh = service.init("fresh.bin", 2, 10, "evidence");
Path staleDir = sessionDir(stale.uploadId());
Path freshDir = sessionDir(fresh.uploadId());
assertTrue(Files.isDirectory(staleDir));
// 回拨 stale 的目录时间(cleanupStaleSessions 以目录 mtime 判龄),fresh 保持当前时间
Instant old = Instant.now().minus(30, ChronoUnit.HOURS);
Files.setLastModifiedTime(staleDir, FileTime.from(old));
for (Method method : scheduled) {
method.setAccessible(true);
try {
method.invoke(service);
} catch (InvocationTargetException ex) {
throw new AssertionError("AC-10:周期回收方法自身抛异常", ex.getTargetException());
}
}
assertFalse(Files.exists(staleDir),
"AC-10:stale 会话仍在盘上(今天只有 init 时才清,断掉的上传永久占盘)");
assertTrue(Files.isDirectory(freshDir),
"AC-10:近 N 分钟有写入的会话被误清(用户大档会从 0 重传)");
}
@Test
void 统一清理必须覆盖chunkUploads目录() throws Exception {
Path chunkDir = tempDir.resolve("chunk-uploads").resolve("c".repeat(32));
Files.createDirectories(chunkDir);
Path part = chunkDir.resolve("part-0");
Files.writeString(part, "x");
Instant old = Instant.now().minus(10, ChronoUnit.DAYS);
setTimes(part, old);
setTimes(chunkDir, old);
StorageProperties properties = new StorageProperties();
properties.setLocalTempDir(tempDir.toString());
properties.setCleanupEnabled(true);
properties.setSourceRetentionHours(24);
properties.setResultRetentionHours(24);
properties.setTransientPayloadRetentionHours(72);
new LocalTempCleanupService(properties).cleanupLocalTempDir();
assertFalse(Files.exists(chunkDir),
"AC-10:chunk-uploads 残骸未纳入统一清理(LocalTempCleanupService 射程外的洞)");
}
private List<Method> scheduledMethods() {
List<Method> methods = new ArrayList<>();
for (Class<?> type = ChunkedUploadService.class; type != null && type != Object.class;
type = type.getSuperclass()) {
Arrays.stream(type.getDeclaredMethods())
.filter(method -> method.isAnnotationPresent(Scheduled.class))
.forEach(methods::add);
}
return methods;
}
private Path sessionDir(String uploadId) {
return tempDir.resolve("chunk-uploads").resolve(uploadId);
}
private static void setTimes(Path path, Instant instant) throws Exception {
FileTime time = FileTime.from(instant);
Files.getFileAttributeView(path,
java.nio.file.attribute.BasicFileAttributeView.class)
.setTimes(time, time, null);
}
}