fix(cr-01/02/03/08/09/10/11/13): 会话世代、损耗共享基线、任务活性与工作台包

- CR-01:users.session_epoch(V172)+ JWT sessionEpoch;登录/登出原子推进,
  check_login/requireUser 先比世代再做设备比对;登出不再依赖 machine CAS
- CR-02/09:cost-guard 基线落 Redis(CostGuardBaselineStore),按 elapsed 归一化;
  非持锁实例刷新共享密钥快照;pause() 返回写成功,失败不通知且不推进基线
- CR-03:stuck_requeue_count(V173,上限 20)与 retry_count 分账;终态父任务心跳
  停止 touchRunning 并 markFailedPermanent;良性停续期 markLost=false
- CR-08:工作台包对象 key 加 UUID、0 字节拒绝、同 objectKey 幂等、同版本重复拒绝、
  前端 confirm 超时抬到 10 分钟
- CR-10:Excel onSheetTotal 统一在 onHeader 之后回调(xlsx/xls)
- CR-13/11:迁移头/文件名一致断言(V170 唯一豁免);V167 前置由 CompanyService 同事务
  复制菜单树,已应用迁移未回改
This commit is contained in:
2026-10-25 03:40:15 +08:00
parent 112c61afa8
commit c2f429f112
43 changed files with 1003 additions and 95 deletions
@@ -101,7 +101,9 @@ class MigratedSqlEquivalenceTest {
"UserSecretUsageMapper#sumByUserAndModule",
"UserSecretUsageMapper#sumForUser",
// B28(2026-10-25)新写的 1 条主键点查:父任务是否已终态。
"TaskFileJobMapper#selectFileTaskStatus");
"TaskFileJobMapper#selectFileTaskStatus",
// CR-01(2026-10-25)新写的 1 条写侧语句:会话世代原子自增。
"LoginUserMapper#incrementSessionEpoch");
/** 账本 5 列:接口 / 方法 / @Select 种类 / 是否含常量插值 / 注解原文(Java 反转义后那条串)。 */
private static final int LEDGER_COLUMNS = 5;
@@ -80,7 +80,9 @@ class SqlLayeringConventionTest {
// 126 → 128:同轮 {@code UserTaskGuardMapper} 的 pause/resume 两条写侧语句从注解迁进 XML。
// 128 → 129:B28 新增 {@code TaskFileJobMapper#selectFileTaskStatus}(按主键读父任务状态),
// 新写语句按约定直接进 XML,同轮登记进 MigratedSqlEquivalenceTest.NOT_MIGRATED(88 → 89)。
private static final int XML_STATEMENT_TOTAL = 129;
// 129 → 130:CR-01 新增 {@code LoginUserMapper#incrementSessionEpoch}(会话世代原子自增),
// 新写语句直接进 mapper/auth/LoginUserMapper.xml,同轮登记进 NOT_MIGRATED。
private static final int XML_STATEMENT_TOTAL = 130;
/**
* wrapper 里夹带函数级 SQL 的现状("这不是简单单表"的证据),文件名 → 条数:
@@ -126,6 +126,70 @@ class ExcelStreamReaderParseFailureTest {
assertEquals("Nike", seen.toString());
}
@Test
void ooxmlCallsSheetTotalAfterHeader() throws Exception {
Path good = writeWorkbook();
java.util.List<String> events = new java.util.ArrayList<>();
ExcelStreamReader.readFirstSheet(good.toFile(), new ExcelStreamReader.SheetRowHandler() {
@Override
public void onHeader(String sheetName, Integer sheetNo, Map<Integer, String> headerMap) {
events.add("header:" + headerMap.get(0));
}
@Override
public void onSheetTotal(String sheetName, Integer sheetNo, Integer approximateTotalRows) {
events.add("total:" + approximateTotalRows);
}
@Override
public void onRow(String sheetName, Integer sheetNo, int rowIndex,
Map<Integer, String> headerMap, Map<Integer, String> rowMap) {
}
});
assertEquals("header:品牌", events.get(0));
assertTrue(events.size() >= 2 && events.get(1).startsWith("total:"),
"onSheetTotal 必须在 onHeader 之后回调,实际=" + events);
}
@Test
void hssfCallsSheetTotalAfterHeader() throws Exception {
Path file = tempDir.resolve("order.xls");
try (HSSFWorkbook workbook = new HSSFWorkbook()) {
Sheet sheet = workbook.createSheet("Sheet1");
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("品牌");
Row data = sheet.createRow(1);
data.createCell(0).setCellValue("Nike");
try (OutputStream out = Files.newOutputStream(file)) {
workbook.write(out);
}
}
java.util.List<String> events = new java.util.ArrayList<>();
ExcelStreamReader.readFirstSheet(file.toFile(), new ExcelStreamReader.SheetRowHandler() {
@Override
public void onHeader(String sheetName, Integer sheetNo, Map<Integer, String> headerMap) {
events.add("header:" + headerMap.get(0));
}
@Override
public void onSheetTotal(String sheetName, Integer sheetNo, Integer approximateTotalRows) {
events.add("total:" + approximateTotalRows);
}
@Override
public void onRow(String sheetName, Integer sheetNo, int rowIndex,
Map<Integer, String> headerMap, Map<Integer, String> rowMap) {
}
});
assertEquals("header:品牌", events.get(0));
assertTrue(events.size() >= 2 && events.get(1).startsWith("total:"),
"xls 路径同样必须先表头、后总行数,实际=" + events);
}
private Path writeWorkbook() throws IOException {
Path file = tempDir.resolve("ok.xlsx");
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
@@ -96,6 +96,20 @@ class AdminAuthSupportTest {
assertThatCode(() -> support.requireUser(requestWithBearer("t"))).doesNotThrowAnyException();
}
@Test
void requireUserRejectsTokenFromBeforeLogoutOrRelogin() {
// 库中世代已是 2,token 还签着 1:登出/新登录之后旧令牌必须 401,与单设备开关无关
AdminUserEntity user = user(7L, "normal", 0, null, null);
user.setSessionEpoch(2L);
AdminAuthSupport support = support(user, "devA", false, 1L);
BusinessException ex = catchThrowableOfType(
() -> support.requireUser(requestWithBearer("t")), BusinessException.class);
assertThat(ex.getCode()).isEqualTo(401);
assertThat(ex.getMessage()).contains("登录已过期");
}
// ---------- 内部令牌:轮换过渡期新旧两把同时接受 ----------
@Test
@@ -199,6 +213,12 @@ class AdminAuthSupportTest {
}
private AdminAuthSupport support(AdminUserEntity user, String claimDeviceId, boolean singleDeviceEnabled) {
return support(user, claimDeviceId, singleDeviceEnabled,
user.getSessionEpoch() == null ? 0L : user.getSessionEpoch());
}
private AdminAuthSupport support(AdminUserEntity user, String claimDeviceId, boolean singleDeviceEnabled,
long claimSessionEpoch) {
JwtService jwtService = mock(JwtService.class);
AdminUserMapper userMapper = mock(AdminUserMapper.class);
AuthProperties props = mock(AuthProperties.class);
@@ -206,6 +226,7 @@ class AdminAuthSupportTest {
Claims claims = mock(Claims.class);
when(claims.getSubject()).thenReturn(String.valueOf(user.getId()));
when(claims.get("deviceId")).thenReturn(claimDeviceId);
when(claims.get("sessionEpoch")).thenReturn(claimSessionEpoch);
when(jwtService.parse("t")).thenReturn(claims);
when(jwtService.parseWithVerdict("t")).thenReturn(
new JwtService.ParseResult(JwtService.Verdict.VALID, claims));
@@ -37,7 +37,7 @@ class JwtVerdictTest {
@Test
void freshTokenIsValidAndParseStillReturnsClaims() {
JwtService s = service(SECRET);
String token = s.issue(7L, "小魏16", "dev-1");
String token = s.issue(7L, "小魏16", "dev-1", 3L);
JwtService.ParseResult result = s.parseWithVerdict(token);
assertThat(result.verdict()).isEqualTo(JwtService.Verdict.VALID);
assertThat(result.isValid()).isTrue();
@@ -59,7 +59,7 @@ class JwtVerdictTest {
@Test
void wrongKeyIsInvalidNotExpired() {
String forged = service("9999999999012345678901234567890123456789")
.issue(7L, "小魏16", "dev-1");
.issue(7L, "小魏16", "dev-1", 3L);
JwtService.ParseResult result = service(SECRET).parseWithVerdict(forged);
assertThat(result.verdict()).isEqualTo(JwtService.Verdict.INVALID);
assertThat(result.claims()).isNull();
@@ -11,6 +11,7 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -91,6 +92,14 @@ class MigrationInventoryTest {
*/
private static final Set<Integer> RETIRED_HOLES = Set.of(132, 133, 148, 168, 169);
/**
* 文件名版本与头注释版本不一致的历史件。V170 的头注释误写成 V168;它已应用到生产,
* 按 AGENTS.md「已发布迁移一个字节都不许改」不能回改(CR-13)。文件名才是 Flyway
* 认的号,这里把它显式登记成唯一豁免;新增任何不一致都会判红。
*/
private static final Map<String, String> KNOWN_HEADER_VERSION_MISMATCH =
Map.of("V170__user_task_guard.sql", "V168");
@Test
void integerVersionsSequentialFromOne() throws IOException {
List<Migration> all = parseAll(versionFiles());
@@ -144,6 +153,24 @@ class MigrationInventoryTest {
@Test
void headerVersionMatchesFileNameExceptKnownImmutableMismatch() throws IOException {
Map<String, String> mismatches = new LinkedHashMap<>();
Pattern fileNameVersion = Pattern.compile("^V(\\d+)");
Pattern headerVersion = Pattern.compile("(?m)^--\\s*迁移\\s+V(\\d+)");
for (Path file : versionFiles()) {
String name = file.getFileName().toString();
Matcher fileMatcher = fileNameVersion.matcher(name);
Matcher headerMatcher = headerVersion.matcher(Files.readString(file));
if (fileMatcher.find() && headerMatcher.find()
&& !fileMatcher.group(1).equals(headerMatcher.group(1))) {
mismatches.put(name, "V" + headerMatcher.group(1));
}
}
assertEquals(KNOWN_HEADER_VERSION_MISMATCH, mismatches,
"迁移头注释的版本号必须与文件名一致;V170 是已应用件,只能登记为豁免,不能回改");
}
@Test
void checksumStableAcrossReads() throws IOException {
Map<String, String> first = sha256All(versionFiles());
@@ -21,6 +21,7 @@ import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
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;
@@ -57,6 +58,7 @@ class AuthServiceCompanyTest {
// 本组用例只关心公司身份,单设备互踢那条路径留给 AuthServiceTest
when(authProperties.isSingleDeviceEnabled()).thenReturn(false);
when(jwtService.ttlSeconds()).thenReturn(604800L);
when(loginUserMapper.incrementSessionEpoch(7L)).thenReturn(1);
service = new AuthService(loginUserMapper, passwordEncoder, jwtService,
mock(PermissionMenuService.class), authProperties, companyService,
mock(AccountExpiryService.class));
@@ -85,7 +87,7 @@ class AuthServiceCompanyTest {
private void stubSuccessfulLogin(LoginUserEntity user) {
when(loginUserMapper.selectOne(any())).thenReturn(user);
when(passwordEncoder.matches("pwd", "hash")).thenReturn(true);
when(jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
when(jwtService.issue(eq(7L), anyString(), anyString(), anyLong())).thenReturn("jwt-token");
}
@Test
@@ -137,7 +139,7 @@ class AuthServiceCompanyTest {
when(claims.get("deviceId")).thenReturn("devA");
when(jwtService.parse("t")).thenReturn(claims);
when(loginUserMapper.selectById(7L)).thenReturn(user);
when(jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
when(jwtService.issue(eq(7L), anyString(), anyString(), anyLong())).thenReturn("jwt-token");
when(companyService.nameOrEmpty(5L)).thenReturn("厦门米椿花");
LoginResultVo vo = service.checkLogin("t", null);
@@ -22,6 +22,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.catchThrowableOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
@@ -48,7 +49,7 @@ class AuthServiceTest {
Fixture f = fixture(true);
when(f.loginUserMapper.selectOne(any())).thenReturn(f.user("devA"));
when(f.passwordEncoder.matches("pwd", "hash")).thenReturn(true);
when(f.jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
when(f.jwtService.issue(eq(7L), anyString(), anyString(), anyLong())).thenReturn("jwt-token");
LoginRequest request = new LoginRequest();
request.setUsername("u7");
@@ -59,7 +60,9 @@ class AuthServiceTest {
// 绑定写入当前设备(last-login-wins),签发的 token 也用当前设备
verify(f.loginUserMapper, times(1)).update(any(), any());
verify(f.jwtService).issue(7L, "u7", "devB");
verify(f.loginUserMapper).incrementSessionEpoch(7L);
// 登录推进世代(0 → 1),新令牌必须签上推进后的值
verify(f.jwtService).issue(7L, "u7", "devB", 1L);
assertThat(vo.getDeviceId()).isEqualTo("devB");
}
@@ -68,7 +71,7 @@ class AuthServiceTest {
Fixture f = fixture(false);
when(f.loginUserMapper.selectOne(any())).thenReturn(f.user("devA"));
when(f.passwordEncoder.matches("pwd", "hash")).thenReturn(true);
when(f.jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
when(f.jwtService.issue(eq(7L), anyString(), anyString(), anyLong())).thenReturn("jwt-token");
LoginRequest request = new LoginRequest();
request.setUsername("u7");
@@ -87,7 +90,7 @@ class AuthServiceTest {
Fixture f = fixture(false);
when(f.loginUserMapper.selectOne(any())).thenReturn(f.user("revoked@7#20261024185618"));
when(f.passwordEncoder.matches("pwd", "hash")).thenReturn(true);
when(f.jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
when(f.jwtService.issue(eq(7L), anyString(), anyString(), anyLong())).thenReturn("jwt-token");
LoginRequest request = new LoginRequest();
request.setUsername("u7");
@@ -115,7 +118,7 @@ class AuthServiceTest {
assertThat(ex.getCode()).isEqualTo(4011);
assertThat(ex.getMessage()).contains("已在其他设备登录");
// 关键:被踢的旧 token 不能在这里换到新 token「复活」
verify(f.jwtService, never()).issue(any(), anyString(), anyString());
verify(f.jwtService, never()).issue(any(), anyString(), anyString(), anyLong());
}
@Test
@@ -124,11 +127,11 @@ class AuthServiceTest {
Claims claims = f.claims("devA");
when(f.jwtService.parse("t")).thenReturn(claims);
when(f.loginUserMapper.selectById(7L)).thenReturn(f.user("devA"));
when(f.jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
when(f.jwtService.issue(eq(7L), anyString(), anyString(), anyLong())).thenReturn("jwt-token");
LoginResultVo vo = f.service.checkLogin("t", null);
verify(f.jwtService).issue(7L, "u7", "devA");
verify(f.jwtService).issue(7L, "u7", "devA", 0L);
assertThat(vo.getDeviceId()).isEqualTo("devA");
}
@@ -155,7 +158,7 @@ class AuthServiceTest {
root.setRole("super_admin");
root.setIsAdmin(1);
when(f.loginUserMapper.selectById(7L)).thenReturn(root);
when(f.jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
when(f.jwtService.issue(eq(7L), anyString(), anyString(), anyLong())).thenReturn("jwt-token");
assertThatCode(() -> f.service.checkLogin("t", null)).doesNotThrowAnyException();
}
@@ -166,11 +169,59 @@ class AuthServiceTest {
Claims claims = f.claims("devB");
when(f.jwtService.parse("t")).thenReturn(claims);
when(f.loginUserMapper.selectById(7L)).thenReturn(f.user("devA"));
when(f.jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
when(f.jwtService.issue(eq(7L), anyString(), anyString(), anyLong())).thenReturn("jwt-token");
assertThatCode(() -> f.service.checkLogin("t", null)).doesNotThrowAnyException();
}
// ---------- 会话世代:登出/新登录必须让旧 token 失效 ----------
@Test
void checkLoginRejectsTokenFromBeforeLogoutOrRelogin() {
Fixture f = fixture(false);
LoginUserEntity user = f.user("");
user.setSessionEpoch(2L);
Claims claims = f.claims("devA", 1L);
when(f.jwtService.parse("old-token")).thenReturn(claims);
when(f.loginUserMapper.selectById(7L)).thenReturn(user);
BusinessException ex = catchThrowableOfType(
() -> f.service.checkLogin("old-token", null), BusinessException.class);
assertThat(ex.getCode()).isEqualTo(401);
assertThat(ex.getMessage()).contains("登录已过期");
verify(f.jwtService, never()).issue(any(), anyString(), anyString(), anyLong());
}
@Test
void logoutAdvancesSessionEpochEvenWhenMachineIsEmpty() {
// 单设备关闭时 machine 往往为空:旧实现按 machine 做 CAS,命中 0 行 = 登出没留下吊销。
Fixture f = fixture(false);
Claims claims = f.claims("devA", 0L);
when(f.jwtService.parse("t")).thenReturn(claims);
when(f.loginUserMapper.selectById(7L)).thenReturn(f.user(""));
f.service.logoutByToken("t");
verify(f.loginUserMapper).incrementSessionEpoch(7L);
}
@Test
void staleTokenCannotLogOutANewerSession() {
// 旧令牌(世代 1)不能把已经登录到世代 2 的新会话一起登出
Fixture f = fixture(false);
LoginUserEntity user = f.user("");
user.setSessionEpoch(2L);
Claims claims = f.claims("devA", 1L);
when(f.jwtService.parse("t")).thenReturn(claims);
when(f.loginUserMapper.selectById(7L)).thenReturn(user);
f.service.logoutByToken("t");
verify(f.loginUserMapper, never()).incrementSessionEpoch(anyLong());
verify(f.loginUserMapper, never()).update(any(), any());
}
// ---------- 夹具 ----------
private Fixture fixture(boolean singleDeviceEnabled) {
@@ -181,6 +232,7 @@ class AuthServiceTest {
AuthProperties authProperties = mock(AuthProperties.class);
when(authProperties.isSingleDeviceEnabled()).thenReturn(singleDeviceEnabled);
when(jwtService.ttlSeconds()).thenReturn(604800L);
when(loginUserMapper.incrementSessionEpoch(7L)).thenReturn(1);
AuthService service = new AuthService(loginUserMapper, passwordEncoder, jwtService,
permissionMenuService, authProperties, mock(CompanyService.class),
mock(AccountExpiryService.class));
@@ -202,9 +254,14 @@ class AuthServiceTest {
}
Claims claims(String deviceId) {
return claims(deviceId, 0L);
}
Claims claims(String deviceId, long sessionEpoch) {
Claims claims = mock(Claims.class);
when(claims.getSubject()).thenReturn("7");
when(claims.get("deviceId")).thenReturn(deviceId);
when(claims.get("sessionEpoch")).thenReturn(sessionEpoch);
return claims;
}
}
@@ -49,7 +49,7 @@ class ClientCredentialRenewalTest {
when(adminAuthSupport.requireUser(any())).thenReturn(user);
when(adminAuthSupport.currentInternalToken()).thenReturn("token-x");
when(adminAuthSupport.currentDeviceId(any())).thenReturn("dev-1");
when(jwtService.issue(eq(7L), eq("小魏16"), eq("dev-1"))).thenReturn("jwt-fresh");
when(jwtService.issue(eq(7L), eq("小魏16"), eq("dev-1"), eq(0L))).thenReturn("jwt-fresh");
configureAll();
ApiResponse<ClientCredentialVo> response = newController().credentials(request);
assertThat(response.isSuccess()).isTrue();
@@ -19,6 +19,25 @@ import static org.mockito.Mockito.when;
*/
class TaskDistributedLockServiceStopRenewalTest {
@Test
void benignTerminalStopRenewalDoesNotMarkRenewalLost() throws Exception {
// 父任务终态是良性停续期;标成 renewalLost=true 会让 _prod_watch 的 ACTIVE_LOST 误报
DistributedJobLockService delegate = mock(DistributedJobLockService.class);
DistributedJobLockService.LockHandle inner = mock(DistributedJobLockService.LockHandle.class);
when(inner.renew(any(Duration.class))).thenReturn(true);
when(delegate.tryLock(anyString(), any(Duration.class))).thenReturn(inner);
TaskDistributedLockService service = new TaskDistributedLockService(delegate);
TaskDistributedLockService.LockHandle handle =
service.acquire("appearance-patent", 32220L, Duration.ofSeconds(1), 0);
handle.stopRenewal("parent-task-terminal", false);
assertEquals(false, service.activeRenewalLost("task:appearance-patent:32220"),
"终态停续期不是续期失败");
handle.close();
}
@Test
void stopRenewalStopsTheWatchdogWithoutReleasingTheLock() throws Exception {
AtomicInteger renewCalls = new AtomicInteger();
@@ -83,6 +83,51 @@ class TaskFileJobServiceTest {
assertUpdateLeavesRetryCountIntact(running.getUpdatedAt());
}
@Test
void stuckRequeueAdvancesIndependentCounterWithoutTouchingRetryBudget() {
TaskFileJobEntity running = runningJob(111L, 1, LocalDateTime.now().minusHours(1));
running.setStuckRequeueCount(19);
TaskFileJobEntity pending = runningJob(111L, 1, LocalDateTime.now());
pending.setStatus("PENDING");
when(taskFileJobMapper.selectList(any())).thenReturn(List.of(running));
when(taskFileJobMapper.update(isNull(), any(LambdaUpdateWrapper.class))).thenReturn(1);
when(taskFileJobMapper.selectBatchIds(any())).thenReturn(List.of(pending));
TaskFileJobService service = new TaskFileJobService(taskFileJobMapper, applicationEventPublisher);
TaskFileJobService.StuckJobResetResult result = service.resetStuckRunningJobsDetailed(30, 20);
assertEquals(1, result.resetCount());
ArgumentCaptor<LambdaUpdateWrapper<TaskFileJobEntity>> update = updateCaptor();
verify(taskFileJobMapper).update(isNull(), update.capture());
assertTrue(update.getValue().getSqlSet().contains("stuck_requeue_count"));
assertFalse(update.getValue().getSqlSet().contains("retry_count"),
"卡死重排仍不得消耗真失败预算");
assertTrue(update.getValue().getParamNameValuePairs().containsValue(20));
}
@Test
void stuckRequeueAtLimitFailsJobAndHandsItToTerminalCallback() {
TaskFileJobEntity running = runningJob(112L, 1, LocalDateTime.now().minusHours(1));
running.setStuckRequeueCount(TaskFileJobService.MAX_STUCK_REQUEUE);
TaskFileJobEntity failed = runningJob(112L, TaskFileJobService.MAX_RETRY_COUNT, LocalDateTime.now());
failed.setStatus("FAILED");
failed.setErrorMessage("文件生成任务连续 20 次卡死重排,已判失败");
when(taskFileJobMapper.selectList(any())).thenReturn(List.of(running));
when(taskFileJobMapper.update(isNull(), any(LambdaUpdateWrapper.class))).thenReturn(1);
when(taskFileJobMapper.selectBatchIds(any())).thenReturn(List.of(failed));
TaskFileJobService service = new TaskFileJobService(taskFileJobMapper, applicationEventPublisher);
TaskFileJobService.StuckJobResetResult result = service.resetStuckRunningJobsDetailed(30, 20);
assertEquals(0, result.resetCount(), "达到上限后不再重排");
assertEquals(List.of(failed), result.exhaustedJobs());
ArgumentCaptor<LambdaUpdateWrapper<TaskFileJobEntity>> update = updateCaptor();
verify(taskFileJobMapper).update(isNull(), update.capture());
assertTrue(update.getValue().getParamNameValuePairs().containsValue("FAILED"));
assertTrue(update.getValue().getParamNameValuePairs().containsValue(TaskFileJobService.MAX_RETRY_COUNT));
verify(applicationEventPublisher, never()).publishEvent(any());
}
@Test
void pendingFailureFinalizationIsReturnedAgainWithoutRequeueing() {
TaskFileJobEntity pending = runningJob(106L, TaskFileJobService.MAX_RETRY_COUNT, LocalDateTime.now());
@@ -48,9 +48,12 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.contains;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.after;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
@@ -349,6 +352,34 @@ class TaskResultFileJobWorkerStuckScanTest {
"只在应用就绪后执行: " + List.of(listener.value()));
}
@Test
void terminalParentHeartbeatStopsTouchingAndFailsJob() throws Exception {
// 父任务在 worker 执行中变终态:心跳不得再 touchRunning(否则 stuck 扫描永远看不到),
// 并主动把 job 收口为 FAILED(终态回调由 markFailedPermanent 打标)。
TaskResultFileJobWorker worker = buildWorker();
setLong(worker, "heartbeatIntervalMillis", 10L);
TaskFileJobEntity job = new TaskFileJobEntity();
job.setId(77L);
job.setTaskId(15L);
job.setModuleType("SIMILAR_ASIN");
job.setStatus("RUNNING");
when(taskFileJobService.isOwningTaskTerminal(15L)).thenReturn(true);
java.lang.reflect.Method method = TaskResultFileJobWorker.class
.getDeclaredMethod("startJobHeartbeat", TaskFileJobEntity.class);
method.setAccessible(true);
java.util.concurrent.ScheduledFuture<?> future =
(java.util.concurrent.ScheduledFuture<?>) method.invoke(worker, job);
try {
// heartbeat 间隔下限 1000ms,首轮在 1s 后才跑;给足调度余量
verify(taskFileJobService, timeout(3000))
.markFailedPermanent(eq(job), contains("父任务已终态"));
verify(taskFileJobService, after(150).never()).touchRunning(77L);
} finally {
future.cancel(true);
}
}
private static void setLong(TaskResultFileJobWorker worker, String fieldName, long value) throws Exception {
Field field = TaskResultFileJobWorker.class.getDeclaredField(fieldName);
field.setAccessible(true);
@@ -1,6 +1,7 @@
package com.nanri.aiimage.modules.usersecret.service;
import com.nanri.aiimage.common.service.DistributedJobLockService;
import com.nanri.aiimage.common.service.NodeSharedStore;
import com.nanri.aiimage.config.CostGuardProperties;
import com.nanri.aiimage.modules.notification.service.NotificationDispatchService;
import com.nanri.aiimage.modules.notification.service.NotificationService;
@@ -12,6 +13,7 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.time.Duration;
import java.time.LocalDate;
import java.util.List;
@@ -41,6 +43,7 @@ class CostGuardSchedulerTest {
private DistributedJobLockService distributedJobLockService;
private CostGuardScheduler scheduler;
private CostGuardBaselineStore baselineStore;
@BeforeEach
void setUp() {
@@ -51,8 +54,16 @@ class CostGuardSchedulerTest {
properties.setDailyProxyThreshold(100);
properties.setDailyLlmCallsThreshold(100);
properties.setDailyLlmTokensThreshold(10_000);
scheduler = new CostGuardScheduler(properties, usageMapper, guardService,
sharedSecretMonitorService, notificationDispatchService, distributedJobLockService);
// null Redis/ObjectMapper ⇒ 本地实现;两个调度器共享同一个 store 即可模拟跨实例
baselineStore = new CostGuardBaselineStore(new NodeSharedStore<>("test-cost-guard",
Duration.ofHours(1), CostGuardBaselineStore.Baseline.class, null, null, 0L));
scheduler = newScheduler(properties);
}
private CostGuardScheduler newScheduler(CostGuardProperties properties) {
return new CostGuardScheduler(properties, usageMapper, guardService,
sharedSecretMonitorService, notificationDispatchService, distributedJobLockService,
baselineStore);
}
@Test
@@ -74,6 +85,7 @@ class CostGuardSchedulerTest {
.thenReturn(List.of(row(7L, "proxy", 0, 0)))
.thenReturn(List.of(row(7L, "proxy", 12, 0)));
when(guardService.isPaused(7L)).thenReturn(false);
when(guardService.pause(anyLong(), anyString())).thenReturn(true);
scheduler.runOnce();
scheduler.runOnce();
@@ -104,6 +116,9 @@ class CostGuardSchedulerTest {
.thenReturn(List.of(row(9L, "proxy", 100, 0)))
.thenReturn(List.of(row(9L, "proxy", 101, 0)));
when(guardService.isPaused(9L)).thenReturn(false);
// 用户点继续时基线对齐到当前 100(不是删掉基线);否则首轮会按日累计立刻再暂停
when(usageMapper.sumForUser(eq(9L), any(LocalDate.class)))
.thenReturn(List.of(row(9L, "proxy", 100, 0)));
scheduler.runOnce();
scheduler.runOnce();
@@ -126,6 +141,95 @@ class CostGuardSchedulerTest {
verify(guardService, never()).pause(anyLong(), anyString());
}
@Test
void firstScanPausesWhenDailyAlreadyOverLimit() {
// 重启/首次部署后当天已经越线:首轮窗口不判,但日累计必须判,否则当天永远不会被拦
when(usageMapper.sumByUserAndModule(any(LocalDate.class)))
.thenReturn(List.of(row(7L, "proxy", 150, 0)));
when(guardService.isPaused(7L)).thenReturn(false);
when(guardService.pause(anyLong(), anyString())).thenReturn(true);
scheduler.runOnce();
verify(guardService).pause(eq(7L), contains("当日上限 100 次"));
verify(notificationDispatchService).pushToUser(eq(7L),
eq(NotificationService.SCENE_COST_GUARD), eq(NotificationService.LEVEL_ERROR),
anyString(), contains("继续任务"), isNull());
}
@Test
void pauseWriteFailureDoesNotNotify() {
when(usageMapper.sumByUserAndModule(any(LocalDate.class)))
.thenReturn(List.of(row(7L, "proxy", 0, 0)))
.thenReturn(List.of(row(7L, "proxy", 12, 0)));
when(guardService.isPaused(7L)).thenReturn(false);
when(guardService.pause(anyLong(), anyString())).thenReturn(false);
scheduler.runOnce();
scheduler.runOnce();
verify(guardService).pause(eq(7L), contains("代理提取 12 次"));
verify(notificationDispatchService, never()).pushToUser(anyLong(), anyString(), anyString(),
anyString(), anyString(), isNull());
}
@Test
void pauseWriteFailureRetriesNextScanInsteadOfSwallowingTheBreach() {
when(usageMapper.sumByUserAndModule(any(LocalDate.class)))
.thenReturn(List.of(row(7L, "proxy", 0, 0)))
.thenReturn(List.of(row(7L, "proxy", 12, 0)))
.thenReturn(List.of(row(7L, "proxy", 13, 0)));
when(guardService.isPaused(7L)).thenReturn(false);
when(guardService.pause(anyLong(), anyString())).thenReturn(false, true);
scheduler.runOnce();
scheduler.runOnce();
scheduler.runOnce();
// 第一次写失败不推进基线,所以第三轮仍能看到增量并重试暂停
verify(guardService, org.mockito.Mockito.times(2))
.pause(eq(7L), contains("代理提取"));
verify(notificationDispatchService, org.mockito.Mockito.times(1)).pushToUser(eq(7L),
eq(NotificationService.SCENE_COST_GUARD), eq(NotificationService.LEVEL_ERROR),
anyString(), contains("继续任务"), isNull());
}
@Test
void multiMinuteGapIsNormalizedToWindowInsteadOfPausingFalsely() {
// 持锁方轮换:上一轮基线是 2 分钟前,增量 360 次。按 60 秒窗口归一化是 180 次,
// 不能把 360 当成一分钟增量(那会把正常的每分钟 180 次误判成失控)。
baselineStore.put(7L, new CostGuardBaselineStore.Baseline(0, 0, 0,
System.currentTimeMillis() - 120_000L));
when(usageMapper.sumByUserAndModule(any(LocalDate.class)))
.thenReturn(List.of(row(7L, "proxy", 360, 0)));
when(guardService.isPaused(7L)).thenReturn(false);
when(guardService.pause(anyLong(), anyString())).thenReturn(true);
scheduler.runOnce();
verify(guardService).pause(eq(7L), contains("代理提取 180 次"));
}
@Test
void resumeAlignsSharedBaselineSoAnotherInstanceDoesNotRepause() {
// 实例 A 写暂停并点继续;实例 B 接手后必须读到对齐后的基线,而不是上一轮的旧值
CostGuardProperties properties = new CostGuardProperties();
properties.setProxyWindowThreshold(10);
properties.setDailyProxyThreshold(100);
CostGuardScheduler instanceB = newScheduler(properties);
when(usageMapper.sumForUser(eq(9L), any(LocalDate.class)))
.thenReturn(List.of(row(9L, "proxy", 100, 0)));
when(usageMapper.sumByUserAndModule(any(LocalDate.class)))
.thenReturn(List.of(row(9L, "proxy", 101, 0)));
when(guardService.isPaused(9L)).thenReturn(false);
scheduler.resetBaseline(9L);
instanceB.runOnce();
verify(guardService, never()).pause(anyLong(), anyString());
}
private UserSecretUsageAggregate row(long userId, String module, long calls, long tokens) {
UserSecretUsageAggregate row = new UserSecretUsageAggregate();
row.setUserId(userId);
@@ -78,6 +78,22 @@ class SharedSecretMonitorServiceTest {
anyString(), anyString(), anyString());
}
@Test
void nonHolderRefreshesSnapshotWithoutPushingDuplicateNotice() {
// 多实例下非持锁实例也要看到共享提示,但通知只由持锁方推,避免三台各推一遍
when(userApiSecretMapper.selectList(any())).thenReturn(List.of(
row(1L, "proxy", "same-cipher"),
row(2L, "proxy", "same-cipher"),
row(3L, "proxy", "same-cipher")));
service.refresh(false);
assertThat(service.warningsFor(1L)).hasSize(1);
assertThat(service.warningsFor(2L)).hasSize(1);
verify(notificationDispatchService, never()).pushToUser(anyLong(), anyString(), anyString(),
anyString(), anyString(), anyString());
}
@Test
void llmModuleIsTypedAsLlm() {
when(userApiSecretMapper.selectList(any())).thenReturn(List.of(
@@ -40,7 +40,7 @@ class UserTaskGuardServiceTest {
UserTaskGuardService service = new UserTaskGuardService(guardMapper);
when(guardMapper.selectById(7L)).thenReturn(null);
service.pause(7L, "LLM 消耗异常");
assertThat(service.pause(7L, "LLM 消耗异常")).isTrue();
verify(guardMapper).pause(7L, "LLM 消耗异常");
service.resume(7L);
@@ -56,6 +56,15 @@ class UserTaskGuardServiceTest {
assertThat(service.isPaused(0L)).isFalse();
}
@Test
void pauseWriteFailureReturnsFalseSoCallerDoesNotNotify() {
UserTaskGuardService service = new UserTaskGuardService(guardMapper);
org.mockito.Mockito.doThrow(new IllegalStateException("db down"))
.when(guardMapper).pause(7L, "LLM 消耗异常");
assertThat(service.pause(7L, "LLM 消耗异常")).isFalse();
}
@Test
void mapperFailureFallsBackToNotPaused() {
when(guardMapper.selectById(7L)).thenThrow(new IllegalStateException("db down"));
@@ -79,6 +79,19 @@ class WorkbenchClientPackageServiceTest {
assertEquals("https://oss.aishufu.top/client/workbench-client/versions/put", result.get("upload_url"));
}
@Test
void presignKeysAreUniqueForTheSameVersion() {
// 同毫秒/同版本两次预签名必须不同:只靠时间戳会在 A/B 实例或并发上传时覆盖对象
when(ossStorageService.presignWorkbenchClientUpload(anyString(), eq(180))).thenReturn("put");
when(ossStorageService.getWorkbenchClientDownloadUrl(anyString())).thenReturn("download");
String first = (String) service().presignPackage("pkg.zip", "1.2.3").get("object_key");
String second = (String) service().presignPackage("pkg.zip", "1.2.3").get("object_key");
assertTrue(!first.equals(second), "同版本两次上传不得生成同一个对象 key");
assertTrue(first.endsWith("-1.2.3.zip") && second.endsWith("-1.2.3.zip"));
}
@Test
void presignRejectsNonZipAndBlankVersion() {
assertThrows(BusinessException.class, () -> service().presignPackage("pkg.exe", "1.0.0"));
@@ -108,6 +121,47 @@ class WorkbenchClientPackageServiceTest {
verify(packageMapper).insert(any(WorkbenchClientPackageEntity.class));
}
@Test
void confirmIsIdempotentForTheSameObjectKey() {
String objectKey = PREFIX + "20261024100000123-uuid-1.2.3.zip";
WorkbenchClientPackageEntity existing = row(99, "1.2.3", objectKey);
when(packageMapper.selectOne(any())).thenReturn(existing);
Map<String, Object> result = service().confirmPackage(
objectKey, "ShuFuAI-Workbench-1.2.3.zip", "1.2.3");
assertEquals(99L, result.get("id"));
verify(ossStorageService, never()).workbenchClientObjectSize(anyString());
verify(ossStorageService, never()).workbenchClientObjectSha256(anyString());
verify(packageMapper, never()).insert(any(WorkbenchClientPackageEntity.class));
}
@Test
void confirmRejectsEmptyObjectAndCleansItUp() {
String objectKey = PREFIX + "20261024100000123-uuid-1.2.3.zip";
when(ossStorageService.workbenchClientObjectSize(objectKey)).thenReturn(0L);
assertThrows(BusinessException.class, () -> service().confirmPackage(
objectKey, "ShuFuAI-Workbench-1.2.3.zip", "1.2.3"));
verify(ossStorageService).removeWorkbenchClientObject(objectKey);
verify(ossStorageService, never()).workbenchClientObjectSha256(anyString());
verify(packageMapper, never()).insert(any(WorkbenchClientPackageEntity.class));
}
@Test
void confirmRejectsDuplicateVersionToPreventSameVersionDifferentBytes() {
String objectKey = PREFIX + "20261024100000123-uuid-1.2.3.zip";
when(ossStorageService.workbenchClientObjectSize(objectKey)).thenReturn(1234L);
when(packageMapper.selectCount(any())).thenReturn(1L);
assertThrows(BusinessException.class, () -> service().confirmPackage(
objectKey, "ShuFuAI-Workbench-1.2.3.zip", "1.2.3"));
verify(ossStorageService, never()).workbenchClientObjectSha256(anyString());
verify(packageMapper, never()).insert(any(WorkbenchClientPackageEntity.class));
}
@Test
void confirmRejectsVersionThatDoesNotMatchObjectKey() {
String objectKey = PREFIX + "20261024100000123-1.2.3.zip";