-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathEmbeddedKafkaStreamsWithSchemaRegistry.scala
52 lines (48 loc) · 2.24 KB
/
EmbeddedKafkaStreamsWithSchemaRegistry.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package net.manub.embeddedkafka.schemaregistry.streams
import net.manub.embeddedkafka.UUIDs
import net.manub.embeddedkafka.schemaregistry.EmbeddedKafkaWithSchemaRegistry.consumerConfigForSchemaRegistry
import net.manub.embeddedkafka.schemaregistry.{
EmbeddedKafkaConfigWithSchemaRegistry,
EmbeddedKafkaWithSchemaRegistry
}
import net.manub.embeddedkafka.streams.TestStreamsConfig
import org.apache.kafka.streams.{KafkaStreams, Topology}
// TODO: need to find a better way of not duplicating this code from the kafka-streams module
/** Helper trait for testing Kafka Streams.
* It creates an embedded Kafka Instance for each test case.
* Use `runStreams` to execute your streams.
*/
trait EmbeddedKafkaStreamsWithSchemaRegistry
extends EmbeddedKafkaWithSchemaRegistry
with TestStreamsConfig {
/** Execute Kafka streams and pass a block of code that can
* operate while the streams are active.
* The code block can be used for publishing and consuming messages in Kafka.
*
* @param topicsToCreate the topics that should be created in Kafka before launching the streams.
* @param topology the streams topology that will be used to instantiate the streams with
* a default configuration (all state directories are different and
* in temp folders)
* @param extraConfig additional KafkaStreams configuration (overwrite existing keys in
* default config)
* @param block the code block that will executed while the streams are active.
* Once the block has been executed the streams will be closed.
*/
def runStreams[T](topicsToCreate: Seq[String],
topology: Topology,
extraConfig: Map[String, AnyRef] = Map.empty)(block: => T)(
implicit config: EmbeddedKafkaConfigWithSchemaRegistry): T =
withRunningKafka {
topicsToCreate.foreach(topic => createCustomTopic(topic))
val streamId = UUIDs.newUuid().toString
val streams = new KafkaStreams(
topology,
streamProps(streamId, extraConfig ++ consumerConfigForSchemaRegistry))
streams.start()
try {
block
} finally {
streams.close()
}
}(config)
}