Copilot commented on code in PR #4169:
URL: https://github.com/apache/hertzbeat/pull/4169#discussion_r3522707821


##########
hertzbeat-collector/hertzbeat-collector-common/src/test/java/org/apache/hertzbeat/collector/timer/TimerDispatcherTest.java:
##########
@@ -179,4 +188,84 @@ void 
testGetNextExecutionIntervalWithCronAndNullExpression() {
         // Verify - Should fall back to interval value
         assertEquals(180L, result);
     }
-}
\ No newline at end of file
+
+    @Test
+    void testAddCyclicJobCancelsPreviousTimeoutForSameJob() {
+        Job firstJob = Job.builder()
+                .id(1L)
+                .app("test")
+                .isCyclic(true)
+                .configmap(List.of())
+                .metrics(List.of(Metrics.builder().interval(60L).build()))
+                .build();
+        timerDispatcher.addJob(firstJob, null);
+
+        Timeout firstTimeout = currentCyclicTaskMap().get(1L);
+
+        Job updatedJob = Job.builder()
+                .id(1L)
+                .app("test")
+                .isCyclic(true)
+                .configmap(List.of())
+                .metrics(List.of(Metrics.builder().interval(60L).build()))
+                .build();
+        timerDispatcher.addJob(updatedJob, null);
+
+        Timeout updatedTimeout = currentCyclicTaskMap().get(1L);
+        assertNotSame(firstTimeout, updatedTimeout);
+        assertTrue(firstTimeout.isCancelled());
+        assertFalse(updatedTimeout.isCancelled());
+    }
+
+    @Test
+    void testAddTemporaryJobCancelsPreviousTimeoutForSameJob() {
+        Job firstJob = Job.builder()
+                .id(1L)
+                .app("test")
+                .isCyclic(false)
+                .configmap(List.of())
+                .metrics(List.of(Metrics.builder().interval(60L).build()))
+                .build();
+        timerDispatcher.addJob(firstJob, new CollectResponseEventListener() {
+        });
+
+        Timeout firstTimeout = currentTempTaskMap().get(1L);
+
+        Job updatedJob = Job.builder()
+                .id(1L)
+                .app("test")
+                .isCyclic(false)
+                .configmap(List.of())
+                .metrics(List.of(Metrics.builder().interval(60L).build()))
+                .build();
+        timerDispatcher.addJob(updatedJob, new CollectResponseEventListener() {
+        });
+
+        Timeout updatedTimeout = currentTempTaskMap().get(1L);
+        assertNotSame(firstTimeout, updatedTimeout);
+        assertTrue(firstTimeout.isCancelled());
+        assertFalse(updatedTimeout.isCancelled());
+    }
+
+    @SuppressWarnings("unchecked")
+    private Map<Long, Timeout> currentCyclicTaskMap() {
+        try {
+            Field field = 
TimerDispatcher.class.getDeclaredField("currentCyclicTaskMap");
+            field.setAccessible(true);
+            return (Map<Long, Timeout>) field.get(timerDispatcher);
+        } catch (ReflectiveOperationException e) {
+            throw new AssertionError(e);
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    private Map<Long, Timeout> currentTempTaskMap() {
+        try {
+            Field field = 
TimerDispatcher.class.getDeclaredField("currentTempTaskMap");
+            field.setAccessible(true);
+            return (Map<Long, Timeout>) field.get(timerDispatcher);
+        } catch (ReflectiveOperationException e) {
+            throw new AssertionError(e);
+        }
+    }
+}

Review Comment:
   This test class now schedules real HashedWheelTimer timeouts (via addJob), 
which starts a background worker thread. The class does not stop the timer 
after each test, which can leak threads/timeouts across the test suite and may 
trigger HashedWheelTimer "too many instances" warnings. Add an `@AfterEach` 
hook to call timerDispatcher.destroy().



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to