C0urante commented on a change in pull request #8069: URL: https://github.com/apache/kafka/pull/8069#discussion_r438380494
########## File path: connect/runtime/src/test/java/org/apache/kafka/connect/integration/BlockingConnectorTest.java ########## @@ -0,0 +1,488 @@ +/* + * 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.integration; + +import org.apache.kafka.common.config.AbstractConfig; +import org.apache.kafka.common.config.Config; +import org.apache.kafka.common.config.ConfigDef; +import org.apache.kafka.connect.connector.ConnectorContext; +import org.apache.kafka.connect.connector.Task; +import org.apache.kafka.connect.runtime.Worker; +import org.apache.kafka.connect.runtime.rest.errors.ConnectRestException; +import org.apache.kafka.connect.runtime.rest.resources.ConnectorsResource; +import org.apache.kafka.connect.source.SourceConnector; +import org.apache.kafka.connect.source.SourceRecord; +import org.apache.kafka.connect.source.SourceTask; +import org.apache.kafka.connect.util.clusters.EmbeddedConnectCluster; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.CountDownLatch; + +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.runtime.SinkConnectorConfig.TOPICS_CONFIG; +import static org.junit.Assert.assertThrows; + +public class BlockingConnectorTest { + + private static final Logger log = LoggerFactory.getLogger(BlockingConnectorTest.class); + + private static final int NUM_WORKERS = 1; + private static final String BLOCKING_CONNECTOR_NAME = "blocking-connector"; + private static final String NORMAL_CONNECTOR_NAME = "normal-connector"; + private static final long REST_REQUEST_TIMEOUT = Worker.CONNECTOR_GRACEFUL_SHUTDOWN_TIMEOUT_MS * 2; + + private EmbeddedConnectCluster connect; + + @Before + public void setup() { + // Artificially reduce the REST request timeout so that these don't take forever + ConnectorsResource.setRequestTimeout(REST_REQUEST_TIMEOUT); + // build a Connect cluster backed by Kafka and Zk + connect = new EmbeddedConnectCluster.Builder() + .name("connect-cluster") + .numWorkers(NUM_WORKERS) + .numBrokers(1) + .workerProps(new HashMap<>()) + .brokerProps(new Properties()) + .build(); + + // start the clusters + connect.start(); + } + + @After + public void close() { + // stop all Connect, Kafka and Zk threads. + connect.stop(); + ConnectorsResource.resetRequestTimeout(); + BlockingConnector.resetBlockLatch(); + } + + @Test + public void testBlockInConnectorValidate() throws Exception { + log.info("Starting test testBlockInConnectorValidate"); + assertThrows(ConnectRestException.class, () -> createConnectorWithBlock(ValidateBlockingConnector.class)); + // Will NOT assert that connector has failed, since the request should fail before it's even created + + // Connector should already be blocked so this should return immediately, but check just to + // make sure that it actually did block + BlockingConnector.waitForBlock(); + + createNormalConnector(); + waitForConnectorStart(NORMAL_CONNECTOR_NAME); + } + + @Test + public void testBlockInConnectorConfig() throws Exception { + log.info("Starting test testBlockInConnectorConfig"); + assertThrows(ConnectRestException.class, () -> createConnectorWithBlock(ConfigBlockingConnector.class)); + // Will NOT assert that connector has failed, since the request should fail before it's even created + + // Connector should already be blocked so this should return immediately, but check just to + // make sure that it actually did block + BlockingConnector.waitForBlock(); + + createNormalConnector(); + waitForConnectorStart(NORMAL_CONNECTOR_NAME); + } + + @Test + public void testBlockInConnectorInitialize() throws Exception { + log.info("Starting test testBlockInConnectorInitialize"); + createConnectorWithBlock(InitializeBlockingConnector.class); + BlockingConnector.waitForBlock(); + + createNormalConnector(); + waitForConnectorStart(NORMAL_CONNECTOR_NAME); + } + + @Test + public void testBlockInConnectorStart() throws Exception { + log.info("Starting test testBlockInConnectorStart"); + createConnectorWithBlock(BlockingConnector.START); + BlockingConnector.waitForBlock(); + + createNormalConnector(); + waitForConnectorStart(NORMAL_CONNECTOR_NAME); + } + + @Test + public void testBlockInConnectorStop() throws Exception { + log.info("Starting test testBlockInConnectorStop"); + createConnectorWithBlock(BlockingConnector.STOP); + waitForConnectorStart(BLOCKING_CONNECTOR_NAME); + connect.deleteConnector(BLOCKING_CONNECTOR_NAME); + BlockingConnector.waitForBlock(); + + createNormalConnector(); + waitForConnectorStart(NORMAL_CONNECTOR_NAME); + } + + @Test + public void testWorkerRestartWithBlockInConnectorStart() throws Exception { + log.info("Starting test testWorkerRestartWithBlockInConnectorStart"); + createNormalConnector(); + createConnectorWithBlock(BlockingConnector.START); + // First instance of the connector should block on startup + BlockingConnector.waitForBlock(); + connect.removeWorker(); + + connect.addWorker(); + // After stopping the only worker and restarting it, a new instance of the blocking + // connector should be created and we can ensure that it blocks again + BlockingConnector.waitForBlock(); + waitForConnectorStart(NORMAL_CONNECTOR_NAME); + } + + @Test + public void testWorkerRestartWithBlockInConnectorStop() throws Exception { + log.info("Starting test testWorkerRestartWithBlockInConnectorStop"); + createNormalConnector(); + createConnectorWithBlock(BlockingConnector.STOP); + waitForConnectorStart(NORMAL_CONNECTOR_NAME); + waitForConnectorStart(BLOCKING_CONNECTOR_NAME); + connect.removeWorker(); + BlockingConnector.waitForBlock(); + + connect.addWorker(); + waitForConnectorStart(NORMAL_CONNECTOR_NAME); + waitForConnectorStart(BLOCKING_CONNECTOR_NAME); + } + + @Test + @Ignore("Interaction with this connector method has not been made asynchronous yet and this test will fail") Review comment: Fair, I'll take them out. Will file a follow-up jira ticket to note the gaps here in case someone runs into problems with the yet-unaddressed connector methods in the future. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected]
