fix(phase-5.2): 消项 CR 第二轮 P1/P2/P3(通道/证据链/交付时序/后管证据页)
This commit is contained in:
+189
@@ -0,0 +1,189 @@
|
||||
package com.nanri.aiimage.modules.evidence.service;
|
||||
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.service.DistributedJobLockService;
|
||||
import com.nanri.aiimage.common.task.LockSkipWitness;
|
||||
import com.nanri.aiimage.config.EvidenceProperties;
|
||||
import com.nanri.aiimage.modules.evidence.mapper.TaskEvidenceMapper;
|
||||
import com.nanri.aiimage.modules.evidence.model.entity.TaskEvidenceEntity;
|
||||
import com.nanri.aiimage.modules.evidence.storage.TaskEvidenceStorageService;
|
||||
import com.nanri.aiimage.modules.file.service.LocalFileStorageService;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* 证据登记的归属与保留期的执行保证(CR 第二轮 P1-2/P1-7/P2-2)。
|
||||
*
|
||||
* <p>P1-2 的判据是「归属先于落桶」:task_id 对得上任务行而 uid 不是行主 ⇒ 403 且
|
||||
* {@code storage.store} 一次都不许被调——校验晚于落桶的话,每一次拒绝都在证据桶里
|
||||
* 留一个收不回的孤儿对象。P1-7 的判据是「保留期必须有执行者且不堵车」:拿不到分布式锁
|
||||
* 要走可聚合出口;单个对象删除失败只牺牲那一行,键集继续往前走。
|
||||
*/
|
||||
class TaskEvidenceOwnershipAndRetentionTest {
|
||||
|
||||
private static final String TASK_ID = "33062";
|
||||
|
||||
private final TaskEvidenceMapper mapper = mock(TaskEvidenceMapper.class);
|
||||
private final TaskEvidenceStorageService storage = mock(TaskEvidenceStorageService.class);
|
||||
private final LocalFileStorageService localFiles = mock(LocalFileStorageService.class);
|
||||
private final EvidenceProperties properties = new EvidenceProperties();
|
||||
private final FileTaskMapper fileTaskMapper = mock(FileTaskMapper.class);
|
||||
private final DistributedJobLockService locks = mock(DistributedJobLockService.class);
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
@Test
|
||||
void 他人任务的证据必须403且一次都不落桶() throws Exception {
|
||||
stubTaskOwner(7L);
|
||||
stubUploadableFile();
|
||||
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> service().register(TASK_ID, 8L, "client", "goods-spider", "evidence.json"));
|
||||
|
||||
assertEquals(403, ex.getCode().intValue(), "冒名登记必须 403,不是 500 也不是静默成功");
|
||||
verify(storage, never()).store(any(), any(), any());
|
||||
verify(mapper, never()).insert(any(TaskEvidenceEntity.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 无归属uid的登记必须拒绝() throws Exception {
|
||||
stubTaskOwner(7L);
|
||||
stubUploadableFile();
|
||||
|
||||
assertThrows(BusinessException.class,
|
||||
() -> service().register(TASK_ID, null, "client", "goods-spider", "evidence.json"));
|
||||
verify(storage, never()).store(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 本人任务照常登记且登记行带上uid() throws Exception {
|
||||
stubTaskOwner(7L);
|
||||
Path file = stubUploadableFile();
|
||||
when(storage.store(any(), eq(TASK_ID), eq("evidence.json"))).thenReturn(
|
||||
new TaskEvidenceStorageService.StoredEvidence("task-evidence/k", 3L, "sha", "application/json"));
|
||||
when(mapper.insert(any(TaskEvidenceEntity.class))).thenReturn(1);
|
||||
|
||||
service().register(TASK_ID, 7L, "client", "goods-spider", "evidence.json");
|
||||
|
||||
org.mockito.ArgumentCaptor<TaskEvidenceEntity> captor =
|
||||
org.mockito.ArgumentCaptor.forClass(TaskEvidenceEntity.class);
|
||||
verify(mapper).insert(captor.capture());
|
||||
assertEquals(7L, captor.getValue().getUid(), "登记行的 uid 必须是行主本人");
|
||||
assertEquals(TASK_ID, captor.getValue().getTaskId());
|
||||
assertTrue(Files.notExists(file) || Files.exists(file), "临时件清理失败只留 warn,不影响登记");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 客户端本地任务号带uid自证即可登记() throws Exception {
|
||||
when(fileTaskMapper.selectById(anyLong())).thenReturn(null);
|
||||
when(fileTaskMapper.selectOne(any())).thenReturn(null);
|
||||
stubUploadableFile();
|
||||
when(storage.store(any(), any(), any())).thenReturn(
|
||||
new TaskEvidenceStorageService.StoredEvidence("task-evidence/k", 3L, "sha", "application/json"));
|
||||
when(mapper.insert(any(TaskEvidenceEntity.class))).thenReturn(1);
|
||||
|
||||
service().register("local-job-abc", 5L, "client", "goods-spider", "evidence.json");
|
||||
|
||||
verify(storage).store(any(), eq("local-job-abc"), eq("evidence.json"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 零字节证据件拒绝登记() throws Exception {
|
||||
when(fileTaskMapper.selectById(anyLong())).thenReturn(null);
|
||||
when(fileTaskMapper.selectOne(any())).thenReturn(null);
|
||||
Path empty = Files.createFile(tempDir.resolve("empty.png"));
|
||||
when(localFiles.findLocalSourceFile("empty.png")).thenReturn(empty.toFile());
|
||||
|
||||
assertThrows(BusinessException.class,
|
||||
() -> service().register(TASK_ID, 7L, "client", "goods-spider", "empty.png"));
|
||||
verify(storage, never()).store(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 同任务证据件数到上限必须拒绝() throws Exception {
|
||||
when(fileTaskMapper.selectById(anyLong())).thenReturn(null);
|
||||
when(fileTaskMapper.selectOne(any())).thenReturn(null);
|
||||
when(mapper.selectCount(any())).thenReturn(50L);
|
||||
stubUploadableFile();
|
||||
|
||||
assertThrows(BusinessException.class,
|
||||
() -> service().register(TASK_ID, 7L, "client", "goods-spider", "evidence.json"));
|
||||
verify(storage, never()).store(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 清理拿不到锁必须走可聚合出口且不碰库() {
|
||||
long before = LockSkipWitness.countOf(TaskEvidenceService.RETENTION_LOCK);
|
||||
when(locks.tryLock(eq(TaskEvidenceService.RETENTION_LOCK), any())).thenReturn(null);
|
||||
|
||||
service().purgeExpired();
|
||||
|
||||
assertEquals(before + 1, LockSkipWitness.countOf(TaskEvidenceService.RETENTION_LOCK),
|
||||
"锁跳过必须有 [lock-skip] 可聚合出口,静默 return 就是 P1-7 的另一半");
|
||||
verifyNoInteractions(mapper, storage);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 单个对象删除失败不堵后面的行() {
|
||||
when(locks.tryLock(eq(TaskEvidenceService.RETENTION_LOCK), any()))
|
||||
.thenReturn(mock(DistributedJobLockService.LockHandle.class));
|
||||
TaskEvidenceEntity first = evidenceRow(1L, "k1");
|
||||
TaskEvidenceEntity stuck = evidenceRow(2L, "k2");
|
||||
TaskEvidenceEntity third = evidenceRow(3L, "k3");
|
||||
when(mapper.selectList(any())).thenReturn(List.of(first, stuck, third), List.of());
|
||||
doThrow(new RuntimeException("oss unavailable")).when(storage).delete("k2");
|
||||
when(mapper.deleteById(anyLong())).thenReturn(1);
|
||||
|
||||
service().purgeExpired();
|
||||
|
||||
verify(mapper).deleteById(1L);
|
||||
verify(mapper, never()).deleteById(2L);
|
||||
verify(mapper).deleteById(3L);
|
||||
}
|
||||
|
||||
private TaskEvidenceService service() {
|
||||
return new TaskEvidenceService(mapper, storage, localFiles, properties, fileTaskMapper, locks);
|
||||
}
|
||||
|
||||
private void stubTaskOwner(long uid) {
|
||||
FileTaskEntity row = new FileTaskEntity();
|
||||
row.setId(Long.parseLong(TASK_ID));
|
||||
row.setUserId(uid);
|
||||
when(fileTaskMapper.selectById(Long.parseLong(TASK_ID))).thenReturn(row);
|
||||
}
|
||||
|
||||
private Path stubUploadableFile() throws Exception {
|
||||
Path file = tempDir.resolve("evidence.json");
|
||||
Files.writeString(file, "{\"traceback\": \"boom\"}");
|
||||
when(localFiles.findLocalSourceFile("evidence.json")).thenReturn(file.toFile());
|
||||
return file;
|
||||
}
|
||||
|
||||
private static TaskEvidenceEntity evidenceRow(long id, String objectKey) {
|
||||
TaskEvidenceEntity row = new TaskEvidenceEntity();
|
||||
row.setId(id);
|
||||
row.setTaskId(TASK_ID);
|
||||
row.setObjectKey(objectKey);
|
||||
return row;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user