jolshan commented on code in PR #13963:
URL: https://github.com/apache/kafka/pull/13963#discussion_r1258697658


##########
group-coordinator/src/test/java/org/apache/kafka/coordinator/group/MockCoordinatorTimer.java:
##########
@@ -0,0 +1,144 @@
+/*
+ * 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;
+
+import org.apache.kafka.common.utils.Time;
+import org.apache.kafka.coordinator.group.runtime.CoordinatorTimer;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.PriorityQueue;
+import java.util.concurrent.TimeUnit;
+
+public class MockCoordinatorTimer<T> implements CoordinatorTimer<T> {
+    public static class ScheduledTimeout<T> {
+        public final String key;
+        public final long deadlineMs;
+        public final TimeoutOperation<T> operation;
+
+        ScheduledTimeout(
+            String key,
+            long deadlineMs,
+            TimeoutOperation<T> operation
+        ) {
+            this.key = key;
+            this.deadlineMs = deadlineMs;
+            this.operation = operation;
+        }
+    }
+
+    public static class ExpiredTimeout<T> {
+        public final String key;
+        public final List<T> records;
+
+        ExpiredTimeout(
+            String key,
+            List<T> records
+        ) {
+            this.key = key;
+            this.records = records;
+        }
+
+        @Override
+        public boolean equals(Object o) {
+            if (this == o) return true;
+            if (o == null || getClass() != o.getClass()) return false;
+
+            ExpiredTimeout<?> that = (ExpiredTimeout<?>) o;
+
+            if (!Objects.equals(key, that.key)) return false;
+            return Objects.equals(records, that.records);
+        }
+
+        @Override
+        public int hashCode() {
+            int result = key != null ? key.hashCode() : 0;
+            result = 31 * result + (records != null ? records.hashCode() : 0);
+            return result;
+        }
+    }
+
+    private final Time time;
+
+    private final Map<String, ScheduledTimeout<T>> timeoutMap = new 
HashMap<>();
+    private final PriorityQueue<ScheduledTimeout<T>> timeoutQueue = new 
PriorityQueue<>(
+        Comparator.comparingLong(entry -> entry.deadlineMs)
+    );
+
+    public MockCoordinatorTimer(Time time) {
+        this.time = time;
+    }
+
+    @Override
+    public void schedule(
+        String key,
+        long delay,
+        TimeUnit unit,
+        boolean retry,
+        TimeoutOperation<T> operation
+    ) {
+        cancel(key);
+
+        long deadlineMs = time.milliseconds() + unit.toMillis(delay);
+        ScheduledTimeout<T> timeout = new ScheduledTimeout<>(key, deadlineMs, 
operation);
+        timeoutQueue.add(timeout);
+        timeoutMap.put(key, timeout);
+    }
+
+    @Override
+    public void cancel(String key) {
+        ScheduledTimeout<T> timeout = timeoutMap.remove(key);
+        if (timeout != null) {
+            timeoutQueue.remove(timeout);
+        }
+    }
+
+    public boolean contains(String key) {
+        return timeoutMap.containsKey(key);
+    }
+
+    public ScheduledTimeout<T> timeout(String key) {
+        return timeoutMap.get(key);
+    }
+
+    public int size() {
+        return timeoutMap.size();
+    }
+
+    public List<ExpiredTimeout<T>> poll() {

Review Comment:
   So we don't expire timeouts until we call this poll?



-- 
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