imaffe commented on a change in pull request #17937: URL: https://github.com/apache/flink/pull/17937#discussion_r763940584
########## File path: flink-connectors/flink-connector-pulsar/src/test/java/org/apache/flink/connector/pulsar/source/reader/source/PulsarSourceReaderTestBase.java ########## @@ -0,0 +1,232 @@ +/* + * 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.api.connector.source.ReaderOutput; +import org.apache.flink.api.connector.source.SourceReaderContext; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.connector.pulsar.source.config.SourceConfiguration; +import org.apache.flink.connector.pulsar.source.enumerator.topic.TopicNameUtils; +import org.apache.flink.connector.pulsar.source.reader.PulsarSourceReaderFactory; +import org.apache.flink.connector.pulsar.source.reader.deserializer.PulsarDeserializationSchema; +import org.apache.flink.connector.pulsar.source.reader.deserializer.PulsarDeserializationSchemaInitializationContext; +import org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit; +import org.apache.flink.connector.pulsar.testutils.PulsarTestSuiteBase; +import org.apache.flink.connector.pulsar.testutils.runtime.PulsarRuntimeOperator; +import org.apache.flink.connector.testutils.source.reader.TestingReaderContext; +import org.apache.flink.connector.testutils.source.reader.TestingReaderOutput; +import org.apache.flink.core.io.InputStatus; +import org.apache.flink.core.testutils.CommonTestUtils; + +import org.apache.pulsar.client.admin.PulsarAdminException; +import org.apache.pulsar.client.api.MessageId; +import org.apache.pulsar.client.api.Schema; +import org.apache.pulsar.client.api.SubscriptionType; +import org.apache.pulsar.common.policies.data.SubscriptionStats; +import org.apache.pulsar.common.policies.data.TopicStats; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.TestInfo; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.time.Duration; +import java.util.Collections; +import java.util.Map; +import java.util.Random; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.function.Supplier; + +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; +import static org.apache.flink.connector.pulsar.source.PulsarSourceOptions.PULSAR_ENABLE_AUTO_ACKNOWLEDGE_MESSAGE; +import static org.apache.flink.connector.pulsar.source.PulsarSourceOptions.PULSAR_MAX_FETCH_RECORDS; +import static org.apache.flink.connector.pulsar.source.PulsarSourceOptions.PULSAR_MAX_FETCH_TIME; +import static org.apache.flink.connector.pulsar.source.PulsarSourceOptions.PULSAR_SUBSCRIPTION_NAME; +import static org.apache.flink.connector.pulsar.source.PulsarSourceOptions.PULSAR_SUBSCRIPTION_TYPE; +import static org.apache.flink.connector.pulsar.source.reader.deserializer.PulsarDeserializationSchema.pulsarSchema; +import static org.apache.flink.connector.pulsar.testutils.PulsarTestCommonUtils.createPartitionSplit; +import static org.apache.flink.shaded.guava30.com.google.common.util.concurrent.Uninterruptibles.sleepUninterruptibly; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +abstract class PulsarSourceReaderTestBase extends PulsarTestSuiteBase { + + protected static final int DEFAULT_POLLING_TIMEOUT_SECONDS = 120; + + /** Expected the add or override config entries in inherited classes. */ + protected abstract void customConfig(Configuration config); + + @BeforeEach + void beforeEach(TestInfo testInfo) { + Random random = new Random(System.currentTimeMillis()); + String topicName = uniqueTestName(testInfo); + operator().setupTopic(topicName, Schema.INT32, () -> random.nextInt(20)); + } + + @AfterEach + void afterEach(TestInfo testInfo) { + String topicName = uniqueTestName(testInfo); + operator().deleteTopic(topicName, true); + } + + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void assignZeroSplitsCreatesZeroSubscription( + boolean autoAcknowledgementEnabled, TestInfo testInfo) throws Exception { + PulsarSourceReaderBase<Integer> reader = sourceReader(autoAcknowledgementEnabled); + String topicName = uniqueTestName(testInfo); + reader.snapshotState(100L); + reader.notifyCheckpointComplete(100L); + // Verify the committed offsets. + reader.close(); + for (int i = 0; i < PulsarRuntimeOperator.DEFAULT_PARTITIONS; i++) { + verifyNoSubscriptionCreated(TopicNameUtils.topicNameWithPartition(topicName, i)); + } + } + + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void assigningEmptySplits(boolean autoAcknowledgementEnabled, TestInfo testInfo) + throws Exception { + PulsarSourceReaderBase<Integer> reader = sourceReader(autoAcknowledgementEnabled); + String topicName = uniqueTestName(testInfo); + final PulsarPartitionSplit emptySplit = + createPartitionSplit( + topicName, 0, Boundedness.CONTINUOUS_UNBOUNDED, MessageId.latest); + + reader.addSplits(Collections.singletonList(emptySplit)); + + TestingReaderOutput<Integer> output = new TestingReaderOutput<>(); + InputStatus status; + status = reader.pollNext(output); + // We sleep for 1 sec to avoid entering false negative case: + // we perform assertion when add splits task + // is dequeued but not executed. + sleepUninterruptibly(1, TimeUnit.SECONDS); Review comment: Oh, there is an edge case. Basically the fetcher could become idle upon entry because : 1. the AddSplitsTask is dequeued from taskQueue, 2. The dequeued splits is not assigned to `assignedSplits`.And if both `assignedSplits` is null and taskQueue is empty, the fetcher could become idle, and return NOTHING_AVAILABLE upon entry. This happened multiple times during local test. -- 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]
