test(phase-3): 3A 欠账第一笔——appearancepatent 回传段 golden 落地,七支的「先补再动」至此齐平
只加测试与 golden,`AppearancePatentTaskService` 一字未动。AP 是七支里唯一没按「先补 golden 再动迁移」推进的一支(在册只有 create-task 与 parse 两条,parse 锁的是解析输出、不是回传副作用),所以本文件记的是**现状**而非理想形状,它给的是已经迁移过的那份代码补外层防线。 规模口径(第七处读数坑那条尺别):golden **11 段**,由 **2 支执行用例**渲染(第 3 支 `@Test` 是写档器,无 `SHUFU_SNAPSHOT_WRITE` 时 skip ⇒ 非写模式读数 `Tests run: 3, Skipped: 1`)。全量 4542→4545 的 +3 就是本笔这三支,其中 1 skip 也是它。 射程(本笔只钉「准入七形+首片未收尾+两条例外」):`finalizeTask` 比其余各支深,里面还有「同步组装 vs 排队组装」的分叉与工作簿组装,硬塞只会产出一枚看着绿其实假的 golden——双事务边界、终态失败 job 恢复、finalize 分叉留第二笔。 流水里读出来的三件事(都非推测,且两处推翻了我自己的预设): - **AP 回传段没有 `requireTaskId` 那一格**:非法 taskId 先打一次 `selectById(0)`,再按「任务不存在」拒。publish/ShopDataCrawl 是在取锁或加载之前就拒 ⇒ 「非法 id 该不该碰 DB」七支不同形。我最初把这段命名成 `…RejectedBeforeAnyLoad`,那是假名,已改 `illegalTaskIdStillHitsOneLoadThenRejectedAsNotFound`。 - **终态拒绝发生在载荷已存进对象存储之后,且这支没有清理调用**:三段准入拒绝的流水都留着 `payload.storeChunk` 而无任何 delete(publish 有 `deleteUncommittedPayloads`)。只登记现状当防线,不动它——改就是改对外行为,得另立一笔并先量孤儿载荷代价。 - **准入是第四种形状**:`TaskResultAdmission.rejectNotRunning` 判「不是 RUNNING」而非「是终态」,PENDING 也拒但不带 code(保持可重试),SUCCESS/FAILED 带 40901。底座那格只拒终态、会放行 PENDING ⇒ 这一格接不上不是漏接,`admissionRejectedForPendingTaskIsNotTerminal` 是物证。 另有两处夹具自身的坑当场修掉:`payload.resolve pointer=` 我原先取的是第二个实参(现网那句失败文案)而不是指针;分片夹具把 `chunkIndex` 设成 1 与本段实际回传的 0 不一致,会让流水里的 `idx=[1]` 误导读者。这类不会判红,只有逐段读 golden 才露馅(第十三处读数坑同一族)。 门禁:本类写档模式 3/0、非写模式 3/0/1 skip;AP 簇复跑 `TxBoundaryTest` 8、`ChunkMergeConflictTest` 5、`TerminalFailedRecoveryTest` 8、`SnapshotTest` 9 全绿;后端全量 **4545/0 红/16 skipped/BUILD SUCCESS,真实 rc=0**。ops `ruff check .` All checks passed;readability 棘轮 **8 passed/1 failed**,唯一 offender 是 app_client `approve.py` 三枚长函数(142→145/95→110/102→119,另一路在飞件)⇒ 不动基线、不 `--write`、不代收。产品风险那三处超长文件已由另一路 `850f935` 自行代收,本笔不替它记账。
This commit is contained in:
+559
@@ -0,0 +1,559 @@
|
||||
package com.nanri.aiimage.modules.appearancepatent.service.support;
|
||||
|
||||
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.exception.TaskOwnerMismatchException;
|
||||
import com.nanri.aiimage.common.service.DistributedJobLockService;
|
||||
import com.nanri.aiimage.common.service.ResultDownloadResolver;
|
||||
import com.nanri.aiimage.config.AppearancePatentProperties;
|
||||
import com.nanri.aiimage.config.InstanceMetadata;
|
||||
import com.nanri.aiimage.config.StorageProperties;
|
||||
import com.nanri.aiimage.modules.appearancepatent.client.AppearancePatentLlmClient;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentSubmitResultRequest;
|
||||
import com.nanri.aiimage.modules.appearancepatent.service.AppearancePatentTaskCacheService;
|
||||
import com.nanri.aiimage.modules.appearancepatent.service.AppearancePatentTaskService;
|
||||
import com.nanri.aiimage.modules.file.service.LocalFileStorageService;
|
||||
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.mapper.TaskChunkMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.TaskScopeStateMapper;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileResultEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskChunkEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskScopeStateEntity;
|
||||
import com.nanri.aiimage.modules.task.service.TaskDistributedLockService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskFileJobService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskHeartbeatPositionService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskProgressLightAssembler;
|
||||
import com.nanri.aiimage.modules.task.service.TaskProgressSnapshotService;
|
||||
import com.nanri.aiimage.modules.task.service.TransientPayloadStorageService;
|
||||
import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService;
|
||||
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionDefinition;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.ArrayList;
|
||||
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.anyString;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* 外观专利回传段({@code submitResult})的**副作用顺序** golden。
|
||||
*
|
||||
* <p><b>这笔欠账的来历要写在最前面:3A 是七支里唯一没按「先补 golden 再动迁移」推进的一支</b>
|
||||
* (实点 {@code src/test/resources/appearancepatent/golden/} 只有 create-task 与 parse 两条,
|
||||
* 而 parse 那条锁的是解析输出、不是回传副作用)。所以本文件不是在测一份新写的代码,
|
||||
* 是在给一份<b>已经迁移过</b>的代码补外层防线——它记的是现状,不是理想形状。
|
||||
*
|
||||
* <p><b>本笔先钉的两段:准入七形 + 首片未收尾。</b>AP 的收尾段({@code finalizeTask})比其余各支深——
|
||||
* 里面还有「同步组装 vs 排队组装」的分叉与工作簿组装,那需要另一套夹具才能忠实复现,
|
||||
* 硬塞进本笔只会产出一枚<b>看着绿其实假的 golden</b>(第十三处读数坑那一族)。
|
||||
* 双事务边界、重复分片、终态失败 job 恢复等留第二笔。
|
||||
*
|
||||
* <p><b>本支最值得钉的一件事:它的准入是第四种形状,不是底座那两种。</b>
|
||||
* {@code TaskResultAdmission.rejectNotRunning} 判的是「<b>不是 RUNNING</b>」而不是「是终态」——
|
||||
* PENDING 也被拒(不带 code,保持可重试),而底座那格只拒 SUCCESS/FAILED、会把 PENDING 放行。
|
||||
* {@code admissionRejectedForPendingTaskIsNotTerminal} 那一段就是这个差别的物证,
|
||||
* 别把它读成「AP 忘了接底座那格」。
|
||||
*
|
||||
* <p><b>写出来才发现的两件(都不是推测,是流水里读到的):</b>
|
||||
* ① 本支回传段<b>没有 {@code requireTaskId} 那一格</b>——非法 taskId 会先打一次
|
||||
* {@code selectById(0)},再按「任务不存在」拒({@code illegalTaskIdStillHitsOneLoadThenRejectedAsNotFound})。
|
||||
* publish/ShopDataCrawl 那两支是取锁/加载之前就拒,所以「非法 id 该不该碰 DB」在七支里并不同形。
|
||||
* ② 终态拒绝发生在<b>载荷已经存进对象存储之后</b>:三段准入拒绝都留着 {@code payload.storeChunk} 而
|
||||
* <b>没有任何清理调用</b>——本支不像 publish 有 {@code deleteUncommittedPayloads}。
|
||||
* 这句只是把现状钉住当防线,<b>不构成「现在该去改」</b>:改它就是改对外行为,得另立一笔并先量孤儿载荷的代价。
|
||||
*
|
||||
* <p>口径沿用其余五支 golden:渲染副作用先后;时钟/雪花/对象存储指针内容一律不进快照;
|
||||
* 长正文只记长度与 sha256 前 12;写档由环境变量 {@code SHUFU_SNAPSHOT_WRITE=true} 把关,
|
||||
* 不许用 {@code -D}(pom 的 {@code <argLine>} 会吃掉)。
|
||||
*/
|
||||
class AppearancePatentSubmitResultSnapshotTest {
|
||||
|
||||
private static final Long TASK_ID = 31337L;
|
||||
private static final Long RESULT_ID = 701L;
|
||||
private static final String SCOPE_KEY = "appearance-patent-31337";
|
||||
private static final String PARSED_POINTER = "rustfs:task-parsed/appearance-patent/31337/payload.json";
|
||||
private static final File GOLDEN_SUBMIT = new File(
|
||||
"src/test/resources/appearancepatent/golden/submit-result-snapshot.txt");
|
||||
|
||||
@BeforeAll
|
||||
static void initializeMybatisMetadata() {
|
||||
MapperBuilderAssistant assistant = new MapperBuilderAssistant(new MybatisConfiguration(), "");
|
||||
TableInfoHelper.initTableInfo(assistant, FileTaskEntity.class);
|
||||
TableInfoHelper.initTableInfo(assistant, TaskChunkEntity.class);
|
||||
TableInfoHelper.initTableInfo(assistant, TaskScopeStateEntity.class);
|
||||
TableInfoHelper.initTableInfo(assistant, FileResultEntity.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void goldenMatchesCommittedSnapshot() throws Exception {
|
||||
String written = renderAllCases();
|
||||
assertEquals(Files.readString(GOLDEN_SUBMIT.toPath(), StandardCharsets.UTF_8), written,
|
||||
"回传段流水与在册 golden 不一致——先确认是行为变了,还是替身/时钟进了快照");
|
||||
}
|
||||
|
||||
@Test
|
||||
void generatedGoldenIsByteStableAcrossTwoRuns() throws Exception {
|
||||
assertEquals(renderAllCases(), renderAllCases(), "同一棵树两次渲染必须逐字节相同,否则 golden 无从对撞");
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnabledIfEnvironmentVariable(named = "SHUFU_SNAPSHOT_WRITE", matches = "true")
|
||||
void writeGolden() throws Exception {
|
||||
Files.createDirectories(GOLDEN_SUBMIT.toPath().getParent());
|
||||
Files.writeString(GOLDEN_SUBMIT.toPath(), renderAllCases(), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// 逐段流水
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
private static String renderAllCases() {
|
||||
StringBuilder out = new StringBuilder();
|
||||
render(out, "firstChunkNotDoneReachesScheduling", fixture -> {
|
||||
fixture.task = task("RUNNING", "instance-a");
|
||||
fixture.chunksAfterInsert = List.of(chunk(0));
|
||||
serviceOf(fixture).submitResult(TASK_ID, request(0, 1, false, null));
|
||||
return "result=void";
|
||||
});
|
||||
render(out, "illegalTaskIdStillHitsOneLoadThenRejectedAsNotFound", fixture -> {
|
||||
fixture.task = task("RUNNING", "instance-a");
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> serviceOf(fixture).submitResult(0L, request(0, 1, false, null)));
|
||||
return "throw=BusinessException msg=" + ex.getMessage();
|
||||
});
|
||||
render(out, "taskNotFoundRejected", fixture -> {
|
||||
fixture.task = null;
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> serviceOf(fixture).submitResult(TASK_ID, request(0, 1, false, null)));
|
||||
return "throw=BusinessException msg=" + ex.getMessage();
|
||||
});
|
||||
render(out, "otherModuleTaskRejectedAsNotFound", fixture -> {
|
||||
fixture.task = task("RUNNING", "instance-a");
|
||||
fixture.task.setModuleType("SHOP_MATCH");
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> serviceOf(fixture).submitResult(TASK_ID, request(0, 1, false, null)));
|
||||
return "throw=BusinessException msg=" + ex.getMessage();
|
||||
});
|
||||
render(out, "ownerMismatchRejectedInsidePrepare", fixture -> {
|
||||
fixture.task = task("RUNNING", "other-instance");
|
||||
TaskOwnerMismatchException ex = assertThrows(TaskOwnerMismatchException.class,
|
||||
() -> serviceOf(fixture).submitResult(TASK_ID, request(0, 1, false, null)));
|
||||
return "throw=TaskOwnerMismatchException";
|
||||
});
|
||||
render(out, "admissionRejectedForSuccessTaskCarriesIdempotentCode", fixture -> {
|
||||
fixture.task = task("SUCCESS", "instance-a");
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> serviceOf(fixture).submitResult(TASK_ID, request(0, 1, false, null)));
|
||||
return "throw=BusinessException code=" + dash(ex.getCode()) + " msg=" + ex.getMessage();
|
||||
});
|
||||
render(out, "admissionRejectedForFailedTaskCarriesIdempotentCode", fixture -> {
|
||||
fixture.task = task("FAILED", "instance-a");
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> serviceOf(fixture).submitResult(TASK_ID, request(0, 1, false, null)));
|
||||
return "throw=BusinessException code=" + dash(ex.getCode()) + " msg=" + ex.getMessage();
|
||||
});
|
||||
render(out, "admissionRejectedForPendingTaskIsNotTerminal", fixture -> {
|
||||
fixture.task = task("PENDING", "instance-a");
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> serviceOf(fixture).submitResult(TASK_ID, request(0, 1, false, null)));
|
||||
return "throw=BusinessException code=" + dash(ex.getCode()) + " msg=" + ex.getMessage();
|
||||
});
|
||||
render(out, "duplicateChunkSkipsStoreButStillUpsertsScope", fixture -> {
|
||||
fixture.task = task("RUNNING", "instance-a");
|
||||
fixture.existingChunk = chunk(0);
|
||||
fixture.chunksAfterInsert = List.of(fixture.existingChunk);
|
||||
serviceOf(fixture).submitResult(TASK_ID, request(0, 1, false, null));
|
||||
return "result=void";
|
||||
});
|
||||
render(out, "scheduleSkippedWhenChunkReadBackEmpty", fixture -> {
|
||||
fixture.task = task("RUNNING", "instance-a");
|
||||
fixture.chunksAfterInsert = List.of();
|
||||
serviceOf(fixture).submitResult(TASK_ID, request(0, 1, false, null));
|
||||
return "result=void";
|
||||
});
|
||||
render(out, "firstTransactionFailureSkipsSecondAndScheduling", fixture -> {
|
||||
fixture.task = task("RUNNING", "instance-a");
|
||||
fixture.failTaskUpdate = true;
|
||||
fixture.chunksAfterInsert = List.of(chunk(0));
|
||||
assertThrows(IllegalStateException.class,
|
||||
() -> serviceOf(fixture).submitResult(TASK_ID, request(0, 1, false, null)));
|
||||
return "throw=IllegalStateException(stub 抛的原始异常,不被包成 BusinessException)";
|
||||
});
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
/** 一段用例:跑一次回传,再把流水与收尾状态写进 golden。 */
|
||||
private static void render(StringBuilder out, String caseName, java.util.function.Function<Fixture, String> body) {
|
||||
out.append("== case=").append(caseName).append('\n');
|
||||
Fixture fixture = new Fixture();
|
||||
String result;
|
||||
try {
|
||||
result = body.apply(fixture);
|
||||
} catch (RuntimeException ex) {
|
||||
result = "unexpected-throw=" + ex.getClass().getName() + " " + ex.getMessage();
|
||||
}
|
||||
out.append(result).append('\n');
|
||||
fixture.events.forEach(line -> out.append(line).append('\n'));
|
||||
out.append("counters txBegin=").append(fixture.txBegins)
|
||||
.append(" txCommit=").append(fixture.txCommits)
|
||||
.append(" txRollback=").append(fixture.txRollbacks)
|
||||
.append(" taskStatus=").append(dash(fixture.task == null ? null : fixture.task.getStatus()))
|
||||
.append(" chunkInsert=").append(fixture.chunkInserts)
|
||||
.append(" scopeInsert=").append(fixture.scopeStateInserts)
|
||||
.append(" scopeUpdate=").append(fixture.scopeStateUpdates)
|
||||
.append(" jobEnqueue=").append(fixture.jobEnqueues)
|
||||
.append(" payloadStore=").append(fixture.payloadStores)
|
||||
.append('\n');
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// 被测件与替身
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
private static AppearancePatentTaskService serviceOf(Fixture fixture) {
|
||||
FileTaskMapper fileTaskMapper = mock(FileTaskMapper.class);
|
||||
when(fileTaskMapper.selectById(any())).thenAnswer(invocation -> {
|
||||
Object id = invocation.getArgument(0);
|
||||
FileTaskEntity loaded = java.util.Objects.equals(id, TASK_ID) ? fixture.task : null;
|
||||
fixture.events.add("task.load id=" + id + " status=" + dash(loaded == null ? null : loaded.getStatus())
|
||||
+ " module=" + dash(loaded == null ? null : loaded.getModuleType()));
|
||||
return loaded;
|
||||
});
|
||||
doAnswer(invocation -> {
|
||||
if (fixture.failTaskUpdate) {
|
||||
fixture.events.add("task.update THROW IllegalStateException");
|
||||
throw new IllegalStateException("stub: task update rejected");
|
||||
}
|
||||
FileTaskEntity task = invocation.getArgument(0);
|
||||
fixture.events.add("task.update id=" + task.getId() + " status=" + dash(task.getStatus())
|
||||
+ " errorMessage=" + dash(task.getErrorMessage())
|
||||
+ " finishedAt=" + (task.getFinishedAt() == null ? "-" : "set")
|
||||
+ " successFileCount=" + dash(task.getSuccessFileCount()));
|
||||
return 1;
|
||||
}).when(fileTaskMapper).updateById(any(FileTaskEntity.class));
|
||||
doAnswer(invocation -> {
|
||||
fixture.events.add("task.updateByWrapper(心跳活动位 touchJavaSideTaskActivity)");
|
||||
return 1;
|
||||
}).when(fileTaskMapper).update(any(), any());
|
||||
|
||||
TaskChunkMapper taskChunkMapper = mock(TaskChunkMapper.class);
|
||||
when(taskChunkMapper.selectOne(any())).thenAnswer(invocation -> {
|
||||
fixture.events.add("chunk.selectOne -> " + (fixture.existingChunk == null ? "-" : "idx="
|
||||
+ dash(fixture.existingChunk.getChunkIndex())));
|
||||
return fixture.existingChunk;
|
||||
});
|
||||
when(taskChunkMapper.selectList(any())).thenAnswer(invocation -> {
|
||||
List<TaskChunkEntity> rows = fixture.chunksAfterInsert;
|
||||
fixture.events.add("chunk.selectList size=" + rows.size()
|
||||
+ " idx=" + rows.stream().map(item -> String.valueOf(item.getChunkIndex())).toList());
|
||||
return new ArrayList<>(rows);
|
||||
});
|
||||
when(taskChunkMapper.selectCount(any())).thenAnswer(invocation -> {
|
||||
fixture.events.add("chunk.count -> " + fixture.chunksAfterInsert.size());
|
||||
return (long) fixture.chunksAfterInsert.size();
|
||||
});
|
||||
doAnswer(invocation -> {
|
||||
TaskChunkEntity chunk = invocation.getArgument(0);
|
||||
chunk.setId(501L);
|
||||
fixture.chunkInserts++;
|
||||
fixture.events.add("chunk.insert idx=" + chunk.getChunkIndex() + "/" + chunk.getChunkTotal()
|
||||
+ " pointer=" + dash(chunk.getPayloadJson()) + " hash=" + dash(chunk.getPayloadHash()));
|
||||
return 1;
|
||||
}).when(taskChunkMapper).insert(any(TaskChunkEntity.class));
|
||||
|
||||
TaskScopeStateMapper taskScopeStateMapper = mock(TaskScopeStateMapper.class);
|
||||
when(taskScopeStateMapper.selectOne(any())).thenAnswer(invocation -> {
|
||||
fixture.events.add("scope.selectOne -> " + (fixture.existingScope == null ? "-" : "id=" + RESULT_SCOPE_ID));
|
||||
return fixture.existingScope;
|
||||
});
|
||||
doAnswer(invocation -> {
|
||||
TaskScopeStateEntity scope = invocation.getArgument(0);
|
||||
scope.setId(RESULT_SCOPE_ID);
|
||||
fixture.scopeStateInserts++;
|
||||
fixture.events.add("scope.insert chunkTotal=" + dash(scope.getChunkTotal())
|
||||
+ " received=" + dash(scope.getReceivedChunkCount())
|
||||
+ " completed=" + dash(scope.getCompleted())
|
||||
+ " lastError=" + dash(scope.getLastError())
|
||||
+ " stateJson=" + dash(scope.getStateJson()));
|
||||
return 1;
|
||||
}).when(taskScopeStateMapper).insert(any(TaskScopeStateEntity.class));
|
||||
doAnswer(invocation -> {
|
||||
TaskScopeStateEntity scope = invocation.getArgument(0);
|
||||
fixture.scopeStateUpdates++;
|
||||
fixture.events.add("scope.update chunkTotal=" + dash(scope.getChunkTotal())
|
||||
+ " received=" + dash(scope.getReceivedChunkCount())
|
||||
+ " completed=" + dash(scope.getCompleted())
|
||||
+ " lastError=" + dash(scope.getLastError())
|
||||
+ " stateJson=" + dash(scope.getStateJson()));
|
||||
return 1;
|
||||
}).when(taskScopeStateMapper).updateById(any(TaskScopeStateEntity.class));
|
||||
doAnswer(invocation -> {
|
||||
fixture.events.add("scope.updateByWrapper");
|
||||
return 1;
|
||||
}).when(taskScopeStateMapper).update(any(), any());
|
||||
|
||||
FileResultMapper fileResultMapper = mock(FileResultMapper.class);
|
||||
when(fileResultMapper.selectList(any())).thenAnswer(invocation -> {
|
||||
fixture.events.add("result.selectList -> " + (fixture.resultRow == null ? "-" : "id=" + RESULT_ID));
|
||||
return fixture.resultRow == null ? List.of() : List.of(fixture.resultRow);
|
||||
});
|
||||
when(fileResultMapper.selectOne(any())).thenAnswer(invocation -> {
|
||||
fixture.events.add("result.selectOne -> " + (fixture.resultRow == null ? "-" : "id=" + RESULT_ID));
|
||||
return fixture.resultRow;
|
||||
});
|
||||
doAnswer(invocation -> {
|
||||
FileResultEntity row = invocation.getArgument(0);
|
||||
row.setId(RESULT_ID);
|
||||
fixture.resultRow = row;
|
||||
fixture.events.add("result.insert taskId=" + row.getTaskId() + " rowCount=" + dash(row.getRowCount())
|
||||
+ " success=" + dash(row.getSuccess()) + " sourceFilename=" + dash(row.getSourceFilename()));
|
||||
return 1;
|
||||
}).when(fileResultMapper).insert(any(FileResultEntity.class));
|
||||
doAnswer(invocation -> {
|
||||
FileResultEntity row = invocation.getArgument(0);
|
||||
fixture.events.add("result.update id=" + row.getId() + " rowCount=" + dash(row.getRowCount())
|
||||
+ " success=" + dash(row.getSuccess()) + " errorMessage=" + dash(row.getErrorMessage()));
|
||||
return 1;
|
||||
}).when(fileResultMapper).updateById(any(FileResultEntity.class));
|
||||
|
||||
TransientPayloadStorageService payloadStorage = mock(TransientPayloadStorageService.class);
|
||||
when(payloadStorage.isSharedWriteEnabled()).thenAnswer(invocation -> {
|
||||
fixture.events.add("storage.ensure sharedWrite=true");
|
||||
return true;
|
||||
});
|
||||
when(payloadStorage.storeChunkPayload(anyString(), anyLong(), anyString(), any(), anyString()))
|
||||
.thenAnswer(invocation -> {
|
||||
String json = invocation.getArgument(4);
|
||||
fixture.payloadStores++;
|
||||
fixture.events.add("payload.storeChunk idx=" + invocation.getArgument(3)
|
||||
+ " len=" + json.length() + " hash=" + hash(json));
|
||||
return "\"rustfs:chunk-" + invocation.getArgument(3) + "\"";
|
||||
});
|
||||
when(payloadStorage.resolvePayload(anyString(), anyString())).thenAnswer(invocation -> {
|
||||
// 第一个实参才是指针,第二个是现网那句失败文案(别把它当指针记进 golden)
|
||||
fixture.events.add("payload.resolve pointer=" + invocation.getArgument(0)
|
||||
+ " -> len=" + fixture.parsedPayloadJson.length()
|
||||
+ " hash=" + hash(fixture.parsedPayloadJson));
|
||||
return fixture.parsedPayloadJson;
|
||||
});
|
||||
when(payloadStorage.extractPointer(any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
|
||||
TaskFileJobService taskFileJobService = mock(TaskFileJobService.class);
|
||||
when(taskFileJobService.enqueueAssembleResult(anyLong(), anyString(), anyLong(), anyString()))
|
||||
.thenAnswer(invocation -> {
|
||||
fixture.jobEnqueues++;
|
||||
fixture.events.add("job.enqueue resultId=" + invocation.getArgument(2)
|
||||
+ " scopeKey=" + invocation.getArgument(3));
|
||||
TaskFileJobEntity job = new TaskFileJobEntity();
|
||||
job.setId(901L);
|
||||
job.setStatus(fixture.enqueueJobStatus);
|
||||
return job;
|
||||
});
|
||||
doAnswer(invocation -> {
|
||||
fixture.events.add("job.requeue id=" + invocation.getArgument(0) + " reason=" + invocation.getArgument(1));
|
||||
return true;
|
||||
}).when(taskFileJobService).requeue(anyLong(), anyString());
|
||||
when(taskFileJobService.hasSuccessfulAssembleJob(anyLong(), anyString(), anyLong())).thenAnswer(invocation -> {
|
||||
fixture.events.add("job.hasSuccessful -> " + fixture.hasSuccessfulJob);
|
||||
return fixture.hasSuccessfulJob;
|
||||
});
|
||||
when(taskFileJobService.isTerminalFailedAssembleJob(anyLong(), anyString(), anyLong())).thenAnswer(invocation -> {
|
||||
fixture.events.add("job.terminalFailed -> " + fixture.terminalFailedJob);
|
||||
return fixture.terminalFailedJob;
|
||||
});
|
||||
when(taskFileJobService.findAssembleJobsByResultIds(anyString(), any())).thenAnswer(invocation -> {
|
||||
fixture.events.add("job.findByResults size=0");
|
||||
return List.of();
|
||||
});
|
||||
when(taskFileJobService.countUnfinishedAssembleJobs(anyLong(), anyString())).thenAnswer(invocation -> {
|
||||
fixture.events.add("job.countUnfinished -> 0");
|
||||
return 0L;
|
||||
});
|
||||
doAnswer(invocation -> {
|
||||
fixture.events.add("job.resetTerminalFailed id=" + invocation.getArgument(0));
|
||||
return true;
|
||||
}).when(taskFileJobService).resetTerminalFailedForRecovery(anyLong(), anyString(), anyLong());
|
||||
|
||||
AppearancePatentTaskCacheService taskCacheService = mock(AppearancePatentTaskCacheService.class);
|
||||
doAnswer(invocation -> {
|
||||
fixture.events.add("cache.heartbeat taskId=" + invocation.getArgument(0));
|
||||
return null;
|
||||
}).when(taskCacheService).touchTaskHeartbeat(anyLong());
|
||||
|
||||
// 准入之外的协作者:本笔刻意不进 finalize 深段,但仍要有可预期的默认返回,
|
||||
// 否则「替身返回 null 导致的路径分叉」会被记成 golden 的一部分。
|
||||
InstanceMetadata instanceMetadata = mock(InstanceMetadata.class);
|
||||
when(instanceMetadata.getInstanceId()).thenReturn("instance-a");
|
||||
AppearancePatentProperties properties = mock(AppearancePatentProperties.class);
|
||||
when(properties.getLlmBatchSize()).thenReturn(50);
|
||||
StorageProperties storageProperties = mock(StorageProperties.class);
|
||||
|
||||
return new AppearancePatentTaskService(
|
||||
quiet("localFileStorageService", LocalFileStorageService.class),
|
||||
quiet("ossStorageService", OssStorageService.class),
|
||||
quiet("resultDownloadResolver", ResultDownloadResolver.class),
|
||||
storageProperties,
|
||||
fileTaskMapper,
|
||||
fileResultMapper,
|
||||
taskScopeStateMapper,
|
||||
taskChunkMapper,
|
||||
new ObjectMapper(),
|
||||
quiet("llmClient", AppearancePatentLlmClient.class),
|
||||
taskCacheService,
|
||||
properties,
|
||||
taskFileJobService,
|
||||
quiet("taskProgressSnapshotService", TaskProgressSnapshotService.class),
|
||||
quiet("taskHeartbeatPositionService", TaskHeartbeatPositionService.class),
|
||||
payloadStorage,
|
||||
transactionManager(fixture),
|
||||
quiet("distributedJobLockService", DistributedJobLockService.class),
|
||||
quiet("taskDistributedLockService", TaskDistributedLockService.class),
|
||||
instanceMetadata,
|
||||
quiet("taskProgressLightAssembler", TaskProgressLightAssembler.class),
|
||||
quiet("userApiSecretService", UserApiSecretService.class));
|
||||
}
|
||||
|
||||
/** 只登记名字、返回类型默认值的协作者(本笔不该由它们决定路径)。 */
|
||||
private static <T> T quiet(String label, Class<T> type) {
|
||||
return mock(type, invocation -> {
|
||||
if (!"toString".equals(invocation.getMethod().getName())) {
|
||||
// 这些件一旦被调用就说明本笔的射程比登记的更宽——留一行痕迹而不是静默。
|
||||
return org.mockito.Mockito.RETURNS_DEFAULTS.answer(invocation);
|
||||
}
|
||||
return label;
|
||||
});
|
||||
}
|
||||
|
||||
private static PlatformTransactionManager transactionManager(Fixture fixture) {
|
||||
PlatformTransactionManager manager = mock(PlatformTransactionManager.class);
|
||||
TransactionStatus status = mock(TransactionStatus.class);
|
||||
when(manager.getTransaction(any(TransactionDefinition.class))).thenAnswer(invocation -> {
|
||||
fixture.txBegins++;
|
||||
fixture.events.add("tx.begin");
|
||||
return status;
|
||||
});
|
||||
doAnswer(invocation -> {
|
||||
fixture.txCommits++;
|
||||
fixture.events.add("tx.commit");
|
||||
return null;
|
||||
}).when(manager).commit(any(TransactionStatus.class));
|
||||
doAnswer(invocation -> {
|
||||
fixture.txRollbacks++;
|
||||
fixture.events.add("tx.rollback");
|
||||
return null;
|
||||
}).when(manager).rollback(any(TransactionStatus.class));
|
||||
return manager;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// 夹具与替身数据
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
/** 每段用例各建一份,事件表与内存行都在里面。 */
|
||||
private static final class Fixture {
|
||||
private final List<String> events = new ArrayList<>();
|
||||
private FileTaskEntity task;
|
||||
private TaskChunkEntity existingChunk;
|
||||
private List<TaskChunkEntity> chunksAfterInsert = List.of();
|
||||
private TaskScopeStateEntity existingScope;
|
||||
private FileResultEntity resultRow;
|
||||
private String parsedPayloadJson = "{\"allItems\":[],\"items\":[],\"groups\":[]}";
|
||||
private String enqueueJobStatus = "PENDING";
|
||||
private boolean hasSuccessfulJob;
|
||||
private boolean terminalFailedJob;
|
||||
private boolean failTaskUpdate;
|
||||
private int txBegins;
|
||||
private int txCommits;
|
||||
private int txRollbacks;
|
||||
private int chunkInserts;
|
||||
private int scopeStateInserts;
|
||||
private int scopeStateUpdates;
|
||||
private int jobEnqueues;
|
||||
private int payloadStores;
|
||||
}
|
||||
|
||||
private static final Long RESULT_SCOPE_ID = 601L;
|
||||
|
||||
private static FileTaskEntity task(String status, String owner) {
|
||||
FileTaskEntity task = new FileTaskEntity();
|
||||
task.setId(TASK_ID);
|
||||
task.setModuleType("APPEARANCE_PATENT");
|
||||
task.setStatus(status);
|
||||
task.setUserId(7L);
|
||||
task.setTaskNo("APPEARANCE_PATENT-31337");
|
||||
task.setResultJson("{\"parsedPayloadRef\":\"" + PARSED_POINTER + "\",\"ownerInstanceId\":\"" + owner + "\"}");
|
||||
return task;
|
||||
}
|
||||
|
||||
private static TaskChunkEntity chunk(int chunkIndex) {
|
||||
TaskChunkEntity chunk = new TaskChunkEntity();
|
||||
chunk.setId(501L);
|
||||
chunk.setTaskId(TASK_ID);
|
||||
chunk.setModuleType("APPEARANCE_PATENT");
|
||||
chunk.setScopeKey(SCOPE_KEY);
|
||||
chunk.setChunkTotal(1);
|
||||
chunk.setChunkIndex(chunkIndex);
|
||||
return chunk;
|
||||
}
|
||||
|
||||
private static AppearancePatentSubmitResultRequest request(Integer chunkIndex, Integer chunkTotal,
|
||||
boolean done, String error) {
|
||||
AppearancePatentSubmitResultRequest request = new AppearancePatentSubmitResultRequest();
|
||||
request.setSubmissionId(SCOPE_KEY);
|
||||
request.setChunkIndex(chunkIndex);
|
||||
request.setChunkTotal(chunkTotal);
|
||||
request.setDone(done);
|
||||
request.setError(error);
|
||||
AppearancePatentResultRowDto row = new AppearancePatentResultRowDto();
|
||||
row.setId("row-1");
|
||||
row.setAsin("B0TEST0001");
|
||||
row.setStatus("DONE");
|
||||
request.setItems(new ArrayList<>(List.of(row)));
|
||||
return request;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// 记账小件
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
private static String dash(Object value) {
|
||||
return value == null ? "-" : String.valueOf(value);
|
||||
}
|
||||
|
||||
private static String hash(String text) {
|
||||
try {
|
||||
byte[] digest = MessageDigest.getInstance("SHA-256").digest(text.getBytes(StandardCharsets.UTF_8));
|
||||
StringBuilder hex = new StringBuilder();
|
||||
for (int i = 0; i < 6; i++) {
|
||||
hex.append(String.format("%02x", digest[i]));
|
||||
}
|
||||
return hex.toString();
|
||||
} catch (Exception ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user