| | |
| | | import java.util.Collections; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | import java.util.concurrent.atomic.AtomicLong; |
| | | |
| | | import static org.junit.jupiter.api.Assertions.assertEquals; |
| | |
| | | } |
| | | |
| | | @Test |
| | | void jobUpdateFailureRecordsFailureJobWhenRecoveryUpdateSucceeds() { |
| | | AtomicInteger updateAttempts = new AtomicInteger(); |
| | | when(configService.getOne(any(Wrapper.class))).thenReturn(config("conveyorStationTaskLimit", "10")); |
| | | when(aiAutoTuneJobService.updateById(any(AiAutoTuneJob.class))).thenAnswer(invocation -> { |
| | | transactionManager.recordJobUpdateCall(); |
| | | return updateAttempts.incrementAndGet() > 1; |
| | | }); |
| | | |
| | | AutoTuneApplyResult result = service.apply(request(false, command("sys_config", null, "conveyorStationTaskLimit", "15"))); |
| | | |
| | | List<AiAutoTuneChange> changes = savedChanges(); |
| | | AiAutoTuneJob updatedJob = updatedJob(); |
| | | assertFalse(result.getSuccess()); |
| | | assertEquals("failed", updatedJob.getStatus()); |
| | | assertEquals(1, changes.size()); |
| | | assertEquals("failed", changes.get(0).getResultStatus()); |
| | | assertTrue(changes.get(0).getRejectReason().contains("更新调参任务状态失败")); |
| | | assertEquals(1, transactionManager.getRollbackCount()); |
| | | assertTrue(transactionManager.getCommitCount() >= 1); |
| | | assertTrue(transactionManager.getJobSaveInsideTransactionCount() > 0); |
| | | assertTrue(transactionManager.getJobUpdateInsideTransactionCount() > 0); |
| | | assertEquals(0, transactionManager.getJobSaveOutsideTransactionCount()); |
| | | assertEquals(0, transactionManager.getJobUpdateOutsideTransactionCount()); |
| | | verify(configService).saveConfigValue("conveyorStationTaskLimit", "15"); |
| | | verify(configService, never()).refreshSystemConfigCache(); |
| | | verify(aiAutoTuneChangeService, times(2)).saveBatch(any(Collection.class)); |
| | | verify(aiAutoTuneJobService, times(2)).updateById(any(AiAutoTuneJob.class)); |
| | | verify(redisUtil).compareAndDelete(eq(RedisKeyType.AI_AUTO_TUNE_APPLY_LOCK.key), anyString()); |
| | | } |
| | | |
| | | @Test |
| | | void realApplyLockNotAcquiredRejectsWithoutTargetWrite() { |
| | | when(redisUtil.trySetStringIfAbsent(anyString(), anyString(), anyLong())).thenReturn(false); |
| | | |
| | |
| | | } |
| | | |
| | | @Test |
| | | void rollbackJobUpdateFailureRecordsFailureJobWhenRecoveryUpdateSucceeds() { |
| | | AtomicInteger updateAttempts = new AtomicInteger(); |
| | | AiAutoTuneJob latestRealJob = job(10L, "manual", "success"); |
| | | AiAutoTuneChange configChange = successChange(10L, "sys_config", "", "conveyorStationTaskLimit", "10", "15"); |
| | | when(aiAutoTuneChangeService.list(any(Wrapper.class))) |
| | | .thenReturn(List.of(configChange)) |
| | | .thenReturn(List.of(configChange)); |
| | | when(aiAutoTuneJobService.getById(10L)).thenReturn(latestRealJob); |
| | | when(configService.getOne(any(Wrapper.class))).thenReturn(config("conveyorStationTaskLimit", "15")); |
| | | when(aiAutoTuneJobService.updateById(any(AiAutoTuneJob.class))).thenAnswer(invocation -> { |
| | | transactionManager.recordJobUpdateCall(); |
| | | return updateAttempts.incrementAndGet() > 1; |
| | | }); |
| | | |
| | | AutoTuneApplyResult result = service.rollbackLastSuccessfulJob("manual rollback"); |
| | | |
| | | List<AiAutoTuneChange> changes = savedChanges(); |
| | | AiAutoTuneJob updatedJob = updatedJob(); |
| | | assertFalse(result.getSuccess()); |
| | | assertEquals("failed", updatedJob.getStatus()); |
| | | assertEquals(1, changes.size()); |
| | | assertEquals("failed", changes.get(0).getResultStatus()); |
| | | assertTrue(changes.get(0).getRejectReason().contains("更新调参任务状态失败")); |
| | | assertEquals(1, transactionManager.getRollbackCount()); |
| | | assertTrue(transactionManager.getCommitCount() >= 1); |
| | | assertTrue(transactionManager.getJobSaveInsideTransactionCount() > 0); |
| | | assertTrue(transactionManager.getJobUpdateInsideTransactionCount() > 0); |
| | | assertEquals(0, transactionManager.getJobSaveOutsideTransactionCount()); |
| | | assertEquals(0, transactionManager.getJobUpdateOutsideTransactionCount()); |
| | | verify(configService).saveConfigValue("conveyorStationTaskLimit", "10"); |
| | | verify(configService, never()).refreshSystemConfigCache(); |
| | | verify(aiAutoTuneChangeService, times(2)).saveBatch(any(Collection.class)); |
| | | verify(aiAutoTuneJobService, times(2)).updateById(any(AiAutoTuneJob.class)); |
| | | verify(redisUtil).compareAndDelete(eq(RedisKeyType.AI_AUTO_TUNE_APPLY_LOCK.key), anyString()); |
| | | } |
| | | |
| | | @Test |
| | | void rollbackLockNotAcquiredReturnsFailedAuditWithoutTargetWrite() { |
| | | when(redisUtil.trySetStringIfAbsent(anyString(), anyString(), anyLong())).thenReturn(false); |
| | | |