abstractdog commented on code in PR #4540:
URL: https://github.com/apache/hive/pull/4540#discussion_r1478141059


##########
kafka-handler/src/test/org/apache/hadoop/hive/kafka/KafkaStorageHandlerInfoTest.java:
##########
@@ -0,0 +1,134 @@
+/*
+ * 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.hadoop.hive.kafka;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.kafka.clients.consumer.ConsumerConfig;
+import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.apache.kafka.clients.consumer.KafkaConsumer;
+import org.apache.kafka.clients.producer.KafkaProducer;
+import org.apache.kafka.clients.producer.ProducerConfig;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.common.serialization.ByteArrayDeserializer;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.charset.Charset;
+import java.util.List;
+import java.util.Properties;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+public class KafkaStorageHandlerInfoTest {
+    private static final Logger LOG = 
LoggerFactory.getLogger(KafkaStorageHandlerInfoTest.class);
+
+    private static final int RECORD_NUMBER = 17384;
+    private static final String TOPIC = "TEST1";
+    private static final byte[] KEY_BYTES = 
"KEY".getBytes(Charset.forName("UTF-8"));
+    private static final KafkaBrokerResource KAFKA_BROKER_RESOURCE = new 
KafkaBrokerResource();
+
+    private static List<ConsumerRecord<byte[], byte[]>> getRecords(String 
topic) {
+        return IntStream.range(0, RECORD_NUMBER).mapToObj(number -> {
+            final byte[] value = ("VALUE-" + 
Integer.toString(number)).getBytes(Charset.forName("UTF-8"));
+            return new ConsumerRecord<>(topic, 0, (long) number, 0L, null, 0L, 
0, 0, KEY_BYTES, value);
+        }).collect(Collectors.toList());
+    }
+    private final Configuration conf = new Configuration();
+    private KafkaConsumer<byte[], byte[]> consumer;
+
+    private Properties consumerProps;
+
+    private static final String RESULT = "Partition(topic = TEST1, partition = 
0, leader = 0, replicas = [0], isr = [0], offlineReplicas = [])" +
+        " [start offset = [0], end offset = [" + RECORD_NUMBER + "]]";
+
+    @BeforeClass
+    public static void setupCluster() throws Throwable {
+        KAFKA_BROKER_RESOURCE.before();
+        sendData();
+    }
+
+    @AfterClass
+    public static void tearDownCluster() {
+        KAFKA_BROKER_RESOURCE.deleteTopic(TOPIC);
+        KAFKA_BROKER_RESOURCE.after();
+    }
+
+    @Before
+    public void setUp() {
+        LOG.info("setting up Config");
+        setupConsumer();
+    }
+
+    @After
+    public void tearDown() {
+        consumer.close();
+        LOG.info("tearDown");
+    }
+
+    private void setupConsumer() {
+        
this.conf.set(KafkaTableProperties.HIVE_KAFKA_BOOTSTRAP_SERVERS.getName(), 
KafkaBrokerResource.BROKER_IP_PORT);
+        consumerProps = new Properties();
+        consumerProps.setProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, 
"false");
+        consumerProps.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, 
"none");
+        consumerProps.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, 
KafkaBrokerResource.BROKER_IP_PORT);
+        
consumerProps.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, 
ByteArrayDeserializer.class.getName());
+        
consumerProps.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, 
ByteArrayDeserializer.class.getName());
+        //The configuration values are not default and are given randomly , 
these can be changed and tested if required
+        consumerProps.setProperty(ConsumerConfig.REQUEST_TIMEOUT_MS_CONFIG, 
"3002");
+        consumerProps.setProperty(ConsumerConfig.FETCH_MAX_WAIT_MS_CONFIG, 
"3001");
+        consumerProps.setProperty(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, 
"3001");
+        consumerProps.setProperty(ConsumerConfig.METADATA_MAX_AGE_CONFIG, 
"100");
+        this.consumer = new KafkaConsumer<>(consumerProps);
+    }
+
+    @Test
+    public void testFormatAsText() {
+        KafkaStorageHandlerInfo storageHandlerInfo = new 
KafkaStorageHandlerInfo(TOPIC, consumerProps);
+        Assert.assertEquals(String.class, 
storageHandlerInfo.formatAsText().getClass());
+        String text = storageHandlerInfo.formatAsText();
+        Assert.assertEquals(RESULT, text);
+    }
+
+    private static void sendData() {
+        List<ConsumerRecord<byte[], byte[]>> RECORDS = getRecords(TOPIC);
+        LOG.info("Setting up kafka producer");

Review Comment:
   what about moving this log message after creating producerProps, and adding 
it too?



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

To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org
For additional commands, e-mail: gitbox-h...@hive.apache.org

Reply via email to