Lucas
12/14/2018, 4:19 AMfun Application.module(database: Database, channel: Channel<String>) {
routing {
webSocket("/") {
while (true) {
val payload = incoming.receive() as Frame.Text
val event = payload.readText()
database.save(event)
channel.send(event)
}
}
}
}
For some reason the test below does not finish and produces a INFO log entry:
val database = mockk<Database>()
val channel = Channel<String>()
val event = "John's birthday"
withTestApplication({ module(database, channel) }) {
runBlocking {
launch {
handleWebSocket("/") {
setBody(event)
}
}
delay(300)
expectThat(channel.count()).isEqualTo(1)
coVerify(exactly = 1) { database.save(event) }
}
}
2018-12-14 02:13:46.002 [DefaultDispatcher-worker-1 @coroutine#3] INFO ktor.test - 101 Switching Protocols: GET - /
When running in production the code works as expected, so I may be missing something in the test case. The Kotlin version is 1.3.10 and Ktor version is 1.0.0.cy
12/14/2018, 2:48 PMhandleWebSocketConversation
cy
12/14/2018, 2:49 PMhandleWebSocket { setBody( text ) }
is sending a text instead of binary websocket framescy
12/14/2018, 2:49 PMLucas
12/14/2018, 6:56 PMhandleWebSocket
with handleWebSocketConversion
didn't work. None log message was printed either.
handleWebSocketConversation("/") { _, outgoing ->
val wasOffered = outgoing.offer(Frame.Text(event))
assertEquals(true, wasOffered) // Do not execute
}