Could you help me testing the WebSocket endpoint b...
# ktor
l
Could you help me testing the WebSocket endpoint below, please? It's supposed to keep receiving data without interruption, persist that data and send it to a Kotlin Coroutines channel to be processed. Simplified main code:
Copy code
fun 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:
Copy code
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) }
    }
}
Copy code
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.
c
Perhaps you need
handleWebSocketConversation
handleWebSocket { setBody( text ) }
is sending a text instead of binary websocket frames
We probably need to deprecate handleWebSocket to avoid confusion, @e5l what do you think?
👍 1
l
Thank you @cy. Unfortunately replacing
handleWebSocket
with
handleWebSocketConversion
didn't work. None log message was printed either.
Copy code
handleWebSocketConversation("/") { _, outgoing ->
    val wasOffered = outgoing.offer(Frame.Text(event))
    assertEquals(true, wasOffered) // Do not execute
}