Skip to content

Commit 6add390

Browse files
committed
Increase the deprecation levels for the old test framework API
1 parent d010110 commit 6add390

16 files changed

+70
-642
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ suspend fun main() = coroutineScope {
4343
* Integration with `Window` via [Window.asCoroutineDispatcher], etc.
4444
* [test](kotlinx-coroutines-test/README.md) — test utilities for coroutines:
4545
* [Dispatchers.setMain] to override [Dispatchers.Main] in tests;
46-
* [TestCoroutineScope] to test suspending functions and coroutines.
46+
* [runTest] and [TestScope] to test suspending functions and coroutines.
4747
* [debug](kotlinx-coroutines-debug/README.md) — debug utilities for coroutines:
4848
* [DebugProbes] API to probe, keep track of, print and dump active coroutines;
4949
* [CoroutinesTimeout] test rule to automatically dump coroutines on test timeout.
@@ -240,7 +240,8 @@ See [Contributing Guidelines](CONTRIBUTING.md).
240240
<!--- INDEX kotlinx.coroutines.test -->
241241

242242
[Dispatchers.setMain]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/set-main.html
243-
[TestCoroutineScope]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-coroutine-scope/index.html
243+
[runTest]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/run-test.html
244+
[TestScope]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-scope/index.html
244245

245246
<!--- MODULE kotlinx-coroutines-debug -->
246247
<!--- INDEX kotlinx.coroutines.debug -->

kotlinx-coroutines-test/jvm/src/migration/DelayController.kt

-206
This file was deleted.

kotlinx-coroutines-test/jvm/src/migration/TestBuildersDeprecated.kt

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@file:Suppress("DEPRECATION")
1+
@file:Suppress("DEPRECATION", "DEPRECATION_ERROR")
22
@file:JvmName("TestBuildersKt")
33
@file:JvmMultifileClass
44

@@ -51,9 +51,9 @@ import kotlin.time.Duration.Companion.milliseconds
5151
"Use `runTest` instead to support completing from other dispatchers. " +
5252
"Please see the migration guide for details: " +
5353
"https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-test/MIGRATION.md",
54-
level = DeprecationLevel.WARNING
54+
level = DeprecationLevel.ERROR
5555
)
56-
// Since 1.6.0, kept as warning in 1.7.0, ERROR in 1.8.0 and removed as experimental in 1.9.0
56+
// Since 1.6.0, kept as warning in 1.7.0, ERROR in 1.9.0 and removed as experimental later
5757
public fun runBlockingTest(
5858
context: CoroutineContext = EmptyCoroutineContext,
5959
testBody: suspend TestCoroutineScope.() -> Unit
@@ -73,8 +73,8 @@ public fun runBlockingTest(
7373
/**
7474
* A version of [runBlockingTest] that works with [TestScope].
7575
*/
76-
@Deprecated("Use `runTest` instead to support completing from other dispatchers.", level = DeprecationLevel.WARNING)
77-
// Since 1.6.0, kept as warning in 1.7.0, ERROR in 1.8.0 and removed as experimental in 1.9.0
76+
@Deprecated("Use `runTest` instead to support completing from other dispatchers.", level = DeprecationLevel.ERROR)
77+
// Since 1.6.0, kept as warning in 1.7.0, ERROR in 1.9.0 and removed as experimental later
7878
public fun runBlockingTestOnTestScope(
7979
context: CoroutineContext = EmptyCoroutineContext,
8080
testBody: suspend TestScope.() -> Unit
@@ -121,17 +121,17 @@ public fun runBlockingTestOnTestScope(
121121
"Use `runTest` instead to support completing from other dispatchers. " +
122122
"Please see the migration guide for details: " +
123123
"https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-test/MIGRATION.md",
124-
level = DeprecationLevel.WARNING
124+
level = DeprecationLevel.ERROR
125125
)
126-
// Since 1.6.0, kept as warning in 1.7.0, ERROR in 1.8.0 and removed as experimental in 1.9.0
126+
// Since 1.6.0, kept as warning in 1.7.0, ERROR in 1.9.0 and removed as experimental later
127127
public fun TestCoroutineScope.runBlockingTest(block: suspend TestCoroutineScope.() -> Unit): Unit =
128128
runBlockingTest(coroutineContext, block)
129129

130130
/**
131131
* Convenience method for calling [runBlockingTestOnTestScope] on an existing [TestScope].
132132
*/
133-
@Deprecated("Use `runTest` instead to support completing from other dispatchers.", level = DeprecationLevel.WARNING)
134-
// Since 1.6.0, kept as warning in 1.7.0, ERROR in 1.8.0 and removed as experimental in 1.9.0
133+
@Deprecated("Use `runTest` instead to support completing from other dispatchers.", level = DeprecationLevel.ERROR)
134+
// Since 1.6.0, kept as warning in 1.7.0, ERROR in 1.9.0 and removed as experimental later
135135
public fun TestScope.runBlockingTest(block: suspend TestScope.() -> Unit): Unit =
136136
runBlockingTestOnTestScope(coroutineContext, block)
137137

@@ -147,18 +147,18 @@ public fun TestScope.runBlockingTest(block: suspend TestScope.() -> Unit): Unit
147147
"Use `runTest` instead to support completing from other dispatchers. " +
148148
"Please see the migration guide for details: " +
149149
"https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-test/MIGRATION.md",
150-
level = DeprecationLevel.WARNING
150+
level = DeprecationLevel.ERROR
151151
)
152-
// Since 1.6.0, kept as warning in 1.7.0, ERROR in 1.8.0 and removed as experimental in 1.9.0
152+
// Since 1.6.0, kept as warning in 1.7.0, ERROR in 1.9.0 and removed as experimental later
153153
public fun TestCoroutineDispatcher.runBlockingTest(block: suspend TestCoroutineScope.() -> Unit): Unit =
154154
runBlockingTest(this, block)
155155

156156
/**
157157
* This is an overload of [runTest] that works with [TestCoroutineScope].
158158
*/
159159
@ExperimentalCoroutinesApi
160-
@Deprecated("Use `runTest` instead.", level = DeprecationLevel.WARNING)
161-
// Since 1.6.0, kept as warning in 1.7.0, ERROR in 1.8.0 and removed as experimental in 1.9.0
160+
@Deprecated("Use `runTest` instead.", level = DeprecationLevel.ERROR)
161+
// Since 1.6.0, kept as warning in 1.7.0, ERROR in 1.9.0 and removed as experimental later
162162
public fun runTestWithLegacyScope(
163163
context: CoroutineContext = EmptyCoroutineContext,
164164
dispatchTimeoutMs: Long = DEFAULT_DISPATCH_TIMEOUT_MS,
@@ -196,8 +196,8 @@ public fun runTestWithLegacyScope(
196196
* immediately from the test body. See the docs for [TestResult] for details.
197197
*/
198198
@ExperimentalCoroutinesApi
199-
@Deprecated("Use `TestScope.runTest` instead.", level = DeprecationLevel.WARNING)
200-
// Since 1.6.0, kept as warning in 1.7.0, ERROR in 1.8.0 and removed as experimental in 1.9.0
199+
@Deprecated("Use `TestScope.runTest` instead.", level = DeprecationLevel.ERROR)
200+
// Since 1.6.0, kept as warning in 1.7.0, ERROR in 1.9.0 and removed as experimental later
201201
public fun TestCoroutineScope.runTest(
202202
dispatchTimeoutMs: Long = DEFAULT_DISPATCH_TIMEOUT_MS,
203203
block: suspend TestCoroutineScope.() -> Unit

kotlinx-coroutines-test/jvm/src/migration/TestCoroutineDispatcher.kt

+21-43
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,14 @@ import kotlinx.coroutines.*
44
import kotlin.coroutines.*
55

66
/**
7-
* [CoroutineDispatcher] that performs both immediate and lazy execution of coroutines in tests
8-
* and uses a [TestCoroutineScheduler] to control its virtual clock.
9-
*
10-
* By default, [TestCoroutineDispatcher] is immediate. That means any tasks scheduled to be run without delay are
11-
* immediately executed. If they were scheduled with a delay, the virtual clock-time must be advanced via one of the
12-
* methods on the dispatcher's [scheduler].
13-
*
14-
* When switched to lazy execution using [pauseDispatcher] any coroutines started via [launch] or [async] will
15-
* not execute until a call to [DelayController.runCurrent] or the virtual clock-time has been advanced via one of the
16-
* methods on [DelayController].
17-
*
18-
* @see DelayController
7+
* @suppress
198
*/
209
@Deprecated("The execution order of `TestCoroutineDispatcher` can be confusing, and the mechanism of " +
2110
"pausing is typically misunderstood. Please use `StandardTestDispatcher` or `UnconfinedTestDispatcher` instead.",
22-
level = DeprecationLevel.WARNING)
23-
// Since 1.6.0, kept as warning in 1.7.0, ERROR in 1.8.0 and removed as experimental in 1.9.0
11+
level = DeprecationLevel.ERROR)
12+
// Since 1.6.0, kept as warning in 1.7.0, ERROR in 1.9.0 and removed as experimental later
2413
public class TestCoroutineDispatcher(public override val scheduler: TestCoroutineScheduler = TestCoroutineScheduler()):
25-
TestDispatcher(), Delay, SchedulerAsDelayController
14+
TestDispatcher(), Delay
2615
{
2716
private var dispatchImmediately = true
2817
set(value) {
@@ -56,36 +45,25 @@ public class TestCoroutineDispatcher(public override val scheduler: TestCoroutin
5645
private fun post(block: Runnable, context: CoroutineContext) =
5746
scheduler.registerEvent(this, 0, block, context) { false }
5847

59-
/** @suppress */
60-
@Deprecated(
61-
"Please use a dispatcher that is paused by default, like `StandardTestDispatcher`.",
62-
level = DeprecationLevel.ERROR
63-
)
64-
override suspend fun pauseDispatcher(block: suspend () -> Unit) {
65-
val previous = dispatchImmediately
66-
dispatchImmediately = false
67-
try {
68-
block()
69-
} finally {
70-
dispatchImmediately = previous
71-
}
72-
}
48+
val currentTime: Long
49+
get() = scheduler.currentTime
7350

74-
/** @suppress */
75-
@Deprecated(
76-
"Please use a dispatcher that is paused by default, like `StandardTestDispatcher`.",
77-
level = DeprecationLevel.ERROR
78-
)
79-
override fun pauseDispatcher() {
80-
dispatchImmediately = false
51+
fun advanceUntilIdle(): Long {
52+
val oldTime = scheduler.currentTime
53+
scheduler.advanceUntilIdle()
54+
return scheduler.currentTime - oldTime
8155
}
8256

83-
/** @suppress */
84-
@Deprecated(
85-
"Please use a dispatcher that is paused by default, like `StandardTestDispatcher`.",
86-
level = DeprecationLevel.ERROR
87-
)
88-
override fun resumeDispatcher() {
89-
dispatchImmediately = true
57+
fun runCurrent(): Unit = scheduler.runCurrent()
58+
59+
fun cleanupTestCoroutines() {
60+
// process any pending cancellations or completions, but don't advance time
61+
scheduler.runCurrent()
62+
if (!scheduler.isIdle(strict = false)) {
63+
throw UncompletedCoroutinesError(
64+
"Unfinished coroutines during tear-down. Ensure all coroutines are" +
65+
" completed or cancelled by your test."
66+
)
67+
}
9068
}
9169
}

0 commit comments

Comments
 (0)