chia7712 commented on code in PR #15714: URL: https://github.com/apache/kafka/pull/15714#discussion_r1565426868
########## storage/src/test/java/org/apache/kafka/storage/internals/checkpoint/PartitionMetadataFileTest.java: ########## @@ -0,0 +1,98 @@ +/* + * 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.checkpoint; + +import org.apache.kafka.common.Uuid; +import org.apache.kafka.common.errors.InconsistentTopicIdException; + +import org.apache.kafka.storage.internals.log.LogDirFailureChannel; +import org.apache.kafka.test.TestUtils; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.io.File; +import java.nio.file.Files; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class PartitionMetadataFileTest { + private final File dir = TestUtils.tempDirectory(); Review Comment: It seems `File file = PartitionMetadataFile.newFile(dir)` is more suitable as it is used by all test cases. ########## storage/src/test/java/org/apache/kafka/storage/internals/checkpoint/PartitionMetadataFileTest.java: ########## @@ -0,0 +1,98 @@ +/* + * 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.checkpoint; + +import org.apache.kafka.common.Uuid; +import org.apache.kafka.common.errors.InconsistentTopicIdException; + +import org.apache.kafka.storage.internals.log.LogDirFailureChannel; +import org.apache.kafka.test.TestUtils; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.io.File; +import java.nio.file.Files; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class PartitionMetadataFileTest { + private final File dir = TestUtils.tempDirectory(); + + @Test + public void testSetRecordWithDifferentTopicId() { + File file = PartitionMetadataFile.newFile(dir); + PartitionMetadataFile partitionMetadataFile = new PartitionMetadataFile(file, null); + Uuid topicId = Uuid.randomUuid(); + assertDoesNotThrow(() -> partitionMetadataFile.record(topicId)); + Uuid differentTopicId = Uuid.randomUuid(); + assertThrows(InconsistentTopicIdException.class, () -> partitionMetadataFile.record(differentTopicId)); + } + + @Test + public void testSetRecordWithSameTopicId() { + File file = PartitionMetadataFile.newFile(dir); + PartitionMetadataFile partitionMetadataFile = new PartitionMetadataFile(file, null); + Uuid topicId = Uuid.randomUuid(); + assertDoesNotThrow(() -> partitionMetadataFile.record(topicId)); + assertDoesNotThrow(() -> partitionMetadataFile.record(topicId)); + } + + @Test + public void testMaybeFlushWithTopicIdPresent() { + File file = PartitionMetadataFile.newFile(dir); + PartitionMetadataFile partitionMetadataFile = new PartitionMetadataFile(file, null); + + Uuid topicId = Uuid.randomUuid(); + assertDoesNotThrow(() -> partitionMetadataFile.record(topicId)); + assertDoesNotThrow(partitionMetadataFile::maybeFlush); + + assertDoesNotThrow(() -> { + List<String> lines = Files.readAllLines(file.toPath()); + assertEquals(2, lines.size()); + assertEquals("version: 0", lines.get(0)); + assertEquals("topic_id: " + topicId, lines.get(1)); + }); + } + + @Test + public void testMaybeFlushWithNoTopicIdPresent() { + File file = PartitionMetadataFile.newFile(dir); + PartitionMetadataFile partitionMetadataFile = new PartitionMetadataFile(file, null); + + assertDoesNotThrow(partitionMetadataFile::maybeFlush); + assertEquals(0, file.length()); + } + + @Test + public void testRead() { + File file = PartitionMetadataFile.newFile(dir); + LogDirFailureChannel channel = Mockito.mock(LogDirFailureChannel.class); + PartitionMetadataFile partitionMetadataFile = new PartitionMetadataFile(file, channel); + + Uuid topicId = Uuid.randomUuid(); + assertDoesNotThrow(() -> partitionMetadataFile.record(topicId)); Review Comment: we don't need to use `assertDoesNotThrow` since it is fine to add any exception to method signature. ########## storage/src/test/java/org/apache/kafka/storage/internals/checkpoint/PartitionMetadataFileTest.java: ########## @@ -0,0 +1,98 @@ +/* + * 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.checkpoint; + +import org.apache.kafka.common.Uuid; +import org.apache.kafka.common.errors.InconsistentTopicIdException; + +import org.apache.kafka.storage.internals.log.LogDirFailureChannel; +import org.apache.kafka.test.TestUtils; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.io.File; +import java.nio.file.Files; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class PartitionMetadataFileTest { + private final File dir = TestUtils.tempDirectory(); + + @Test + public void testSetRecordWithDifferentTopicId() { + File file = PartitionMetadataFile.newFile(dir); + PartitionMetadataFile partitionMetadataFile = new PartitionMetadataFile(file, null); + Uuid topicId = Uuid.randomUuid(); + assertDoesNotThrow(() -> partitionMetadataFile.record(topicId)); + Uuid differentTopicId = Uuid.randomUuid(); + assertThrows(InconsistentTopicIdException.class, () -> partitionMetadataFile.record(differentTopicId)); + } + + @Test + public void testSetRecordWithSameTopicId() { + File file = PartitionMetadataFile.newFile(dir); + PartitionMetadataFile partitionMetadataFile = new PartitionMetadataFile(file, null); + Uuid topicId = Uuid.randomUuid(); + assertDoesNotThrow(() -> partitionMetadataFile.record(topicId)); + assertDoesNotThrow(() -> partitionMetadataFile.record(topicId)); + } + + @Test + public void testMaybeFlushWithTopicIdPresent() { + File file = PartitionMetadataFile.newFile(dir); + PartitionMetadataFile partitionMetadataFile = new PartitionMetadataFile(file, null); + + Uuid topicId = Uuid.randomUuid(); + assertDoesNotThrow(() -> partitionMetadataFile.record(topicId)); + assertDoesNotThrow(partitionMetadataFile::maybeFlush); + + assertDoesNotThrow(() -> { + List<String> lines = Files.readAllLines(file.toPath()); + assertEquals(2, lines.size()); + assertEquals("version: 0", lines.get(0)); Review Comment: it would be nice to add comments about those contents are from `PartitionMetadata#encode`. otherwise, it would be hard to debug if this test is broken by something changes in the future. ########## storage/src/test/java/org/apache/kafka/storage/internals/checkpoint/PartitionMetadataFileTest.java: ########## @@ -0,0 +1,98 @@ +/* + * 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.checkpoint; + +import org.apache.kafka.common.Uuid; +import org.apache.kafka.common.errors.InconsistentTopicIdException; + +import org.apache.kafka.storage.internals.log.LogDirFailureChannel; +import org.apache.kafka.test.TestUtils; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.io.File; +import java.nio.file.Files; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class PartitionMetadataFileTest { + private final File dir = TestUtils.tempDirectory(); + + @Test + public void testSetRecordWithDifferentTopicId() { + File file = PartitionMetadataFile.newFile(dir); + PartitionMetadataFile partitionMetadataFile = new PartitionMetadataFile(file, null); + Uuid topicId = Uuid.randomUuid(); + assertDoesNotThrow(() -> partitionMetadataFile.record(topicId)); + Uuid differentTopicId = Uuid.randomUuid(); + assertThrows(InconsistentTopicIdException.class, () -> partitionMetadataFile.record(differentTopicId)); + } + + @Test + public void testSetRecordWithSameTopicId() { + File file = PartitionMetadataFile.newFile(dir); + PartitionMetadataFile partitionMetadataFile = new PartitionMetadataFile(file, null); + Uuid topicId = Uuid.randomUuid(); + assertDoesNotThrow(() -> partitionMetadataFile.record(topicId)); + assertDoesNotThrow(() -> partitionMetadataFile.record(topicId)); + } + + @Test + public void testMaybeFlushWithTopicIdPresent() { + File file = PartitionMetadataFile.newFile(dir); + PartitionMetadataFile partitionMetadataFile = new PartitionMetadataFile(file, null); + + Uuid topicId = Uuid.randomUuid(); + assertDoesNotThrow(() -> partitionMetadataFile.record(topicId)); + assertDoesNotThrow(partitionMetadataFile::maybeFlush); + + assertDoesNotThrow(() -> { + List<String> lines = Files.readAllLines(file.toPath()); + assertEquals(2, lines.size()); Review Comment: it is a bit weird that we call `assert` in the `assertDoesNotThrow`. It should be fine to expose the exception to method signature. -- 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