Skip to content

Commit

Permalink
[ktor] Remove KTOR-6883 workaround in Ktor JS tests (artificial delay)
Browse files Browse the repository at this point in the history
  • Loading branch information
joffrey-bion committed Apr 24, 2024
1 parent 374205b commit bb60044
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ import io.ktor.client.engine.*
import io.ktor.client.plugins.websocket.*
import org.hildan.krossbow.websocket.*
import org.hildan.krossbow.websocket.test.*
import kotlin.time.*

abstract class KtorClientTestSuite(
supportsStatusCodes: Boolean,
headersTestDelay: Duration? = null,
) : WebSocketClientTestSuite(supportsStatusCodes, headersTestDelay) {
abstract class KtorClientTestSuite(supportsStatusCodes: Boolean) : WebSocketClientTestSuite(supportsStatusCodes) {

override fun provideClient(): WebSocketClient = KtorWebSocketClient(
HttpClient(provideEngine()) { install(WebSockets) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ private val Platform.supportsStatusCodes: Boolean
// Also, it covers cases of dynamically-selected implementations.
class KtorMppWebSocketClientTest : WebSocketClientTestSuite(
supportsStatusCodes = currentPlatform().supportsStatusCodes,
// workaround for https://youtrack.jetbrains.com/issue/KTOR-6883
headersTestDelay = 200.milliseconds.takeIf { currentPlatform() == Platform.Js.NodeJs },
) {
override fun provideClient(): WebSocketClient = KtorWebSocketClient(
HttpClient {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ class KtorJsWebSocketClientTest : KtorClientTestSuite(
// JS node: error is too generic and doesn't differ per status code (ECONNREFUSED, unlike 'ws')
// JS browser: cannot support status codes for security reasons
supportsStatusCodes = false,
// workaround for https://youtrack.jetbrains.com/issue/KTOR-6883
headersTestDelay = 200.milliseconds.takeIf { currentPlatform() == Platform.Js.NodeJs },
) {
override fun provideEngine(): HttpClientEngineFactory<*> = Js
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import kotlin.time.Duration.Companion.seconds

abstract class WebSocketClientTestSuite(
private val supportsStatusCodes: Boolean = true,
private val headersTestDelay: Duration? = null,
) {
abstract fun provideClient(): WebSocketClient

Expand Down Expand Up @@ -193,16 +192,14 @@ abstract class WebSocketClientTestSuite(
fun testHandshakeHeaders() = runTestRealTime {
if (wsClient.supportsCustomHeaders) {
println("Connecting with agent $agent to ${testServerConfig.wsUrl}/sendHandshakeHeaders")
// workaround for https://youtrack.jetbrains.com/issue/KTOR-6883
val extraParams = if (headersTestDelay != null) mapOf("scheduleDelay" to headersTestDelay.toString()) else emptyMap()
val session = wsClient.connect(
url = testUrl(path = "/sendHandshakeHeaders", otherParams = extraParams),
url = testUrl(path = "/sendHandshakeHeaders", testCaseName = "testHandshakeCustomHeaders"),
headers = mapOf("My-Header-1" to "my-value-1", "My-Header-2" to "my-value-2"),
)
println("Connected with agent $agent to ${testServerConfig.wsUrl}/sendHandshakeHeaders")
try {
// for some reason, this can be pretty long with the Ktor/JS client in nodeJS tests on macOS
val echoedHeadersFrame = session.expectTextFrame("header info frame", 50.seconds)
val echoedHeadersFrame = session.expectTextFrame("header info frame")
val headers = echoedHeadersFrame.text.lines()
assertContains(headers, "My-Header-1=my-value-1")
assertContains(headers, "My-Header-2=my-value-2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import java.nio.*
import kotlin.time.*

internal class EchoWebSocketServer(port: Int = 0) : WebSocketServer(InetSocketAddress(port)) {

private val delayedHeadersScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)

override fun onStart() {
}
Expand All @@ -20,30 +18,18 @@ internal class EchoWebSocketServer(port: Int = 0) : WebSocketServer(InetSocketAd
println("Connection to URI $uri")

if (uri.path == "/sendHandshakeHeaders") {
val queryParams = uri.queryAsMap()
val scheduleDelay = queryParams["scheduleDelay"]?.let(Duration::parse)
conn.sendMessageWithHeaders(handshake, scheduleDelay)
conn.sendMessageWithHeaders(handshake)
} else {
println("Not sending headers frame for URI $uri")
}
}

private fun WebSocket.sendMessageWithHeaders(handshake: ClientHandshake, scheduleDelay: Duration? = null) {
private fun WebSocket.sendMessageWithHeaders(handshake: ClientHandshake) {
val headerNames = handshake.iterateHttpFields().asSequence().toList()
val headersData = headerNames.joinToString("\n") { "$it=${handshake.getFieldValue(it)}" }
if (scheduleDelay != null) {
// necessary due to https://youtrack.jetbrains.com/issue/KTOR-6883
println("Scheduling message with headers in $scheduleDelay")
delayedHeadersScope.launch {
delay(scheduleDelay)
send(headersData)
println("Headers frame sent!")
}
} else {
println("Sending message with headers...")
send(headersData)
println("Headers frame sent!")
}
println("Sending message with headers...")
send(headersData)
println("Headers frame sent!")
}

override fun onMessage(conn: WebSocket, message: String?) {
Expand Down

0 comments on commit bb60044

Please sign in to comment.