test: add RabbitMQ test
This commit is contained in:
@@ -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<String, GameEventBus> =
|
||||||
|
mapOf(
|
||||||
|
GameEventBusInMemory::class.java.simpleName to GameEventBusInMemory(),
|
||||||
|
GameEventBusInRabbinMQ::class.java.simpleName to GameEventBusInRabbinMQ(get<ConnectionFactory>()),
|
||||||
|
)
|
||||||
|
|
||||||
|
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() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
70
src/test/kotlin/eventDemo/externalServices/RabbitMQTest.kt
Normal file
70
src/test/kotlin/eventDemo/externalServices/RabbitMQTest.kt
Normal file
@@ -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<ConnectionFactory>()
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
@@ -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") { }
|
|
||||||
})
|
|
||||||
Reference in New Issue
Block a user