Github user tweise commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/134#discussion_r48452288
--- Diff:
kafka/src/test/java/org/apache/apex/malhar/kafka/KafkaInputOperatorTest.java ---
@@ -0,0 +1,221 @@
+/**
+ * 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.apex.malhar.kafka;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.slf4j.LoggerFactory;
+
+import org.apache.kafka.clients.consumer.ConsumerConfig;
+import org.apache.kafka.common.serialization.Deserializer;
+import org.apache.kafka.common.serialization.StringDeserializer;
+
+import com.datatorrent.api.Context;
+import com.datatorrent.api.DAG;
+import com.datatorrent.api.DAG.Locality;
+import com.datatorrent.api.DefaultInputPort;
+import com.datatorrent.api.LocalMode;
+import com.datatorrent.common.util.BaseOperator;
+
+/**
+ * A bunch of test to verify the input operator will be automatically
partitioned per kafka partition This test is launching its
+ * own Kafka cluster.
+ */
+@RunWith(Parameterized.class)
+public class KafkaInputOperatorTest extends KafkaOperatorTestBase
+{
+
+ private int totalBrokers = 0;
+
+
+
+ @Parameterized.Parameters(name = "multi-cluster: {0}, multi-partition:
{1}")
+ public static Collection<Boolean[]> testScenario()
+ {
+ return Arrays.asList(new Boolean[][]{{true, false}, // multi cluster
with single partition
+ {true, true}, // multi cluster with multi partitions
+ {false, true}, // single cluster with multi partitions
+ {false, false}, // single cluster with single partitions
+ });
+ }
+
+ public KafkaInputOperatorTest(boolean hasMultiCluster, boolean
hasMultiPartition)
+ {
+ // This class want to initialize several kafka brokers for multiple
partitions
+ this.hasMultiCluster = hasMultiCluster;
+ this.hasMultiPartition = hasMultiPartition;
+ int cluster = 1 + (hasMultiCluster ? 1 : 0);
+ totalBrokers = (1 + (hasMultiPartition ? 1 : 0)) * cluster;
+
+ }
+
+ static final org.slf4j.Logger logger =
LoggerFactory.getLogger(KafkaInputOperatorTest.class);
+ static List<String> tupleCollection = new LinkedList<>();
+ static CountDownLatch latch;
+ static boolean hasFailure = false;
+ static int failureTrigger = 3000;
+ static int k = 0;
+
+ /**
+ * Test Operator to collect tuples from KafkaSingleInputStringOperator.
+ *
+ * @param
+ */
+ public static class CollectorModule extends BaseOperator
+ {
+ public final transient CollectorInputPort inputPort = new
CollectorInputPort();
+ }
+
+ public static class CollectorInputPort extends DefaultInputPort<byte[]>
+ {
+
+ @Override
+ public void process(byte[] bt)
+ {
+ String tuple = new String(bt);
+ if (hasFailure && k++ == failureTrigger) {
+ //you can only kill yourself once
+ hasFailure = false;
+ throw new RuntimeException();
+ }
+ if (tuple.equals(KafkaOperatorTestBase.END_TUPLE)) {
+ if (latch != null) {
+ latch.countDown();
+ }
+ return;
+ }
+ tupleCollection.add(tuple);
+ }
+
+ @Override
+ public void setConnected(boolean flag)
+ {
+ if (flag) {
+ tupleCollection.clear();
+ }
+ }
+ }
+
+ /**
+ * Test AbstractKafkaSinglePortInputOperator (i.e. an input adapter for
Kafka, aka consumer). This module receives
+ * data from an outside test generator through Kafka message bus and
feed that data into Malhar streaming platform.
+ *
+ * [Generate message and send that to Kafka message bus] ==> [Receive
that message through Kafka input adapter(i.e.
+ * consumer) and send using emitTuples() interface on output port]
+ *
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testPartitionableInputOperator() throws Exception
+ {
+ hasFailure = false;
+ testInputOperator(false);
+ }
+
+
+ @Test
+ public void testPartitionableInputOperatorWithFailure() throws Exception
+ {
+ hasFailure = true;
+ testInputOperator(true);
+ }
+
+ public void testInputOperator(boolean hasFailure) throws Exception
+ {
+
+ // each broker should get a END_TUPLE message
+ latch = new CountDownLatch(totalBrokers);
+
+ int totalCount = 10000;
+
+ // Start producer
+ KafkaTestProducer p = new KafkaTestProducer(TEST_TOPIC,
hasMultiPartition, hasMultiCluster);
+ p.setSendCount(totalCount);
+ new Thread(p).start();
+
+ // Create DAG for testing.
+ LocalMode lma = LocalMode.newInstance();
+ DAG dag = lma.getDAG();
+
+ // Create KafkaSinglePortStringInputOperator
+ KafkaSinglePortInputOperator node = dag.addOperator("Kafka input",
KafkaSinglePortInputOperator.class);
+ node.setInitialPartitionCount(1);
+ // set topic
+ node.setTopics(TEST_TOPIC);
+
node.setInitialOffset(AbstractKafkaInputOperator.InitialOffset.EARLIEST.name());
+ node.setClusters(getClusterConfig());
+
+ // Create Test tuple collector
+ CollectorModule collector = dag.addOperator("TestMessageCollector",
new CollectorModule());
+
+ // Connect ports
+ dag.addStream("Kafka message", node.outputPort,
collector.inputPort).setLocality(Locality.CONTAINER_LOCAL);
+
+ // Create local cluster
+ final LocalMode.Controller lc = lma.getController();
+ lc.setHeartbeatMonitoringEnabled(false);
+
+ if (hasFailure) {
+ setupHasFailureTest(node, dag);
+ }
+ lc.runAsync();
+
+ // Wait 30s for consumer finish consuming all the messages
+ boolean notTimeout = latch.await(40000, TimeUnit.MILLISECONDS);
+ Assert.assertTrue("TIMEOUT: 40s Collected " + tupleCollection,
notTimeout);
+
+ // Check results
+ Assert.assertEquals("Tuple count", totalCount, tupleCollection.size());
+ logger.debug(String.format("Number of emitted tuples: %d",
tupleCollection.size()));
+
+ p.close();
+ lc.shutdown();
+ // kafka has a bug shutdown connector you have to make sure kafka
client resource has been cleaned before clean the broker
+ Thread.sleep(5000);
--- End diff --
Check for that condition instead?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---