dajac commented on code in PR #13991:
URL: https://github.com/apache/kafka/pull/13991#discussion_r1263354270


##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/util/SystemTimerReaperTest.java:
##########
@@ -0,0 +1,65 @@
+/*
+ * 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.kafka.coordinator.group.util;
+
+import org.apache.kafka.common.errors.TimeoutException;
+import org.apache.kafka.server.util.timer.SystemTimer;
+import org.apache.kafka.server.util.timer.Timer;
+import org.apache.kafka.server.util.timer.TimerTask;
+import org.apache.kafka.test.TestUtils;
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.CompletableFuture;
+
+public class SystemTimerReaperTest {
+    private static class FutureTimerTask<T> extends TimerTask {
+        CompletableFuture<T> future = new CompletableFuture<>();
+
+        FutureTimerTask(long delayMs) {
+            super(delayMs);
+        }
+
+        @Override
+        public void run() {
+            // We use org.apache.kafka.common.errors.TimeoutException to 
differentiate
+            // from java.util.concurrent.TimeoutException.
+            future.completeExceptionally(new TimeoutException(
+                String.format("Future failed to be completed before timeout of 
%sMs ms was reached", delayMs)));
+        }
+    }
+
+    private <T> CompletableFuture<T> add(Timer timer, long delayMs) {
+        FutureTimerTask<T> task = new FutureTimerTask<>(delayMs);
+        timer.add(task);
+        return task.future;
+    }
+
+    @Test
+    public void testReaper() throws Exception {
+        Timer timer = new SystemTimerReaper("reaper", new 
SystemTimer("timer"));
+        try {
+            CompletableFuture<Void> t1 = add(timer, 100L);
+            CompletableFuture<Void> t2 = add(timer, 200L);
+            CompletableFuture<Void> t3 = add(timer, 300L);
+            TestUtils.assertFutureThrows(t1, TimeoutException.class);

Review Comment:
   As I said earlier, the aim of this test is not to verify that tasks are 
expired at the right time but rather to verify that tasks are expired at all. I 
just wanted to validate that the reaper thread is started and makes progress 
here. The SystemTimer already has tests to verify the expiration logic so I 
thought that it is not necessary to replicate this here. After all, this class 
just introduces the reaper thread.
   
   It seems hard to me to verify that the tasks are expired at the correct time 
here without controlling the time and the ticks of the thread. We could try to 
measure but that would likely result in a flaky test. I am open to suggestion 
here if you have a better idea.



-- 
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: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to