From 0a13eabe642d7b502f91467f6c93e54f6c9aa7a2 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期一, 27 四月 2026 12:44:15 +0800
Subject: [PATCH] fix: release auto tune scheduler lock on guard failure

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

diff --git a/src/test/java/com/zy/ai/service/AutoTuneCoordinatorServiceImplTest.java b/src/test/java/com/zy/ai/service/AutoTuneCoordinatorServiceImplTest.java
index 2f880c2..fc3f594 100644
--- a/src/test/java/com/zy/ai/service/AutoTuneCoordinatorServiceImplTest.java
+++ b/src/test/java/com/zy/ai/service/AutoTuneCoordinatorServiceImplTest.java
@@ -346,6 +346,29 @@
     }
 
     @Test
+    void coordinatorRunsAgentAndReleasesLockWhenGuardWriteFails() {
+        AutoTuneAgentService.AutoTuneAgentResult agentResult = successfulAgentResult();
+        when(configService.getConfigValue("aiAutoTuneEnabled", "N")).thenReturn("Y");
+        when(configService.getConfigValue("aiAutoTuneIntervalMinutes", "10")).thenReturn("10");
+        when(wrkMastService.count(any(Wrapper.class))).thenReturn(1L);
+        when(redisUtil.get(RedisKeyType.AI_AUTO_TUNE_LAST_TRIGGER_GUARD.key)).thenReturn(null);
+        when(aiAutoTuneJobService.list(any(Wrapper.class))).thenReturn(Collections.emptyList());
+        when(redisUtil.trySetStringIfAbsent(anyString(), anyString(), anyLong())).thenReturn(true);
+        doThrow(new RuntimeException("guard failed"))
+                .when(redisUtil)
+                .set(eq(RedisKeyType.AI_AUTO_TUNE_LAST_TRIGGER_GUARD.key), any(), eq(600L));
+        when(autoTuneAgentService.runAutoTune(AutoTuneTriggerType.AUTO.getCode())).thenReturn(agentResult);
+
+        AutoTuneCoordinatorService.AutoTuneCoordinatorResult result = coordinatorService().runAutoTuneIfEligible();
+
+        assertFalse(result.getSkipped());
+        assertTrue(result.getTriggered());
+        assertSame(agentResult, result.getAgentResult());
+        verify(autoTuneAgentService).runAutoTune(AutoTuneTriggerType.AUTO.getCode());
+        verify(redisUtil).compareAndDelete(anyString(), anyString());
+    }
+
+    @Test
     void coordinatorSetsGuardWhenAgentReturnsFailure() {
         AutoTuneAgentService.AutoTuneAgentResult agentResult = failedAgentResult();
         when(configService.getConfigValue("aiAutoTuneEnabled", "N")).thenReturn("Y");

--
Gitblit v1.9.1