https://kotlinlang.org logo
#ktor
Title
# ktor
o

Osman Saral

03/27/2023, 3:39 PM
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

Aleksei Tirman [JB]

03/27/2023, 6:14 PM
I cannot reproduce the
ClosedReceiveChannelException
. Can you please share a complete test where that exception is thrown during the execution?
o

Osman Saral

03/27/2023, 7:00 PM
a

Aleksei Tirman [JB]

03/28/2023, 8:59 AM
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

Osman Saral

03/28/2023, 9:01 AM
This shouldn't happen right? We should see the exception.
a

Aleksei Tirman [JB]

03/28/2023, 9:02 AM
You can see it by wrapping a handler with
try/catch
.
o

Osman Saral

03/28/2023, 9:02 AM
Is it possible to close the connection manually? Just signal server to close or vise versa?
What causes to close the connection?
a

Aleksei Tirman [JB]

03/28/2023, 9:03 AM
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

Osman Saral

03/28/2023, 9:06 AM
but i can't read close frames from serverside. right?
a

Aleksei Tirman [JB]

03/28/2023, 9:06 AM
They are handled automatically if the connection isn’t raw.
👍 1
o

Osman Saral

03/28/2023, 9:08 AM
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

Aleksei Tirman [JB]

03/28/2023, 9:12 AM
I have logs configured for JVM. I’m unsure how to do this for Android.
o

Osman Saral

03/28/2023, 9:15 AM
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
89 Views