refactor(phase-2): 后端任务管线公共层下沉——三段沉实测逐字件,跑批段如实登记无可沉
新增 modules/task/service/pipeline 两支公共件(AbstractFileTaskPipeline 394 行/ AbstractShopPayloadTaskPipeline 149 行)+ 29 例单测(19+10,覆盖正常/边界/异常)。 签名逐条来自 2026-10-18 对七支现网代码的定点实贴,注释里带出处行号: - 解析建单沉 task 头行与结果行的落库形状(七支公共列,出处逐一点名); - 回传落库沉准入序列(校验→加载→模块校验→终态拒绝→归属→心跳), 终态拒绝按 BusinessCodes 拆成 IDEMPOTENT(40901)/ERROR(无 code) 两格——这不是文案,是线协议; - 出结果沉「上传对象存储→写 6 列→updateById」,六支同形,ShopDataCrawl 走批量条件更新 (updateResultFileColumns:1880-1892,还会传 null objectKey)不在其列,已从名单点名撤出; - 跑批段服务端无可沉(机制早已收在 TaskFileJobService/TaskResultFileJobWorker, ClientTaskPullSpi 只 3/7 支实现),故不立转发钩子=不造空壳; - report 段不新造 SPI,复用既有 ResultFileJobHandler;缓存/心跳复用 AbstractModuleTaskCacheService。 七支巨石内部一行未动(Phase 2 硬边界)。 两处「没替现网做决定」: 1) writeJson(序列化失败即 BusinessException、不带 cause,Publish:2122/AP:213/CollectData:362 同形) 本轮不沉——CatchSilenceRatchetTest 自述「新增捕获块要么别加,要么同批减一处同形状」, 而 Phase 2 不许改七支 ⇒ 减不掉就只能不加,该件随 Phase 3 首迁同笔落(本文件零捕获块)。 2) successFileCount/failedFileCount 不由底座初始化:建单时五支写 0、Publish 写真实失败数、 DeleteBrand 首次 insert 两列留 NULL(:146-157 → :268-278 才回填),底座一-default 0 就是偷换语义。 门禁:mvn -o test 4460 例/3 红,三红逐条取证均不归本笔(catch 棘轮 1004=另一路在飞 CollectDataService 新增的一处捕获块;CollectDataBrandBatchFilterTest 与 ShopDataCrawlTemplateLargeWorkbookTest 单独复跑皆绿,前者是顺序污染、后者是 93s 耗时断言被抢核)。 本笔单独跑 88 例全绿(新件 29+ArchitectureBoundaryTest 6+八套 Phase 0 快照), src/test/resources 下 golden 一字未动=未迁移模块行为逐字节相等;task→业务 依赖仍为 0。
This commit is contained in:
+404
@@ -0,0 +1,404 @@
|
||||
package com.nanri.aiimage.modules.task.service.pipeline;
|
||||
|
||||
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileResultMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileResultEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||
import com.nanri.aiimage.modules.task.service.AbstractModuleTaskCacheService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskDistributedLockService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskFileJobService;
|
||||
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
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.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* 任务管线底座的行为契约(Phase 2 新增公共件,AGENTS.md §3 阶段 3 第 2 条要求覆盖正常/边界/异常)。
|
||||
*
|
||||
* <p>这些用例锁的不是「基类能跑」,而是<b>Phase 3 迁移时七支必须换得上的那截形状</b>:
|
||||
* 每条断言旁边注着它抄自哪支现网代码的哪一行。任何一条改了,就是在替现网做决定。
|
||||
*/
|
||||
class AbstractFileTaskPipelineTest {
|
||||
|
||||
private static final String MODULE_TYPE = "PIPE_TEST";
|
||||
|
||||
private FileTaskMapper fileTaskMapper;
|
||||
private FileResultMapper fileResultMapper;
|
||||
private TaskDistributedLockService lockService;
|
||||
private TaskFileJobService jobService;
|
||||
private OssStorageService ossStorageService;
|
||||
private TestPipeline pipeline;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
// LambdaQueryWrapper 的列名解析吃 lambda 缓存,未预置会在 getTargetSql() 处炸(仓内 136 支同此套路)
|
||||
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""),
|
||||
FileResultEntity.class);
|
||||
fileTaskMapper = mock(FileTaskMapper.class);
|
||||
fileResultMapper = mock(FileResultMapper.class);
|
||||
lockService = mock(TaskDistributedLockService.class);
|
||||
jobService = mock(TaskFileJobService.class);
|
||||
ossStorageService = mock(OssStorageService.class);
|
||||
pipeline = new TestPipeline(fileTaskMapper, fileResultMapper, lockService, jobService, ossStorageService);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// 段 1:解析建单
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
void taskHeaderCarriesTheSevenModuleCommonFieldsVerbatim() {
|
||||
FileTaskEntity task = pipeline.newTaskHeader("RUNNING", "PYTHON_QUEUE", 42L, " dev-1 ", 3);
|
||||
|
||||
assertTrue(task.getTaskNo().startsWith(MODULE_TYPE + "-"), "taskNo 必须是 <moduleType>-<雪花>");
|
||||
assertEquals(MODULE_TYPE, task.getModuleType());
|
||||
assertEquals("PYTHON_QUEUE", task.getTaskMode());
|
||||
assertEquals("RUNNING", task.getStatus());
|
||||
assertEquals(3, task.getSourceFileCount());
|
||||
assertEquals("user:42", task.getCreatedBy());
|
||||
assertEquals(42L, task.getUserId());
|
||||
assertEquals("dev-1", task.getDeviceId(), "deviceId 走 trim(AdminAuthSupport.normalizeDeviceId)");
|
||||
assertNotNull(task.getCreatedAt());
|
||||
assertSame(task.getCreatedAt(), task.getUpdatedAt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankDeviceIdStaysUnrecordedRatherThanEmptyString() {
|
||||
assertNull(pipeline.newTaskHeader("PENDING", "IMMEDIATE", 1L, " ", 0).getDeviceId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void taskHeaderDoesNotInventDefaultsForModuleDecidedColumns() {
|
||||
FileTaskEntity task = pipeline.newTaskHeader("SCHEDULED", "PYTHON_QUEUE", 7L, "dev", 1);
|
||||
// ShopMatch 的 scheduledAt、Publish 的 finishedAt/errorMessage、各支 requestJson/resultJson
|
||||
// 都由调用方自己 set:底座一设默认值,迁移时就会把「没写」变成「写了个错的」。
|
||||
assertNull(task.getScheduledAt());
|
||||
assertNull(task.getFinishedAt());
|
||||
assertNull(task.getErrorMessage());
|
||||
assertNull(task.getRequestJson());
|
||||
assertNull(task.getResultJson());
|
||||
// 两个计数列同理:DeleteBrand 建单时留 NULL、循环后才回填(:146-157 → :268-278),
|
||||
// Publish 写的是真实失败数(:1061)——底座一-default 0 就把「还没统计」偷换成「零成功零失败」。
|
||||
assertNull(task.getSuccessFileCount());
|
||||
assertNull(task.getFailedFileCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void illegalUserIdIsRejectedBeforeAnyRowIsBuilt() {
|
||||
for (Long bad : new Long[]{null, 0L, -1L}) {
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> pipeline.newTaskHeader("RUNNING", "PYTHON_QUEUE", bad, "dev", 1));
|
||||
assertEquals("user_id 不合法", ex.getMessage(), "七支文案逐字相同,不许改");
|
||||
}
|
||||
verifyNoInteractions(fileTaskMapper, fileResultMapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
void insertTaskWritesOnceAndReturnsTheSameEntityForIdBackfill() {
|
||||
FileTaskEntity task = pipeline.newTaskHeader("RUNNING", "PYTHON_QUEUE", 9L, "dev", 1);
|
||||
assertSame(task, pipeline.insertTask(task));
|
||||
verify(fileTaskMapper, times(1)).insert(task);
|
||||
}
|
||||
|
||||
@Test
|
||||
void scopeRowKeepsNullableColumnsUntouched() {
|
||||
// ShopDataCrawl/ShopMatch 建单时不写 rowCount,AppearancePatent 不写 success:
|
||||
// 底座一归一,结果行的「未开始」就被读成「0 行且失败」。
|
||||
FileResultEntity row = pipeline.newScopeRow(100L, 9L, "shop-a", "shop-id-1", null, null);
|
||||
assertNull(row.getRowCount());
|
||||
assertNull(row.getSuccess());
|
||||
assertEquals(100L, row.getTaskId());
|
||||
assertEquals(MODULE_TYPE, row.getModuleType());
|
||||
assertEquals("shop-a", row.getSourceFilename(), "sourceFilename 是回传侧的作用域匹配键");
|
||||
assertEquals("shop-id-1", row.getSourceFileUrl());
|
||||
assertEquals(9L, row.getUserId());
|
||||
assertNotNull(row.getCreatedAt());
|
||||
|
||||
assertEquals(-1, pipeline.newScopeRow(1L, 1L, "s", "u", null, -1).getSuccess(),
|
||||
"ShopDataCrawl 的 RESULT_PENDING=-1 必须原样传下去");
|
||||
}
|
||||
|
||||
@Test
|
||||
void scopeRowsAreInsertedOneByOneNotBatched() {
|
||||
List<FileResultEntity> rows = List.of(
|
||||
pipeline.newScopeRow(1L, 1L, "a", "ua", 10, 0),
|
||||
pipeline.newScopeRow(1L, 1L, "b", "ub", 20, 0));
|
||||
|
||||
List<FileResultEntity> saved = pipeline.insertScopeRows(rows);
|
||||
|
||||
assertEquals(2, saved.size());
|
||||
assertSame(rows.get(0), saved.get(0));
|
||||
assertSame(rows.get(1), saved.get(1));
|
||||
verify(fileResultMapper, times(2)).insert(any(FileResultEntity.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyScopeRowListInsertsNothing() {
|
||||
assertTrue(pipeline.insertScopeRows(List.of()).isEmpty());
|
||||
verify(fileResultMapper, never()).insert(any(FileResultEntity.class));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// 段 3:回传准入
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
void illegalTaskIdRejectedWithoutTouchingAnyCollaborator() {
|
||||
for (Long bad : new Long[]{null, 0L, -5L}) {
|
||||
assertEquals("taskId 不合法",
|
||||
assertThrows(BusinessException.class, () -> pipeline.requireTaskId(bad)).getMessage());
|
||||
}
|
||||
verifyNoInteractions(fileTaskMapper, fileResultMapper, lockService, jobService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingTaskOrForeignModuleTypeIsBothReportedAsAbsent() {
|
||||
pipeline.loadedTask = null;
|
||||
assertAdmissionFails("任务不存在");
|
||||
|
||||
FileTaskEntity foreign = new FileTaskEntity();
|
||||
foreign.setModuleType("SOMETHING_ELSE");
|
||||
foreign.setStatus("RUNNING");
|
||||
pipeline.loadedTask = foreign;
|
||||
assertAdmissionFails("任务不存在");
|
||||
}
|
||||
|
||||
@Test
|
||||
void terminalTaskRejectionKeepsTheTwoWireProtocolShapes() {
|
||||
for (String terminal : List.of("SUCCESS", "FAILED")) {
|
||||
pipeline.loadedTask = taskWithStatus(terminal);
|
||||
|
||||
BusinessException idempotent = assertThrows(BusinessException.class,
|
||||
() -> pipeline.admitSubmission(1L, AbstractFileTaskPipeline.TerminalRejection.IDEMPOTENT, null));
|
||||
assertEquals(40901, idempotent.getCode(),
|
||||
"40901 → HTTP 200 + success=true,Python 停止重试(BusinessCodes 已拆码,不可含糊)");
|
||||
assertEquals("任务已结束,拒绝重复提交", idempotent.getMessage());
|
||||
|
||||
BusinessException error = assertThrows(BusinessException.class,
|
||||
() -> pipeline.admitSubmission(1L, AbstractFileTaskPipeline.TerminalRejection.ERROR, null));
|
||||
assertNull(error.getCode(), "无 code 的拒绝会让 success=false,与 IDEMPOTENT 是两件事");
|
||||
assertEquals("任务已结束,拒绝重复提交", error.getMessage());
|
||||
assertEquals(terminal, pipeline.loadedTask.getStatus());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void runningTaskIsAdmittedAndHeartbeatOnlyWhenTheModuleHasACacheService() {
|
||||
pipeline.loadedTask = taskWithStatus("RUNNING");
|
||||
|
||||
pipeline.admitSubmission(1L, AbstractFileTaskPipeline.TerminalRejection.IDEMPOTENT, null);
|
||||
assertNull(pipeline.ownershipOperation, "ownershipOperation 传 null 即不校验归属(四支现网如此)");
|
||||
assertTrue(pipeline.cacheRequested, "准入必须真的问一次缓存服务,否则这个钩子是死代码");
|
||||
verifyNoInteractions(ossStorageService, jobService);
|
||||
|
||||
pipeline.cacheServiceHolder = mock(AbstractModuleTaskCacheService.class);
|
||||
pipeline.admitSubmission(1L, AbstractFileTaskPipeline.TerminalRejection.IDEMPOTENT, null);
|
||||
verify(pipeline.cacheServiceHolder, times(1)).touchTaskHeartbeat(1L);
|
||||
|
||||
pipeline.cacheServiceHolder = mock(AbstractModuleTaskCacheService.class);
|
||||
pipeline.admitSubmission(2L, AbstractFileTaskPipeline.TerminalRejection.ERROR, "submit shop data crawl result");
|
||||
assertEquals("submit shop data crawl result", pipeline.ownershipOperation,
|
||||
"AP/Publish/SDC 三支要归属校验,钩子必须真被走到");
|
||||
}
|
||||
|
||||
@Test
|
||||
void lockContentionRejectsSubmissionWithTaskBusyCode() {
|
||||
when(lockService.acquire(anyString(), anyLong())).thenReturn(null);
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> pipeline.acquireSubmissionLockOrThrow(1L, "shop-match"));
|
||||
assertEquals(40902, ex.getCode(), "锁竞争必须 success=false 让调用方重试,不能静默降级");
|
||||
assertEquals("任务正在处理中,请稍后再试", ex.getMessage());
|
||||
|
||||
when(lockService.acquire(anyString(), anyLong(), anyLong())).thenReturn(null);
|
||||
assertEquals(40902, assertThrows(BusinessException.class,
|
||||
() -> pipeline.acquireSubmissionLockOrThrow(1L, "shop-match", 5_000L)).getCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void lockHandleIsHandedBackWhenAcquired() {
|
||||
TaskDistributedLockService.LockHandle handle = mock(TaskDistributedLockService.LockHandle.class);
|
||||
when(lockService.acquire(MODULE_TYPE, 1L)).thenReturn(handle);
|
||||
assertSame(handle, pipeline.acquireSubmissionLockOrThrow(1L, "shop-match"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void scopeRowsAreListedForThisTaskAndModuleOrderedById() {
|
||||
List<FileResultEntity> rows = List.of(new FileResultEntity(), new FileResultEntity());
|
||||
when(fileResultMapper.selectList(any())).thenReturn(rows);
|
||||
|
||||
assertSame(rows, pipeline.listScopeRows(77L));
|
||||
|
||||
// 光看返回值抓不到「读了别的任务/别的模块的行」——mock 的 selectList 是无条件回桩的,
|
||||
// 所以必须把 wrapper 抓出来钉住条件列与值(仓内既有惯例)。
|
||||
ArgumentCaptor<com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<FileResultEntity>> captor =
|
||||
ArgumentCaptor.forClass(
|
||||
com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper.class);
|
||||
verify(fileResultMapper).selectList(captor.capture());
|
||||
captor.getValue().getSqlSegment(); // MP 惯例:先求值一次才铺出参数
|
||||
String sql = captor.getValue().getSqlSegment();
|
||||
assertTrue(sql.contains("task_id") && sql.contains("module_type"),
|
||||
"条件必须双卡 taskId+moduleType,少了后者就会把别的模块的结果行读进来:" + sql);
|
||||
assertTrue(captor.getValue().getParamNameValuePairs().containsValue(77L));
|
||||
assertTrue(captor.getValue().getParamNameValuePairs().containsValue(MODULE_TYPE));
|
||||
assertTrue(sql.toLowerCase().contains("order by"),
|
||||
"回传收尾判定依赖行序,丢了顺序就是行为变更:" + sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resultAssemblyJobIsEnqueuedUnderTheModuleType() {
|
||||
pipeline.enqueueResultAssembly(5L, 6L, "task:5:owner:i-1");
|
||||
verify(jobService).enqueueAssembleResult(5L, MODULE_TYPE, 6L, "task:5:owner:i-1");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// 段 4:出结果
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
void publishResultFileUploadsThenWritesTheSixColumns(@TempDir java.nio.file.Path dir) throws IOException {
|
||||
File xlsx = write(dir, "a.xlsx", "hello");
|
||||
FileResultEntity row = resultRowWithFailure("上一轮有部分行没跑完");
|
||||
when(ossStorageService.uploadResultFile(xlsx, MODULE_TYPE)).thenReturn("object/key/a");
|
||||
|
||||
pipeline.publishResultFile(row, xlsx, "店铺A.xlsx", "application/vnd.ms-excel", 12, false);
|
||||
|
||||
verify(ossStorageService).uploadResultFile(xlsx, MODULE_TYPE);
|
||||
assertEquals("店铺A.xlsx", row.getResultFilename());
|
||||
assertEquals("object/key/a", row.getResultFileUrl());
|
||||
assertEquals(xlsx.length(), row.getResultFileSize(), "文件大小取真实上传件字节数");
|
||||
assertEquals("application/vnd.ms-excel", row.getResultContentType());
|
||||
assertEquals(12, row.getRowCount());
|
||||
assertEquals(1, row.getSuccess());
|
||||
assertNull(row.getErrorMessage(), "非 preserveFailure 必须清掉旧失败原因(六支现网无条件 success=1)");
|
||||
verify(fileResultMapper).updateById(row);
|
||||
}
|
||||
|
||||
@Test
|
||||
void preserveFailureKeepsTheRowVisiblyFailedWhileStillAttachingTheFile(@TempDir java.nio.file.Path dir)
|
||||
throws IOException {
|
||||
File xlsx = write(dir, "b.xlsx", "data");
|
||||
FileResultEntity row = resultRowWithFailure("店铺处理失败");
|
||||
row.setSuccess(0);
|
||||
|
||||
pipeline.publishResultFile(row, xlsx, "店铺B.xlsx", "application/vnd.ms-excel", 3, true);
|
||||
|
||||
assertEquals(0, row.getSuccess(), "失败店铺的部分结果被组装后仍是失败态,否则用户看不到「其实没跑完」");
|
||||
assertEquals("店铺处理失败", row.getErrorMessage());
|
||||
assertEquals("店铺B.xlsx", row.getResultFilename(), "文件照挂,用户仍能下载已跑出来的行");
|
||||
verify(fileResultMapper).updateById(row);
|
||||
}
|
||||
|
||||
@Test
|
||||
void uploadFailureLeavesTheRowUntouched(@TempDir java.nio.file.Path dir) throws IOException {
|
||||
File xlsx = write(dir, "c.xlsx", "data");
|
||||
FileResultEntity row = resultRowWithFailure(null);
|
||||
when(ossStorageService.uploadResultFile(any(), anyString())).thenThrow(new IllegalStateException("对象存储不可达"));
|
||||
|
||||
assertThrows(IllegalStateException.class,
|
||||
() -> pipeline.publishResultFile(row, xlsx, "店铺C.xlsx", "x", 1, false));
|
||||
|
||||
verify(fileResultMapper, never()).updateById(any(FileResultEntity.class));
|
||||
assertNull(row.getResultFileUrl(), "上传没成就不能把 URL 写进去,否则下载即 404");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// 夹具
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
private void assertAdmissionFails(String expectedMessage) {
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> pipeline.admitSubmission(1L, AbstractFileTaskPipeline.TerminalRejection.IDEMPOTENT, null));
|
||||
assertEquals(expectedMessage, ex.getMessage());
|
||||
}
|
||||
|
||||
private static FileTaskEntity taskWithStatus(String status) {
|
||||
FileTaskEntity task = new FileTaskEntity();
|
||||
task.setId(1L);
|
||||
task.setModuleType(MODULE_TYPE);
|
||||
task.setStatus(status);
|
||||
return task;
|
||||
}
|
||||
|
||||
private static FileResultEntity resultRowWithFailure(String errorMessage) {
|
||||
FileResultEntity row = new FileResultEntity();
|
||||
row.setId(500L);
|
||||
row.setTaskId(1L);
|
||||
row.setModuleType(MODULE_TYPE);
|
||||
row.setSuccess(0);
|
||||
row.setErrorMessage(errorMessage);
|
||||
return row;
|
||||
}
|
||||
|
||||
private static File write(java.nio.file.Path dir, String name, String content) throws IOException {
|
||||
return Files.write(dir.resolve(name), content.getBytes(StandardCharsets.UTF_8)).toFile();
|
||||
}
|
||||
|
||||
/** 被测底座的具体子类:只补三个钩子,公共件一律不重写(重写就等于没沉)。 */
|
||||
private static class TestPipeline extends AbstractFileTaskPipeline<Map<String, Object>> {
|
||||
|
||||
FileTaskEntity loadedTask;
|
||||
String ownershipOperation;
|
||||
boolean cacheRequested;
|
||||
AbstractModuleTaskCacheService<Map<String, Object>> cacheServiceHolder;
|
||||
|
||||
TestPipeline(FileTaskMapper fileTaskMapper,
|
||||
FileResultMapper fileResultMapper,
|
||||
TaskDistributedLockService lockService,
|
||||
TaskFileJobService jobService,
|
||||
OssStorageService ossStorageService) {
|
||||
super(fileTaskMapper, fileResultMapper, lockService, jobService, ossStorageService);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String moduleType() {
|
||||
return MODULE_TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected FileTaskEntity loadTaskForSubmission(Long taskId) {
|
||||
return loadedTask;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractModuleTaskCacheService<Map<String, Object>> taskCacheService() {
|
||||
cacheRequested = true;
|
||||
return cacheServiceHolder;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void ensureTaskOwnedByCurrentInstance(FileTaskEntity task, String operation) {
|
||||
this.ownershipOperation = operation;
|
||||
}
|
||||
}
|
||||
}
|
||||
+276
@@ -0,0 +1,276 @@
|
||||
package com.nanri.aiimage.modules.task.service.pipeline;
|
||||
|
||||
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileResultMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileResultEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||
import com.nanri.aiimage.modules.task.service.TaskDistributedLockService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskFileJobService;
|
||||
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
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.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* 「店铺维度合并载荷」家族骨架的行为契约。
|
||||
*
|
||||
* <p>逐条对撞的是 ShopMatch{@code submitResult:630-710} 与 PriceTrack{@code submitResult:591-663}
|
||||
* 这两支行级孪生件的共同形状:命中与否由 {@code row.sourceFilename} 对上归一化店铺名决定,
|
||||
* 一个都没命中就<b>不重读也不重算状态</b>,任何退出路径都必须把任务锁还回去。
|
||||
*/
|
||||
class AbstractShopPayloadTaskPipelineTest {
|
||||
|
||||
private static final String MODULE_TYPE = "SHOP_PAYLOAD_TEST";
|
||||
|
||||
private FileResultMapper fileResultMapper;
|
||||
private TaskDistributedLockService lockService;
|
||||
private TaskDistributedLockService.LockHandle lockHandle;
|
||||
private ShopPipeline pipeline;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""),
|
||||
FileResultEntity.class);
|
||||
fileResultMapper = mock(FileResultMapper.class);
|
||||
lockService = mock(TaskDistributedLockService.class);
|
||||
lockHandle = mock(TaskDistributedLockService.LockHandle.class);
|
||||
when(lockService.acquire(anyString(), any())).thenReturn(lockHandle);
|
||||
pipeline = new ShopPipeline(mock(FileTaskMapper.class), fileResultMapper, lockService,
|
||||
mock(TaskFileJobService.class), mock(OssStorageService.class));
|
||||
pipeline.loadedTask = runningTask();
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchedShopRowsAreHandledOnceAndStatusIsRecomputedFromFreshRows() {
|
||||
FileResultEntity shopA = row("shop-a");
|
||||
FileResultEntity shopB = row("shop-b");
|
||||
List<FileResultEntity> firstRead = List.of(shopA, shopB);
|
||||
List<FileResultEntity> secondRead = List.of(row("shop-a"));
|
||||
// 第一次读=本轮待处理行,第二次读=changed 闸门后重算状态用的最新行(两支现网都是重读,不复用旧行)
|
||||
when(fileResultMapper.selectList(any())).thenReturn(firstRead, secondRead);
|
||||
|
||||
pipeline.submitShopScopedResults(1L, List.of("Shop-A"), String::valueOf,
|
||||
AbstractFileTaskPipeline.TerminalRejection.IDEMPOTENT, null);
|
||||
|
||||
assertEquals(1, pipeline.handled.size(), "只处理对上的那一行,对不上的行不许被顺手改写");
|
||||
assertEquals("shop-a", pipeline.handled.get(0).shopKey);
|
||||
assertSame(shopA, pipeline.handled.get(0).row);
|
||||
assertEquals("Shop-A", pipeline.handled.get(0).incoming, "钩子收的是原始入参,合并时机留给各支自己定");
|
||||
assertSame(pipeline.loadedTask, pipeline.handled.get(0).task);
|
||||
assertEquals(1, pipeline.recomputed.size());
|
||||
assertSame(secondRead, pipeline.recomputed.get(0), "收尾必须拿重读后的最新行,不能吃本轮的旧快照");
|
||||
verify(lockHandle, times(1)).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void noMatchedRowLeavesStatusUntouchedButStillReleasesTheLock() {
|
||||
when(fileResultMapper.selectList(any())).thenReturn(List.of(row("shop-x")));
|
||||
|
||||
pipeline.submitShopScopedResults(1L, List.of("Shop-Z"), String::valueOf,
|
||||
AbstractFileTaskPipeline.TerminalRejection.IDEMPOTENT, null);
|
||||
|
||||
assertTrue(pipeline.handled.isEmpty());
|
||||
assertTrue(pipeline.recomputed.isEmpty(), "changed 闸门是两支共有的:没改动就不许重算任务状态");
|
||||
verify(lockHandle, times(1)).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void rowWithoutScopeKeyIsSkippedInsteadOfCrashingTheLoop() {
|
||||
when(fileResultMapper.selectList(any())).thenReturn(List.of(row(null)));
|
||||
|
||||
pipeline.submitShopScopedResults(1L, List.of("Shop-A"), String::valueOf,
|
||||
AbstractFileTaskPipeline.TerminalRejection.IDEMPOTENT, null);
|
||||
|
||||
assertTrue(pipeline.handled.isEmpty());
|
||||
assertTrue(pipeline.recomputed.isEmpty());
|
||||
verify(lockHandle, times(1)).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyShopListIsRejectedBeforeTakingTheLock() {
|
||||
BusinessException empty = assertThrows(BusinessException.class,
|
||||
() -> pipeline.submitShopScopedResults(1L, List.of(), String::valueOf,
|
||||
AbstractFileTaskPipeline.TerminalRejection.IDEMPOTENT, null));
|
||||
assertEquals("shops 不能为空", empty.getMessage());
|
||||
|
||||
BusinessException nullList = assertThrows(BusinessException.class,
|
||||
() -> pipeline.submitShopScopedResults(1L, null, String::valueOf,
|
||||
AbstractFileTaskPipeline.TerminalRejection.IDEMPOTENT, null));
|
||||
assertEquals("shops 不能为空", nullList.getMessage());
|
||||
|
||||
verifyNoInteractions(lockService, fileResultMapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
void illegalTaskIdKeepsItsOwnMessageAndDoesNotReachTheShopValidation() {
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> pipeline.submitShopScopedResults(0L, List.of("Shop-A"), String::valueOf,
|
||||
AbstractFileTaskPipeline.TerminalRejection.IDEMPOTENT, null));
|
||||
assertEquals("taskId 不合法", ex.getMessage());
|
||||
verifyNoInteractions(lockService, fileResultMapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
void terminalTaskRejectionStillReleasesTheLock() {
|
||||
pipeline.loadedTask.setStatus("SUCCESS");
|
||||
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> pipeline.submitShopScopedResults(1L, List.of("Shop-A"), String::valueOf,
|
||||
AbstractFileTaskPipeline.TerminalRejection.IDEMPOTENT, null));
|
||||
|
||||
assertEquals(40901, ex.getCode(), "终态重复提交必须回 40901,让 Python 侧幂等忽略");
|
||||
verify(lockHandle, times(1)).close();
|
||||
verify(fileResultMapper, never()).selectList(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void hookFailurePropagatesAndTheLockIsNotLeaked() {
|
||||
when(fileResultMapper.selectList(any())).thenReturn(List.of(row("shop-a")));
|
||||
pipeline.failOnHandle = new IllegalStateException("合并载荷时对象存储不可达");
|
||||
|
||||
assertThrows(IllegalStateException.class,
|
||||
() -> pipeline.submitShopScopedResults(1L, List.of("Shop-A"), String::valueOf,
|
||||
AbstractFileTaskPipeline.TerminalRejection.IDEMPOTENT, null));
|
||||
|
||||
verify(lockHandle, times(1)).close();
|
||||
assertTrue(pipeline.recomputed.isEmpty(), "单行炸了就不许带着半截状态去重算任务");
|
||||
}
|
||||
|
||||
@Test
|
||||
void incomingIndexSkipsNullAndUnnormalizableShopNamesKeepingRequestOrder() {
|
||||
// 每项尾缀一个序号,好让「同名后者覆盖」这件事看得见(List.of 不收 null,这里用 Arrays.asList)
|
||||
List<String> incoming = java.util.Arrays.asList("Shop-B#1", null, " ", "Shop-A#1", "Shop-B#2");
|
||||
Function<String, String> shopNameOf = item -> item.split("#")[0];
|
||||
|
||||
var indexed = pipeline.indexByShopKey(incoming, shopNameOf);
|
||||
|
||||
assertEquals(List.of("shop-b", "shop-a"), List.copyOf(indexed.keySet()),
|
||||
"空名与归一化后为空的项不进索引;请求顺序保留(两支都用 LinkedHashMap)");
|
||||
assertEquals("Shop-B#2", indexed.get("shop-b"), "同名后到的覆盖先到的——两支现网 put 同形,不许改成合并");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shopKeyNormalizerIsUsedForBothIndexingAndMatching() {
|
||||
// 行上的 sourceFilename 是建单时按同一规则写进去的归一化名(ShopMatch:601/PriceTrack:528-543)
|
||||
when(fileResultMapper.selectList(any())).thenReturn(List.of(row("shop-a")));
|
||||
|
||||
pipeline.submitShopScopedResults(1L, List.of("Shop-A"), String::valueOf,
|
||||
AbstractFileTaskPipeline.TerminalRejection.IDEMPOTENT, null);
|
||||
|
||||
assertEquals(List.of("Shop-A"), pipeline.normalized, "索引侧必须调归一化钩子,两侧同规则才对得上");
|
||||
assertEquals(1, pipeline.handled.size());
|
||||
assertEquals("shop-a", pipeline.handled.get(0).shopKey);
|
||||
}
|
||||
|
||||
@Test
|
||||
void lockLogTagDefaultsToModuleTypeSoSubclassesMustOverrideIt() {
|
||||
assertEquals(MODULE_TYPE, pipeline.submissionLockLogTag(),
|
||||
"默认给个不缺的标签;现网各支是小写短名(shop-match/price-track),迁移时必须覆写回历史值");
|
||||
|
||||
ShopPipeline tagged = new ShopPipeline(mock(FileTaskMapper.class), mock(FileResultMapper.class),
|
||||
mock(TaskDistributedLockService.class), mock(TaskFileJobService.class),
|
||||
mock(OssStorageService.class)) {
|
||||
@Override
|
||||
protected String submissionLockLogTag() {
|
||||
return "shop-match";
|
||||
}
|
||||
};
|
||||
assertEquals("shop-match", tagged.submissionLockLogTag());
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// 夹具
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
private static FileTaskEntity runningTask() {
|
||||
FileTaskEntity task = new FileTaskEntity();
|
||||
task.setId(1L);
|
||||
task.setModuleType(MODULE_TYPE);
|
||||
task.setStatus("RUNNING");
|
||||
return task;
|
||||
}
|
||||
|
||||
private static FileResultEntity row(String shopKey) {
|
||||
FileResultEntity row = new FileResultEntity();
|
||||
row.setTaskId(1L);
|
||||
row.setModuleType(MODULE_TYPE);
|
||||
row.setSourceFilename(shopKey);
|
||||
row.setSuccess(0);
|
||||
return row;
|
||||
}
|
||||
|
||||
/** 家族层的具体子类:三个钩子只做记录,不引入任何模块语义。 */
|
||||
private static class ShopPipeline extends AbstractShopPayloadTaskPipeline<String> {
|
||||
|
||||
FileTaskEntity loadedTask;
|
||||
final List<Handled> handled = new ArrayList<>();
|
||||
final List<List<FileResultEntity>> recomputed = new ArrayList<>();
|
||||
final List<String> normalized = new ArrayList<>();
|
||||
RuntimeException failOnHandle;
|
||||
|
||||
ShopPipeline(FileTaskMapper fileTaskMapper,
|
||||
FileResultMapper fileResultMapper,
|
||||
TaskDistributedLockService lockService,
|
||||
TaskFileJobService jobService,
|
||||
OssStorageService ossStorageService) {
|
||||
super(fileTaskMapper, fileResultMapper, lockService, jobService, ossStorageService);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String moduleType() {
|
||||
return MODULE_TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected FileTaskEntity loadTaskForSubmission(Long taskId) {
|
||||
return loadedTask;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String normalizeShopKey(String rawShopName) {
|
||||
normalized.add(rawShopName);
|
||||
return rawShopName == null ? null : rawShopName.trim().toLowerCase();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void applyShopResultRow(Long taskId,
|
||||
FileTaskEntity task,
|
||||
FileResultEntity row,
|
||||
String shopKey,
|
||||
String incoming) {
|
||||
if (failOnHandle != null) {
|
||||
throw failOnHandle;
|
||||
}
|
||||
handled.add(new Handled(taskId, task, row, shopKey, incoming));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onShopRowsRecomputed(FileTaskEntity task, List<FileResultEntity> latestRows) {
|
||||
recomputed.add(latestRows);
|
||||
}
|
||||
}
|
||||
|
||||
private record Handled(Long taskId, FileTaskEntity task, FileResultEntity row, String shopKey, String incoming) {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user