vcrfxia commented on code in PR #13126: URL: https://github.com/apache/kafka/pull/13126#discussion_r1093912650
########## streams/src/test/java/org/apache/kafka/streams/state/internals/RocksDBVersionedStoreSegmentValueFormatterTest.java: ########## @@ -0,0 +1,316 @@ +/* + * 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.streams.state.internals; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThrows; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.kafka.streams.state.internals.RocksDBVersionedStoreSegmentValueFormatter.SegmentValue; +import org.apache.kafka.streams.state.internals.RocksDBVersionedStoreSegmentValueFormatter.SegmentValue.SegmentSearchResult; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +@RunWith(Parameterized.class) +public class RocksDBVersionedStoreSegmentValueFormatterTest { + + private static final List<TestCase> TEST_CASES = new ArrayList<>(); + static { + // test cases are expected to have timestamps in strictly decreasing order (except for the degenerate case) + TEST_CASES.add(new TestCase("degenerate", 10, new TestRecord(null, 10))); + TEST_CASES.add(new TestCase("single record", 10, new TestRecord("foo".getBytes(), 1))); + TEST_CASES.add(new TestCase("multiple records", 10, new TestRecord("foo".getBytes(), 8), new TestRecord("bar".getBytes(), 3), new TestRecord("baz".getBytes(), 0))); + TEST_CASES.add(new TestCase("single tombstone", 10, new TestRecord(null, 1))); + TEST_CASES.add(new TestCase("multiple tombstone", 10, new TestRecord(null, 4), new TestRecord(null, 1))); + TEST_CASES.add(new TestCase("tombstones and records (r, t, r)", 10, new TestRecord("foo".getBytes(), 5), new TestRecord(null, 2), new TestRecord("bar".getBytes(), 1))); + TEST_CASES.add(new TestCase("tombstones and records (t, r, t)", 10, new TestRecord(null, 5), new TestRecord("foo".getBytes(), 2), new TestRecord(null, 1))); + TEST_CASES.add(new TestCase("tombstones and records (r, r, t, t)", 10, new TestRecord("foo".getBytes(), 6), new TestRecord("bar".getBytes(), 5), new TestRecord(null, 2), new TestRecord(null, 1))); + TEST_CASES.add(new TestCase("tombstones and records (t, t, r, r)", 10, new TestRecord(null, 7), new TestRecord(null, 6), new TestRecord("foo".getBytes(), 2), new TestRecord("bar".getBytes(), 1))); + TEST_CASES.add(new TestCase("record with empty bytes", 10, new TestRecord(new byte[0], 1))); + TEST_CASES.add(new TestCase("records with empty bytes (r, e)", 10, new TestRecord("foo".getBytes(), 4), new TestRecord(new byte[0], 1))); + TEST_CASES.add(new TestCase("records with empty bytes (e, e, r)", 10, new TestRecord(new byte[0], 8), new TestRecord(new byte[0], 2), new TestRecord("foo".getBytes(), 1))); + } + + private final TestCase testCase; + + public RocksDBVersionedStoreSegmentValueFormatterTest(final TestCase testCase) { + this.testCase = testCase; + } + + @Parameterized.Parameters(name = "{0}") + public static Collection<TestCase> data() { + return TEST_CASES; + } + + @Test + public void shouldSerializeAndDeserialize() { + final SegmentValue segmentValue = buildSegmentWithInsertLatest(testCase); + + final byte[] serialized = segmentValue.serialize(); + final SegmentValue deserialized = RocksDBVersionedStoreSegmentValueFormatter.deserialize(serialized); + + verifySegmentContents(deserialized, testCase); + } + + @Test + public void shouldBuildWithInsertLatest() { + final SegmentValue segmentValue = buildSegmentWithInsertLatest(testCase); + + verifySegmentContents(segmentValue, testCase); + } + + @Test + public void shouldBuildWithInsertEarliest() { + final SegmentValue segmentValue = buildSegmentWithInsertEarliest(testCase); + + verifySegmentContents(segmentValue, testCase); + } + + @Test + public void shouldInsertAtIndex() { + if (testCase.isDegenerate) { + // cannot insert into degenerate segment + return; + } + + // test inserting at each possible index + for (int insertIdx = 0; insertIdx <= testCase.records.size(); insertIdx++) { + // build record to insert + final long newRecordTimestamp; + if (insertIdx == 0) { + newRecordTimestamp = testCase.records.get(0).timestamp + 1; + if (newRecordTimestamp == testCase.nextTimestamp) { + // cannot insert because no timestamp exists between last record and nextTimestamp + continue; + } + } else { + newRecordTimestamp = testCase.records.get(insertIdx - 1).timestamp - 1; + if (newRecordTimestamp < 0 || (insertIdx < testCase.records.size() && newRecordTimestamp == testCase.records.get(insertIdx).timestamp)) { + // cannot insert because timestamps of existing records are adjacent + continue; + } + } + final TestRecord newRecord = new TestRecord("new".getBytes(), newRecordTimestamp); + + final SegmentValue segmentValue = buildSegmentWithInsertLatest(testCase); + + // insert() first requires a call to find() + if (insertIdx > 0) { + segmentValue.find(testCase.records.get(insertIdx - 1).timestamp, false); + } + segmentValue.insert(newRecord.timestamp, newRecord.value, insertIdx); + + // create expected results + final List<TestRecord> expectedRecords = new ArrayList<>(testCase.records); + expectedRecords.add(insertIdx, newRecord); + + verifySegmentContents(segmentValue, new TestCase("expected", testCase.nextTimestamp, expectedRecords)); + } + } + + @Test + public void shouldUpdateAtIndex() { + if (testCase.isDegenerate) { + // cannot update degenerate segment + return; + } + + // test updating at each possible index + for (int updateIdx = 0; updateIdx < testCase.records.size(); updateIdx++) { + // build updated record + long updatedRecordTimestamp = testCase.records.get(updateIdx).timestamp - 1; Review Comment: There are enough test cases with adjacent timestamps that both cases are exercised. I don't think we need to explicitly test both in all positions, since the underlying implementation doesn't care what the new timestamps are. This logic here is just to ensure that the timestamps we end up with are valid, i.e., in strictly increasing order. -- 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