-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow specifying dispatcher for KafkaConsumerSubscriptionHealthCheck
- Loading branch information
Showing
3 changed files
with
90 additions
and
8 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
75 changes: 75 additions & 0 deletions
75
...fka/src/test/kotlin/com/sksamuel/cohort/kafka/KafkaConsumerSubscriptionHealthCheckTest.kt
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,75 @@ | ||
package com.sksamuel.cohort.kafka | ||
|
||
import com.sksamuel.cohort.HealthStatus | ||
import io.kotest.assertions.timing.continually | ||
import io.kotest.assertions.timing.eventually | ||
import io.kotest.core.extensions.install | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.extensions.testcontainers.kafka.KafkaContainerExtension | ||
import io.kotest.extensions.testcontainers.kafka.admin | ||
import io.kotest.extensions.testcontainers.kafka.consumer | ||
import io.kotest.extensions.testcontainers.kafka.producer | ||
import io.kotest.matchers.shouldBe | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.asCoroutineDispatcher | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.isActive | ||
import kotlinx.coroutines.launch | ||
import org.apache.kafka.clients.admin.NewTopic | ||
import org.apache.kafka.clients.producer.ProducerRecord | ||
import org.apache.kafka.common.utils.Bytes | ||
import org.testcontainers.containers.KafkaContainer | ||
import org.testcontainers.utility.DockerImageName | ||
import java.time.Duration | ||
import java.util.concurrent.Executors | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
class KafkaConsumerSubscriptionHealthCheckTest : FunSpec({ | ||
|
||
val kafka = install(KafkaContainerExtension(KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka")))) | ||
|
||
test("health check should pass while the consumer is subscribed") { | ||
|
||
kafka.admin().createTopics(listOf(NewTopic("topic1234", 1, 1))).all().get() | ||
val consumer = kafka.consumer() | ||
consumer.subscribe(listOf("topic1234")) | ||
|
||
val healthcheck = KafkaConsumerSubscriptionHealthCheck(consumer) | ||
healthcheck.check().status shouldBe HealthStatus.Healthy | ||
|
||
consumer.close() | ||
healthcheck.check().status shouldBe HealthStatus.Unhealthy | ||
} | ||
|
||
test("health check should support running on the kafka thread") { | ||
|
||
kafka.admin().createTopics(listOf(NewTopic("topic4412", 1, 1))).all().get() | ||
val consumer = kafka.consumer() | ||
kafka.producer().use { | ||
it.send(ProducerRecord("topic4412", Bytes.wrap(byteArrayOf()), Bytes.wrap(byteArrayOf()))) | ||
} | ||
|
||
consumer.subscribe(listOf("topic4412")) | ||
|
||
val dispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher() | ||
val job = launch(dispatcher) { | ||
while (isActive) { | ||
consumer.poll(Duration.ofMillis(100)) // keep a lock on the consumer | ||
delay(100) | ||
} | ||
} | ||
job.invokeOnCompletion { consumer.close() } | ||
|
||
val healthcheck = KafkaConsumerSubscriptionHealthCheck(consumer, dispatcher) | ||
continually(3.seconds) { | ||
healthcheck.check().status shouldBe HealthStatus.Healthy | ||
} | ||
|
||
job.cancel() | ||
|
||
eventually(3.seconds) { | ||
healthcheck.check().status shouldBe HealthStatus.Unhealthy | ||
} | ||
} | ||
|
||
}) |
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