fix testApplicationWithConfig

This commit is contained in:
2025-04-05 04:44:48 +02:00
parent e1529ee52c
commit fd954e631d
7 changed files with 60 additions and 56 deletions

View File

@@ -5,7 +5,6 @@ import eventDemo.business.entity.Deck
import eventDemo.configuration.business.configureGameListener
import eventDemo.configuration.injection.appKoinModule
import eventDemo.configuration.ktor.configuration
import io.kotest.engine.runBlocking
import io.ktor.server.config.ApplicationConfig
import io.ktor.server.testing.ApplicationTestBuilder
import io.ktor.server.testing.testApplication
@@ -13,6 +12,7 @@ import io.ktor.utils.io.KtorDsl
import org.koin.core.Koin
import org.koin.core.module.KoinApplicationDslMarker
import org.koin.dsl.koinApplication
import org.koin.ktor.ext.getKoin
import redis.clients.jedis.UnifiedJedis
import javax.sql.DataSource
@@ -24,22 +24,31 @@ fun Deck.allCards(): Set<Card> =
@KoinApplicationDslMarker
suspend fun <T> testKoinApplicationWithConfig(block: suspend Koin.() -> T): T =
koinApplication { modules(appKoinModule(ApplicationConfig("application.conf").configuration())) }.koin.block()
koinApplication { modules(appKoinModule(ApplicationConfig("application.conf").configuration())) }
.koin
.apply {
cleanDataTest()
configureGameListener()
}.block()
@KtorDsl
suspend fun testApplicationWithConfig(block: suspend ApplicationTestBuilder.(koin: Koin) -> Unit) {
fun testApplicationWithConfig(
configBuilder: Koin.() -> Unit = {},
block: suspend ApplicationTestBuilder.() -> Unit,
) {
testApplication {
val conf = ApplicationConfig("application.conf")
environment {
config = conf
}
val koin = koinApplication { modules(appKoinModule(conf.configuration())) }.koin
koin.cleanDataTest()
koin.configureGameListener()
runBlocking {
block(koin)
application {
val koin = getKoin()
koin.cleanDataTest()
koin.configureGameListener()
configBuilder(koin)
}
block()
}
}

View File

@@ -44,15 +44,18 @@ class GameListRouteTest :
}
test("/games return a game with status OPENING") {
testApplicationWithConfig { koin ->
val gameId = GameId()
val player1 = Player(name = "Nikola")
val eventHandler = koin.get<GameEventHandler>()
runBlocking {
eventHandler.handle(gameId) { NewPlayerEvent(gameId, player1, it) }
}
val gameId = GameId()
val player1 = Player(name = "Nikola")
testApplicationWithConfig(
{
runBlocking {
get<GameEventHandler>()
.handle(gameId) {
NewPlayerEvent(gameId, player1, it)
}
}
},
) {
// Wait until the projection is created
eventually(3.seconds) {
httpClient()
@@ -73,11 +76,11 @@ class GameListRouteTest :
}
test("/games return a game with status IS_STARTED") {
testApplicationWithConfig { koin ->
val gameId = GameId()
val player1 = Player(name = "Nikola")
val player2 = Player(name = "Einstein")
val eventHandler = koin.get<GameEventHandler>()
val gameId = GameId()
val player1 = Player(name = "Nikola")
val player2 = Player(name = "Einstein")
testApplicationWithConfig({
val eventHandler = get<GameEventHandler>()
runBlocking {
eventHandler.handle(gameId) { NewPlayerEvent(gameId, player1, it) }
eventHandler.handle(gameId) { NewPlayerEvent(gameId, player2, it) }
@@ -92,7 +95,7 @@ class GameListRouteTest :
)
}
}
}) {
httpClient()
.get("/games") {
withAuth(player1)

View File

@@ -21,7 +21,6 @@ import eventDemo.business.notification.PlayerAsPlayACardNotification
import eventDemo.business.notification.PlayerWasReadyNotification
import eventDemo.business.notification.TheGameWasStartedNotification
import eventDemo.business.notification.WelcomeToTheGameNotification
import eventDemo.configuration.business.configureGameListener
import eventDemo.libs.event.projection.ProjectionSnapshotRepositoryInMemory
import eventDemo.testKoinApplicationWithConfig
import io.kotest.assertions.nondeterministic.until
@@ -177,7 +176,6 @@ class GameSimulationTest :
val commandHandler by inject<GameCommandHandler>()
val eventStore by inject<GameEventStore>()
val playerNotificationListener by inject<PlayerNotificationListener>()
configureGameListener()
playerNotificationListener.startListening(player1, gameId) { channelNotification1.trySendBlocking(it) }
playerNotificationListener.startListening(player2, gameId) { channelNotification2.trySendBlocking(it) }

View File

@@ -48,14 +48,13 @@ class GameStateRouteTest :
}
test("/games/{id}/card/last") {
testApplicationWithConfig { koin ->
val gameId = GameId()
val player1 = Player(name = "Nikola")
val player2 = Player(name = "Einstein")
var lastPlayedCard: Card? = null
val eventHandler = koin.get<GameEventHandler>()
val stateRepo = koin.get<GameStateRepository>()
val gameId = GameId()
val player1 = Player(name = "Nikola")
val player2 = Player(name = "Einstein")
var lastPlayedCard: Card? = null
testApplicationWithConfig({
val eventHandler = get<GameEventHandler>()
val stateRepo = get<GameStateRepository>()
runBlocking {
eventHandler.handle(gameId) { NewPlayerEvent(gameId, player1, it) }
@@ -89,7 +88,7 @@ class GameStateRouteTest :
}
delay(100)
}
}) {
httpClient()
.get("/games/$gameId/card/last") {
withAuth(player1)

View File

@@ -8,7 +8,6 @@ import eventDemo.business.event.projection.projectionListener.PlayerNotification
import eventDemo.business.notification.CommandSuccessNotification
import eventDemo.business.notification.Notification
import eventDemo.business.notification.WelcomeToTheGameNotification
import eventDemo.configuration.business.configureGameListener
import eventDemo.testKoinApplicationWithConfig
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.collections.shouldContain
@@ -26,11 +25,10 @@ import kotlin.time.Duration.Companion.seconds
class GameCommandHandlerTest :
FunSpec({
test("handle a command should execute the command") {
withTimeout(1.seconds) {
withTimeout(5.seconds) {
testKoinApplicationWithConfig {
configureGameListener()
val commandHandler by inject<GameCommandHandler>()
val notificationListener by inject<PlayerNotificationListener>()
val commandHandler = get<GameCommandHandler>()
val notificationListener = get<PlayerNotificationListener>()
val gameId = GameId()
val player = Player("Tesla")
val channelCommand = Channel<GameCommand>(Channel.BUFFERED)

View File

@@ -6,7 +6,7 @@ import eventDemo.business.event.GameEventHandler
import eventDemo.business.event.event.NewPlayerEvent
import eventDemo.business.event.projection.gameState.GameState
import eventDemo.business.event.projection.gameState.GameStateRepository
import eventDemo.testApplicationWithConfig
import eventDemo.testKoinApplicationWithConfig
import io.kotest.assertions.nondeterministic.eventually
import io.kotest.assertions.nondeterministic.eventuallyConfig
import io.kotest.core.spec.style.FunSpec
@@ -29,9 +29,9 @@ class GameStateRepositoryTest :
test("GameStateRepository should build the projection when a new event occurs") {
val aggregateId = GameId()
testApplicationWithConfig { koin ->
val repo = koin.get<GameStateRepository>()
val eventHandler = koin.get<GameEventHandler>()
testKoinApplicationWithConfig {
val repo = get<GameStateRepository>()
val eventHandler = get<GameEventHandler>()
eventHandler
.handle(aggregateId) { NewPlayerEvent(aggregateId = aggregateId, player = player1, version = it) }
.also { event ->
@@ -50,10 +50,10 @@ class GameStateRepositoryTest :
test("get should build the last version of the state") {
val aggregateId = GameId()
testApplicationWithConfig { koin ->
val repo = koin.get<GameStateRepository>()
val eventHandler = koin.get<GameEventHandler>()
val projectionBus = koin.get<GameProjectionBus>()
testKoinApplicationWithConfig {
val repo = get<GameStateRepository>()
val eventHandler = get<GameEventHandler>()
val projectionBus = get<GameProjectionBus>()
var state: GameState? = null
projectionBus.subscribe {
@@ -86,9 +86,9 @@ class GameStateRepositoryTest :
test("getUntil should build the state until the event") {
repeat(10) {
val aggregateId = GameId()
testApplicationWithConfig { koin ->
val repo = koin.get<GameStateRepository>()
val eventHandler = koin.get<GameEventHandler>()
testKoinApplicationWithConfig {
val repo = get<GameStateRepository>()
val eventHandler = get<GameEventHandler>()
val event1 =
eventHandler
@@ -115,9 +115,9 @@ class GameStateRepositoryTest :
test("getUntil should be concurrently secure") {
val aggregateId = GameId()
testApplicationWithConfig { koin ->
val repo = koin.get<GameStateRepository>()
val eventHandler = koin.get<GameEventHandler>()
testKoinApplicationWithConfig {
val repo = get<GameStateRepository>()
val eventHandler = get<GameEventHandler>()
(1..10)
.map { r ->

View File

@@ -1,6 +1,5 @@
package eventDemo.libs.event
import eventDemo.cleanEventSource
import eventDemo.testKoinApplicationWithConfig
import io.kotest.core.spec.style.FunSpec
import io.kotest.datatest.withData
@@ -13,7 +12,6 @@ import kotlinx.coroutines.launch
import kotlinx.serialization.json.Json
import org.junit.jupiter.api.assertNull
import org.junit.jupiter.api.assertThrows
import javax.sql.DataSource
import kotlin.test.assertNotNull
@DelicateCoroutinesApi
@@ -29,7 +27,6 @@ class EventStreamTest :
suspend fun eventStreams(): List<EventStream<EventXTest, IdTest>> =
testKoinApplicationWithConfig {
get<DataSource>().cleanEventSource()
listOf(
EventStreamInMemory(IdTest()),
EventStreamInPostgresql(