create a FrameChannelConverter
This commit is contained in:
55
src/test/kotlin/eventDemo/libs/FrameChannelConverterTest.kt
Normal file
55
src/test/kotlin/eventDemo/libs/FrameChannelConverterTest.kt
Normal file
@@ -0,0 +1,55 @@
|
||||
package eventDemo.libs
|
||||
|
||||
import eventDemo.libs.command.Command
|
||||
import eventDemo.libs.command.CommandId
|
||||
import io.kotest.core.spec.style.FunSpec
|
||||
import io.kotest.matchers.equals.shouldBeEqual
|
||||
import io.ktor.websocket.Frame
|
||||
import io.ktor.websocket.readText
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.serialization.Serializable
|
||||
import java.util.UUID
|
||||
import kotlin.test.assertIs
|
||||
|
||||
@Serializable
|
||||
data class CommandTest(
|
||||
override val id: CommandId,
|
||||
) : Command
|
||||
|
||||
class FrameChannelConverterTest :
|
||||
FunSpec({
|
||||
|
||||
test("toObjectChannel") {
|
||||
val uuid = "d737c631-76af-406e-bc29-f3e5b97226a5"
|
||||
val id = CommandId(UUID.fromString(uuid))
|
||||
val jsonCommand = """{"id":"$uuid"}"""
|
||||
|
||||
val channel = Channel<Frame>()
|
||||
|
||||
launch {
|
||||
val commandChannel = toObjectChannel<CommandTest>(channel)
|
||||
commandChannel.receive().id shouldBeEqual id
|
||||
channel.close()
|
||||
}
|
||||
|
||||
channel.send(Frame.Text(jsonCommand))
|
||||
}
|
||||
|
||||
test("fromFrameChannel") {
|
||||
val uuid = "d737c631-76af-406e-bc29-f3e5b97226a5"
|
||||
val id = CommandId(UUID.fromString(uuid))
|
||||
val command = CommandTest(id)
|
||||
val jsonCommand = """{"id":"$uuid"}"""
|
||||
|
||||
val channel = Channel<Frame>()
|
||||
|
||||
launch {
|
||||
val commandChannel = fromFrameChannel<CommandTest>(channel)
|
||||
commandChannel.send(command)
|
||||
commandChannel.close()
|
||||
}
|
||||
|
||||
assertIs<Frame.Text>(channel.receive()).readText() shouldBeEqual jsonCommand
|
||||
}
|
||||
})
|
||||
@@ -1,12 +1,10 @@
|
||||
package eventDemo.libs.command
|
||||
|
||||
import io.kotest.core.spec.style.FunSpec
|
||||
import io.ktor.websocket.Frame
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
@Serializable
|
||||
class CommandTest(
|
||||
@@ -19,12 +17,9 @@ class CommandStreamChannelTest :
|
||||
test("send and receive") {
|
||||
val command = CommandTest(CommandId())
|
||||
|
||||
val channel = Channel<Frame>()
|
||||
val channel = Channel<CommandTest>()
|
||||
val stream =
|
||||
CommandStreamChannel<CommandTest>(
|
||||
incoming = channel,
|
||||
deserializer = { Json.decodeFromString(it) },
|
||||
)
|
||||
CommandStreamChannel(channel)
|
||||
|
||||
val spyCall: () -> Unit = mockk(relaxed = true)
|
||||
|
||||
@@ -32,7 +27,7 @@ class CommandStreamChannelTest :
|
||||
println("In action ${it.id}")
|
||||
spyCall()
|
||||
}
|
||||
channel.send(Frame.Text(Json.encodeToString(command)))
|
||||
channel.send(command)
|
||||
verify(exactly = 1) { spyCall() }
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user