Hi, I'm trying to write unit tests for a websocket...
# ktor
o
Hi, I'm trying to write unit tests for a websocket client and I was looking at the tests on ktor. 🧵
This test passes, But when I change
"null"
to
"foo"
it doesn't fail with assertion, instead it throws kotlinx.coroutines.channels.ClosedReceiveChannelException: Channel was closed. Is there something wrong or is it normal.
Copy code
@Test
    fun testJsonNullWithWebsocketsClient(): Unit = testApplication {
        install(io.ktor.server.websocket.WebSockets)
        routing {
            webSocket("/") {
                for (frame in incoming) {
                    assertEquals("null", (frame as Frame.Text).readText())
                    outgoing.send(frame)
                }
            }
        }

        createClient {
            install(WebSockets) {
                contentConverter = KotlinxWebsocketSerializationConverter(
                    Json {
                        ignoreUnknownKeys = true
                    }
                )
            }
        }.ws("/") {
            val user: User? = null
            sendSerialized(user)
            val received = receiveDeserialized<User?>()
            assertNull(received)
        }
    }
a
I cannot reproduce the
ClosedReceiveChannelException
. Can you please share a complete test where that exception is thrown during the execution?
o
a
The problem is that the
assertEquals("error", (frame as Frame.Text).readText())
fails but the assertion exception doesn’t propagates to the top level. Then the test server Websockets connection is closed which leads to closing the receive channel on the client side.
o
This shouldn't happen right? We should see the exception.
a
You can see it by wrapping a handler with
try/catch
.
o
Is it possible to close the connection manually? Just signal server to close or vise versa?
What causes to close the connection?
a
Also, I can see the
ComparisonFailure
in the logs.
The assertion error causes closing of a connection
On the client side you can call the
close
method to close a connection.
o
but i can't read close frames from serverside. right?
a
They are handled automatically if the connection isn’t raw.
👍 1
o
how do you run the tests? I can't see the
ComparisonFailure
in the logs. Or any other exception other than
ClosedReceiveChannelException
, That makes things very hard
I'm running them using the IDE
a
I have logs configured for JVM. I’m unsure how to do this for Android.
o
This is actually a kmm project. But still the same. Do you know any documentation about logging, or any lead you can show me?
adding org.slf4j:slf4j-simple to the project did the trick
136 Views