aliehsaeedii commented on code in PR #22677: URL: https://github.com/apache/kafka/pull/22677#discussion_r3481124304
########## streams/src/test/java/org/apache/kafka/streams/processor/api/ReadOnlyRecordTest.java: ########## @@ -0,0 +1,68 @@ +/* + * 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.processor.api; + +import org.apache.kafka.common.header.Headers; +import org.apache.kafka.common.header.internals.RecordHeader; +import org.apache.kafka.common.header.internals.RecordHeaders; + +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ReadOnlyRecordTest { + + private static Headers headers() { + return new RecordHeaders().add(new RecordHeader("k", "v".getBytes(StandardCharsets.UTF_8))); + } + + @Test + public void recordShouldBeAssignableToReadOnlyRecordAndExposeSameAccessors() { + final Headers headers = headers(); + final Record<String, String> record = new Record<>("key", "value", 123L, headers); + + // The assignment itself proves Record is a ReadOnlyRecord (source/binary compatible change). + final ReadOnlyRecord<String, String> readOnly = record; + + assertEquals(record.key(), readOnly.key()); + assertEquals(record.value(), readOnly.value()); + assertEquals(record.timestamp(), readOnly.timestamp()); + assertEquals(record.headers(), readOnly.headers()); Review Comment: These four assertions are tautological: `readOnly` is the *same object* as `record` (the assignment on line 41 is a reference, not a copy), so `record.key()` and `readOnly.key()` invoke the same method on the same instance and can never differ. They provide no signal about the read-only view. The meaningful checks are the literal-value assertions just below (lines 48-51), plus the fact that the assignment on line 41 compiles — that compile is what actually proves `Record` is a `ReadOnlyRecord` ########## streams/src/test/java/org/apache/kafka/streams/processor/api/ReadOnlyRecordTest.java: ########## @@ -0,0 +1,68 @@ +/* + * 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.processor.api; + +import org.apache.kafka.common.header.Headers; +import org.apache.kafka.common.header.internals.RecordHeader; +import org.apache.kafka.common.header.internals.RecordHeaders; + +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ReadOnlyRecordTest { + + private static Headers headers() { + return new RecordHeaders().add(new RecordHeader("k", "v".getBytes(StandardCharsets.UTF_8))); + } + + @Test + public void recordShouldBeAssignableToReadOnlyRecordAndExposeSameAccessors() { + final Headers headers = headers(); + final Record<String, String> record = new Record<>("key", "value", 123L, headers); + + // The assignment itself proves Record is a ReadOnlyRecord (source/binary compatible change). + final ReadOnlyRecord<String, String> readOnly = record; + + assertEquals(record.key(), readOnly.key()); + assertEquals(record.value(), readOnly.value()); + assertEquals(record.timestamp(), readOnly.timestamp()); + assertEquals(record.headers(), readOnly.headers()); + + assertEquals("key", readOnly.key()); + assertEquals("value", readOnly.value()); + assertEquals(123L, readOnly.timestamp()); + assertEquals(headers, readOnly.headers()); + } + + @Test + public void readOnlyHeadersShouldBeNonNullAndEmptyWhenConstructedWithoutHeaders() { + final ReadOnlyRecord<String, String> readOnly = new Record<>("key", "value", 123L); + assertEquals(new RecordHeaders(), readOnly.headers()); + } + + @Test + public void readOnlyAccessorsShouldTolerateNullKeyAndValue() { + final ReadOnlyRecord<String, String> readOnly = new Record<>(null, null, 0L); + assertEquals(null, readOnly.key()); + assertEquals(null, readOnly.value()); Review Comment: Minor: prefer `assertNull(readOnly.key())` / `assertNull(readOnly.value())` over `assertEquals(null, ...)` — it's the JUnit 5 idiom and reads more clearly. ########## streams/src/main/java/org/apache/kafka/streams/processor/api/Record.java: ########## @@ -35,7 +35,7 @@ * @param <K> The type of the key * @param <V> The type of the value */ -public class Record<K, V> { +public class Record<K, V> implements ReadOnlyRecord<K, V> { Review Comment: Now that `Record` implements `ReadOnlyRecord`, consider adding `@Override` to the four accessors (`key()`, `value()`, `timestamp()`, `headers()`). It documents that they satisfy the interface contract and lets the compiler catch any future signature drift between the two. Optional — this PR doesn't otherwise touch those methods. -- 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]
