C0urante commented on a change in pull request #8069:
URL: https://github.com/apache/kafka/pull/8069#discussion_r438378091



##########
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")
+    public void testBlockInConnectorTaskClass() throws Exception {
+        createConnectorWithBlock(BlockingConnector.TASK_CLASS);
+        BlockingConnector.waitForBlock();
+
+        createNormalConnector();
+        waitForConnectorStart(NORMAL_CONNECTOR_NAME);
+    }
+
+    @Test
+    @Ignore("Interaction with this connector method has not been made 
asynchronous yet and this test will fail")
+    public void testBlockInConnectorTaskConfigs() throws Exception {
+        createConnectorWithBlock(BlockingConnector.TASK_CONFIGS);
+        BlockingConnector.waitForBlock();
+
+        createNormalConnector();
+        waitForConnectorStart(NORMAL_CONNECTOR_NAME);
+    }
+
+    @Test
+    @Ignore("Interaction with this connector method has not been made 
asynchronous yet and this test will fail")
+    public void testConnectorRestartWithBlockInConnectorTaskConfigs() throws 
Exception {
+        createConnectorWithBlock(BlockingConnector.TASK_CONFIGS);
+        // Test both explicit restart and implicit (pause then resume) restart
+        connect.restartConnector(BLOCKING_CONNECTOR_NAME);
+        connect.pauseConnector(BLOCKING_CONNECTOR_NAME);
+        connect.resumeConnector(BLOCKING_CONNECTOR_NAME);
+        BlockingConnector.waitForBlock();
+
+        createNormalConnector();
+        waitForConnectorStart(NORMAL_CONNECTOR_NAME);
+    }
+
+    // TODO: Consider patching plugin scanning to handle connectors that block 
in their version methods
+    // @Test
+    // public void testBlockInConnectorVersion() throws Exception {
+    //     createConnectorWithBlock(VersionBlockingConnector.class);
+    //     createNormalConnector();
+    //     waitForConnectorStart(NORMAL_CONNECTOR_NAME);
+    // }
+
+    @Test
+    @Ignore("This connector method is never invoked by the framework")
+    public void testBlockInConnectorInitializeWithTaskConfigs() throws 
Exception {
+        
createConnectorWithBlock(BlockingConnector.INITIALIZE_WITH_TASK_CONFIGS);
+        BlockingConnector.waitForBlock();
+
+        createNormalConnector();
+        waitForConnectorStart(NORMAL_CONNECTOR_NAME);
+    }
+
+    @Test
+    @Ignore("This connector method is never invoked by the framework")
+    public void testBlockInConnectorReconfigure() throws Exception {
+        createConnectorWithBlock(BlockingConnector.RECONFIGURE);
+        Map<String, String> newProps = baseBlockingConnectorProps();
+        newProps.put("foo", "bar");
+
+        connect.configureConnector(BLOCKING_CONNECTOR_NAME, newProps);
+        BlockingConnector.waitForBlock();
+
+        createNormalConnector();
+        waitForConnectorStart(NORMAL_CONNECTOR_NAME);
+    }
+
+    private void createConnectorWithBlock(String block) {
+        Map<String, String> props = baseBlockingConnectorProps();
+        props.put(BlockingConnector.BLOCK_CONFIG, block);
+        log.info("Creating connector with block during {}", block);
+        try {
+            connect.configureConnector(BLOCKING_CONNECTOR_NAME, props);
+        } catch (RuntimeException e) {
+            log.info("Failed to create connector", e);
+            throw e;
+        }
+    }
+
+    private void createConnectorWithBlock(Class<? extends BlockingConnector> 
connectorClass) {
+        Map<String, String> props = baseBlockingConnectorProps();
+        props.put(CONNECTOR_CLASS_CONFIG, connectorClass.getName());
+        log.info("Creating blocking connector of type {}", 
connectorClass.getSimpleName());
+        try {
+            connect.configureConnector(BLOCKING_CONNECTOR_NAME, props);
+        } catch (RuntimeException e) {
+            log.info("Failed to create connector", e);
+            throw e;
+        }
+    }
+
+    private Map<String, String> baseBlockingConnectorProps() {
+        Map<String, String> result = new HashMap<>();
+        result.put(CONNECTOR_CLASS_CONFIG, BlockingConnector.class.getName());
+        result.put(TASKS_MAX_CONFIG, "1");
+        return result;
+    }
+
+    private void createNormalConnector() {
+        Map<String, String> props = new HashMap<>();
+        props.put(CONNECTOR_CLASS_CONFIG, 
MonitorableSinkConnector.class.getName());

Review comment:
       Yeah, I think we can do that.




----------------------------------------------------------------
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]


Reply via email to