Skip to content

Commit 2001616

Browse files
committed
Add deprecations to TestCoroutineContext.
Add deprecations to new API for changes.
1 parent 9a88429 commit 2001616

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

core/kotlinx-coroutines-core/src/test_/TestCoroutineContext.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ import kotlin.coroutines.*
3030
*
3131
* @param name A user-readable name for debugging purposes.
3232
*/
33-
@ObsoleteCoroutinesApi
33+
@Deprecated("This API has been deprecated to integrate with Structured Concurrency.",
34+
ReplaceWith("TestCoroutineScope", "kotlin.coroutines.test"),
35+
level = DeprecationLevel.WARNING)
3436
class TestCoroutineContext(private val name: String? = null) : CoroutineContext {
3537
private val uncaughtExceptions = mutableListOf<Throwable>()
3638

@@ -281,12 +283,14 @@ private class TimedRunnable(
281283
* provided instead.
282284
* @param testBody The code of the unit-test.
283285
*/
284-
@ObsoleteCoroutinesApi
286+
@Deprecated("This API has been deprecated to integrate with Structured Concurrency.",
287+
ReplaceWith("testContext.runBlockingTest(testBody)", "kotlin.coroutines.test"),
288+
level = DeprecationLevel.WARNING)
285289
public fun withTestContext(testContext: TestCoroutineContext = TestCoroutineContext(), testBody: TestCoroutineContext.() -> Unit) {
286290
with (testContext) {
287291
testBody()
288292
if (!exceptions.all { it is CancellationException }) {
289293
throw AssertionError("Coroutine encountered unhandled exceptions:\n$exceptions")
290294
}
291295
}
292-
}
296+
}

core/kotlinx-coroutines-test/src/TestBuilders.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ fun asyncTest(context: CoroutineContext? = null, testBody: TestCoroutineScope.()
8282
fun TestCoroutineScope.asyncTest(testBody: TestCoroutineScope.() -> Unit) =
8383
asyncTest(coroutineContext, testBody)
8484

85+
/**
86+
* This method is deprecated.
87+
*
88+
* @see [cleanupTestCoroutines]
89+
*/
90+
@Deprecated("This API has been deprecated to integrate with Structured Concurrency.",
91+
ReplaceWith("scope.runBlockingTest(testBody)", "kotlinx.coroutines.test"),
92+
level = DeprecationLevel.ERROR)
93+
fun withTestContext(scope: TestCoroutineScope, testBody: suspend TestCoroutineScope.() -> Unit) {
94+
scope.runBlockingTest(testBody)
95+
}
8596

8697
/**
8798
* Executes a [testBody] inside an immediate execution dispatcher.
@@ -150,15 +161,15 @@ fun runBlockingTest(context: CoroutineContext? = null, testBody: suspend TestCor
150161
/**
151162
* Convenience method for calling [runBlockingTest] on an existing [TestCoroutineScope].
152163
*/
153-
fun TestCoroutineScope.runBlockingTest(block: suspend CoroutineScope.() -> Unit) {
164+
fun TestCoroutineScope.runBlockingTest(block: suspend TestCoroutineScope.() -> Unit) {
154165
runBlockingTest(coroutineContext, block)
155166
}
156167

157168
/**
158169
* Convenience method for calling [runBlockingTest] on an existing [TestCoroutineDispatcher].
159170
*
160171
*/
161-
fun TestCoroutineDispatcher.runBlockingTest(block: suspend CoroutineScope.() -> Unit) {
172+
fun TestCoroutineDispatcher.runBlockingTest(block: suspend TestCoroutineScope.() -> Unit) {
162173
runBlockingTest(this, block)
163174
}
164175

core/kotlinx-coroutines-test/src/TestCoroutineDispatcher.kt

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,24 @@ interface DelayController {
9696
* Setting it to false will resume lazy execution.
9797
*/
9898
var dispatchImmediately: Boolean
99+
100+
@Deprecated("This API has been deprecated to integrate with Structured Concurrency.",
101+
ReplaceWith("if (targetTime > currentTime(unit)) { advanceTimeBy(targetTime - currentTime(unit), unit) }",
102+
"kotlinx.coroutines.test"),
103+
level = DeprecationLevel.WARNING)
104+
fun advanceTimeTo(targetTime: Long, unit: TimeUnit = TimeUnit.MILLISECONDS) {
105+
advanceTimeBy(targetTime - currentTime(unit), unit)
106+
}
107+
108+
@Deprecated("This API has been deprecated to integrate with Structured Concurrency.",
109+
ReplaceWith("currentTime(unit)", "kotlinx.coroutines.test"),
110+
level = DeprecationLevel.WARNING)
111+
fun now(unit: TimeUnit = TimeUnit.MILLISECONDS) = currentTime(unit)
112+
113+
@Deprecated("This API has been deprecated to integrate with Structured Concurrency.",
114+
ReplaceWith("runCurrent()", "kotlinx.coroutines.test"),
115+
level = DeprecationLevel.WARNING)
116+
fun triggerActions() = runCurrent()
99117
}
100118

101119
/**
@@ -197,7 +215,7 @@ class TestCoroutineDispatcher:
197215

198216
override fun advanceTimeBy(delayTime: Long, unit: TimeUnit): Long {
199217
val oldTime = time
200-
advanceTimeTo(oldTime + unit.toNanos(delayTime), TimeUnit.NANOSECONDS)
218+
advanceUntilTime(oldTime + unit.toNanos(delayTime), TimeUnit.NANOSECONDS)
201219
return unit.convert(time - oldTime, TimeUnit.NANOSECONDS)
202220
}
203221

@@ -207,7 +225,7 @@ class TestCoroutineDispatcher:
207225
* @param targetTime The point in time to which to move the CoroutineContext's clock.
208226
* @param unit The [TimeUnit] in which [targetTime] is expressed.
209227
*/
210-
private fun advanceTimeTo(targetTime: Long, unit: TimeUnit) {
228+
private fun advanceUntilTime(targetTime: Long, unit: TimeUnit) {
211229
val nanoTime = unit.toNanos(targetTime)
212230
triggerActions(nanoTime)
213231
if (nanoTime > time) time = nanoTime
@@ -217,7 +235,7 @@ class TestCoroutineDispatcher:
217235
val oldTime = time
218236
runCurrent()
219237
val next = queue.peek() ?: return 0
220-
advanceTimeTo(next.time, TimeUnit.NANOSECONDS)
238+
advanceUntilTime(next.time, TimeUnit.NANOSECONDS)
221239
return time - oldTime
222240
}
223241

core/kotlinx-coroutines-test/src/TestCoroutineScope.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ class TestCoroutineScope(
2525
}
2626

2727
override val coroutineContext = context
28+
29+
/**
30+
* This method is deprecated.
31+
*
32+
* @see [cleanupTestCoroutines]
33+
*/
34+
@Deprecated("This API has been deprecated to integrate with Structured Concurrency.",
35+
ReplaceWith("cleanupTestCoroutines()"),
36+
level = DeprecationLevel.WARNING)
37+
fun cancelAllActions() = cleanupTestCoroutines()
2838
}
2939

3040
fun TestCoroutineScope(dispatcher: TestCoroutineDispatcher) =

0 commit comments

Comments
 (0)