This is an automated email from the ASF dual-hosted git repository.

likeguo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new 781e7222fe feat(ut): add some tests (#6105)
781e7222fe is described below

commit 781e7222fe0ea8516747e32e420885e9c556fdb4
Author: shown <[email protected]>
AuthorDate: Sat Aug 23 13:35:11 2025 +0800

    feat(ut): add some tests (#6105)
    
    * feat(ut): add some tests
    
    Signed-off-by: yuluo-yx <[email protected]>
    
    * fix
    
    Signed-off-by: yuluo-yx <[email protected]>
    
    ---------
    
    Signed-off-by: yuluo-yx <[email protected]>
    Co-authored-by: likeguo <[email protected]>
---
 .../shenyu/common/timer/TimerTaskListTest.java     | 116 +++++++++++++++++++++
 1 file changed, 116 insertions(+)

diff --git 
a/shenyu-common/src/test/java/org/apache/shenyu/common/timer/TimerTaskListTest.java
 
b/shenyu-common/src/test/java/org/apache/shenyu/common/timer/TimerTaskListTest.java
new file mode 100644
index 0000000000..42f74362ad
--- /dev/null
+++ 
b/shenyu-common/src/test/java/org/apache/shenyu/common/timer/TimerTaskListTest.java
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.common.timer;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.Iterator;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class TimerTaskListTest {
+
+    private TimerTaskList list;
+
+    private AtomicInteger counter;
+
+    @BeforeEach
+    void setUp() {
+
+        counter = new AtomicInteger(0);
+        list = new TimerTaskList(counter);
+    }
+
+    @Test
+    void testAddAndRemove() {
+
+        TestTimerTask task = new TestTimerTask(100L);
+
+        TimerTaskList.TimerTaskEntry entry = new 
TimerTaskList.TimerTaskEntry(null, task, 100L);
+
+        list.add(entry);
+        assertEquals(1, counter.get());
+
+        list.remove(entry);
+        assertEquals(0, counter.get());
+    }
+
+    @Test
+    void testSetAndGetExpiration() {
+
+        assertTrue(list.setExpiration(123L));
+        assertEquals(123L, list.getExpiration());
+        assertFalse(list.setExpiration(123L));
+    }
+
+    @Test
+    void testFlush() {
+
+        TestTimerTask task = new TestTimerTask(100L);
+
+        TimerTaskList.TimerTaskEntry entry = new 
TimerTaskList.TimerTaskEntry(null, task, 100L);
+
+        list.add(entry);
+        list.flush(e -> assertNotNull(e));
+
+        assertEquals(-1L, list.getExpiration());
+        assertEquals(0, counter.get());
+    }
+
+    @Test
+    void testIterator() {
+
+        TestTimerTask task = new TestTimerTask(100L);
+        TimerTaskList.TimerTaskEntry entry = new 
TimerTaskList.TimerTaskEntry(null, task, 100L);
+
+        list.add(entry);
+        Iterator<TimerTask> it = list.iterator();
+
+        assertTrue(it.hasNext());
+        assertEquals(task, it.next());
+    }
+
+    @Test
+    void testGetDelay() {
+
+        list.setExpiration(System.currentTimeMillis() + 1000);
+
+        long delay = list.getDelay(TimeUnit.MILLISECONDS);
+
+        assertTrue(delay >= 0);
+    }
+
+    static final class TestTimerTask extends TimerTask {
+
+        private TestTimerTask(final long delayMs) {
+            super(delayMs);
+        }
+
+        @Override
+        public void run(final TaskEntity taskEntity) {
+            boolean executed = true;
+        }
+    }
+
+}

Reply via email to