Completed for a grade of four

This commit is contained in:
2026-10-23 16:08:53 +03:00
parent 92c2fdc254
commit af184cabe3
50 changed files with 1297 additions and 92 deletions
@@ -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)