Poorvankbhatia commented on code in PR #26274: URL: https://github.com/apache/flink/pull/26274#discussion_r2027495183
########## flink-connectors/flink-connector-base/src/test/java/org/apache/flink/connector/base/sink/writer/BatchTest.java: ########## @@ -0,0 +1,48 @@ +/* + * 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.flink.connector.base.sink.writer; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Collections; + +import static org.assertj.core.api.Assertions.assertThat; + +/** Unit Test for BatchCreationResult, majorly testing constructor, setter and getters. */ Review Comment: Corrected this. ########## flink-connectors/flink-connector-base/src/test/java/org/apache/flink/connector/base/sink/writer/DequeueRequestBufferTest.java: ########## @@ -0,0 +1,111 @@ +/* + * 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.flink.connector.base.sink.writer; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +/** Test for {@link DequeRequestBuffer}. */ +public class DequeueRequestBufferTest { + private DequeRequestBuffer<String> bufferWrapper; + + @BeforeEach + void setUp() { + bufferWrapper = new DequeRequestBuffer.Builder<String>().build(); + } + + /** Test entries should be added in FIFO Fashion. */ + @Test + void shouldAddEntriesInFifoOrder() { + RequestEntryWrapper<String> entry1 = new RequestEntryWrapper<>("Entry1", 10); + RequestEntryWrapper<String> entry2 = new RequestEntryWrapper<>("Entry2", 20); + + bufferWrapper.add(entry1, false); + bufferWrapper.add(entry2, false); + + assertThat(bufferWrapper.size()).isEqualTo(2); + assertThat(bufferWrapper.peek()).isEqualTo(entry1); + assertThat(bufferWrapper.poll()).isEqualTo(entry1); + assertThat(bufferWrapper.poll()).isEqualTo(entry2); + assertThat(bufferWrapper.isEmpty()).isTrue(); + } + + /** Test that priority entries are added to the HEAD. */ + @Test + void shouldPrioritizeEntriesAddedAtHead() { + RequestEntryWrapper<String> entry1 = new RequestEntryWrapper<>("Entry1", 10); + RequestEntryWrapper<String> entry2 = new RequestEntryWrapper<>("Entry2", 20); + RequestEntryWrapper<String> priorityEntry = new RequestEntryWrapper<>("PriorityEntry", 30); + + bufferWrapper.add(entry1, false); + bufferWrapper.add(entry2, false); + bufferWrapper.add(priorityEntry, true); // Should be added at the front + + assertThat(bufferWrapper.size()).isEqualTo(3); + assertThat(bufferWrapper.peek()).isEqualTo(priorityEntry); + assertThat(bufferWrapper.poll()).isEqualTo(priorityEntry); + assertThat(bufferWrapper.poll()).isEqualTo(entry1); + assertThat(bufferWrapper.poll()).isEqualTo(entry2); + } + + /** Test stack trace correctly. */ + @Test + void shouldTrackTotalSizeCorrectly() { + RequestEntryWrapper<String> entry1 = new RequestEntryWrapper<>("Entry1", 10); + RequestEntryWrapper<String> entry2 = new RequestEntryWrapper<>("Entry2", 20); + + bufferWrapper.add(entry1, false); + bufferWrapper.add(entry2, false); + + assertThat(bufferWrapper.totalSizeInBytes()).isEqualTo(30); + + bufferWrapper.poll(); // Removes entry1 (10 bytes) + assertThat(bufferWrapper.totalSizeInBytes()).isEqualTo(20); + + bufferWrapper.poll(); // Removes entry2 (20 bytes) + assertThat(bufferWrapper.totalSizeInBytes()).isEqualTo(0); + } + + /** Test get buffered state. */ + @Test + void shouldReturnBufferedStateSnapshot() { + RequestEntryWrapper<String> entry1 = new RequestEntryWrapper<>("Entry1", 10); + RequestEntryWrapper<String> entry2 = new RequestEntryWrapper<>("Entry2", 20); + + bufferWrapper.add(entry1, false); + bufferWrapper.add(entry2, false); + + List<RequestEntryWrapper<String>> snapshot = + (List<RequestEntryWrapper<String>>) bufferWrapper.getBufferedState(); + assertThat(snapshot).containsExactly(entry1, entry2); Review Comment: Uh.. `containsExactly` verifies the order too. Doc [here](https://www.javadoc.io/doc/org.assertj/assertj-core/3.27.3/org/assertj/core/api/AbstractIterableAssert.html#containsExactly(ELEMENT...)) Let me know if i misunderstood your comment. ########## flink-connectors/flink-connector-base/src/test/java/org/apache/flink/connector/base/sink/writer/SimpleBatchCreatorTest.java: ########## @@ -0,0 +1,194 @@ +/* + * 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.flink.connector.base.sink.writer; + +import org.apache.flink.connector.base.sink.writer.strategy.RequestInfo; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +/** Unit test for SimpleBatchCreator. */ +public class SimpleBatchCreatorTest { + + /** If no MaxBatchSizeInBytes is configured error should be thrown. */ + @Test + public void testInvalidBatchCreator() { + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> new SimpleBatchCreator.Builder<String>().build()); + } + + /** Ensures no entries are returned when the buffer is empty. */ + @Test + public void testCreatNextBatchWithEmptyBuffer() { + SimpleBatchCreator<String> creator = + new SimpleBatchCreator.Builder<String>().setMaxBatchSizeInBytes(100L).build(); + RequestBuffer<String> buffer = new DequeRequestBuffer.Builder<String>().build(); + // No entries in the buffer + RequestInfo requestInfo = () -> 10; + Batch<String> result = creator.createNextBatch(requestInfo, buffer); + assertThat(result.getBatchEntries()).isEmpty(); + assertThat(result.getRecordCount()).isEqualTo(0); + assertThat(result.getSizeInBytes()).isEqualTo(0L); + } + + /** + * Verifies that the maximum batch size (count of entries) is observed even when the size in + * bytes would allow more entries. + */ + @Test + public void testCreateNextBatchRespectsBatchCountLimit() { + SimpleBatchCreator<String> creator = + new SimpleBatchCreator.Builder<String>().setMaxBatchSizeInBytes(100L).build(); + RequestBuffer<String> buffer = new DequeRequestBuffer.Builder<String>().build(); + + // Add multiple items to the buffer + for (int i = 0; i < 10; i++) { + buffer.add(new RequestEntryWrapper<>("elem-" + i, 10L), false); + } + RequestInfo requestInfo = + () -> { + return 5; // limit to 5 items + }; + Batch<String> result = creator.createNextBatch(requestInfo, buffer); + + // Should only take 5 items, ignoring the size limit because each item is 10 bytes + assertThat(result.getBatchEntries().size()).isEqualTo(5); + assertThat(result.getRecordCount()).isEqualTo(5); + assertThat(result.getSizeInBytes()).isEqualTo(50L); + + // Check the buffer was drained of exactly 5 elements + assertThat(buffer.size()).isEqualTo(5); + } + + /** + * Ensures that the total byte size limit causes the batch creation to stop before exceeding it. + */ + @Test + public void testCreateNextBatchRespectSizeLimit() { + // The total size limit for a batch is 25 + SimpleBatchCreator<String> creator = + new SimpleBatchCreator.Builder<String>().setMaxBatchSizeInBytes(25L).build(); + RequestBuffer<String> buffer = new DequeRequestBuffer.Builder<String>().build(); + // The first three have size=10, the last has size=1 + buffer.add(new RequestEntryWrapper<>("A", 10L), false); + buffer.add(new RequestEntryWrapper<>("B", 10L), false); + buffer.add(new RequestEntryWrapper<>("C", 10L), false); + buffer.add(new RequestEntryWrapper<>("D", 1L), false); + + RequestInfo requestInfo = + new RequestInfo() { + @Override + public int getBatchSize() { + return 10; // large enough that size becomes the limiting factor + } + }; + Batch<String> result = creator.createNextBatch(requestInfo, buffer); + // Should only take 5 items, ignoring the size limit because each item is 10 bytes Review Comment: Thanks. Corrected this. -- 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]
