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

Osman Saral

03/29/2023, 8:05 AM
Hi everyone, Why does this test pass even if it throws
AssertionFailedError
?
Copy code
@Test
    fun test() = testApplication {
        install(io.ktor.server.websocket.WebSockets) {
            contentConverter = KotlinxWebsocketSerializationConverter(
                Json {
                    ignoreUnknownKeys = true
                }
            )
        }
        routing {
            webSocket("/") {
                sendSerialized(Incoming.Ping("123"))
                val outgoing = receiveDeserialized<Outgoing.Pong>()

                val a = "3"
                assertEquals("2", a) //this throws AssertionFailedError but test passes
            }
        }
        val client = createClient {
            install(WebSockets) {
                contentConverter = KotlinxWebsocketSerializationConverter(
                    Json {
                        ignoreUnknownKeys = true
                    }
                )
            }
        }

        client
            .wss("/") {
                val received = receiveDeserialized<Incoming.Ping>()
                assertIs<Incoming.Ping>(received)
                sendSerialized(Outgoing.Pong(received.id))
                val a = "3"
                assertEquals("3", a) //if this fails, test doesn't pass as expected.
            }
    }
a

Aleksei Tirman [JB]

03/29/2023, 10:27 AM
What are the definitions for the
Incoming
and
Outgoing
classes?
o

Osman Saral

03/29/2023, 10:28 AM
I don't think they'er related but here it is
Copy code
@Serializable
sealed class Outgoing(val msg: String) {
    @Serializable data class Pong(val id: String): Outgoing("pong")
}

@Serializable
sealed class Incoming(val msg: String) {
    @Serializable data class Ping(val id: String): Incoming("ping")
}
a

Aleksei Tirman [JB]

03/29/2023, 3:03 PM
That’s because any exceptions in a Websockets handler are swallowed to not crash the server.
o

Osman Saral

03/29/2023, 7:10 PM
is it possible to prevent this behaviour?
I'm developing a websocket client and I want to test if pong message is sent, when a ping is received. how can I test sendSerialized method if that's the case?
there are some tests on ktor source like
testJsonWithNullWebsocketsServer
. it also tests the received message on server. when I change the assertion to assertNotNull on that test, it still passes (AssertionFailedError is still printed to the logs)
Copy code
@Test
fun testJsonWithNullWebsocketsServer(): Unit = testApplication {
    install(io.ktor.server.websocket.WebSockets) {
        contentConverter = KotlinxWebsocketSerializationConverter(
            Json {
                ignoreUnknownKeys = true
            }
        )
    }
    routing {
        webSocket("/") {
            val user: User? = null
            sendSerialized(user)
            val received = receiveDeserialized<User?>()
            assertNotNull(received)
        }
    }

    createClient {
        install(WebSockets)
    }.ws("/") {
        for (frame in incoming) {
            assertEquals("null", (frame as Frame.Text).readText())
            outgoing.send(frame)
        }
    }
}
5 Views