Github user tdas commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17043#discussion_r103053432
  
    --- Diff: 
external/kafka-0-10-sql/src/test/scala/org/apache/spark/sql/kafka010/KafkaSinkSuite.scala
 ---
    @@ -0,0 +1,413 @@
    +/*
    + * 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.concurrent.atomic.AtomicInteger
    +
    +import org.scalatest.time.SpanSugar._
    +
    +import org.apache.spark.SparkException
    +import org.apache.spark.sql.{AnalysisException, SaveMode}
    +import org.apache.spark.sql.catalyst.InternalRow
    +import org.apache.spark.sql.catalyst.expressions.{AttributeReference, 
SpecificInternalRow, UnsafeProjection}
    +import org.apache.spark.sql.execution.streaming.MemoryStream
    +import org.apache.spark.sql.streaming._
    +import org.apache.spark.sql.test.SharedSQLContext
    +import org.apache.spark.sql.types.{BinaryType, DataType}
    +
    +class KafkaSinkSuite extends StreamTest with SharedSQLContext {
    +  import testImplicits._
    +
    +  case class AddMoreData(
    +      ms: MemoryStream[String],
    +      q: StreamingQuery,
    +      values: String*) extends ExternalAction {
    +    override def runAction(): Unit = {
    +      ms.addData(values)
    +      q.processAllAvailable()
    +      Thread.sleep(5000) // wait for data to appear in Kafka
    +    }
    +  }
    +
    +  protected var testUtils: KafkaTestUtils = _
    +
    +  override val streamingTimeout = 30.seconds
    +
    +  override def beforeAll(): Unit = {
    +    super.beforeAll()
    +    testUtils = new KafkaTestUtils(
    +      withBrokerProps = Map("auto.create.topics.enable" -> "false"))
    +    testUtils.setup()
    +  }
    +
    +  override def afterAll(): Unit = {
    +    if (testUtils != null) {
    +      testUtils.teardown()
    +      testUtils = null
    +      super.afterAll()
    +    }
    +  }
    +
    +  private val topicId = new AtomicInteger(0)
    +
    +  private def newTopic(): String = s"topic-${topicId.getAndIncrement()}"
    +
    +  test("write to stream with topic field") {
    --- End diff --
    
    This is a hugely verbose and round about way of testing kafka - using the 
streaming source to test the sink. I think we should investigate a better way. 
How about you try this, similar to the FileStreamSinkSuite
    
    - Use memory stream as you are doing, just use AddData (no need for 
AddMoreData)
    - Define a function called `checkKafka(expectedData)` which will 
        - processAllAvailable
        - read data in kafka as batch query and verify the results. 
    
    Then the test would look simpler.
    ```
    testStream (
       AddData(...)
       checkKafka(...)
       AddData(...)
       checkKafka(...)
    ...
    )
    ```
    
    Here is a what the checkKafka method would look like. See 
    
https://github.com/apache/spark/blob/master/sql/core/src/test/scala/org/apache/spark/sql/streaming/StateStoreMetricsTest.scala#L22
 
    ```
    def checkKafka[T](expectedResults: T*): AssertOnQuery = AssertOnQuery { q 
=> 
       q.processAllAvailable
       val kafkaData = spark.read.format("kafka") ....
       checkDataset(expectedResults, kafkaData)
    }
    ```


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

Reply via email to