Repository navigation
Remove the deprecated kotlinx-coroutines-test APIs - #4604
Conversation
* Remove the APIs that were deprecated with an ERROR. * Advance the deprecations for `runTest` overloads accepting the dispatch timeout to `ERROR`. * Reword the documentation so that it no longer references the removed APIs as a point of comparison. * Remove the migration guide from the obsolete APIs. * Remove the code that was only used by the now-removed APIs and simplify some of the remaining code.
By default, `runTest` detects if some uncaught exceptions happened after the last `runTest` invocation, and if so, fail to execute its own test. This is done so that uncaught exceptions get reported at least somehow; we don't have a better place to report them. There is a hidden code path that gets triggered when `internal var catchNonTestRelatedExceptions: Boolean = true` is set to `false` that disables this behavior. Normally, this can only be done via reflection, and we intentionally avoid advertising its existence. It turns out, the hidden code path didn't work anyway. `TestScope.leave()` would trigger a failed `check` in `ExceptionCollector`. This was reported to us by a user who discovered the hidden path and was desperate to suppress the failures due to uncaught exceptions for the migration to `kotlinx-coroutines-test`. Since we do have the hidden path, we should at least ensure it works.
| /** | ||
| * Checks that the only tasks remaining in the scheduler are cancelled. | ||
| */ | ||
| internal fun isIdle(strict: Boolean = true): Boolean = | ||
| synchronized(lock) { | ||
| if (strict) events.isEmpty else events.none { !it.isCancelled() } | ||
| } | ||
| internal fun isIdle(): Boolean = | ||
| synchronized(lock) { events.isEmpty } |
There was a problem hiding this comment.
The kdoc also needs a rewording?
| /** | ||
| * Checks that the only tasks remaining in the scheduler are cancelled. | ||
| */ | ||
| internal fun isIdle(strict: Boolean = true): Boolean = | ||
| synchronized(lock) { | ||
| if (strict) events.isEmpty else events.none { !it.isCancelled() } | ||
| } | ||
| internal fun isIdle(): Boolean = | ||
| synchronized(lock) { events.isEmpty } |
There was a problem hiding this comment.
Also doesn't need the synchronized now, since events is thread-safe by itself, and, in particular, isEmpty/size is atomic.
There was a problem hiding this comment.
In general, such an argument doesn't hold: thread safety/atomicity is not enough to remove a mutex. See:
synchronized { list.remove(0); list.add('x') } | synchronized { list.isEmpty() }
If we remove synchronized around isEmpty, it may start observing an intermediate state where the list is empty, even if logically, the other operation was replacing one value with another.
In this specific case, not synchronizing isIdle does feel safe, but I'd rather not dive too deep nor touch it: it's only called from a deprecated-with-error function and will be removed in the next release anyway.
| * The usual way to access a [TestScope] is to call [runTest], but it can also be constructed manually, in order to | ||
| * use it to initialize the components that participate in the test. | ||
| * | ||
| * #### Differences from the deprecated [TestCoroutineScope] |
There was a problem hiding this comment.
Is it our general policy to remove docs on the deprecated stuff when it is promoted to Error? I can imagine how some projects postpone updating usages while it is in Warning, and are only update once they are forced to do it when it is promoted to Error. It is unfortunate that by that point all the useful details are gone from the kdocs!
I also understand that keeping details on APIs that no longer compile is also undesirable. Just wondering what's the user journey of a procrastinating project would be.
There was a problem hiding this comment.
Is it our general policy to remove docs on the deprecated stuff when it is promoted to Error?
I think the docs should get removed when the API is removed/hidden. If you encounter an error, you may be forced to deal with it after having ignored the warnings, and that means finally learning what the API you used to use does.
TestCoroutineScope was deprecated with ERROR in 1.9.0 (two major releases ago, in 2024), and this PR removes it entirely. The warnings have been there for five years.
| * If you need to have a specific [CoroutineExceptionHandler], please pass it to [launch] on an already-created | ||
| * [TestCoroutineScope] and share your use case at | ||
| * [our issue tracker](https://github.com/Kotlin/kotlinx.coroutines/issues). | ||
| * - A [CoroutineExceptionHandler] is created that makes [runTest] throw after the test is finished if there were | ||
| * any uncaught exceptions, or forwards the exceptions further in a platform-specific manner if by the time an | ||
| * exception happened, the test has completed. Passing a [CoroutineExceptionHandler] in [context] is unsupported. |
There was a problem hiding this comment.
This info is removed from the new kdoc but is kept in the implementation. Is this intentional?
If you need to have a specific [CoroutineExceptionHandler], please pass it to [launch] on an already-created [TestCoroutineScope] and share your use case at [our issue tracker]
There was a problem hiding this comment.
Yeah, the notice has been there since the initial introduction of the API, and no one ever approached us about this.
This PR contains the following updates: | Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) | |---|---|---|---| | [org.jetbrains.kotlinx:kotlinx-coroutines-test](https://github.com/Kotlin/kotlinx.coroutines) | `1.10.1` → `1.11.0` |  |  | | [org.jetbrains.kotlinx:kotlinx-coroutines-android](https://github.com/Kotlin/kotlinx.coroutines) | `1.10.1` → `1.11.0` |  |  | | [org.jetbrains.kotlinx:kotlinx-coroutines-core](https://github.com/Kotlin/kotlinx.coroutines) | `1.10.1` → `1.11.0` |  |  | --- ### Release Notes <details> <summary>Kotlin/kotlinx.coroutines (org.jetbrains.kotlinx:kotlinx-coroutines-test)</summary> ### [`v1.11.0`](https://github.com/Kotlin/kotlinx.coroutines/blob/HEAD/CHANGES.md#Version-1110) [Compare Source](Kotlin/kotlinx.coroutines@1.10.2...1.11.0) ##### Various - Kotlin was updated to 2.2.20 ([#​4545](Kotlin/kotlinx.coroutines#4545)). - Improved the published jar files ([#​3842](Kotlin/kotlinx.coroutines#3842), [#​4599](Kotlin/kotlinx.coroutines#4599)). - Various documentation improvements, including complete rewrites of structured concurrency and error handling-related KDoc ([#​4433](Kotlin/kotlinx.coroutines#4433), [#​4596](Kotlin/kotlinx.coroutines#4596)). ##### Breaking changes and deprecations - Moved `Promise`-related functions from JS and Wasm/JS to the new `web` target. On Wasm/JS, this is a breaking change. Before the change, `Promise` on Wasm/JS could work with arbitrary Kotlin types, but now, only `JsAny` subtypes are accepted ([#​4563](Kotlin/kotlinx.coroutines#4563)). - Changed handling of coroutine exceptions that can't be propagated on JS and Wasm/JS. Before, exceptions were logged, but now, they are reported to the JS runtime ([#​4451](Kotlin/kotlinx.coroutines#4451), [#​4631](Kotlin/kotlinx.coroutines#4631)). - Deprecated using `CoroutineDispatcher` as the coroutine context key; now, `ContinuationInterceptor` has to be used instead ([#​4333](Kotlin/kotlinx.coroutines#4333)). - Advanced the deprecation levels on `kotlinx-coroutines-test` APIs ([#​4604](Kotlin/kotlinx.coroutines#4604)). - Added lint functions that mark passing a `Job` to coroutine builders as deprecated ([#​4435](Kotlin/kotlinx.coroutines#4435)). ##### Bug fixes and improvements - Added a `callsInPlace(EXACTLY_ONCE)` contract to `runBlocking` in code shared between JVM and Native ([#​4368](Kotlin/kotlinx.coroutines#4368)). - Added a `callsInPlace(EXACTLY_ONCE)` contract to `suspendCancellableCoroutine` ([#​4574](Kotlin/kotlinx.coroutines#4574)). - Fixed `flowOn` incorrectly handling `ThreadContextElement` updates ([#​4403](Kotlin/kotlinx.coroutines#4403)). - Fixed exceptions in user-supplied `Thread.UncaughtExceptionHandler` instances causing the internal coroutines machinery to fail ([#​4516](Kotlin/kotlinx.coroutines#4516)). - Fixed `CoroutineDispatcher.asScheduler` in the RxJava integration not cancelling outstanding work when a `Worker` gets cancelled, which led to memory leaks in some scenarios ([#​4615](Kotlin/kotlinx.coroutines#4615)). - Fixed `SharedFlow` entering an invalid state when a subscriber and an emitter are cancelled simultaneously ([#​4583](Kotlin/kotlinx.coroutines#4583)). - Fixed an R8 optimization leading to `shareIn`/`stateIn` coroutines getting garbage-collected ([#​4646](Kotlin/kotlinx.coroutines#4646)). Thanks, [@​solevic](https://github.com/solevic)! ##### Small additions - Added `CompletableDeferred.asDeferred` for obtaining a read-only `Deferred` view ([#​4408](Kotlin/kotlinx.coroutines#4408)). - Added `SharedFlow.asFlow` for obtaining a `Flow` view with hidden hot flow semantics ([#​4530](Kotlin/kotlinx.coroutines#4530)). Thanks, [@​g000sha256](https://github.com/g000sha256)! - Added a `StateFlow.collectLatest` overload returning `Nothing` to assist with finding unreachable code ([#​4454](Kotlin/kotlinx.coroutines#4454)). - Added `ReceiveChannel.consumeTo` for consuming a `ReceiveChannel` into a `MutableCollection` ([#​4520](Kotlin/kotlinx.coroutines#4520)). - Added a `StateFlow<T>.onSubscription` overload returning a `StateFlow<T>`, similar to `SharedFlow<T>.onSubscription` returning `SharedFlow<T>` ([#​4275](Kotlin/kotlinx.coroutines#4275)). Thanks, [@​xit0c](https://github.com/xit0c)! - Added terminal `Flow` operators for collecting a `Flow` to a `Map` ([#​1541](Kotlin/kotlinx.coroutines#1541)). ##### Changelog relative to version 1.11.0 No changes, only the version is increased. ### [`v1.10.2`](https://github.com/Kotlin/kotlinx.coroutines/blob/HEAD/CHANGES.md#Version-1102) [Compare Source](Kotlin/kotlinx.coroutines@1.10.1...1.10.2) - Fixed the `kotlinx-coroutines-debug` JAR file including the `module-info.class` file twice, resulting in failures in various tooling ([#​4314](Kotlin/kotlinx.coroutines#4314)). Thanks, [@​RyuNen344](https://github.com/RyuNen344)! - Fixed `Flow.stateIn` hanging when the scope is cancelled in advance or the flow is empty ([#​4322](Kotlin/kotlinx.coroutines#4322)). Thanks, [@​francescotescari](https://github.com/francescotescari)! - Improved handling of dispatcher failures in `.limitedParallelism` ([#​4330](Kotlin/kotlinx.coroutines#4330)) and during flow collection ([#​4272](Kotlin/kotlinx.coroutines#4272)). - Fixed `runBlocking` failing to run its coroutine to completion in some cases if its JVM thread got interrupted ([#​4399](Kotlin/kotlinx.coroutines#4399)). - Small tweaks, fixes, and documentation improvements. </details> --- ### Configuration 📅 **Schedule**: (UTC) - Branch creation - "before 9am on tuesday" - Automerge - At any time (no schedule defined) 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about these updates again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate CLI](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0NC4xMzMuMCIsInVwZGF0ZWRJblZlciI6IjQ0LjEzMy4wIiwidGFyZ2V0QnJhbmNoIjoiZGV2ZWxvcCIsImxhYmVscyI6WyJkZXBlbmRlbmNpZXMiLCJyZW5vdmF0ZSJdfQ==--> Reviewed-on: https://code.futo.org/harbor/harbor/pulls/1829
ERROR.runTestoverloads accepting the dispatch timeout toERROR.