ktorio / ktor

Framework for quickly creating connected applications in Kotlin with minimal effort
https://ktor.io
Apache License 2.0
12.72k stars 1.04k forks source link

Conflict between WebSockets and StatusPages #889

Closed neworld closed 5 years ago

neworld commented 5 years ago

Following test just hangs. This issue could be similar to #745.

    @Test
    fun testConversation() {
        withTestApplication {
            application.install(WebSockets)
            application.install(StatusPages) // <-- I added only this

            val received = arrayListOf<String>()
            application.routing {
                webSocket("/echo") {
                    try {
                        while (true) {
                            val text = (incoming.receive() as Frame.Text).readText()
                            received += text
                            outgoing.send(Frame.Text(text))
                        }
                    } catch (e: ClosedReceiveChannelException) {
                        // Do nothing!
                    } catch (e: Throwable) {
                        e.printStackTrace()
                    }
                }
            }

            handleWebSocketConversation("/echo") { incoming, outgoing ->
                val textMessages = listOf("HELLO", "WORLD")
                for (msg in textMessages) {
                    outgoing.send(Frame.Text(msg))
                    assertEquals(msg, (incoming.receive() as Frame.Text).readText())
                }
                assertEquals(textMessages, received)
            }
        }
    }

Versions

Ktor Engine Used(client or server and name)

Test engine.

Thread dump

"Test worker" #13 prio=5 os_prio=0 cpu=355.78ms elapsed=20.86s tid=0x00007f9344b56000 nid=0x5c4c waiting on condition  [0x00007f92ff095000]
   java.lang.Thread.State: WAITING (parking)
        at jdk.internal.misc.Unsafe.park(java.base@11.0.1/Native Method)
        - parking to wait for  <0x000000062dd27610> (a java.util.concurrent.CompletableFuture$Signaller)
        at java.util.concurrent.locks.LockSupport.park(java.base@11.0.1/LockSupport.java:194)
        at java.util.concurrent.CompletableFuture$Signaller.block(java.base@11.0.1/CompletableFuture.java:1796)
        at java.util.concurrent.ForkJoinPool.managedBlock(java.base@11.0.1/ForkJoinPool.java:3128)
        at java.util.concurrent.CompletableFuture.waitingGet(java.base@11.0.1/CompletableFuture.java:1823)
        at java.util.concurrent.CompletableFuture.join(java.base@11.0.1/CompletableFuture.java:2043)
        at io.ktor.server.testing.TestApplicationEngine.handleWebSocket(TestApplicationEngine.kt:145)
        at io.ktor.server.testing.TestApplicationEngine.handleWebSocketConversation(TestApplicationEngine.kt:160)
        at io.ktor.server.testing.TestApplicationEngine.handleWebSocketConversation$default(TestApplicationEngine.kt:156)
        at com.neworldwar.backend.controller.ChatControllerTest$testConversation$1.invoke(ChatControllerTest.kt:57)

Full: https://gist.github.com/neworld/ffb5e2792870b6b4c7b72e7d70d8ba57

cy6erGn0m commented 5 years ago

This seems to be fixed in the master but I can reproduce it with 1.1.1 . Not exactly clear how it is fixed yet.

cy6erGn0m commented 5 years ago

Fixed.