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:
+4
-2
@@ -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);
|
||||
|
||||
+66
-9
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -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
@@ -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();
|
||||
|
||||
+45
@@ -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());
|
||||
|
||||
+31
@@ -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);
|
||||
|
||||
+106
-2
@@ -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);
|
||||
|
||||
+16
@@ -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(
|
||||
|
||||
+10
-1
@@ -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"));
|
||||
|
||||
+54
@@ -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";
|
||||
|
||||
Reference in New Issue
Block a user