Create PlayerNotificationListenerTest

This commit is contained in:
2025-03-18 22:43:08 +01:00
parent c762f31449
commit c28bc00679
5 changed files with 41 additions and 7 deletions

View File

@@ -59,10 +59,9 @@ private fun DefaultWebSocketServerSession.runWebSocket(
}
playerNotificationListener.startListening(
{ outgoingFrameChannel.trySendBlocking(it) },
currentPlayer,
gameId,
)
) { outgoingFrameChannel.trySendBlocking(it) }
}
}

View File

@@ -32,9 +32,9 @@ class PlayerNotificationListener(
private val logger = KotlinLogging.logger {}
fun startListening(
outgoingNotification: (Notification) -> Unit,
currentPlayer: Player,
gameId: GameId,
outgoingNotification: (Notification) -> Unit,
) {
projectionBus.subscribe { currentState ->
if (currentState !is GameState) return@subscribe

View File

@@ -175,8 +175,8 @@ class GameSimulationTest :
val eventStore by inject<GameEventStore>()
val playerNotificationListener by inject<PlayerNotificationListener>()
ReactionListener(get(), get()).init()
playerNotificationListener.startListening({ channelNotification1.trySendBlocking(it) }, player1, gameId)
playerNotificationListener.startListening({ channelNotification2.trySendBlocking(it) }, player2, gameId)
playerNotificationListener.startListening(player1, gameId) { channelNotification1.trySendBlocking(it) }
playerNotificationListener.startListening(player2, gameId) { channelNotification2.trySendBlocking(it) }
GlobalScope.launch(Dispatchers.IO) {
commandHandler.handle(player1, gameId, channelCommand1, channelNotification1)

View File

@@ -34,10 +34,9 @@ class GameCommandHandlerTest :
val channelNotification = Channel<Notification>(Channel.BUFFERED)
ReactionListener(get(), get()).init()
notificationListener.startListening(
{ channelNotification.trySendBlocking(it) },
player,
gameId,
)
) { channelNotification.trySendBlocking(it) }
GlobalScope.launch {
commandHandler.handle(

View File

@@ -0,0 +1,36 @@
package eventDemo.business.event.projection.projectionListener
import eventDemo.adapter.infrastructureLayer.event.projection.GameProjectionBusInMemory
import eventDemo.business.entity.GameId
import eventDemo.business.entity.Player
import eventDemo.business.event.event.NewPlayerEvent
import eventDemo.business.event.projection.gameState.GameState
import eventDemo.business.notification.WelcomeToTheGameNotification
import io.kotest.core.spec.style.FunSpec
import io.mockk.mockk
import io.mockk.verify
import kotlin.test.assertIs
class PlayerNotificationListenerTest :
FunSpec({
test("startListening should react when a projection is sent to the bus") {
val player = Player("Tesla")
val gameId = GameId()
val bus = GameProjectionBusInMemory()
val state =
GameState(
aggregateId = gameId,
lastEvent = NewPlayerEvent(gameId, player, 1),
players = setOf(player),
)
val spyCall: () -> Unit = mockk(relaxed = true)
PlayerNotificationListener(bus).startListening(player, gameId) {
assertIs<WelcomeToTheGameNotification>(it)
spyCall()
}
bus.publish(state)
verify(exactly = 1) { spyCall() }
}
})