Github user viirya commented on a diff in the pull request:
https://github.com/apache/spark/pull/17308#discussion_r118719834
--- Diff:
external/kafka-0-10-sql/src/test/scala/org/apache/spark/sql/kafka010/CachedKafkaProducerSuite.scala
---
@@ -0,0 +1,75 @@
+/*
+ * 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.spark.sql.kafka010
+
+import java.{util => ju}
+import java.util.concurrent.ConcurrentMap
+
+import org.apache.kafka.clients.producer.KafkaProducer
+import org.apache.kafka.common.serialization.ByteArraySerializer
+import org.scalatest.PrivateMethodTester
+
+import org.apache.spark.sql.test.SharedSQLContext
+
+class CachedKafkaProducerSuite extends SharedSQLContext with
PrivateMethodTester {
+
+ type KP = KafkaProducer[Array[Byte], Array[Byte]]
+
+ protected override def beforeEach(): Unit = {
+ super.beforeEach()
+ val clear = PrivateMethod[Unit]('clear)
+ CachedKafkaProducer.invokePrivate(clear())
+ }
+
+ test("Should return the cached instance on calling getOrCreate with same
params.") {
+ val kafkaParams = new ju.HashMap[String, Object]()
+ kafkaParams.put("acks", "0")
+ // Here only host should be resolvable, it does not need a running
instance of kafka server.
+ kafkaParams.put("bootstrap.servers", "127.0.0.1:9022")
+ kafkaParams.put("key.serializer", classOf[ByteArraySerializer].getName)
+ kafkaParams.put("value.serializer",
classOf[ByteArraySerializer].getName)
+ val producer = CachedKafkaProducer.getOrCreate(kafkaParams)
+ val producer2 = CachedKafkaProducer.getOrCreate(kafkaParams)
+ assert(producer == producer2)
+
+ val cacheMap = PrivateMethod[ConcurrentMap[String,
Option[KP]]]('getAsMap)
+ val map = CachedKafkaProducer.invokePrivate(cacheMap())
+ assert(map.size == 1)
+ }
+
+ test("Should close the correct kafka producer for the given
kafkaPrams.") {
+ val kafkaParams = new ju.HashMap[String, Object]()
+ kafkaParams.put("acks", "0")
+ kafkaParams.put("bootstrap.servers", "127.0.0.1:9022")
+ kafkaParams.put("key.serializer", classOf[ByteArraySerializer].getName)
+ kafkaParams.put("value.serializer",
classOf[ByteArraySerializer].getName)
+ val producer: KP = CachedKafkaProducer.getOrCreate(kafkaParams)
+ kafkaParams.put("acks", "1")
+ val producer2: KP = CachedKafkaProducer.getOrCreate(kafkaParams)
+ // With updated conf, a new producer instance should be created.
+ assert(producer != producer2)
+
+ val cacheMap = PrivateMethod[ConcurrentMap[String,
Option[KP]]]('getAsMap)
+ val map = CachedKafkaProducer.invokePrivate(cacheMap())
+ assert(map.size == 2)
+
+ CachedKafkaProducer.close(kafkaParams)
+ val map2 = CachedKafkaProducer.invokePrivate(cacheMap())
+ assert(map2.size == 1)
--- End diff --
We just know there is one KP by this assert. Seems we should also verify if
we close the correct KP?
---
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.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]