Skip to content

Commit 4064009

Browse files
authored
Fix some flaky tests (#4759)
* Migrate CurlWebSocketTests to ClientEngineTest * KTOR-8361 Fix flaky tests in AuthTokenHolderTest * KTOR-8362 Fix flaky tests in RunTestWithDataTest
1 parent 788b811 commit 4064009

File tree

4 files changed

+132
-155
lines changed

4 files changed

+132
-155
lines changed

ktor-client/ktor-client-curl/desktop/test/io/ktor/client/engine/curl/test/CurlWebSocketTests.kt

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,26 @@
44

55
package io.ktor.client.engine.curl.test
66

7-
import io.ktor.client.*
87
import io.ktor.client.engine.curl.*
98
import io.ktor.client.plugins.websocket.*
109
import io.ktor.client.request.*
10+
import io.ktor.client.test.base.*
1111
import io.ktor.websocket.*
12-
import kotlinx.coroutines.*
13-
import kotlinx.serialization.json.*
12+
import kotlinx.coroutines.CompletableDeferred
13+
import kotlinx.coroutines.delay
14+
import kotlinx.coroutines.launch
15+
import kotlinx.serialization.json.Json
1416
import kotlin.test.*
1517

16-
class CurlWebSocketTests {
17-
18-
private val TEST_SERVER: String = "http://127.0.0.1:8080"
19-
private val TEST_WEBSOCKET_SERVER: String = "ws://127.0.0.1:8080"
18+
class CurlWebSocketTests : ClientEngineTest<CurlClientEngineConfig>(Curl) {
2019

2120
@Test
22-
fun testEcho() {
23-
val client = HttpClient(Curl) {
21+
fun testEcho() = testClient {
22+
config {
2423
install(WebSockets)
2524
}
2625

27-
runBlocking {
26+
test { client ->
2827
client.webSocket("$TEST_WEBSOCKET_SERVER/websockets/echo") {
2928
send(Frame.Text("Hello, world"))
3029

@@ -38,12 +37,12 @@ class CurlWebSocketTests {
3837

3938
@Ignore // TODO: for some reason we doesn't get a response
4039
@Test
41-
fun testEmptyFrame() {
42-
val client = HttpClient(Curl) {
40+
fun testEmptyFrame() = testClient {
41+
config {
4342
install(WebSockets)
4443
}
4544

46-
runBlocking {
45+
test { client ->
4746
client.webSocket("$TEST_WEBSOCKET_SERVER/websockets/echo") {
4847
send(Frame.Text(""))
4948

@@ -56,12 +55,12 @@ class CurlWebSocketTests {
5655
}
5756

5857
@Test
59-
fun testWebSocketHeaders() {
60-
val client = HttpClient(Curl) {
58+
fun testWebSocketHeaders() = testClient {
59+
config {
6160
install(WebSockets)
6261
}
6362

64-
runBlocking {
63+
test { client ->
6564
client.webSocket("$TEST_WEBSOCKET_SERVER/websockets/headers") {
6665
val actual = incoming.receive()
6766

@@ -80,12 +79,12 @@ class CurlWebSocketTests {
8079
}
8180

8281
@Test
83-
fun testParallelSessions() {
84-
val client = HttpClient(Curl) {
82+
fun testParallelSessions() = testClient {
83+
config {
8584
install(WebSockets)
8685
}
8786

88-
runBlocking {
87+
test { client ->
8988
val websocketInitialized = CompletableDeferred<Boolean>()
9089

9190
launch {

ktor-client/ktor-client-plugins/ktor-client-auth/common/src/io/ktor/client/plugins/auth/providers/AuthTokenHolder.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44

55
package io.ktor.client.plugins.auth.providers
66

7-
import kotlinx.coroutines.DelicateCoroutinesApi
8-
import kotlinx.coroutines.GlobalScope
9-
import kotlinx.coroutines.launch
7+
import kotlinx.coroutines.*
108
import kotlinx.coroutines.sync.Mutex
119
import kotlinx.coroutines.sync.withLock
12-
import kotlinx.coroutines.withContext
1310
import kotlin.concurrent.Volatile
1411
import kotlin.coroutines.CoroutineContext
1512
import kotlin.coroutines.coroutineContext
@@ -91,12 +88,12 @@ internal class AuthTokenHolder<T>(private val loadTokens: suspend () -> T?) {
9188
* Resets the cached value.
9289
*/
9390
@OptIn(DelicateCoroutinesApi::class)
94-
internal fun clearToken() {
91+
internal fun clearToken(coroutineScope: CoroutineScope = GlobalScope) {
9592
if (mutex.tryLock()) {
9693
value = null
9794
mutex.unlock()
9895
} else {
99-
GlobalScope.launch {
96+
coroutineScope.launch {
10097
mutex.withLock {
10198
value = null
10299
}

0 commit comments

Comments
 (0)