-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-15179: Add integration tests for the file sink and source connectors #14279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
C0urante
merged 9 commits into
apache:trunk
from
yashmayya:KAFKA-15179-file-stream-integration-tests
Sep 7, 2023
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
546d467
KAFKA-15179: Add integration tests for the file sink and source conne…
yashmayya ea817a6
Minor improvements based on review suggestions
yashmayya 9bc4592
Use consumeAll to verify exact number of messages to reduce test runt…
yashmayya ec7296d
Remove unnecessary append option
yashmayya 043a5a5
Fix sink tests
yashmayya ad415ec
Add utility methods to alter the offset for a single partition for so…
yashmayya 743890c
Add check to verify exact number of lines in sink tests
yashmayya 49836fd
Use BufferedReader::lines instead of BufferedReader::readLine one by …
yashmayya c4e6686
Minor Javadoc update
yashmayya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
192 changes: 192 additions & 0 deletions
192
...ava/org/apache/kafka/connect/file/integration/FileStreamSinkConnectorIntegrationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| /* | ||
| * 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.connect.file.integration; | ||
|
|
||
| import org.apache.kafka.common.TopicPartition; | ||
| import org.apache.kafka.connect.file.FileStreamSinkConnector; | ||
| import org.apache.kafka.connect.util.clusters.EmbeddedConnectCluster; | ||
| import org.apache.kafka.test.TestUtils; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Tag; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.File; | ||
| import java.io.InputStreamReader; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import static org.apache.kafka.connect.file.FileStreamSinkConnector.FILE_CONFIG; | ||
| import static org.apache.kafka.connect.runtime.ConnectorConfig.CONNECTOR_CLASS_CONFIG; | ||
| import static org.apache.kafka.connect.runtime.ConnectorConfig.TASKS_MAX_CONFIG; | ||
| import static org.apache.kafka.connect.sink.SinkConnector.TOPICS_CONFIG; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| @Tag("integration") | ||
| public class FileStreamSinkConnectorIntegrationTest { | ||
|
|
||
| private static final String CONNECTOR_NAME = "test-connector"; | ||
| private static final String TOPIC = "test-topic"; | ||
| private static final String MESSAGE_PREFIX = "Message "; | ||
| private static final int NUM_MESSAGES = 5; | ||
| private static final String FILE_NAME = "test-file"; | ||
| private final EmbeddedConnectCluster connect = new EmbeddedConnectCluster.Builder().build(); | ||
|
|
||
| @BeforeEach | ||
| public void setup() { | ||
| connect.start(); | ||
| connect.kafka().createTopic(TOPIC); | ||
| produceMessagesToTopic(TOPIC, NUM_MESSAGES); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void tearDown() { | ||
| connect.stop(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSimpleSink() throws Exception { | ||
| File tempDir = TestUtils.tempDirectory(); | ||
| Path tempFilePath = tempDir.toPath().resolve(FILE_NAME); | ||
| Map<String, String> connectorConfigs = baseConnectorConfigs(TOPIC, tempFilePath.toString()); | ||
| connect.configureConnector(CONNECTOR_NAME, connectorConfigs); | ||
| connect.assertions().assertConnectorAndExactlyNumTasksAreRunning(CONNECTOR_NAME, 1, | ||
| "Connector and task did not start in time"); | ||
|
|
||
| verifyLinesInFile(tempFilePath, NUM_MESSAGES, true); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAlterOffsets() throws Exception { | ||
| File tempDir = TestUtils.tempDirectory(); | ||
| Path tempFilePath = tempDir.toPath().resolve(FILE_NAME); | ||
| Map<String, String> connectorConfigs = baseConnectorConfigs(TOPIC, tempFilePath.toString()); | ||
| connect.configureConnector(CONNECTOR_NAME, connectorConfigs); | ||
| connect.assertions().assertConnectorAndExactlyNumTasksAreRunning(CONNECTOR_NAME, 1, | ||
| "Connector and task did not start in time"); | ||
|
|
||
| verifyLinesInFile(tempFilePath, NUM_MESSAGES, true); | ||
|
|
||
| connect.stopConnector(CONNECTOR_NAME); | ||
| connect.assertions().assertConnectorIsStopped(CONNECTOR_NAME, "Connector did not stop in time"); | ||
|
|
||
| // Alter the offsets to cause the last message in the topic to be re-processed | ||
| connect.alterSinkConnectorOffset(CONNECTOR_NAME, new TopicPartition(TOPIC, 0), (long) (NUM_MESSAGES - 1)); | ||
|
|
||
| connect.resumeConnector(CONNECTOR_NAME); | ||
| connect.assertions().assertConnectorAndExactlyNumTasksAreRunning(CONNECTOR_NAME, 1, | ||
| "Connector and task did not resume in time"); | ||
|
|
||
| // The last message should be re-processed when the connector is resumed after the offsets are altered | ||
| verifyLinesInFile(tempFilePath, NUM_MESSAGES + 1, false); | ||
| } | ||
|
|
||
| @Test | ||
| public void testResetOffsets() throws Exception { | ||
| File tempDir = TestUtils.tempDirectory(); | ||
| Path tempFilePath = tempDir.toPath().resolve(FILE_NAME); | ||
| Map<String, String> connectorConfigs = baseConnectorConfigs(TOPIC, tempFilePath.toString()); | ||
| connect.configureConnector(CONNECTOR_NAME, connectorConfigs); | ||
| connect.assertions().assertConnectorAndExactlyNumTasksAreRunning(CONNECTOR_NAME, 1, | ||
| "Connector and task did not start in time"); | ||
|
|
||
| verifyLinesInFile(tempFilePath, NUM_MESSAGES, true); | ||
|
|
||
| connect.stopConnector(CONNECTOR_NAME); | ||
| connect.assertions().assertConnectorIsStopped(CONNECTOR_NAME, "Connector did not stop in time"); | ||
|
|
||
| // Reset the offsets to cause all the message in the topic to be re-processed | ||
| connect.resetConnectorOffsets(CONNECTOR_NAME); | ||
|
|
||
| connect.resumeConnector(CONNECTOR_NAME); | ||
| connect.assertions().assertConnectorAndExactlyNumTasksAreRunning(CONNECTOR_NAME, 1, | ||
| "Connector and task did not resume in time"); | ||
|
|
||
| // All the messages should be re-processed when the connector is resumed after the offsets are reset | ||
| verifyLinesInFile(tempFilePath, 2 * NUM_MESSAGES, false); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSinkMultipleTopicsWithMultipleTasks() throws Exception { | ||
| String topic2 = "test-topic-2"; | ||
| connect.kafka().createTopic(topic2); | ||
| produceMessagesToTopic(topic2, NUM_MESSAGES); | ||
|
|
||
| File tempDir = TestUtils.tempDirectory(); | ||
| Path tempFilePath = tempDir.toPath().resolve(FILE_NAME); | ||
| Map<String, String> connectorConfigs = baseConnectorConfigs(TOPIC + "," + topic2, tempFilePath.toString()); | ||
| connectorConfigs.put(TASKS_MAX_CONFIG, "2"); | ||
|
|
||
| connect.configureConnector(CONNECTOR_NAME, connectorConfigs); | ||
| connect.assertions().assertConnectorAndExactlyNumTasksAreRunning(CONNECTOR_NAME, 2, | ||
| "Connector and task did not start in time"); | ||
|
|
||
| // Only verify the number of lines since the messages can be consumed in any order across the two topics | ||
| verifyLinesInFile(tempFilePath, 2 * NUM_MESSAGES, false); | ||
| } | ||
|
|
||
| private void produceMessagesToTopic(String topic, int numMessages) { | ||
| for (int i = 0; i < numMessages; i++) { | ||
| connect.kafka().produce(topic, MESSAGE_PREFIX + i); | ||
| } | ||
| } | ||
|
|
||
| private Map<String, String> baseConnectorConfigs(String topics, String filePath) { | ||
| Map<String, String> connectorConfigs = new HashMap<>(); | ||
| connectorConfigs.put(CONNECTOR_CLASS_CONFIG, FileStreamSinkConnector.class.getName()); | ||
| connectorConfigs.put(TOPICS_CONFIG, topics); | ||
| connectorConfigs.put(FILE_CONFIG, filePath); | ||
| return connectorConfigs; | ||
| } | ||
|
|
||
| /** | ||
| * Verify that the number of lines in the file at {@code filePath} is equal to {@code numLines} and that they all begin with the | ||
| * prefix {@link #MESSAGE_PREFIX}. | ||
| * <p> | ||
| * If {@code verifyLinearity} is true, this method will also verify that the lines have a linearly increasing message number | ||
| * (beginning with 0) after the prefix. | ||
| * | ||
| * @param filePath the file path | ||
| * @param numLines the expected number of lines in the file | ||
| * @param verifyLinearity true if the line contents are to be verified | ||
| */ | ||
| private void verifyLinesInFile(Path filePath, int numLines, boolean verifyLinearity) throws Exception { | ||
| try (BufferedReader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(filePath)))) { | ||
| AtomicInteger i = new AtomicInteger(0); | ||
| TestUtils.waitForCondition(() -> { | ||
| reader.lines().forEach(line -> { | ||
| if (verifyLinearity) { | ||
| assertEquals(MESSAGE_PREFIX + i, line); | ||
| } else { | ||
| assertTrue(line.startsWith(MESSAGE_PREFIX)); | ||
| } | ||
| i.getAndIncrement(); | ||
| }); | ||
|
|
||
| return i.get() >= numLines; | ||
| }, "Expected to read " + numLines + " lines from the file"); | ||
| } | ||
|
|
||
| // Ensure that there are exactly the expected number of lines present | ||
| assertEquals(numLines, Files.readAllLines(filePath).size()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @yashmayya - we could also check the number of times a message is expected to be present in the file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, I intentionally kept the check looser to make it a little more generic and allow for covering all the scenarios. In the alter offsets case for instance, only the last message would be written twice and all the others would only be written once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the checks here, while fairly loose, are sufficient. We have pretty granular unit testing coverage for the sink and source task classes. It's not impossible to add the same level of granularity to these tests but it'd be fairly complex to do it right (i.e., handle offset resets and consumption from multiple topics). I'm fine with erring on the side of simplicity for now and, if necessary, hardening these tests in the future.