Skip to content

Instantly share code, notes, and snippets.

@Szer
Last active September 15, 2022 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Szer/cfa677f61ec49aefe03c74cab1a0165f to your computer and use it in GitHub Desktop.
Save Szer/cfa677f61ec49aefe03c74cab1a0165f to your computer and use it in GitHub Desktop.
package com.thriveglobal.connect.postgresql.junit
import com.thriveglobal.connect.postgresql.Env
import com.thriveglobal.env.CommonEnv
import java.time.Instant
import org.flywaydb.core.Flyway
import org.flywaydb.core.api.output.MigrateResult
import org.junit.jupiter.api.Tag
import org.junit.platform.engine.TestTag
import org.junit.platform.launcher.TestExecutionListener
import org.junit.platform.launcher.TestPlan
import org.testcontainers.containers.PostgreSQLContainer
const val pgContainer = "pg-container"
private val pgContainerTag = TestTag.create(pgContainer)
@Tag(pgContainer)
class PgContainerListener : TestExecutionListener {
private val postgresqlVersion = "11.11-alpine"
companion object {
private var isInitialized = false
private var exn: Throwable? = null
private var server: PostgreSQLContainer<*>? = null
private var connectionUri = ""
fun getConnectionUri(): String {
val now = Instant.now()
while (!isInitialized) {
Thread.sleep(100)
if (Instant.now().isAfter(now.plusSeconds(60))) {
throw RuntimeException("Timeout waiting for PG container to initialize")
}
if (exn != null) {
throw exn!!
}
}
return connectionUri
}
fun getVertxConnectionUri(): String {
return getConnectionUri().substringAfter("jdbc:")
}
}
override fun testPlanExecutionStarted(testPlan: TestPlan) {
val postgresTagCount = testPlan.countTestIdentifiers { test ->
test.tags.contains(pgContainerTag)
}
if (postgresTagCount > 0) {
if (CommonEnv.isCi) {
connectionUri =
"jdbc:postgresql://${Env.dbHost}:${Env.dbPort}/${Env.dbName}?ssl=${Env.dbSsl}&user=${Env.dbUser}&password=${Env.dbSecret}"
isInitialized = true
} else {
startServer(postgresqlVersion)?.let {
isInitialized = true
}
}
}
}
override fun testPlanExecutionFinished(testPlan: TestPlan?) {
runCatching {
if (server != null) {
server!!.stop()
}
}
}
private fun migrateFlyway(): MigrateResult {
val flyway = Flyway
.configure()
.dataSource(
connectionUri,
Env.dbUser,
Env.dbSecret
)
.schemas("public")
.locations("filesystem:./db/migrations", "classpath:/db/testdata")
.baselineOnMigrate(true)
.baselineVersion("0")
.load()
return flyway.migrate()
}
@Suppress("UPPER_BOUND_VIOLATED_WARNING")
@Synchronized
fun startServer(databaseVersion: String?): PostgreSQLContainer<*>? {
val instance = PostgreSQLContainer<PostgreSQLContainer<*>>("postgres:$databaseVersion")
.withUsername("postgres")
.withPassword("postgres")
.withInitScript("db/init_script.sql")
return try {
instance.start()
connectionUri = "${instance.jdbcUrl}&user=${Env.dbUser}&password=${Env.dbSecret}"
val migration = migrateFlyway()
if (migration.warnings.isNotEmpty())
throw Error(migration.warnings.joinToString("\n"))
server = instance
instance
} catch (e: Throwable) {
exn = e
null
}
}
}
package com.thriveglobal.identity.graphapi.storage
import com.thriveglobal.connect.postgresql.junit.PgContainerListener
import com.thriveglobal.connect.postgresql.junit.pgContainer
import io.vertx.pgclient.PgConnectOptions
import io.vertx.pgclient.PgPool
import io.vertx.sqlclient.PoolOptions
import java.util.*
import kotlin.test.*
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test
@Tag(pgContainer)
class ScopeStorageTests {
private val pool = run {
val dbOptions = PgConnectOptions.fromUri(
PgContainerListener.getConnectionUri().removePrefix("jdbc:")
)
val poolOpts = PoolOptions().setMaxSize(Env.dbPoolSize)
PgPool.pool(dbOptions, poolOpts)
}
@Test
fun `super test with Postgres`() {
// use pool here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment