chia7712 commented on code in PR #16341:
URL: https://github.com/apache/kafka/pull/16341#discussion_r1641733994


##########
storage/src/test/java/org/apache/kafka/storage/internals/log/TransactionIndexTest.java:
##########
@@ -0,0 +1,207 @@
+/*
+ * 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.log;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Collections;
+
+import org.apache.kafka.test.TestUtils;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+public class TransactionIndexTest {
+    private final File file = assertDoesNotThrow(() -> TestUtils.tempFile());
+    private final TransactionIndex index = assertDoesNotThrow(() -> new 
TransactionIndex(0, file));
+
+    @AfterEach
+    public void teardown() throws IOException {
+        index.close();
+    }
+
+    @Test
+    public void testPositionSetCorrectlyWhenOpened() throws IOException {
+        List<AbortedTxn> abortedTxns = new ArrayList<>(Arrays.asList(
+                new AbortedTxn(0L, 0, 10, 11),
+                new AbortedTxn(1L, 5, 15, 13),
+                new AbortedTxn(2L, 18, 35, 25),
+                new AbortedTxn(3L, 32, 50, 40)));
+        abortedTxns.forEach(txn -> assertDoesNotThrow(() -> 
index.append(txn)));
+        index.close();
+
+        TransactionIndex reopenedIndex = assertDoesNotThrow(() -> new 
TransactionIndex(0L, file));
+        AbortedTxn anotherAbortedTxn = new AbortedTxn(3L, 50, 60, 55);
+        reopenedIndex.append(anotherAbortedTxn);
+        abortedTxns.add(anotherAbortedTxn);
+        assertEquals(abortedTxns, reopenedIndex.allAbortedTxns());
+    }
+
+    @Test
+    public void testSanityCheck() throws IOException {
+        List<AbortedTxn> abortedTxns = Arrays.asList(
+                new AbortedTxn(0L, 0, 10, 11),
+                new AbortedTxn(1L, 5, 15, 13),
+                new AbortedTxn(2L, 18, 35, 25),
+                new AbortedTxn(3L, 32, 50, 40));
+        abortedTxns.forEach(txn -> assertDoesNotThrow(() -> 
index.append(txn)));
+        index.close();
+
+        // open the index with a different starting offset to fake invalid data
+        try (TransactionIndex reopenedIndex = assertDoesNotThrow(() -> new 
TransactionIndex(100L, file))) {
+            assertThrows(CorruptIndexException.class, 
reopenedIndex::sanityCheck);
+        }
+    }
+
+    @Test
+    public void testLastOffsetMustIncrease() throws IOException {
+        index.append(new AbortedTxn(1L, 5, 15, 13));
+        assertThrows(IllegalArgumentException.class, () -> index.append(new 
AbortedTxn(0L, 0,
+                15, 11)));
+    }
+
+    @Test
+    public void testLastOffsetCannotDecrease() throws IOException {
+        index.append(new AbortedTxn(1L, 5, 15, 13));
+        assertThrows(IllegalArgumentException.class, () -> index.append(new 
AbortedTxn(0L, 0,
+                10, 11)));
+    }
+
+    @Test
+    public void testCollectAbortedTransactions() {
+        List<AbortedTxn> abortedTransactions = Arrays.asList(
+                new AbortedTxn(0L, 0, 10, 11),
+                new AbortedTxn(1L, 5, 15, 13),
+                new AbortedTxn(2L, 18, 35, 25),
+                new AbortedTxn(3L, 32, 50, 40));
+
+        abortedTransactions.forEach(txn -> assertDoesNotThrow(() -> 
index.append(txn)));
+
+        TxnIndexSearchResult result = index.collectAbortedTxns(0L, 100L);
+        assertEquals(abortedTransactions, result.abortedTransactions);
+        assertFalse(result.isComplete);
+
+        result = index.collectAbortedTxns(0L, 32);
+        assertEquals(abortedTransactions.subList(0, 3), 
result.abortedTransactions);
+        assertTrue(result.isComplete);
+
+        result = index.collectAbortedTxns(0L, 35);
+        assertEquals(abortedTransactions, result.abortedTransactions);
+        assertTrue(result.isComplete);
+
+        result = index.collectAbortedTxns(10, 35);
+        assertEquals(abortedTransactions, result.abortedTransactions);
+        assertTrue(result.isComplete);
+
+        result = index.collectAbortedTxns(11, 35);
+        assertEquals(abortedTransactions.subList(1, 4), 
result.abortedTransactions);
+        assertTrue(result.isComplete);
+
+        result = index.collectAbortedTxns(20, 41);
+        assertEquals(abortedTransactions.subList(2, 4), 
result.abortedTransactions);
+        assertFalse(result.isComplete);
+    }
+
+    @Test
+    public void testTruncate() {
+        List<AbortedTxn> abortedTransactions = Arrays.asList(
+                new AbortedTxn(0L, 0, 10, 2),
+                new AbortedTxn(1L, 5, 15, 16),
+                new AbortedTxn(2L, 18, 35, 25),
+                new AbortedTxn(3L, 32, 50, 40));
+
+        abortedTransactions.forEach(txn -> assertDoesNotThrow(() -> 
index.append(txn)));
+
+        assertDoesNotThrow(() -> index.truncateTo(51));
+        assertEquals(abortedTransactions, index.collectAbortedTxns(0L, 
100L).abortedTransactions);
+
+        assertDoesNotThrow(() -> index.truncateTo(50));
+        assertEquals(abortedTransactions.subList(0, 3), 
index.collectAbortedTxns(0L, 100L).abortedTransactions);
+
+        assertDoesNotThrow(() -> index.reset());
+        assertEquals(Collections.emptyList(), index.collectAbortedTxns(0L, 
100L).abortedTransactions);
+    }
+
+    @Test
+    public void testAbortedTxnSerde() {
+        long pid = 983493L;
+        long firstOffset = 137L;
+        long lastOffset = 299L;
+        long lastStableOffset = 200L;
+
+        AbortedTxn abortedTxn = new AbortedTxn(pid, firstOffset, lastOffset, 
lastStableOffset);
+        assertEquals(AbortedTxn.CURRENT_VERSION, abortedTxn.version());
+        assertEquals(pid, abortedTxn.producerId());
+        assertEquals(firstOffset, abortedTxn.firstOffset());
+        assertEquals(lastOffset, abortedTxn.lastOffset());
+        assertEquals(lastStableOffset, abortedTxn.lastStableOffset());
+    }
+
+    @Test
+    public void testRenameIndex() {
+        File renamed = assertDoesNotThrow(() -> TestUtils.tempFile());
+        assertDoesNotThrow(() -> index.append(new AbortedTxn(0L, 0, 10, 2)));
+
+        assertDoesNotThrow(() -> index.renameTo(renamed));
+        assertDoesNotThrow(() -> index.append(new AbortedTxn(1L, 5, 15, 16)));
+
+        List<AbortedTxn> abortedTxns = index.collectAbortedTxns(0L, 
100L).abortedTransactions;
+        assertEquals(2, abortedTxns.size());
+        assertEquals(0, abortedTxns.get(0).firstOffset());
+        assertEquals(5, abortedTxns.get(1).firstOffset());
+    }
+
+    @Test
+    public void testUpdateParentDir() {
+        File tmpParentDir = new File(TestUtils.tempDirectory(), "parent");
+        tmpParentDir.mkdir();
+        assertNotEquals(tmpParentDir, index.file().getParentFile());
+        index.updateParentDir(tmpParentDir);
+        assertEquals(tmpParentDir, index.file().getParentFile());
+    }
+
+    @Test
+    public void testFlush() {
+        File nonExistentFile = assertDoesNotThrow(() -> TestUtils.tempFile());
+        assertTrue(nonExistentFile.delete());
+        TransactionIndex testIndex = assertDoesNotThrow(() -> new 
TransactionIndex(0, nonExistentFile));
+
+        assertDoesNotThrow(testIndex::flush);

Review Comment:
   ditto, please remove those `assertDoesNotThrow`



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