1
1
/*
2
- * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
2
+ * Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3
3
*/
4
4
5
5
package io.ktor.server.test.base
6
6
7
+ import io.ktor.test.*
7
8
import io.ktor.test.dispatcher.*
8
9
import io.ktor.utils.io.*
9
10
import io.ktor.utils.io.locks.*
@@ -12,15 +13,14 @@ import kotlinx.coroutines.test.TestResult
12
13
import kotlin.time.Duration
13
14
import kotlin.time.Duration.Companion.seconds
14
15
16
+ @OptIn(InternalAPI ::class )
15
17
actual abstract class BaseTest actual constructor() {
16
18
actual open val timeout: Duration = 10 .seconds
17
19
18
20
private val errors = mutableListOf<Throwable >()
19
21
20
- @OptIn(InternalAPI ::class )
21
22
private val errorsLock = SynchronizedObject ()
22
23
23
- @OptIn(InternalAPI ::class )
24
24
actual fun collectUnhandledException (error : Throwable ) {
25
25
synchronized(errorsLock) {
26
26
errors.add(error)
@@ -31,6 +31,9 @@ actual abstract class BaseTest actual constructor() {
31
31
}
32
32
33
33
actual open fun afterTest () {
34
+ val errors = synchronized(errorsLock) { errors.toList() }
35
+ this .errors.clear()
36
+
34
37
if (errors.isEmpty()) return
35
38
36
39
val error = UnhandledErrorsException (
@@ -46,15 +49,21 @@ actual abstract class BaseTest actual constructor() {
46
49
47
50
actual fun runTest (
48
51
timeout : Duration ,
52
+ retries : Int ,
49
53
block : suspend CoroutineScope .() -> Unit
50
- ): TestResult = runTestWithRealTime(timeout = timeout) {
51
- beforeTest()
52
- try {
53
- block()
54
- } finally {
55
- afterTest()
54
+ ): TestResult = retryTest(retries) { retry ->
55
+ runTestWithRealTime(timeout = timeout) {
56
+ if (retry > 0 ) println (" [Retry $retry /$retries ]" )
57
+ beforeTest()
58
+ try {
59
+ block()
60
+ } finally {
61
+ afterTest()
62
+ }
56
63
}
57
64
}
58
65
}
59
66
67
+ internal actual const val DEFAULT_RETRIES : Int = 1
68
+
60
69
private class UnhandledErrorsException (override val message : String ) : Exception()
0 commit comments