Skip to content

Commit

Permalink
egen cleanup-strategi
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsteinsland committed Feb 7, 2024
1 parent e5a8442 commit 41e436f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import java.util.concurrent.TimeUnit
class DatabaseContainer(
private val appnavn: String,
private val poolSize: Int,
private val cleanUpTables: String? = null,
private val cleanUpTables: CleanupStrategy? = null,
private val maxHikariPoolSize: Int = 2
) {
private val instance by lazy {
Expand Down Expand Up @@ -49,13 +49,13 @@ class DatabaseContainer(
throw RuntimeException("Ventet i ${timeout.toMillis()} millisekunder uten å få en ledig database")
}

private fun opprettTilkoblinger(cleanUpTables: String?, maxHikariPoolSize: Int) = runBlocking(Dispatchers.IO) {
private fun opprettTilkoblinger(cleanUpTables: CleanupStrategy?, maxHikariPoolSize: Int) = runBlocking(Dispatchers.IO) {
(1..poolSize)
.map { async { opprettTilkobling("testdb_$it", cleanUpTables, maxHikariPoolSize) } }
.awaitAll()
}

private fun opprettTilkobling(dbnavn: String, cleanUpTables: String? = null, maxHikariPoolSize: Int): TestDataSource {
private fun opprettTilkobling(dbnavn: String, cleanUpTables: CleanupStrategy?, maxHikariPoolSize: Int): TestDataSource {
opprettDatabase(dbnavn)
instance.withDatabaseName(dbnavn)
return TestDataSource(dbnavn, HikariConfig().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object DatabaseContainers {

// gjenbruker containers med samme navn for å unngå
// å spinne opp mange containers
fun container(appnavn: String, cleanUpTables: String? = null, maxHikariPoolSize: Int = 2, databasePoolSize: Int = POOL_SIZE): DatabaseContainer {
fun container(appnavn: String, cleanUpTables: CleanupStrategy? = null, maxHikariPoolSize: Int = 2, databasePoolSize: Int = POOL_SIZE): DatabaseContainer {
return instances.getOrPut(appnavn) {
DatabaseContainer(appnavn, databasePoolSize, cleanUpTables, maxHikariPoolSize)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import java.time.Duration
import org.flywaydb.core.Flyway
import java.sql.Connection

class TestDataSource(
private val dbnavn: String,
config: HikariConfig,
private val cleanUpTables: String? = null, // komma-separert liste over tabeller som skal tømmes
private val cleanUpTables: CleanupStrategy? = null, // komma-separert liste over tabeller som skal tømmes
maxHikariPoolSize: Int = 2 // hvor stor hikari-poolen skal være
) {
private val migrationConfig = HikariConfig()
Expand Down Expand Up @@ -55,12 +56,25 @@ class TestDataSource(
}
println("Tømmer tabellene $cleanUpTables")
migrationDataSource.connection.use {
it.createStatement().execute("truncate table $cleanUpTables restart identity cascade;")
cleanUpTables.cleanup(it)
}
}
fun teardown(dropDatabase: (String) -> Unit) {
migrationDataSource.close()
dataSource.close()
dropDatabase(dbnavn)
}
}

fun interface CleanupStrategy {
fun cleanup(connection: Connection)

companion object {
// comma-separated list of tables
fun tables(tables: String): CleanupStrategy {
return CleanupStrategy {
it.createStatement().execute("truncate table $tables restart identity cascade;")
}
}
}
}

0 comments on commit 41e436f

Please sign in to comment.