imaffe commented on a change in pull request #17937: URL: https://github.com/apache/flink/pull/17937#discussion_r763938676
########## File path: flink-connectors/flink-connector-pulsar/src/test/java/org/apache/flink/connector/pulsar/source/reader/source/PulsarOrderedSourceReaderTest.java ########## @@ -0,0 +1,167 @@ +/* + * 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.pulsar.source.reader.source; + +import org.apache.flink.api.connector.source.Boundedness; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.connector.pulsar.source.PulsarSourceOptions; +import org.apache.flink.connector.pulsar.source.enumerator.topic.TopicNameUtils; +import org.apache.flink.connector.testutils.source.reader.TestingReaderOutput; +import org.apache.flink.connectors.test.common.junit.extensions.TestLoggerExtension; +import org.apache.flink.core.io.InputStatus; +import org.apache.flink.core.testutils.CommonTestUtils; + +import org.apache.pulsar.client.api.Schema; +import org.apache.pulsar.client.api.SubscriptionType; +import org.junit.jupiter.api.TestInfo; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.time.Duration; +import java.util.concurrent.TimeUnit; + +import static org.apache.flink.connector.pulsar.testutils.PulsarTestCommonUtils.createPartitionSplits; +import static org.apache.flink.connector.pulsar.testutils.runtime.PulsarRuntimeOperator.DEFAULT_PARTITIONS; +import static org.apache.flink.connector.pulsar.testutils.runtime.PulsarRuntimeOperator.NUM_RECORDS_PER_PARTITION; +import static org.apache.flink.shaded.guava30.com.google.common.util.concurrent.Uninterruptibles.sleepUninterruptibly; +import static org.junit.jupiter.api.Assertions.assertEquals; + +@ExtendWith(TestLoggerExtension.class) +class PulsarOrderedSourceReaderTest extends PulsarSourceReaderTestBase { + + private static final int MAX_EMPTY_POLLING_TIMES = 10; + + @Override + protected void customConfig(Configuration config) { + config.set(PulsarSourceOptions.PULSAR_SUBSCRIPTION_TYPE, SubscriptionType.Failover); + } + + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void pollMessageWithIntervalLongerThanTimeout( + boolean autoAcknowledgementEnabled, TestInfo testInfo) throws Exception { + PulsarSourceReaderBase<Integer> reader = sourceReader(autoAcknowledgementEnabled); + Boundedness boundedness = Boundedness.CONTINUOUS_UNBOUNDED; + String topicName = uniqueTestName(testInfo); + setupSourceReader(reader, topicName, 0, boundedness); + sleepUninterruptibly(15, TimeUnit.SECONDS); + operator() + .sendMessage(TopicNameUtils.topicNameWithPartition(topicName, 0), Schema.INT32, 5); + + // execute + TestingReaderOutput<Integer> output = new TestingReaderOutput<>(); + int expectedRecords = NUM_RECORDS_PER_PARTITION + 1; + pollUtilReadExpectedNumberOfRecordsAndValidate( + reader, + output, + expectedRecords, + TopicNameUtils.topicNameWithPartition(topicName, 0)); + } + + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void consumeMessagesAndCommitOffsets(boolean autoAcknowledgementEnabled, TestInfo testInfo) + throws Exception { + // set up the partition + PulsarOrderedSourceReader<Integer> reader = + (PulsarOrderedSourceReader<Integer>) sourceReader(autoAcknowledgementEnabled); + String topicName = uniqueTestName(testInfo); + setupSourceReader(reader, topicName, 0, Boundedness.CONTINUOUS_UNBOUNDED); + + // waiting for results + TestingReaderOutput<Integer> output = new TestingReaderOutput<>(); + pollUntil( + reader, + output, + () -> output.getEmittedRecords().size() == NUM_RECORDS_PER_PARTITION, + "The output didn't poll enough records before timeout."); + reader.snapshotState(100L); + reader.notifyCheckpointComplete(100L); + pollUntil( + reader, + output, + reader.cursorsToCommit::isEmpty, + "The offset commit did not finish before timeout."); + + // verify consumption + reader.close(); + verifyAllMessageAcknowledged( + NUM_RECORDS_PER_PARTITION, TopicNameUtils.topicNameWithPartition(topicName, 0)); + } + + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void offsetCommitOnCheckpointComplete(boolean autoAcknowledgementEnabled, TestInfo testInfo) + throws Exception { + PulsarOrderedSourceReader<Integer> reader = + (PulsarOrderedSourceReader<Integer>) sourceReader(autoAcknowledgementEnabled); + String topicName = uniqueTestName(testInfo); + // consume more than 1 partition + reader.addSplits( + createPartitionSplits( + topicName, DEFAULT_PARTITIONS, Boundedness.CONTINUOUS_UNBOUNDED)); + reader.notifyNoMoreSplits(); + TestingReaderOutput<Integer> output = new TestingReaderOutput<>(); + long checkpointId = 0; + int emptyResultTime = 0; + InputStatus status; + do { + checkpointId++; + status = reader.pollNext(output); + // Create a checkpoint for each message consumption, but not complete them. + reader.snapshotState(checkpointId); + // the first couple of pollNext() might return NOTHING_AVAILABLE before data appears + if (InputStatus.NOTHING_AVAILABLE == status) { + emptyResultTime++; + sleepUninterruptibly(1, TimeUnit.SECONDS); + } + + } while (emptyResultTime < MAX_EMPTY_POLLING_TIMES + && status != InputStatus.END_OF_INPUT + && output.getEmittedRecords().size() + < NUM_RECORDS_PER_PARTITION * DEFAULT_PARTITIONS); + + // The completion of the last checkpoint should subsume all previous checkpoints. + assertEquals(checkpointId, reader.cursorsToCommit.size()); + + long lastCheckpointId = checkpointId; + CommonTestUtils.waitUtil( + () -> { + try { + reader.notifyCheckpointComplete(lastCheckpointId); Review comment: I think the logic is when the checkpoint complete and the cursorsToCommit is empty, then it means the checkpoint is OK. It will run multiple times if there is an exception. Ideally after one run the cursorsToCommit should become empty. But I do agree maybe we can optimize this so that if one exception is thrown, we consider the test as fail. -- 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]
