-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement withRunningKafkaOnFoundPort (#76)
* Make withRunningKafka generic * Factor out withTempDir helper method * Factor out withRunningZooKeeper helper method * Implement withRunningKafkaOnFoundPort This is useful for writing tests that configure kafka/zookeeper to listen on port 0, i.e. listen on an arbitrary available port.
- Loading branch information
Showing
4 changed files
with
137 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...src/test/scala/net/manub/embeddedkafka/EmbeddedKafkaWithRunningKafkaOnFoundPortSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package net.manub.embeddedkafka | ||
|
||
class EmbeddedKafkaWithRunningKafkaOnFoundPortSpec | ||
extends EmbeddedKafkaSpecSupport | ||
with EmbeddedKafka { | ||
|
||
"the withRunningKafkaOnFoundPort method" should { | ||
"start and stop Kafka and Zookeeper successfully on non-zero ports" in { | ||
val userDefinedConfig = EmbeddedKafkaConfig(kafkaPort = 12345, zooKeeperPort = 12346) | ||
val actualConfig = withRunningKafkaOnFoundPort(userDefinedConfig) { actualConfig => | ||
actualConfig shouldBe userDefinedConfig | ||
bothKafkaAndZkAreAvailable(actualConfig) | ||
actualConfig | ||
} | ||
bothKafkaAndZkAreNotAvailable(actualConfig) | ||
} | ||
|
||
"start and stop multiple Kafka and Zookeeper successfully on arbitrary available ports" in { | ||
val userDefinedConfig = EmbeddedKafkaConfig(kafkaPort = 0, zooKeeperPort = 0) | ||
val actualConfig1 = withRunningKafkaOnFoundPort(userDefinedConfig) { actualConfig1 => | ||
bothKafkaAndZkAreAvailable(actualConfig1) | ||
publishStringMessageToKafka("topic", "message1")(actualConfig1) | ||
consumeFirstStringMessageFrom("topic")(actualConfig1) shouldBe "message1" | ||
val actualConfig2 = withRunningKafkaOnFoundPort(userDefinedConfig) { actualConfig2 => | ||
bothKafkaAndZkAreAvailable(actualConfig2) | ||
publishStringMessageToKafka("topic", "message2")(actualConfig2) | ||
consumeFirstStringMessageFrom("topic")(actualConfig2) shouldBe "message2" | ||
val allConfigs = Seq(userDefinedConfig, actualConfig1, actualConfig2) | ||
// Confirm both actual configs are running on separate non-zero ports, but otherwise equal | ||
allConfigs.map(_.kafkaPort).distinct should have size 3 | ||
allConfigs.map(_.zooKeeperPort).distinct should have size 3 | ||
allConfigs.map(_.copy(kafkaPort = 0, zooKeeperPort = 0)).distinct should have size 1 | ||
actualConfig2 | ||
} | ||
bothKafkaAndZkAreNotAvailable(actualConfig2) | ||
actualConfig1 | ||
} | ||
bothKafkaAndZkAreNotAvailable(actualConfig1) | ||
} | ||
|
||
"work with a simple example using implicits" in { | ||
val userDefinedConfig = EmbeddedKafkaConfig(kafkaPort = 0, zooKeeperPort = 0) | ||
withRunningKafkaOnFoundPort(userDefinedConfig) { implicit actualConfig => | ||
publishStringMessageToKafka("topic", "message") | ||
consumeFirstStringMessageFrom("topic") shouldBe "message" | ||
} | ||
} | ||
} | ||
|
||
private def bothKafkaAndZkAreAvailable(config: EmbeddedKafkaConfig): Unit = { | ||
kafkaIsAvailable(config.kafkaPort) | ||
zookeeperIsAvailable(config.zooKeeperPort) | ||
} | ||
|
||
private def bothKafkaAndZkAreNotAvailable(config: EmbeddedKafkaConfig): Unit = { | ||
kafkaIsNotAvailable(config.kafkaPort) | ||
zookeeperIsNotAvailable(config.zooKeeperPort) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters