Skip to content

Commit

Permalink
KTOR-5540 Fix darwin ws pong message (#3747)
Browse files Browse the repository at this point in the history
* KTOR-5540 Fix darwin ws pong message

* Update ktor-client/ktor-client-darwin/darwin/test/DarwinEngineTest.kt

Co-authored-by: Vitor Hugo Schwaab <vitor@schwaab.dev>

---------

Co-authored-by: Rustam <rxsinukov@gmail.com>
Co-authored-by: Vitor Hugo Schwaab <vitor@schwaab.dev>
  • Loading branch information
3 people authored Sep 11, 2023
1 parent 17a5386 commit 375b0d3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ import kotlinx.coroutines.*
import platform.Foundation.*
import platform.posix.*

@OptIn(DelicateCoroutinesApi::class, UnsafeNumber::class, InternalAPI::class, ExperimentalForeignApi::class)
@OptIn(
DelicateCoroutinesApi::class,
UnsafeNumber::class,
InternalAPI::class,
ExperimentalForeignApi::class,
BetaInteropApi::class
)
internal suspend fun OutgoingContent.toDataOrStream(): Any? {
if (this is OutgoingContent.ByteArrayContent) return bytes().toNSData()
if (this is OutgoingContent.NoContent) return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,13 @@ internal class DarwinWebsocketSession(
}

FrameType.PING -> {
val payload = frame.readBytes()
task.sendPingWithPongReceiveHandler { error ->
if (error != null) {
cancel("Error receiving pong", DarwinHttpRequestException(error))
return@sendPingWithPongReceiveHandler
}
_incoming.trySend(Frame.Pong(ByteReadPacket.Empty))
_incoming.trySend(Frame.Pong(payload))
}
}

Expand Down Expand Up @@ -167,6 +168,7 @@ internal class DarwinWebsocketSession(
socketJob.completeExceptionally(exception)
}

@OptIn(DelicateCoroutinesApi::class)
fun didClose(
code: NSURLSessionWebSocketCloseCode,
reason: NSData?,
Expand Down
20 changes: 20 additions & 0 deletions ktor-client/ktor-client-darwin/darwin/test/DarwinEngineTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.client.tests.utils.*
import io.ktor.http.*
import io.ktor.test.dispatcher.*
import io.ktor.websocket.*
import kotlinx.cinterop.*
import kotlinx.coroutines.*
Expand Down Expand Up @@ -231,6 +232,25 @@ class DarwinEngineTest {
session.close()
}


@Test
fun testWebSocketPingInterval() = testSuspend {
val client = HttpClient(Darwin) {
install(WebSockets) {
pingInterval = 1000
}
}

assertFailsWith<TimeoutCancellationException> {
client.webSocket("$TEST_WEBSOCKET_SERVER/websockets/echo") {
withTimeout(5000) {
for (frame in incoming) {
}
}
}
}
}

private fun stringToNSUrlString(value: String): String {
return Url(value).toNSUrl().absoluteString!!
}
Expand Down

0 comments on commit 375b0d3

Please sign in to comment.