-
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.
Added test for DataSourceConnectionWarmup
- Loading branch information
Showing
4 changed files
with
57 additions
and
22 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
cohort-core/src/main/kotlin/com/sksamuel/cohort/db/DataSourceConnectionWarmup.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,28 @@ | ||
package com.sksamuel.cohort.db | ||
|
||
import com.sksamuel.cohort.Warmup | ||
import javax.sql.DataSource | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
/** | ||
* A Cohort [Warmup] that warms a [DataSource] by executing a query. | ||
* | ||
* Uses the JDBC4 method isValid(timeout) with the given [timeout] to check that the connection | ||
* returned is open and usable. | ||
*/ | ||
class DataSourceConnectionWarmup( | ||
private val ds: DataSource, | ||
private val query: String, | ||
private val timeout: Duration = 1.seconds, | ||
) : Warmup { | ||
|
||
override val name: String = "datasource_connection_warmup" | ||
|
||
override suspend fun warm(iteration: Int) { | ||
ds.connection.use { conn -> | ||
conn.isValid(timeout.inWholeSeconds.toInt()) | ||
conn.createStatement().use { it.execute(query) } | ||
} | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
cohort-core/src/test/kotlin/com/sksamuel/cohort/db/DataSourceConnectionWarmupTest.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,23 @@ | ||
package com.sksamuel.cohort.db | ||
|
||
import com.sksamuel.cohort.WarmupRegistry | ||
import com.sksamuel.cohort.WarmupState | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.matchers.shouldBe | ||
import kotlinx.coroutines.delay | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
class DataSourceConnectionWarmupTest : FunSpec({ | ||
|
||
test("happy path") { | ||
val ds = createHikariDS() | ||
ds.connection.use { it.createStatement().executeUpdate("CREATE TABLE foo (id int)") } | ||
val warmups = WarmupRegistry { | ||
register(DataSourceConnectionWarmup(ds, "select * from foo"), 3.seconds) | ||
} | ||
warmups.state() shouldBe WarmupState.Running | ||
delay(5.seconds) | ||
warmups.state() shouldBe WarmupState.Completed | ||
} | ||
|
||
}) |
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