Skip to content

Commit

Permalink
add coroutine driver support for mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesHepp committed May 15, 2024
1 parent c732f8f commit 224e301
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
2 changes: 2 additions & 0 deletions cohort-mongo/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
dependencies {
implementation(projects.cohortApi)
compileOnly(libs.mongodb.driver.sync)
compileOnly(libs.mongodb.driver.coroutine)
testImplementation(libs.testcontainers.mongodb)
testImplementation(libs.mongodb.driver.sync)
testImplementation(libs.mongodb.driver.coroutine)
}

apply("../publish.gradle.kts")
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.sksamuel.cohort.mongo
import com.mongodb.client.MongoClient
import com.sksamuel.cohort.HealthCheck
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.runInterruptible

class MongoConnectionHealthCheck private constructor(
Expand All @@ -19,4 +20,13 @@ class MongoConnectionHealthCheck private constructor(
}
}
)

constructor(
client: com.mongodb.kotlin.client.coroutine.MongoClient,
name: String = "mongo_connection",
) : this(
GenericMongoConnectionHealthCheck(name) {
client.listDatabaseNames().toList()
},
)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sksamuel.cohort.mongo

import com.mongodb.client.MongoClients
import com.mongodb.kotlin.client.coroutine.MongoClient
import com.sksamuel.cohort.HealthCheckResult
import io.kotest.core.extensions.install
import io.kotest.core.spec.style.FunSpec
Expand All @@ -11,17 +12,30 @@ import org.testcontainers.utility.DockerImageName

class MongoConnectionHealthCheckTest : FunSpec({

val container = MongoDBContainer(DockerImageName.parse("mongo:4.0.10"))
install(TestContainerExtension(container))
val container = MongoDBContainer(DockerImageName.parse("mongo:4.0.10"))
install(TestContainerExtension(container))

test("mongo health check should connect to mongo") {
val client = MongoClients.create(container.connectionString)
MongoConnectionHealthCheck(client).check() shouldBe HealthCheckResult.healthy("Connected to mongo instance (3 databases)")
}
test("mongo health check should connect to mongo") {
val client = MongoClients.create(container.connectionString)
MongoConnectionHealthCheck(
client,
).check() shouldBe HealthCheckResult.healthy("Connected to mongo instance (3 databases)")
}

test("mongo health check should fail if cannot connect") {
val client = MongoClients.create("mongodb://localhost:11111")
MongoConnectionHealthCheck(client).check().message.shouldBe("Could not connect to mongo instance")
}
test("mongo health check should fail if cannot connect") {
val client = MongoClients.create("mongodb://localhost:11111")
MongoConnectionHealthCheck(client).check().message.shouldBe("Could not connect to mongo instance")
}

test("mongo coroutine health check should connect to mongo") {
val client = MongoClient.create(container.connectionString)
MongoConnectionHealthCheck(
client,
).check() shouldBe HealthCheckResult.healthy("Connected to mongo instance (3 databases)")
}

test("mongo coroutine check should fail if cannot connect") {
val client = MongoClient.create("mongodb://localhost:11111")
MongoConnectionHealthCheck(client).check().message.shouldBe("Could not connect to mongo instance")
}
})
3 changes: 2 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ dependencyResolutionManagement {
library("dbcp2", "org.apache.commons:commons-dbcp2:2.11.0")
library("pulsar-client", "org.apache.pulsar:pulsar-client:2.11.2")

val mongo = "4.9.1"
val mongo = "5.1.0"
library("mongodb-driver-sync", "org.mongodb:mongodb-driver-sync:$mongo")
library("mongodb-driver-coroutine", "org.mongodb:mongodb-driver-kotlin-coroutine:$mongo")

library(
"elasticsearch-rest-high-level-client",
Expand Down

0 comments on commit 224e301

Please sign in to comment.