尝试通过Scala运行KafkaServerStartable时测试冻结

时间:2018-12-04 01:28:34

标签: java scala testing apache-kafka sbt

我正在尝试将Java测试样本转换为Scala测试样本,但似乎无法正常工作。

class KafkaMostBasicTest extends FlatSpec with Matchers with BeforeAndAfterEach {

  private val topic: String = "topic1-" + System.currentTimeMillis

  private var server: KafkaTestFixture = _
  private var producer: Producer[String, String] = _
  private var consumerConnector: ConsumerConnector = _

  override def beforeEach() {
    this.server = new KafkaTestFixture()
    this.server.start(serverProperties())

    //Create a producer
    this.producer = new KafkaProducer[String, String](producerProps())

    //send a message
    this.producer.send(new ProducerRecord(topic, "message")).get()

  }

  override def afterEach() {
    this.producer.close()
    this.consumerConnector.shutdown()
    this.server.stop()
  }

  "The kefka message" should "be 'message'" in {

    //Create a consumer
    val it: ConsumerIterator[String, String] = buildConsumer(topic)

    //read it back
    val messageAndMetadata: MessageAndMetadata[String, String] = it.next()
    val value: String = messageAndMetadata.message()
    value shouldEqual "message"
  }

  private def serverProperties(): Properties = {
    val props = new Properties()
    props.put("zookeeper.connect", "localhost:2181")
    props.put("broker.id", "1")
    props
  }

  private class KafkaTestFixture {
    private var zk: TestingServer = _
    private var kafka: KafkaServerStartable = _

    @throws[Exception]
    def start(properties: Properties) {
      val port: Integer = getZkPort(properties)
      this.zk = new TestingServer(port)
      this.zk.start()

      val kafkaConfig = new KafkaConfig(properties)
      this.kafka = new KafkaServerStartable(kafkaConfig)
      this.kafka.startup()
    }

    @throws[IOException]
    def stop()  {
      this.kafka.shutdown()
      this.zk.stop()
      this.zk.close()
    }

    private def getZkPort(properties: Properties): Integer = {
      val url: String = properties.get("zookeeper.connect").asInstanceOf[String]
      val port: String = url.split(":")(1)
      Integer.valueOf(port)
    }
  }
}

使用消除功能,我跟踪了有问题的行this.kafka.startup()。在该行之前,测试成功运行,但是在尝试执行此行时,测试只是停止。没有错误,没有失败。

我尝试添加try / catch并打印出可能发生的任何错误,但是我看不到任何问题/错误。

测试运行的结果输出为:

$ sbt test
[info] Loading project definition from /mnt/c/tmp/sbt-kafka-test/project
[info] Loading settings for project root from build.sbt ...
[info] Set current project to sbt-kafka-test (in build file:/mnt/c/tmp/sbt-kafka-test/)
[info] Compiling 1 Scala source to /mnt/c/tmp/sbt-kafka-test/target/scala-2.12/test-classes ...
[warn] there was one deprecation warning; re-run with -deprecation for details
[warn] one warning found
[info] Done compiling.
log4j:WARN No appenders could be found for logger (org.apache.zookeeper.server.ZooKeeperServerMain).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[info] HelloTest:
[info] The Hello object
[info] - should say hello

完整的Scala项目is available on GitHub

正在运行的Java项目is also available on GitHub

0 个答案:

没有答案