diff --git a/src/test/kotlin/eventDemo/adapter/infrastructureLayer/GameEventBusInRabbitMQTest.kt b/src/test/kotlin/eventDemo/adapter/infrastructureLayer/GameEventBusInRabbitMQTest.kt new file mode 100644 index 0000000..c9c0d26 --- /dev/null +++ b/src/test/kotlin/eventDemo/adapter/infrastructureLayer/GameEventBusInRabbitMQTest.kt @@ -0,0 +1,49 @@ +package eventDemo.adapter.infrastructureLayer + +import com.rabbitmq.client.ConnectionFactory +import eventDemo.adapter.infrastructureLayer.event.GameEventBusInMemory +import eventDemo.adapter.infrastructureLayer.event.GameEventBusInRabbinMQ +import eventDemo.business.entity.GameId +import eventDemo.business.entity.Player +import eventDemo.business.event.GameEventBus +import eventDemo.business.event.event.NewPlayerEvent +import eventDemo.testKoinApplicationWithConfig +import io.kotest.assertions.nondeterministic.eventually +import io.kotest.core.spec.style.FunSpec +import io.kotest.datatest.withData +import io.kotest.matchers.equals.shouldBeEqual +import io.mockk.mockk +import io.mockk.spyk +import io.mockk.verify +import kotlin.time.Duration.Companion.seconds + +class GameEventBusInRabbitMQTest : + FunSpec({ + context("Pub/sub") { + testKoinApplicationWithConfig { + val busListToTest: Map = + mapOf( + GameEventBusInMemory::class.java.simpleName to GameEventBusInMemory(), + GameEventBusInRabbinMQ::class.java.simpleName to GameEventBusInRabbinMQ(get()), + ) + + withData(busListToTest) { bus -> + val spy = spyk(mockk<() -> Unit>()) + val aggregateId = GameId() + val player1 = Player(name = "Tesla") + val player2 = Player(name = "Einstein") + + bus.subscribe { obj -> + spy() + obj.aggregateId shouldBeEqual aggregateId + } + bus.publish(NewPlayerEvent(aggregateId, player1, 1)) + bus.publish(NewPlayerEvent(aggregateId, player2, 2)) + + eventually(1.seconds) { + verify(exactly = 2) { spy() } + } + } + } + } + }) diff --git a/src/test/kotlin/eventDemo/externalServices/RabbitMQTest.kt b/src/test/kotlin/eventDemo/externalServices/RabbitMQTest.kt new file mode 100644 index 0000000..d714ff4 --- /dev/null +++ b/src/test/kotlin/eventDemo/externalServices/RabbitMQTest.kt @@ -0,0 +1,70 @@ +package eventDemo.externalServices + +import com.rabbitmq.client.AMQP.BasicProperties +import com.rabbitmq.client.BuiltinExchangeType +import com.rabbitmq.client.ConnectionFactory +import com.rabbitmq.client.DefaultConsumer +import com.rabbitmq.client.Envelope +import eventDemo.testKoinApplicationWithConfig +import io.kotest.assertions.nondeterministic.eventually +import io.kotest.core.NamedTag +import io.kotest.core.spec.style.FunSpec +import io.kotest.matchers.string.shouldStartWith +import io.mockk.mockk +import io.mockk.spyk +import io.mockk.verify +import java.util.UUID +import kotlin.time.Duration.Companion.seconds + +class RabbitMQTest : + FunSpec({ + tags(NamedTag("rabbitmq")) + + test("test connection with RabbitMQ") { + testKoinApplicationWithConfig { + val factory = get() + + val exchangeName = "test_" + UUID.randomUUID() + + val spy = spyk(mockk<() -> Unit>()) + + factory.newConnection().use { connection -> + connection + .createChannel() + .use { channel -> + channel.exchangeDeclare(exchangeName, BuiltinExchangeType.FANOUT) + val queue = channel.queueDeclare("qqq", true, false, false, emptyMap()).queue + channel.queueBind(queue, exchangeName, "") + + channel + .basicConsume( + queue, + object : DefaultConsumer(channel) { + override fun handleDelivery( + consumerTag: String, + envelope: Envelope, + properties: BasicProperties, + body: ByteArray, + ) { + val msg = body.toString(Charsets.UTF_8) + msg shouldStartWith "testMessage" + spy() + channel.basicAck(envelope.deliveryTag, false) + } + }, + ) + + channel.basicPublish(exchangeName, "", BasicProperties(), "testMessage1".toByteArray()) + channel.basicPublish(exchangeName, "", BasicProperties(), "testMessage2".toByteArray()) + + eventually(3.seconds) { + verify(exactly = 2) { spy() } + } + + channel.queueDelete(queue) + channel.exchangeDelete(exchangeName) + } + } + } + } + }) diff --git a/src/test/kotlin/eventDemo/libs/event/EventBusInMemoryTest.kt b/src/test/kotlin/eventDemo/libs/event/EventBusInMemoryTest.kt deleted file mode 100644 index ac92888..0000000 --- a/src/test/kotlin/eventDemo/libs/event/EventBusInMemoryTest.kt +++ /dev/null @@ -1,10 +0,0 @@ -package eventDemo.libs.event - -import io.kotest.core.spec.style.FunSpec - -class EventBusInMemoryTest : - FunSpec({ - - xtest("publish should call the subscribed functions") { } - xtest("publish should call the subscribed functions on the priority order") { } - })