ibuenros commented on a change in pull request #2582: UnitTest for KafkaSource
URL: https://github.com/apache/incubator-gobblin/pull/2582#discussion_r270198707
 
 

 ##########
 File path: 
gobblin-modules/gobblin-kafka-09/src/test/java/org/apache/gobblin/kafka/KafakaSourceTest.java
 ##########
 @@ -0,0 +1,160 @@
+/*
+ * 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.gobblin.kafka;
+
+import com.typesafe.config.Config;
+import java.io.IOException;
+import java.lang.management.ManagementFactory;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.gobblin.configuration.ConfigurationKeys;
+import org.apache.gobblin.configuration.SourceState;
+import org.apache.gobblin.configuration.WorkUnitState;
+import org.apache.gobblin.kafka.client.GobblinKafkaConsumerClient;
+import org.apache.gobblin.kafka.client.Kafka09ConsumerClient;
+import org.apache.gobblin.runtime.util.MultiWorkUnitUnpackingIterator;
+import org.apache.gobblin.source.extractor.Extractor;
+import org.apache.gobblin.source.extractor.extract.kafka.KafkaSource;
+import org.apache.gobblin.source.workunit.MultiWorkUnit;
+import org.apache.gobblin.source.workunit.WorkUnit;
+import org.apache.kafka.clients.consumer.MockConsumer;
+import org.apache.kafka.clients.consumer.OffsetResetStrategy;
+import org.apache.kafka.common.Node;
+import org.apache.kafka.common.PartitionInfo;
+import org.apache.kafka.common.TopicPartition;
+import org.testng.Assert;
+import org.testng.annotations.AfterSuite;
+import org.testng.annotations.BeforeSuite;
+import org.testng.annotations.Test;
+
+
+@Slf4j
+public class KafakaSourceTest {
+
+  public static final String BOOTSTRAP_WITH_OFFSET = "bootstrap.with.offset";
+  public static final String PARTITION_ID = "partition.id";
+
+  private final KafkaTestBase kafkaTestHelper;
+  public KafakaSourceTest()
+      throws InterruptedException, RuntimeException {
+    kafkaTestHelper = new KafkaTestBase();
+  }
+  @BeforeSuite
+  public void beforeSuite() {
+    log.info("Process id = " + ManagementFactory.getRuntimeMXBean().getName());
+    kafkaTestHelper.startServers();
+  }
+
+  @AfterSuite
+  public void afterSuite()
+      throws IOException {
+    try {
+      kafkaTestHelper.stopClients();
+    } finally {
+      kafkaTestHelper.stopServers();
+    }
+  }
+  private SourceState generateState()
+  {
+    SourceState state = new SourceState();
+    state.setProp(KafkaSource.GOBBLIN_KAFKA_CONSUMER_CLIENT_FACTORY_CLASS,
+        TestKafkaConsumerClientFactory.class.getName());
+    state.setProp(ConfigurationKeys.KAFKA_BROKERS, "localhost:" + 
kafkaTestHelper.getKafkaServerPort());
+    state.setProp(KafkaSource.TOPIC_WHITELIST, "test_topic.*");
+    state.setProp(BOOTSTRAP_WITH_OFFSET, "earliest");
+    return state;
+  }
+
+  @Test
+  public void testWhenNoTargetMapperSize() {
+
+    KafkaSourceForTest source = new KafkaSourceForTest();
+    SourceState state = generateState();
+    state.setProp(ConfigurationKeys.MR_JOB_MAX_MAPPERS_KEY, 100);
+    List<WorkUnit> workUnits =  source.getWorkunits(state);
+    Assert.assertEquals(workUnits.size(), 100);
+
+    MultiWorkUnitUnpackingIterator iterator = new 
MultiWorkUnitUnpackingIterator(workUnits.iterator());
+    int numOfWorkUnits = 0;
+    while(iterator.hasNext())
 
 Review comment:
   You can use `numOfWorkUnits = Iterators.size(new 
MultiWorkUnitUnpackingIterator(workUnits.iterator())`

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to