Repository: ktorio/ktor
File: ktor-server/ktor-server-jetty-jakarta/jvm/test/io/ktor/tests/server/jetty/jakarta/JettyIdleTimeoutTest.kt (Kotlin)

Code before:
        }) {
            assertEquals(HttpStatusCode.OK, status)
        }

        // Paused stream - timeout
        socket {
            outputStream.writePacket(
                RequestResponseBuilder().apply {
                    requestLine(HttpMethod.Post, "/echo", "HTTP/1.1")
                    headerLine("Host", "localhost:$port")
                    headerLine("Content-Length", "25") // <-- greater than actual body
                    emptyLine()
                    bytes(requestMessage)
                }.build()
            )
            outputStream.flush()

            val response = inputStream.bufferedReader().readText()
            assertEquals("HTTP/1.1 500 Server Error", response.lines().first())
        }
    }

    @Test
    fun idleTimeoutResponseWriter(): Unit = runTest {
        val responseMessage = "Hello, world!".toByteArray()
        var writeError: Exception? = null

        createAndStartServer {
            get("/hello") {
                call.respondBytesWriter {
                    try {
                        while (true) {
                            writeByteArray(responseMessage)
                            flush()
                            yield()
                        }
                    } catch (cause: Exception) {
                        writeError = cause
                    }
                }
            }
        }

        // Too long reading response -> timeout
        socket {
            // use a very small buffer so the server blocks on writing
            sendBufferSize = 1
            receiveBufferSize = 1

            outputStream.writePacket(
                RequestResponseBuilder().apply {

Review comment (suggestion):
It's probably better to await the exception using deferred:
```kotlin
val writeError = CompletableDeferred<Exception>()

// Inside catch block
writeError.complete(cause)
        
// And later
assertIs<CloseByteChannelException>(writeError.await())
```

Code after:
        }) {
            assertEquals(HttpStatusCode.OK, status)
        }

        // Paused stream - timeout
        socket {
            outputStream.writePacket(
                RequestResponseBuilder().apply {
                    requestLine(HttpMethod.Post, "/echo", "HTTP/1.1")
                    headerLine("Host", "localhost:$port")
                    headerLine("Content-Length", "25") // <-- greater than actual body
                    emptyLine()
                    bytes(requestMessage)
                }.build()
            )
            outputStream.flush()

            val response = inputStream.bufferedReader().readText()
            assertEquals("HTTP/1.1 500 Server Error", response.lines().first())
        }
    }

    @Test
    fun idleTimeoutResponseWriter(): Unit = runTest {
        val responseMessage = "Hello, world!".toByteArray()
        var writeError = CompletableDeferred<Exception>()

        createAndStartServer {
            get("/hello") {
                call.respondBytesWriter {
                    try {
                        while (true) {
                            writeByteArray(responseMessage)
                            flush()
                            yield()
                        }
                    } catch (cause: Exception) {
                        writeError.complete(cause)
                    }
                }
            }
        }

        // Too long reading response -> timeout
        socket {
            // use a very small buffer so the server blocks on writing
            sendBufferSize = 1
            receiveBufferSize = 1

            outputStream.writePacket(
                RequestResponseBuilder().apply {