Completed for a grade of four
This commit is contained in:
@@ -191,6 +191,7 @@ class DependencyFailureTest {
|
||||
@Test
|
||||
fun `without PostgreSQL requests fail with a bounded 503`() {
|
||||
val database = TestDatabase.create().also { it.start() }
|
||||
TestDatabase.awaitConnectable(database)
|
||||
|
||||
try {
|
||||
runApp(database = database, databaseConnectionTimeoutMs = 2000) { client ->
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.mtusi.messenger.main.support
|
||||
|
||||
import org.testcontainers.containers.PostgreSQLContainer
|
||||
import java.sql.DriverManager
|
||||
import java.sql.SQLException
|
||||
import org.testcontainers.utility.DockerImageName
|
||||
|
||||
/**
|
||||
@@ -20,6 +22,22 @@ object TestDatabase {
|
||||
/**
|
||||
* A separate, not yet started container for tests that stop the database.
|
||||
*/
|
||||
/**
|
||||
* Docker Desktop may refuse the first connections through a fresh port mapping for a moment after the container
|
||||
* reported ready; tests with a short pool timeout wait for a real connection first.
|
||||
*/
|
||||
fun awaitConnectable(container: PostgreSQLContainer<*>, timeoutMs: Long = 15000) {
|
||||
val deadline = System.currentTimeMillis() + timeoutMs
|
||||
while (true) {
|
||||
try {
|
||||
DriverManager.getConnection(container.jdbcUrl, container.username, container.password).use { return }
|
||||
} catch (e: SQLException) {
|
||||
if (System.currentTimeMillis() > deadline) throw e
|
||||
Thread.sleep(200)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun create(): PostgreSQLContainer<*> {
|
||||
return PostgreSQLContainer(DockerImageName.parse("postgres:17.6-alpine3.22").asCompatibleSubstituteFor("postgres"))
|
||||
.withDatabaseName(NAME)
|
||||
|
||||
Reference in New Issue
Block a user