kirktrue commented on code in PR #15640:
URL: https://github.com/apache/kafka/pull/15640#discussion_r1599236609


##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/events/CompletableEventReaperTest.java:
##########
@@ -0,0 +1,211 @@
+/*
+ * 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.clients.consumer.internals.events;
+
+import org.apache.kafka.clients.consumer.internals.ConsumerUtils;
+import org.apache.kafka.common.errors.TimeoutException;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.MockTime;
+import org.apache.kafka.common.utils.Time;
+import org.apache.kafka.common.utils.Timer;
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.LinkedBlockingQueue;
+
+import static 
org.apache.kafka.clients.consumer.internals.events.CompletableEvent.calculateDeadlineMs;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class CompletableEventReaperTest {
+
+    private final LogContext logContext = new LogContext();
+    private final Time time = new MockTime();
+    private final CompletableEventReaper<CompletableApplicationEvent<?>> 
reaper = new CompletableEventReaper<>(logContext);
+
+    @Test
+    public void testExpired() {
+        // Add a new event to the reaper.
+        Timer timer = time.timer(100);
+        UnsubscribeEvent event = new 
UnsubscribeEvent(calculateDeadlineMs(timer));
+        reaper.add(event);
+
+        // Without any time passing, we check the reaper and verify that the 
event is not done amd is still
+        // being tracked.
+        reaper.reapExpiredAndCompleted(time.milliseconds());
+        assertFalse(event.future().isDone());
+        assertEquals(1, reaper.size());
+
+        // Sleep for at least 1 ms. *more* than the timeout so that the event 
is considered expired.
+        time.sleep(timer.timeoutMs() + 1);
+        timer.update(time.milliseconds());
+        assertEquals(0, timer.remainingMs());
+
+        // However, until we actually invoke the reaper, the event isn't 
complete and is still being tracked.
+        assertFalse(event.future().isDone());
+        assertEquals(1, reaper.size());
+
+        // Call the reaper and validate that the event is now "done" 
(expired), the correct exception type is
+        // thrown, and the event is no longer tracked.
+        reaper.reapExpiredAndCompleted(time.milliseconds());
+        assertTrue(event.future().isDone());
+        assertThrows(TimeoutException.class, () -> 
ConsumerUtils.getResult(event.future()));
+        assertEquals(0, reaper.size());
+    }
+
+    @Test
+    public void testCompleted() {
+        // Add a new event to the reaper.
+        Timer timer = time.timer(100);
+        UnsubscribeEvent event = new 
UnsubscribeEvent(calculateDeadlineMs(timer));
+        reaper.add(event);
+
+        // Without any time passing, we check the reaper and verify that the 
event is not done amd is still
+        // being tracked.
+        reaper.reapExpiredAndCompleted(time.milliseconds());
+        assertFalse(event.future().isDone());
+        assertEquals(1, reaper.size());
+
+        // We'll cause the event to be completed normally. Note that because 
we haven't called the reaper, the
+        // event is still being tracked.
+        event.future().complete(null);
+        assertTrue(event.future().isDone());
+        assertEquals(1, reaper.size());
+
+        // To ensure we don't accidentally expire an event that completed 
normally, sleep past the timeout.
+        time.sleep(timer.timeoutMs() + 1);
+        timer.update(time.milliseconds());
+        assertEquals(0, timer.remainingMs());
+
+        // Call the reaper and validate that the event is not considered 
expired, but is still no longer tracked.
+        reaper.reapExpiredAndCompleted(time.milliseconds());
+        assertTrue(event.future().isDone());
+        assertNull(ConsumerUtils.getResult(event.future()));
+        assertEquals(0, reaper.size());
+    }
+
+    @Test
+    public void testCompletedAndExpired() {
+        // Add two events to the reaper. One event will be completed, the 
other we will let expire.
+        Timer timer = time.timer(100);
+        UnsubscribeEvent event1 = new 
UnsubscribeEvent(calculateDeadlineMs(timer));
+        UnsubscribeEvent event2 = new 
UnsubscribeEvent(calculateDeadlineMs(timer));
+        reaper.add(event1);
+        reaper.add(event2);
+
+        // Without any time passing, we check the reaper and verify that the 
event is not done amd is still
+        // being tracked.
+        reaper.reapExpiredAndCompleted(time.milliseconds());
+        assertFalse(event1.future().isDone());
+        assertFalse(event2.future().isDone());
+        assertEquals(2, reaper.size());
+
+        // We'll cause the first event to be completed normally, but then 
sleep past the timer deadline.
+        event1.future().complete(null);
+        assertTrue(event1.future().isDone());
+
+        time.sleep(timer.timeoutMs() + 1);
+        timer.update(time.milliseconds());
+        assertEquals(0, timer.remainingMs());
+
+        // Though the first event is completed, it's still being tracked, 
along with the second expired event.
+        assertEquals(2, reaper.size());
+
+        // Validate that the first (completed) event is not expired, but the 
second one is expired. In either case,
+        // both should be completed and neither should be tracked anymore.
+        reaper.reapExpiredAndCompleted(time.milliseconds());
+        assertTrue(event1.future().isDone());
+        assertTrue(event2.future().isDone());
+        assertNull(ConsumerUtils.getResult(event1.future()));
+        assertThrows(TimeoutException.class, () -> 
ConsumerUtils.getResult(event2.future()));
+        assertEquals(0, reaper.size());
+    }
+
+    @Test
+    public void testIncompleteQueue() {
+        // Add two events to the queue.
+        BlockingQueue<CompletableApplicationEvent<?>> queue = new 
LinkedBlockingQueue<>();

Review Comment:
   Done.



##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/events/CompletableEventReaperTest.java:
##########
@@ -0,0 +1,211 @@
+/*
+ * 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.clients.consumer.internals.events;
+
+import org.apache.kafka.clients.consumer.internals.ConsumerUtils;
+import org.apache.kafka.common.errors.TimeoutException;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.MockTime;
+import org.apache.kafka.common.utils.Time;
+import org.apache.kafka.common.utils.Timer;
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.LinkedBlockingQueue;
+
+import static 
org.apache.kafka.clients.consumer.internals.events.CompletableEvent.calculateDeadlineMs;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class CompletableEventReaperTest {
+
+    private final LogContext logContext = new LogContext();
+    private final Time time = new MockTime();
+    private final CompletableEventReaper<CompletableApplicationEvent<?>> 
reaper = new CompletableEventReaper<>(logContext);
+
+    @Test
+    public void testExpired() {
+        // Add a new event to the reaper.
+        Timer timer = time.timer(100);
+        UnsubscribeEvent event = new 
UnsubscribeEvent(calculateDeadlineMs(timer));
+        reaper.add(event);
+
+        // Without any time passing, we check the reaper and verify that the 
event is not done amd is still
+        // being tracked.
+        reaper.reapExpiredAndCompleted(time.milliseconds());
+        assertFalse(event.future().isDone());
+        assertEquals(1, reaper.size());
+
+        // Sleep for at least 1 ms. *more* than the timeout so that the event 
is considered expired.
+        time.sleep(timer.timeoutMs() + 1);
+        timer.update(time.milliseconds());
+        assertEquals(0, timer.remainingMs());
+
+        // However, until we actually invoke the reaper, the event isn't 
complete and is still being tracked.
+        assertFalse(event.future().isDone());
+        assertEquals(1, reaper.size());
+
+        // Call the reaper and validate that the event is now "done" 
(expired), the correct exception type is
+        // thrown, and the event is no longer tracked.
+        reaper.reapExpiredAndCompleted(time.milliseconds());
+        assertTrue(event.future().isDone());
+        assertThrows(TimeoutException.class, () -> 
ConsumerUtils.getResult(event.future()));
+        assertEquals(0, reaper.size());
+    }
+
+    @Test
+    public void testCompleted() {
+        // Add a new event to the reaper.
+        Timer timer = time.timer(100);
+        UnsubscribeEvent event = new 
UnsubscribeEvent(calculateDeadlineMs(timer));
+        reaper.add(event);
+
+        // Without any time passing, we check the reaper and verify that the 
event is not done amd is still
+        // being tracked.
+        reaper.reapExpiredAndCompleted(time.milliseconds());
+        assertFalse(event.future().isDone());
+        assertEquals(1, reaper.size());
+
+        // We'll cause the event to be completed normally. Note that because 
we haven't called the reaper, the
+        // event is still being tracked.
+        event.future().complete(null);
+        assertTrue(event.future().isDone());
+        assertEquals(1, reaper.size());
+
+        // To ensure we don't accidentally expire an event that completed 
normally, sleep past the timeout.
+        time.sleep(timer.timeoutMs() + 1);
+        timer.update(time.milliseconds());
+        assertEquals(0, timer.remainingMs());
+
+        // Call the reaper and validate that the event is not considered 
expired, but is still no longer tracked.
+        reaper.reapExpiredAndCompleted(time.milliseconds());
+        assertTrue(event.future().isDone());
+        assertNull(ConsumerUtils.getResult(event.future()));
+        assertEquals(0, reaper.size());
+    }
+
+    @Test
+    public void testCompletedAndExpired() {
+        // Add two events to the reaper. One event will be completed, the 
other we will let expire.
+        Timer timer = time.timer(100);
+        UnsubscribeEvent event1 = new 
UnsubscribeEvent(calculateDeadlineMs(timer));
+        UnsubscribeEvent event2 = new 
UnsubscribeEvent(calculateDeadlineMs(timer));
+        reaper.add(event1);
+        reaper.add(event2);
+
+        // Without any time passing, we check the reaper and verify that the 
event is not done amd is still
+        // being tracked.
+        reaper.reapExpiredAndCompleted(time.milliseconds());
+        assertFalse(event1.future().isDone());
+        assertFalse(event2.future().isDone());
+        assertEquals(2, reaper.size());
+
+        // We'll cause the first event to be completed normally, but then 
sleep past the timer deadline.
+        event1.future().complete(null);
+        assertTrue(event1.future().isDone());
+
+        time.sleep(timer.timeoutMs() + 1);
+        timer.update(time.milliseconds());
+        assertEquals(0, timer.remainingMs());
+
+        // Though the first event is completed, it's still being tracked, 
along with the second expired event.
+        assertEquals(2, reaper.size());
+
+        // Validate that the first (completed) event is not expired, but the 
second one is expired. In either case,
+        // both should be completed and neither should be tracked anymore.
+        reaper.reapExpiredAndCompleted(time.milliseconds());
+        assertTrue(event1.future().isDone());
+        assertTrue(event2.future().isDone());
+        assertNull(ConsumerUtils.getResult(event1.future()));
+        assertThrows(TimeoutException.class, () -> 
ConsumerUtils.getResult(event2.future()));
+        assertEquals(0, reaper.size());
+    }
+
+    @Test
+    public void testIncompleteQueue() {
+        // Add two events to the queue.
+        BlockingQueue<CompletableApplicationEvent<?>> queue = new 
LinkedBlockingQueue<>();
+        Timer timer = time.timer(100);
+        UnsubscribeEvent event1 = new 
UnsubscribeEvent(calculateDeadlineMs(timer));
+        UnsubscribeEvent event2 = new 
UnsubscribeEvent(calculateDeadlineMs(timer));
+        queue.add(event1);
+        queue.add(event2);
+
+        // Complete one of our events, just to make sure it isn't 
inadvertently canceled.
+        event1.future().complete(null);
+
+        // In this test, our events aren't tracked in the reaper, just in the 
queue.
+        assertEquals(0, reaper.size());
+        assertEquals(2, queue.size());
+
+        // Go ahead and reap the incomplete from the queue.
+        reaper.reapIncomplete(queue);
+
+        // The first event was completed, so we didn't cancel it in the reaper.
+        assertTrue(event1.future().isDone());
+        assertFalse(event1.future().isCancelled());
+        assertNull(ConsumerUtils.getResult(event1.future()));
+
+        // The second event was incomplete, so it was canceled.
+        assertTrue(event2.future().isDone());
+        assertTrue(event2.future().isCancelled());
+        assertThrows(CancellationException.class, () -> 
ConsumerUtils.getResult(event2.future()));
+
+        // Because the events aren't tracked in the reaper *and* the queue is 
cleared as part of the
+        // cancellation process, our data structures should both be the same 
as above.
+        assertEquals(0, reaper.size());
+        assertEquals(0, queue.size());
+    }
+
+    @Test
+    public void testIncompleteTracked() {
+        // This queue is just here to test the case where the queue is empty.
+        BlockingQueue<CompletableApplicationEvent<?>> queue = new 
LinkedBlockingQueue<>();

Review Comment:
   Done.



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