Skip to content

Commit 7ac08a5

Browse files
committed
ref: Migrating Kafka tests to TestContainers
1 parent 6c769ef commit 7ac08a5

File tree

7 files changed

+43
-175
lines changed

7 files changed

+43
-175
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
2626
restore-keys: ${{ runner.os }}-m2
2727
- name: Build with Maven
28-
run: ./mvnw -B package --file pom.xml -Pscala-2.12 -Dkotest.tags="!Kafka"
28+
run: ./mvnw -B package --file pom.xml -Pscala-2.12
2929
# qodana:
3030
# runs-on: ubuntu-latest
3131
# steps:

.github/workflows/publish_dev_version.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
2525
restore-keys: ${{ runner.os }}-m2
2626
- name: Deploy to GH Packages
27-
run: ./mvnw --batch-mode deploy -Dkotest.tags="!Kafka"
27+
run: ./mvnw --batch-mode deploy
2828
env:
2929
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3030

kotlin-spark-api/3.2/pom_2.12.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@
7373
<version>${kotest-extensions-allure.version}</version>
7474
<scope>test</scope>
7575
</dependency>
76+
<dependency>
77+
<groupId>io.kotest.extensions</groupId>
78+
<artifactId>kotest-extensions-testcontainers</artifactId>
79+
<version>${kotest-extensions-testcontainers.version}</version>
80+
<scope>test</scope>
81+
</dependency>
7682
<dependency>
7783
<groupId>io.github.embeddedkafka</groupId>
7884
<artifactId>embedded-kafka_${scala.compat.version}</artifactId>

kotlin-spark-api/3.2/src/test/kotlin/org/jetbrains/kotlinx/spark/api/KafkaHelper.kt

Lines changed: 0 additions & 148 deletions
This file was deleted.

kotlin-spark-api/3.2/src/test/kotlin/org/jetbrains/kotlinx/spark/api/KafkaStreamingTest.kt

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -20,7 +20,11 @@
2020
package org.jetbrains.kotlinx.spark.api
2121

2222
import io.kotest.core.Tag
23+
import io.kotest.core.extensions.install
2324
import io.kotest.core.spec.style.ShouldSpec
25+
import io.kotest.extensions.testcontainers.TestContainerExtension
26+
import io.kotest.extensions.testcontainers.kafka.createProducer
27+
import io.kotest.extensions.testcontainers.kafka.createStringStringProducer
2428
import io.kotest.matchers.collections.shouldBeIn
2529
import org.apache.kafka.clients.consumer.ConsumerConfig
2630
import org.apache.kafka.clients.consumer.ConsumerRecord
@@ -32,27 +36,33 @@ import org.apache.spark.streaming.kafka010.ConsumerStrategies
3236
import org.apache.spark.streaming.kafka010.KafkaUtils
3337
import org.apache.spark.streaming.kafka010.LocationStrategies
3438
import org.jetbrains.kotlinx.spark.api.tuples.*
39+
import org.testcontainers.containers.KafkaContainer
40+
import org.testcontainers.utility.DockerImageName
3541
import java.io.Serializable
42+
import java.util.concurrent.TimeUnit
3643

3744
object Kafka : Tag()
3845

