From 91fbede5aa117b3c010e9cbb8804df90757dbe5c Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期一, 27 四月 2026 13:32:13 +0800
Subject: [PATCH] fix: isolate auto tune cache refresh failures

---
 src/test/java/com/zy/ai/service/AutoTuneApplyServiceImplTest.java |   48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 48 insertions(+), 0 deletions(-)

diff --git a/src/test/java/com/zy/ai/service/AutoTuneApplyServiceImplTest.java b/src/test/java/com/zy/ai/service/AutoTuneApplyServiceImplTest.java
index 04c55c4..a138e1e 100644
--- a/src/test/java/com/zy/ai/service/AutoTuneApplyServiceImplTest.java
+++ b/src/test/java/com/zy/ai/service/AutoTuneApplyServiceImplTest.java
@@ -50,8 +50,10 @@
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.inOrder;
 import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
@@ -316,6 +318,27 @@
     }
 
     @Test
+    void applyCacheRefreshFailureDoesNotAppendFailedAuditOrJob() {
+        when(configService.getOne(any(Wrapper.class))).thenReturn(config("conveyorStationTaskLimit", "10"));
+        doThrow(new IllegalStateException("cache refresh failed"))
+                .when(configService).refreshSystemConfigCache();
+
+        AutoTuneApplyResult result = service.apply(request(false,
+                command("sys_config", null, "conveyorStationTaskLimit", "15")
+        ));
+
+        List<AiAutoTuneChange> changes = savedChanges();
+        AiAutoTuneJob updatedJob = updatedJob();
+        assertTrue(result.getSuccess());
+        assertEquals("success", updatedJob.getStatus());
+        assertEquals(1, changes.size());
+        assertEquals("success", changes.get(0).getResultStatus());
+        verify(aiAutoTuneChangeService, times(1)).saveBatch(any(Collection.class));
+        verify(aiAutoTuneJobService, times(1)).updateById(any(AiAutoTuneJob.class));
+        verify(configService).refreshSystemConfigCache();
+    }
+
+    @Test
     void writeFailureReturnsFailedAuditWithoutThrowing() {
         when(configService.getOne(any(Wrapper.class))).thenReturn(config("conveyorStationTaskLimit", "10"));
         when(configService.saveConfigValue("conveyorStationTaskLimit", "15")).thenThrow(new IllegalStateException("db write failed"));
@@ -438,6 +461,31 @@
     }
 
     @Test
+    void rollbackCacheRefreshFailureDoesNotAppendFailedAuditOrJob() {
+        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"));
+        doThrow(new IllegalStateException("cache refresh failed"))
+                .when(configService).refreshSystemConfigCache();
+
+        AutoTuneApplyResult result = service.rollbackLastSuccessfulJob("manual rollback");
+
+        List<AiAutoTuneChange> changes = savedChanges();
+        AiAutoTuneJob updatedJob = updatedJob();
+        assertTrue(result.getSuccess());
+        assertEquals("success", updatedJob.getStatus());
+        assertEquals(1, changes.size());
+        assertEquals("success", changes.get(0).getResultStatus());
+        verify(aiAutoTuneChangeService, times(1)).saveBatch(any(Collection.class));
+        verify(aiAutoTuneJobService, times(1)).updateById(any(AiAutoTuneJob.class));
+        verify(configService).refreshSystemConfigCache();
+    }
+
+    @Test
     void rollbackAuditSaveBatchFailureRollsBackTargetWriteAndReturnsFailedAudit() {
         AiAutoTuneJob latestRealJob = job(10L, "manual", "success");
         AiAutoTuneChange configChange = successChange(10L, "sys_config", "", "conveyorStationTaskLimit", "10", "15");

--
Gitblit v1.9.1