dengziming commented on code in PR #12711: URL: https://github.com/apache/kafka/pull/12711#discussion_r990577785
########## metadata/src/test/java/org/apache/kafka/metadata/util/SnapshotFileReaderTest.java: ########## @@ -0,0 +1,88 @@ +/* + * 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.metadata.util; + +import org.apache.kafka.common.header.Header; +import org.apache.kafka.common.message.SnapshotFooterRecord; +import org.apache.kafka.common.message.SnapshotHeaderRecord; +import org.apache.kafka.common.protocol.ApiMessage; +import org.apache.kafka.common.protocol.ByteBufferAccessor; +import org.apache.kafka.common.protocol.ObjectSerializationCache; +import org.apache.kafka.common.record.ControlRecordType; +import org.apache.kafka.common.record.DefaultRecord; +import org.apache.kafka.common.record.FileLogInputStream; +import org.apache.kafka.common.record.Record; +import org.apache.kafka.common.utils.ByteBufferOutputStream; +import org.apache.log4j.Logger; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.io.DataOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.Iterator; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.ArgumentMatchers.any; + +final public class SnapshotFileReaderTest { + @Test + public void testHeaderFooterControlRecord() throws Exception { + SnapshotFileReader mockReader = Mockito.mock(SnapshotFileReader.class); + Mockito.doCallRealMethod().when(mockReader).handleControlBatch(any(FileLogInputStream.FileChannelRecordBatch.class)); + + SnapshotHeaderRecord snapshotHeaderMessage = new SnapshotHeaderRecord().setVersion((short) 0).setLastContainedLogTimestamp(1); + SnapshotFooterRecord snapshotFooterMessage = new SnapshotFooterRecord().setVersion((short) 0); + Record snapshotHeaderRecord = generateControlRecord(snapshotHeaderMessage, ControlRecordType.SNAPSHOT_HEADER.getType()); + Record snapshotFooterRecord = generateControlRecord(snapshotFooterMessage, ControlRecordType.SNAPSHOT_FOOTER.getType()); + + FileLogInputStream.FileChannelRecordBatch mockBatch = Mockito.mock(FileLogInputStream.FileChannelRecordBatch.class); + Iterator<Record> recordIterator = Arrays.asList(snapshotHeaderRecord, snapshotFooterRecord).iterator(); + Mockito.when(mockBatch.iterator()).thenReturn(recordIterator); + + LogCaptureAppender appender = new LogCaptureAppender(); + Logger.getRootLogger().addAppender(appender); + try { + mockReader.handleControlBatch(mockBatch); + // should not log any messages + assertThat(appender.getMessages(), not(hasItem(containsString("Ignoring control record")))); Review Comment: I'm not sure whether it's worth adding a complicated test for a minor log change, instead, I think we can add a test for `LeaderChangeMessage`. ########## metadata/src/test/java/org/apache/kafka/metadata/util/SnapshotFileReaderTest.java: ########## @@ -0,0 +1,88 @@ +/* + * 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.metadata.util; + +import org.apache.kafka.common.header.Header; +import org.apache.kafka.common.message.SnapshotFooterRecord; +import org.apache.kafka.common.message.SnapshotHeaderRecord; +import org.apache.kafka.common.protocol.ApiMessage; +import org.apache.kafka.common.protocol.ByteBufferAccessor; +import org.apache.kafka.common.protocol.ObjectSerializationCache; +import org.apache.kafka.common.record.ControlRecordType; +import org.apache.kafka.common.record.DefaultRecord; +import org.apache.kafka.common.record.FileLogInputStream; +import org.apache.kafka.common.record.Record; +import org.apache.kafka.common.utils.ByteBufferOutputStream; +import org.apache.log4j.Logger; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.io.DataOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.Iterator; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.ArgumentMatchers.any; + +final public class SnapshotFileReaderTest { + @Test + public void testHeaderFooterControlRecord() throws Exception { + SnapshotFileReader mockReader = Mockito.mock(SnapshotFileReader.class); Review Comment: We can directly use `new SnapshotFileReader("", new MockMetaLogManagerListener(1))` here to avoid extra mock. -- 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]