3946
class KafkaStreamingTest : ShouldSpec({
4047

41-
// making sure it can be skipped on Github actions since it times out
4248
tags(Kafka)
49+
val kafka = install(TestContainerExtension(KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:6.2.4")))) {
50+
withEmbeddedZookeeper()
51+
withEnv("KAFKA_AUTO_CREATE_TOPICS_ENABLE", "true")
52+
}
53+
4354

4455
context("kafka") {
45-
val port = 9092
46-
val broker = "localhost:$port"
4756
val topic1 = "test1"
4857
val topic2 = "test2"
49-
val kafkaListener = EmbeddedKafkaListener(port)
50-
listener(kafkaListener)
5158

5259
should("support kafka streams") {
53-
val producer = kafkaListener.stringStringProducer()
54-
producer.send(ProducerRecord(topic1, "Hello this is a test test test"))
55-
producer.send(ProducerRecord(topic2, "This is also also a test test something"))
60+
val producer = kafka.createStringStringProducer()
61+
listOf(
62+
producer.send(ProducerRecord(topic1, "Hello this is a test test test")),
63+
producer.send(ProducerRecord(topic2, "This is also also a test test something")),
64+
)
65+
.map { it.get(10, TimeUnit.SECONDS) }
5666
producer.close()
5767

5868
withSparkStreaming(
@@ -62,7 +72,7 @@ class KafkaStreamingTest : ShouldSpec({
6272
) {
6373

6474
val kafkaParams: Map<String, Serializable> = mapOf(
65-
ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG to broker,
75+
ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG to kafka.bootstrapServers,
6676
ConsumerConfig.GROUP_ID_CONFIG to "consumer-group",
6777
ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG to StringDeserializer::class.java,
6878
ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG to StringDeserializer::class.java,
@@ -113,4 +123,4 @@ class KafkaStreamingTest : ShouldSpec({
113123
}
114124

115125
}
116-
})
126+
})

kotlin-spark-api/3.2/src/test/kotlin/org/jetbrains/kotlinx/spark/api/ProjectConfig.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -24,7 +24,6 @@ import io.kotest.extensions.allure.AllureTestReporter
2424

2525
@Suppress("unused")
2626
object ProjectConfig : AbstractProjectConfig() {
27-
override fun listeners() = super.listeners() + AllureTestReporter(true)
28-
2927
override fun extensions() = super.extensions() + AllureTestReporter(true)
28+
3029
}

pom.xml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,35 @@
1010
<packaging>pom</packaging>
1111

1212
<properties>
13-
<kotlin.version>1.6.21</kotlin.version>
14-
<dokka.version>1.6.10</dokka.version>
1513
<atrium.version>0.17.0</atrium.version>
16-
<kotest.version>5.2.3</kotest.version>
17-
<kotest-extensions-allure.version>1.1.0</kotest-extensions-allure.version>
14+
<dokka.version>1.6.10</dokka.version>
1815
<embedded-kafka.version>3.1.0</embedded-kafka.version>
19-
<spark3.version>3.2.1</spark3.version>
16+
<hadoop.version>3.3.1</hadoop.version>
17+
<kotest-extensions-allure.version>1.1.0</kotest-extensions-allure.version>
18+
<kotest-extensions-testcontainers.version>1.3.1</kotest-extensions-testcontainers.version>
19+
<kotest.version>5.2.3</kotest.version>
2020
<kotlin-jupyter-api.version>0.11.0-83</kotlin-jupyter-api.version>
21+
<kotlin.version>1.6.21</kotlin.version>
2122
<kotlinx.html.version>0.7.5</kotlinx.html.version>
22-
<hadoop.version>3.3.1</hadoop.version>
23+
<spark3.version>3.2.1</spark3.version>
2324

2425
<!-- Plugin versions -->
2526
<allure-maven.version>2.10.0</allure-maven.version>
2627
<jacoco-maven-plugin.version>0.8.7</jacoco-maven-plugin.version>
2728
<klaxon.version>5.5</klaxon.version>
29+
<kotlin.code.style>official</kotlin.code.style>
2830
<license-maven-plugin.version>2.0.0</license-maven-plugin.version>
2931
<maven-assembly-plugin.version>3.3.0</maven-assembly-plugin.version>
30-
<maven-gpg-plugin.version>3.0.1</maven-gpg-plugin.version>
32+
<maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
3133
<maven-deploy-plugin.version>3.0.0-M1</maven-deploy-plugin.version>
3234
<maven-enforcer-plugin.version>3.0.0-M3</maven-enforcer-plugin.version>
35+
<maven-gpg-plugin.version>3.0.1</maven-gpg-plugin.version>
3336
<maven-jar-plugin.version>3.2.0</maven-jar-plugin.version>
3437
<maven-site-plugin.version>3.9.1</maven-site-plugin.version>
3538
<maven-source-plugin.version>3.2.1</maven-source-plugin.version>
36-
<maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
3739
<maven-surefire-plugin.version>3.0.0-M6</maven-surefire-plugin.version>
3840
<nexus-staging-plugin.version>1.6.8</nexus-staging-plugin.version>
3941
<scala-maven-plugin.version>4.5.6</scala-maven-plugin.version>
40-
<kotlin.code.style>official</kotlin.code.style>
4142
</properties>
4243

4344
<modules>

0 commit comments

Comments
 (0)