Skip to content

Commit

Permalink
bytter til parallelle db-tester
Browse files Browse the repository at this point in the history
Co-authored-by: Håkon Arneng Holmstedt <hakon.arneng.holmstedt@nav.no>
  • Loading branch information
davidsteinsland and hholmste committed Nov 13, 2024
1 parent f9f986f commit 0188fd2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 134 deletions.
9 changes: 5 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,8 @@ dependencies {
testImplementation("org.wiremock:wiremock:$wiremockVersion")

testImplementation("org.awaitility:awaitility:$awaitilityVersion")
testImplementation("org.testcontainers:postgresql:$testcontainersVersion")
testImplementation("org.testcontainers:testcontainers:$testcontainersVersion")
testImplementation("org.testcontainers:junit-jupiter:$testcontainersVersion")


testImplementation("com.github.navikt.tbd-libs:postgres-testdatabaser:$tbdLibsVersion")
testImplementation("com.github.navikt.tbd-libs:naisful-test-app:$tbdLibsVersion")
testImplementation("org.skyscreamer:jsonassert:$jsonAssertVersion")

Expand Down Expand Up @@ -78,6 +75,10 @@ tasks {
testLogging {
events("passed", "skipped", "failed")
}
systemProperty("junit.jupiter.execution.parallel.enabled", "true")
systemProperty("junit.jupiter.execution.parallel.mode.default", "concurrent")
systemProperty("junit.jupiter.execution.parallel.config.strategy", "fixed")
systemProperty("junit.jupiter.execution.parallel.config.fixed.parallelism", "8")
}

named<Jar>("jar") {
Expand Down
15 changes: 8 additions & 7 deletions src/test/kotlin/no/nav/helse/spokelse/AbstractE2ETest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.github.navikt.tbd_libs.naisful.test.naisfulTestApp
import com.github.navikt.tbd_libs.test_support.TestDataSource
import com.github.tomakehurst.wiremock.WireMockServer
import com.github.tomakehurst.wiremock.client.WireMock
import com.github.tomakehurst.wiremock.core.WireMockConfiguration
Expand All @@ -19,6 +20,7 @@ import no.nav.helse.spokelse.gamleutbetalinger.GamleUtbetalingerDao
import no.nav.helse.spokelse.tbdutbetaling.TbdUtbetalingDao
import no.nav.helse.spokelse.tbdutbetaling.TbdUtbetalingApi
import org.awaitility.Awaitility
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.BeforeEach
Expand All @@ -31,7 +33,6 @@ import java.util.*
import java.util.concurrent.TimeUnit
import javax.sql.DataSource

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
internal abstract class AbstractE2ETest {
private val env = mapOf(
"AZURE_OPENID_CONFIG_TOKEN_ENDPOINT" to "http://localhost",
Expand All @@ -51,27 +52,27 @@ internal abstract class AbstractE2ETest {
audience = "spokelse_azure_ad_app_id"
)

protected lateinit var dataSource: DataSource
protected lateinit var testDataSource: TestDataSource
protected val dataSource: DataSource get() = testDataSource.ds
protected lateinit var dokumentDao: DokumentDao
protected lateinit var utbetaltDao: UtbetaltDao
protected lateinit var gamleUtbetalingerDao: GamleUtbetalingerDao
protected lateinit var lagreVedtakDao: LagreVedtakDao
protected lateinit var tbdUtbetalingDao: TbdUtbetalingDao

@BeforeAll
@BeforeEach
fun setup() {
PgDb.start()
dataSource = PgDb.connection()
testDataSource = databaseContainer.nyTilkobling()
dokumentDao = DokumentDao(dataSource)
utbetaltDao = UtbetaltDao(dataSource)
gamleUtbetalingerDao = GamleUtbetalingerDao(::dataSource)
lagreVedtakDao = LagreVedtakDao(dataSource)
tbdUtbetalingDao = TbdUtbetalingDao(::dataSource)
}

@BeforeEach
@AfterEach
protected fun reset() {
PgDb.reset()
databaseContainer.droppTilkobling(testDataSource)
}

protected fun assertApiRequest(
Expand Down
126 changes: 3 additions & 123 deletions src/test/kotlin/no/nav/helse/spokelse/PgDb.kt
Original file line number Diff line number Diff line change
@@ -1,126 +1,6 @@
package no.nav.helse.spokelse

import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import kotliquery.queryOf
import kotliquery.sessionOf
import org.flywaydb.core.Flyway
import org.intellij.lang.annotations.Language
import org.testcontainers.containers.PostgreSQLContainer
import java.time.Duration
import javax.sql.DataSource
import com.github.navikt.tbd_libs.test_support.CleanupStrategy
import com.github.navikt.tbd_libs.test_support.DatabaseContainers

internal object PgDb {

private var state: DBState = NotStarted
private val postgres = PostgreSQLContainer<Nothing>("postgres:14")
private lateinit var dataSource: DataSource

fun start(): PgDb {
state.start(this)
return this
}

fun reset() {
state.reset(this)
}

fun hardReset() {
state.hardReset(this)
}

fun config() = state.config(this)
fun connection() = state.connection(this)
private val DataSource.flyway get() = Flyway.configure().dataSource(this).cleanDisabled(false).load()

private fun stop(): PgDb {
state.stop(this)
return this
}

private fun startDatbase() {
postgres.start()
dataSource = HikariDataSource(config())
createSchema(connection())
Runtime.getRuntime().addShutdownHook(Thread(this::stop))
}

private fun createSchema(dataSource: DataSource) {
dataSource.flyway.migrate()
sessionOf(dataSource).use { it.run(queryOf(truncateTablesSql).asExecute) }
}

private fun resetSchema() {
sessionOf(connection()).use { it.run(queryOf("SELECT truncate_tables();").asExecute) }
}

private fun stopDatabase() {
postgres.stop()
}

private interface DBState {
fun config(db: PgDb): HikariConfig {
throw IllegalStateException("Cannot create config in state ${this::class.simpleName}")
}
fun connection(db: PgDb): DataSource {
throw IllegalStateException("Cannot create connection in state ${this::class.simpleName}")
}
fun start(db: PgDb) {}
fun stop(db: PgDb) {}
fun reset(db: PgDb) {}
fun hardReset(db: PgDb) {}
}

private object NotStarted : DBState {
override fun start(db: PgDb) {
state = Started
db.startDatbase()
}
}

private object Started : DBState {
override fun stop(db: PgDb) {
db.state = NotStarted
db.stopDatabase()
}

override fun config(db: PgDb) = HikariConfig().apply {
jdbcUrl = db.postgres.jdbcUrl
username = db.postgres.username
password = db.postgres.password
maximumPoolSize = 2
connectionTimeout = Duration.ofSeconds(5).toMillis()
initializationFailTimeout = Duration.ofMinutes(1).toMillis()
}

override fun connection(db: PgDb): DataSource {
return db.dataSource
}

override fun reset(db: PgDb) {
db.resetSchema()
}

override fun hardReset(db: PgDb) {
val ds = connection()
ds.flyway.clean()
createSchema(ds)
}

}

@Language("PostgreSQL")
private val truncateTablesSql = """
CREATE OR REPLACE FUNCTION truncate_tables() RETURNS void AS ${'$'}${'$'}
DECLARE
statements CURSOR FOR
SELECT tablename FROM pg_tables
WHERE schemaname = 'public' AND tablename NOT LIKE 'flyway%';
BEGIN
FOR stmt IN statements LOOP
EXECUTE 'TRUNCATE TABLE ' || quote_ident(stmt.tablename) || ' CASCADE;';
END LOOP;
END;
${'$'}${'$'} LANGUAGE plpgsql;
"""
}
val databaseContainer = DatabaseContainers.container("spokelse", CleanupStrategy.tables("alle_annulleringer, annullering, gamle_utbetalinger, hendelse, old_utbetaling, old_vedtak, oppdrag, tbdutbetaling_melding, tbdutbetaling_utbetaling, tbdutbetaling_utbetalingslinje, utbetaling, vedtak, vedtak_utbetalingsref"))

0 comments on commit 0188fd2

Please sign in to comment.