m1a2st commented on code in PR #23380:
URL: https://github.com/apache/kafka/pull/23380#discussion_r3975010934


##########
storage/src/test/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCacheTest.java:
##########
@@ -0,0 +1,559 @@
+/*
+ * 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.storage.internals.epoch;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.server.util.MockTime;
+import org.apache.kafka.storage.internals.checkpoint.LeaderEpochCheckpointFile;
+import org.apache.kafka.storage.internals.log.EpochEntry;
+import org.apache.kafka.storage.internals.log.LogDirFailureChannel;
+import org.apache.kafka.test.TestUtils;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.OptionalInt;
+
+import static 
org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH;
+import static 
org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH_OFFSET;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Unit test for the LeaderEpochFileCache.
+ */
+public class LeaderEpochFileCacheTest {
+    private final TopicPartition tp = new TopicPartition("TestTopic", 5);
+
+    private LeaderEpochCheckpointFile checkpoint;
+    private LeaderEpochFileCache cache;
+
+    @BeforeEach
+    public void setup() throws IOException {
+        MockTime mockTime = new MockTime();
+        checkpoint = new LeaderEpochCheckpointFile(TestUtils.tempFile(), new 
LogDirFailureChannel(1));
+        cache = new LeaderEpochFileCache(tp, checkpoint, mockTime.scheduler);
+    }
+
+    @Test
+    public void testPreviousEpoch() {
+        assertEquals(OptionalInt.empty(), cache.previousEpoch());
+
+        cache.assign(2, 10);
+        assertEquals(OptionalInt.empty(), cache.previousEpoch());
+
+        cache.assign(4, 15);
+        assertEquals(OptionalInt.of(2), cache.previousEpoch());
+
+        cache.assign(10, 20);
+        assertEquals(OptionalInt.of(4), cache.previousEpoch());
+
+        cache.truncateFromEndAsyncFlush(18);
+        assertEquals(OptionalInt.of(2), cache.previousEpoch());
+    }
+
+    @Test
+    public void shouldAddEpochAndMessageOffsetToCache() {
+        cache.assign(2, 10);
+        long logEndOffset = 11;
+
+        assertEquals(Optional.of(2), cache.latestEpoch());
+        assertEquals(new EpochEntry(2, 10), cache.epochEntries().get(0));
+        assertEquals(Map.entry(2, logEndOffset), cache.endOffsetFor(2, 
logEndOffset));
+    }
+
+    @Test
+    public void shouldReturnLogEndOffsetIfLatestEpochRequested() {
+        cache.assign(2, 11);
+        cache.assign(2, 12);
+        long logEndOffset = 14;
+
+        assertEquals(Map.entry(2, logEndOffset), cache.endOffsetFor(2, 
logEndOffset));
+    }
+
+    @Test
+    public void shouldReturnUndefinedOffsetIfUndefinedEpochRequested() {
+        Map.Entry<Integer, Long> expectedEpochEndOffset = 
Map.entry(UNDEFINED_EPOCH, UNDEFINED_EPOCH_OFFSET);
+
+        cache.assign(2, 11);
+        cache.assign(3, 12);
+
+        Map.Entry<Integer, Long> epochAndOffsetFor = 
cache.endOffsetFor(UNDEFINED_EPOCH, 0L);
+
+        assertEquals(
+                expectedEpochEndOffset,
+                epochAndOffsetFor,
+                "Expected undefined epoch and offset if undefined epoch 
requested. Cache not empty.");
+    }
+
+    @Test
+    public void shouldEnforceMonotonicallyIncreasingStartOffsets() {
+        cache.assign(2, 9);
+
+        cache.assign(3, 9);
+
+        assertEquals(List.of(new EpochEntry(3, 9)), cache.epochEntries());
+    }
+
+    @Test
+    public void shouldNotOverwriteOffsetForALeaderEpochOnceItHasBeenAssigned() 
{
+        cache.assign(2, 6);
+
+        cache.assign(2, 10);
+
+        assertEquals(List.of(new EpochEntry(2, 6)), cache.epochEntries());
+    }

Review Comment:
   It seems that `shouldNotOverwriteOffsetForALeaderEpochOnceItHasBeenAssigned` 
and `shouldNotUpdateEpochAndStartOffsetIfItDidNotChange` are testing the same 
behavior as well. Would it make sense to combine these two tests too.
   



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

Reply via email to