diff --git a/.gitignore b/.gitignore index c447f28b73..aed7103292 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,13 @@ -.idea +**/.idea/* +!/.idea/icon.png +!/.idea/vcs.xml +!/.idea/copyright +!/.idea/codeStyleSettings.xml +!/.idea/codeStyles *.iml .gradle .gradletasknamecache build out target -local.properties \ No newline at end of file +local.properties diff --git a/.idea/codeStyleSettings.xml b/.idea/codeStyleSettings.xml new file mode 100644 index 0000000000..2287edb44c --- /dev/null +++ b/.idea/codeStyleSettings.xml @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000000..62fd5c7dfd --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,15 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000000..79ee123c2b --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/copyright/kotlinx_coroutines.xml b/.idea/copyright/kotlinx_coroutines.xml new file mode 100644 index 0000000000..f175624311 --- /dev/null +++ b/.idea/copyright/kotlinx_coroutines.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000000..5e22a9977e --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/icon.png b/.idea/icon.png new file mode 100644 index 0000000000..d957c64af5 Binary files /dev/null and b/.idea/icon.png differ diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000..fd137b62c9 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/CHANGES.md b/CHANGES.md index 9043d27b57..bce941c70b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,119 @@ # Change log for kotlinx.coroutines +## Version 1.4.0 + +### Improvements + +* `StateFlow`, `SharedFlow` and corresponding operators are promoted to stable API (#2316). +* `Flow.debounce` operator with timeout selector based on each individual element is added (#1216, thanks to @mkano9!). +* `CoroutineContext.job` extension property is introduced (#2159). +* `Flow.combine operator` is reworked: + * Complete fairness is maintained for single-threaded dispatchers. + * Its performance is improved, depending on the use-case, by at least 50% (#2296). + * Quadratic complexity depending on the number of upstream flows is eliminated (#2296). + * `crossinline` and `inline`-heavy internals are removed, fixing sporadic SIGSEGV on Mediatek Android devices (#1683, #1743). +* `Flow.zip` operator performance is improved by 40%. +* Various API has been promoted to stable or its deprecation level has been raised (#2316). + +### Bug fixes + +* Suspendable `stateIn` operator propagates exception to the caller when upstream fails to produce initial value (#2329). +* Fix `SharedFlow` with replay for subscribers working at different speed (#2325). +* Do not fail debug agent installation when security manager does not provide access to system properties (#2311). +* Cancelled lazy coroutines are properly cleaned up from debug agent output (#2294). +* `BlockHound` false-positives are correctly filtered out (#2302, #2190, #2303). +* Potential crash during a race between cancellation and upstream in `Observable.asFlow` is fixed (#2104, #2299, thanks to @LouisCAD and @drinkthestars). + +## Version 1.4.0-M1 + +### Breaking changes + +* The concept of atomic cancellation in channels is removed. All operations in channels + and corresponding `Flow` operators are cancellable in non-atomic way (#1813). +* If `CoroutineDispatcher` throws `RejectedExecutionException`, cancel current `Job` and schedule its execution to `Dispatchers.IO` (#2003). +* `CancellableContinuation.invokeOnCancellation` is invoked if the continuation was cancelled while its resume has been dispatched (#1915). +* `Flow.singleOrNull` operator is aligned with standard library and does not longer throw `IllegalStateException` on multiple values (#2289). + +### New experimental features + +* `SharedFlow` primitive for managing hot sources of events with support of various subscription mechanisms, replay logs and buffering (#2034). +* `Flow.shareIn` and `Flow.stateIn` operators to transform cold instances of flow to hot `SharedFlow` and `StateFlow` respectively (#2047). + +### Other + +* Support leak-free closeable resources transfer via `onUndeliveredElement` in channels (#1936). +* Changed ABI in reactive integrations for Java interoperability (#2182). +* Fixed ProGuard rules for `kotlinx-coroutines-core` (#2046, #2266). +* Lint settings were added to `Flow` to avoid accidental capturing of outer `CoroutineScope` for cancellation check (#2038). + +### External contributions + +* Allow nullable types in `Flow.firstOrNull` and `Flow.singleOrNull` by @ansman (#2229). +* Add `Publisher.awaitSingleOrDefault|Null|Else` extensions by @sdeleuze (#1993). +* `awaitCancellation` top-level function by @LouisCAD (#2213). +* Significant part of our Gradle build scripts were migrated to `.kts` by @turansky. + +Thank you for your contributions and participation in the Kotlin community! + +## Version 1.3.9 + +* Support of `CoroutineContext` in `Flow.asPublisher` and similar reactive builders (#2155). +* Kotlin updated to 1.4.0. +* Transition to new HMPP publication scheme for multiplatform usages: + * Artifacts `kotlinx-coroutines-core-common` and `kotlinx-coroutines-core-native` are removed. + * For multiplatform usages, it's enough to [depend directly](README.md#multiplatform) on `kotlinx-coroutines-core` in `commonMain` source-set. + * The same artifact coordinates can be used to depend on platform-specific artifact in platform-specific source-set. + +## Version 1.3.8 + +### New experimental features + +* Added `Flow.transformWhile operator` (#2065). +* Replaced `scanReduce` with `runningReduce` to be consistent with the Kotlin standard library (#2139). + +### Bug fixes and improvements + +* Improve user experience for the upcoming coroutines debugger (#2093, #2118, #2131). +* Debugger no longer retains strong references to the running coroutines (#2129). +* Fixed race in `Flow.asPublisher` (#2109). +* Fixed `ensureActive` to work in the empty context case to fix `IllegalStateException` when using flow from `suspend fun main` (#2044). +* Fixed a problem with `AbortFlowException` in the `Flow.first` operator to avoid erroneous `NoSuchElementException` (#2051). +* Fixed JVM dependency on Android annotations (#2075). +* Removed keep rules mentioning `kotlinx.coroutines.android` from core module (#2061 by @mkj-gram). +* Corrected some docs and examples (#2062, #2071, #2076, #2107, #2098, #2127, #2078, #2135). +* Improved the docs and guide on flow cancellation (#2043). +* Updated Gradle version to `6.3` (it only affects multiplatform artifacts in this release). + +## Version 1.3.7 + +* Fixed problem that triggered Android Lint failure (#2004). +* New `Flow.cancellable()` operator for cooperative cancellation (#2026). +* Emissions from `flow` builder now check cancellation status and are properly cancellable (#2026). +* New `currentCoroutineContext` function to use unambiguously in the contexts with `CoroutineScope` in receiver position (#2026). +* `EXACTLY_ONCE` contract support in coroutine builders. +* Various documentation improvements. + +## Version 1.3.6 + +### Flow + +* `StateFlow`, new primitive for state handling (#1973, #1816, #395). The `StateFlow` is designed to eventually replace `ConflatedBroadcastChannel` for state publication scenarios. Please, try it and share your feedback. Note, that Flow-based primitives to publish events will be added later. For events you should continue to either use `BroadcastChannel(1)`, if you put events into the `StateFlow`, protect them from double-processing with flags. +* `Flow.onEmpty` operator is introduced (#1890). +* Behavioural change in `Flow.onCompletion`, it is aligned with `invokeOnCompletion` now and passes `CancellationException` to its cause parameter (#1693). +* A lot of Flow operators have left its experimental status and are promoted to stable API. + +### Other + +* `runInterruptible` primitive to tie cancellation with thread interruption for blocking calls. Contributed by @jxdabc (#1947). +* Integration module with RxJava3 is introduced. Contributed by @ZacSweers (#1883) +* Integration with [BlockHound](https://github.com/reactor/BlockHound) in `kotlinx-coroutines-debug` module (#1821, #1060). +* Memory leak in ArrayBroadcastChannel is fixed (#1885). +* Behavioural change in `suspendCancellableCoroutine`, cancellation is established before invoking passed block argument (#1671). +* Debug agent internals are moved into `kotlinx-coroutines-core` for better integration with IDEA. It should not affect library users and all the redundant code should be properly eliminated with R8. +* ClassCastException with reusable continuations bug is fixed (#1966). +* More precise scheduler detection for `Executor.asCoroutineDispatcher` (#1992). +* Kotlin updated to 1.3.71. + ## Version 1.3.5 * `firstOrNull` operator. Contributed by @bradynpoulsen. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000000..7737062fa3 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,113 @@ +# Contributing Guidelines + +There are two main ways to contribute to the project — submitting issues and submitting +fixes/changes/improvements via pull requests. + +## Submitting issues + +Both bug reports and feature requests are welcome. +Submit issues [here](https://github.com/Kotlin/kotlinx.coroutines/issues). + +* Search for existing issues to avoid reporting duplicates. +* When submitting a bug report: + * Test it against the most recently released version. It might have been already fixed. + * By default, we assume that your problem reproduces in Kotlin/JVM. Please, mention if the problem is + specific to Kotlin/JS or Kotlin/Native. + * Include the code that reproduces the problem. Provide the complete reproducer code, yet minimize it as much as possible. + * However, don't put off reporting any weird or rarely appearing issues just because you cannot consistently + reproduce them. + * If the bug is in behavior, then explain what behavior you've expected and what you've got. +* When submitting a feature request: + * Explain why you need the feature — what's your use-case, what's your domain. + * Explaining the problem you face is more important than suggesting a solution. + Report your problem even if you don't have any proposed solution. + * If there is an alternative way to do what you need, then show the code of the alternative. + +## Submitting PRs + +We love PRs. Submit PRs [here](https://github.com/Kotlin/kotlinx.coroutines/pulls). +However, please keep in mind that maintainers will have to support the resulting code of the project, +so do familiarize yourself with the following guidelines. + +* All development (both new features and bug fixes) is performed in the `develop` branch. + * The `master` branch always contains sources of the most recently released version. + * Base PRs against the `develop` branch. + * The `develop` branch is pushed to the `master` branch during release. + * Documentation in markdown files can be updated directly in the `master` branch, + unless the documentation is in the source code, and the patch changes line numbers. +* If you fix documentation: + * After fixing/changing code examples in the [`docs`](docs) folder or updating any references in the markdown files + run the [Knit tool](#running-the-knit-tool) and commit the resulting changes as well. + It will not pass the tests otherwise. + * If you plan extensive rewrites/additions to the docs, then please [contact the maintainers](#contacting-maintainers) + to coordinate the work in advance. +* If you make any code changes: + * Follow the [Kotlin Coding Conventions](https://kotlinlang.org/docs/reference/coding-conventions.html). + Use 4 spaces for indentation. + * [Build the project](#building) to make sure it all works and passes the tests. +* If you fix a bug: + * Write the test that reproduces the bug. + * Fixes without tests are accepted only in exceptional circumstances if it can be shown that writing the + corresponding test is too hard or otherwise impractical. + * Follow the style of writing tests that is used in this project: + name test functions as `testXxx`. Don't use backticks in test names. +* If you introduce any new public APIs: + * All new APIs must come with documentation and tests. + * All new APIs are initially released with `@ExperimentalCoroutineApi` annotation and are graduated later. + * [Update the public API dumps](#updating-the-public-api-dump) and commit the resulting changes as well. + It will not pass the tests otherwise. + * If you plan large API additions, then please start by submitting an issue with the proposed API design + to gather community feedback. + * [Contact the maintainers](#contacting-maintainers) to coordinate any big piece of work in advance. +* Comment on the existing issue if you want to work on it. Ensure that the issue not only describes a problem, + but also describes a solution that had received a positive feedback. Propose a solution if there isn't any. +* Steps for contributing new integration modules are explained [here](integration/README.md#Contributing). + +## Building + +This library is built with Gradle. + +* Run `./gradlew build` to build. It also runs all the tests. +* Run `./gradlew :test` to test the module you are looking at to speed + things up during development. +* Run `./gradlew jvmTest` to perform only fast JVM tests of the core multiplatform module. + +You can import this project into IDEA, but you have to delegate build actions +to Gradle (in Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle -> Runner) + +### Environment requirements + +* JDK >= 11 referred to by the `JAVA_HOME` environment variable. +* JDK 1.6 referred to by the `JDK_16` environment variable. + It is OK to have `JDK_16` pointing to a non 1.6 JDK (e.g. `JAVA_HOME`) for external contributions. +* JDK 1.8 referred to by the `JDK_18` environment variable. Only used by nightly stress-tests. + It is OK to have `JDK_18` to a non 1.8 JDK (e.g. `JAVA_HOME`) for external contributions. + +For external contributions you can for example add this to your shell startup scripts (e.g. `~/.zshrc`): +```shell +# This assumes JAVA_HOME is set already to a JDK >= 11 version +export JDK_16="$JAVA_HOME" +export JDK_18="$JAVA_HOME" +``` + +### Running the Knit tool + +* Use [Knit](https://github.com/Kotlin/kotlinx-knit/blob/master/README.md) for updates to documentation: + * Run `./gradlew knit` to update example files, links, tables of content. + * Commit updated documents and examples together with other changes. + +### Updating the public API dump + +* Use [Binary Compatibility Validator](https://github.com/Kotlin/binary-compatibility-validator/blob/master/README.md) for updates to public API: + * Run `./gradlew apiDump` to update API index files. + * Commit updated API indexes together with other changes. + +## Releases + +* Full release procedure checklist is [here](RELEASE.md). + +## Contacting maintainers + +* If something cannot be done, not convenient, or does not work — submit an [issue](#submitting-issues). +* "How to do something" questions — [StackOverflow](https://stackoverflow.com). +* Discussions and general inquiries — use `#coroutines` channel in [KotlinLang Slack](https://kotl.in/slack). diff --git a/LICENSE.txt b/LICENSE.txt index dead69c1be..9c308d958b 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,13 +1,202 @@ -Copyright 2016-2019 JetBrains s.r.o. -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ -http://www.apache.org/licenses/LICENSE-2.0 + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. \ No newline at end of file + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2000-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/README.md b/README.md index b5456857fe..2bc45cf451 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,12 @@ [![official JetBrains project](https://jb.gg/badges/official.svg)](https://confluence.jetbrains.com/display/ALL/JetBrains+on+GitHub) [![GitHub license](https://img.shields.io/badge/license-Apache%20License%202.0-blue.svg?style=flat)](https://www.apache.org/licenses/LICENSE-2.0) -[![Download](https://api.bintray.com/packages/kotlin/kotlinx/kotlinx.coroutines/images/download.svg?version=1.3.5) ](https://bintray.com/kotlin/kotlinx/kotlinx.coroutines/1.3.5) +[![Download](https://api.bintray.com/packages/kotlin/kotlinx/kotlinx.coroutines/images/download.svg?version=1.4.0) ](https://bintray.com/kotlin/kotlinx/kotlinx.coroutines/1.4.0) +[![Kotlin](https://img.shields.io/badge/kotlin-1.4.0-blue.svg?logo=kotlin)](http://kotlinlang.org) +[![Slack channel](https://img.shields.io/badge/chat-slack-green.svg?logo=slack)](https://kotlinlang.slack.com/messages/coroutines/) Library support for Kotlin coroutines with [multiplatform](#multiplatform) support. -This is a companion version for Kotlin `1.3.70` release. +This is a companion version for Kotlin `1.4.0` release. ```kotlin suspend fun main() = coroutineScope { @@ -27,7 +29,7 @@ suspend fun main() = coroutineScope { * [delay] and [yield] top-level suspending functions; * [Flow] — cold asynchronous stream with [flow][_flow] builder and comprehensive operator set ([filter], [map], etc); * [Channel], [Mutex], and [Semaphore] communication and synchronization primitives; - * [coroutineScope], [supervisorScope], [withContext], and [withTimeout] scope builders; + * [coroutineScope][_coroutineScope], [supervisorScope][_supervisorScope], [withContext], and [withTimeout] scope builders; * [MainScope()] for Android and UI applications; * [SupervisorJob()] and [CoroutineExceptionHandler] for supervision of coroutines hierarchies; * [select] expression support and more. @@ -44,9 +46,10 @@ suspend fun main() = coroutineScope { * [DebugProbes] API to probe, keep track of, print and dump active coroutines; * [CoroutinesTimeout] test rule to automatically dump coroutines on test timeout. * [reactive](reactive/README.md) — modules that provide builders and iteration support for various reactive streams libraries: - * Reactive Streams ([Publisher.collect], [Publisher.awaitSingle], [publish], etc), + * Reactive Streams ([Publisher.collect], [Publisher.awaitSingle], [kotlinx.coroutines.reactive.publish], etc), * Flow (JDK 9) (the same interface as for Reactive Streams), * RxJava 2.x ([rxFlowable], [rxSingle], etc), and + * RxJava 3.x ([rxFlowable], [rxSingle], etc), and * Project Reactor ([flux], [mono], etc). * [ui](ui/README.md) — modules that provide coroutine dispatchers for various single-threaded UI libraries: * Android, JavaFX, and Swing. @@ -83,7 +86,7 @@ Add dependencies (you can also add other modules that you need): org.jetbrains.kotlinx kotlinx-coroutines-core - 1.3.5 + 1.4.0 ``` @@ -91,7 +94,7 @@ And make sure that you use the latest Kotlin version: ```xml - 1.3.70 + 1.4.0 ``` @@ -101,7 +104,7 @@ Add dependencies (you can also add other modules that you need): ```groovy dependencies { - implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5' + implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.0' } ``` @@ -109,7 +112,7 @@ And make sure that you use the latest Kotlin version: ```groovy buildscript { - ext.kotlin_version = '1.3.70' + ext.kotlin_version = '1.4.0' } ``` @@ -127,7 +130,7 @@ Add dependencies (you can also add other modules that you need): ```groovy dependencies { - implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.0") } ``` @@ -135,7 +138,7 @@ And make sure that you use the latest Kotlin version: ```groovy plugins { - kotlin("jvm") version "1.3.70" + kotlin("jvm") version "1.4.0" } ``` @@ -145,9 +148,14 @@ Make sure that you have either `jcenter()` or `mavenCentral()` in the list of re Core modules of `kotlinx.coroutines` are also available for [Kotlin/JS](#js) and [Kotlin/Native](#native). -In common code that should get compiled for different platforms, add dependency to -[`kotlinx-coroutines-core-common`](https://search.maven.org/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core-common/1.3.5/jar) -(follow the link to get the dependency declaration snippet). +In common code that should get compiled for different platforms, you can add dependency to `kotlinx-coroutines-core` right to the `commonMain` source set: +```groovy +commonMain { + dependencies { + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.0") + } +} +``` ### Android @@ -155,7 +163,7 @@ Add [`kotlinx-coroutines-android`](ui/kotlinx-coroutines-android) module as dependency when using `kotlinx.coroutines` on Android: ```groovy -implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.5' +implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.0' ``` This gives you access to Android [Dispatchers.Main] @@ -168,10 +176,21 @@ threads are handled by Android runtime. R8 and ProGuard rules are bundled into the [`kotlinx-coroutines-android`](ui/kotlinx-coroutines-android) module. For more details see ["Optimization" section for Android](ui/kotlinx-coroutines-android/README.md#optimization). +#### Avoiding including the debug infrastructure in the resulting APK + +The `kotlinx-coroutines-core` artifact contains a resource file that is not required for the coroutines to operate +normally and is only used by the debugger. To exclude it at no loss of functionality, add the following snippet to the +`android` block in your gradle file for the application subproject: +```groovy +packagingOptions { + exclude "DebugProbesKt.bin" +} +``` + ### JS [Kotlin/JS](https://kotlinlang.org/docs/reference/js-overview.html) version of `kotlinx.coroutines` is published as -[`kotlinx-coroutines-core-js`](https://search.maven.org/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core-js/1.3.5/jar) +[`kotlinx-coroutines-core-js`](https://search.maven.org/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core-js/1.4.0/jar) (follow the link to get the dependency declaration snippet). You can also use [`kotlinx-coroutines-core`](https://www.npmjs.com/package/kotlinx-coroutines-core) package via NPM. @@ -179,7 +198,7 @@ You can also use [`kotlinx-coroutines-core`](https://www.npmjs.com/package/kotli ### Native [Kotlin/Native](https://kotlinlang.org/docs/reference/native-overview.html) version of `kotlinx.coroutines` is published as -[`kotlinx-coroutines-core-native`](https://search.maven.org/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core-native/1.3.5/jar) +[`kotlinx-coroutines-core-native`](https://search.maven.org/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core-native/1.4.0/jar) (follow the link to get the dependency declaration snippet). Only single-threaded code (JS-style) on Kotlin/Native is currently supported. @@ -193,35 +212,9 @@ enableFeaturePreview('GRADLE_METADATA') Since Kotlin/Native does not generally provide binary compatibility between versions, you should use the same version of Kotlin/Native compiler as was used to build `kotlinx.coroutines`. -## Building - -This library is built with Gradle. To build it, use `./gradlew build`. -You can import this project into IDEA, but you have to delegate build actions -to Gradle (in Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle -> Runner) - -### Requirements - -* JDK >= 11 referred to by the `JAVA_HOME` environment variable. -* JDK 1.6 referred to by the `JDK_16` environment variable. It is okay to have `JDK_16` pointing to `JAVA_HOME` for external contributions. -* JDK 1.8 referred to by the `JDK_18` environment variable. Only used by nightly stress-tests. It is okay to have `JDK_16` pointing to `JAVA_HOME` for external contributions. +## Building and Contributing -## Contributions and releases - -All development (both new features and bug fixes) is performed in `develop` branch. -This way `master` sources always contain sources of the most recently released version. -Please send PRs with bug fixes to `develop` branch. -Fixes to documentation in markdown files are an exception to this rule. They are updated directly in `master`. - -The `develop` branch is pushed to `master` during release. - -* Full release procedure checklist is [here](RELEASE.md). -* Steps for contributing new integration modules are explained [here](integration/README.md#Contributing). -* Use [Knit](https://github.com/Kotlin/kotlinx-knit/blob/master/README.md) for updates to documentation: - * In project root directory run `./gradlew knit`. - * Commit updated documents and examples together with other changes. -* Use [Binary Compatibility Validator](https://github.com/Kotlin/binary-compatibility-validator/blob/master/README.md) for updates to public API: - * In project root directory run `./gradlew apiDump`. - * Commit updated API index together with other changes. +See [Contributing Guidelines](CONTRIBUTING.md). @@ -234,8 +227,8 @@ The `develop` branch is pushed to `master` during release. [Dispatchers.Default]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-default.html [delay]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/delay.html [yield]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/yield.html -[coroutineScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/coroutine-scope.html -[supervisorScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/supervisor-scope.html +[_coroutineScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/coroutine-scope.html +[_supervisorScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/supervisor-scope.html [withContext]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/with-context.html [withTimeout]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/with-timeout.html [MainScope()]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-main-scope.html @@ -283,11 +276,13 @@ The `develop` branch is pushed to `master` during release. [Publisher.collect]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-reactive/kotlinx.coroutines.reactive/org.reactivestreams.-publisher/collect.html [Publisher.awaitSingle]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-reactive/kotlinx.coroutines.reactive/org.reactivestreams.-publisher/await-single.html -[publish]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-reactive/kotlinx.coroutines.reactive/publish.html +[kotlinx.coroutines.reactive.publish]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-reactive/kotlinx.coroutines.reactive/publish.html [rxFlowable]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx2/kotlinx.coroutines.rx2/rx-flowable.html [rxSingle]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx2/kotlinx.coroutines.rx2/rx-single.html + + [flux]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-reactor/kotlinx.coroutines.reactor/flux.html diff --git a/RELEASE.md b/RELEASE.md index efb361f1e5..22cb61c42f 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -64,18 +64,14 @@ To release new `` of `kotlinx-coroutines`: 5. Announce new release in [Slack](https://kotlinlang.slack.com) -6. Create a ticket to update coroutines version on [try.kotlinlang.org](try.kotlinlang.org). - * Use [KT-30870](https://youtrack.jetbrains.com/issue/KT-30870) as a template - * This step should be skipped for eap versions that are not merged to `master` - -7. Switch into `develop` branch:
+6. Switch into `develop` branch:
`git checkout develop` -8. Fetch the latest `master`:
+7. Fetch the latest `master`:
`git fetch` -9. Merge release from `master`:
+8. Merge release from `master`:
`git merge origin/master` -0. Push updates to `develop`:
+9. Push updates to `develop`:
`git push` diff --git a/benchmarks/build.gradle b/benchmarks/build.gradle deleted file mode 100644 index a192f2795e..0000000000 --- a/benchmarks/build.gradle +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ -sourceCompatibility = 1.8 -targetCompatibility = 1.8 - -apply plugin: "net.ltgt.apt" -apply plugin: "com.github.johnrengelman.shadow" -apply plugin: "me.champeau.gradle.jmh" - -repositories { - maven { url "https://repo.typesafe.com/typesafe/releases/" } -} - -compileJmhKotlin { - kotlinOptions { - jvmTarget = "1.8" - freeCompilerArgs += ['-Xjvm-default=enable'] - } -} - -/* - * Due to a bug in the inliner it sometimes does not remove inlined symbols (that are later renamed) from unused code paths, - * and it breaks JMH that tries to post-process these symbols and fails because they are renamed. - */ -task removeRedundantFiles(type: Delete) { - delete "$buildDir/classes/kotlin/jmh/benchmarks/flow/scrabble/FlowPlaysScrabbleOpt\$play\$buildHistoOnScore\$1\$\$special\$\$inlined\$filter\$1\$1.class" - delete "$buildDir/classes/kotlin/jmh/benchmarks/flow/scrabble/FlowPlaysScrabbleOpt\$play\$nBlanks\$1\$\$special\$\$inlined\$map\$1\$1.class" - delete "$buildDir/classes/kotlin/jmh/benchmarks/flow/scrabble/FlowPlaysScrabbleOpt\$play\$score2\$1\$\$special\$\$inlined\$map\$1\$1.class" - delete "$buildDir/classes/kotlin/jmh/benchmarks/flow/scrabble/FlowPlaysScrabbleOpt\$play\$bonusForDoubleLetter\$1\$\$special\$\$inlined\$map\$1\$1.class" - delete "$buildDir/classes/kotlin/jmh/benchmarks/flow/scrabble/FlowPlaysScrabbleOpt\$play\$nBlanks\$1\$\$special\$\$inlined\$map\$1\$2\$1.class" - delete "$buildDir/classes/kotlin/jmh/benchmarks/flow/scrabble/FlowPlaysScrabbleOpt\$play\$bonusForDoubleLetter\$1\$\$special\$\$inlined\$map\$1\$2\$1.class" - delete "$buildDir/classes/kotlin/jmh/benchmarks/flow/scrabble/FlowPlaysScrabbleOpt\$play\$score2\$1\$\$special\$\$inlined\$map\$1\$2\$1.class" - delete "$buildDir/classes/kotlin/jmh/benchmarks/flow/scrabble/FlowPlaysScrabbleOptKt\$\$special\$\$inlined\$collect\$1\$1.class" - delete "$buildDir/classes/kotlin/jmh/benchmarks/flow/scrabble/FlowPlaysScrabbleOptKt\$\$special\$\$inlined\$collect\$2\$1.class" - delete "$buildDir/classes/kotlin/jmh/benchmarks/flow/scrabble/FlowPlaysScrabbleOpt\$play\$histoOfLetters\$1\$\$special\$\$inlined\$fold\$1\$1.class" - delete "$buildDir/classes/kotlin/jmh/benchmarks/flow/scrabble/FlowPlaysScrabbleBase\$play\$buildHistoOnScore\$1\$\$special\$\$inlined\$filter\$1\$1.class" - delete "$buildDir/classes/kotlin/jmh/benchmarks/flow/scrabble/FlowPlaysScrabbleBase\$play\$histoOfLetters\$1\$\$special\$\$inlined\$fold\$1\$1.class" - delete "$buildDir/classes/kotlin/jmh/benchmarks/flow/scrabble//SaneFlowPlaysScrabble\$play\$buildHistoOnScore\$1\$\$special\$\$inlined\$filter\$1\$1.class" - - // Primes - delete "$buildDir/classes/kotlin/jmh/benchmarks/flow/misc/Numbers\$\$special\$\$inlined\$filter\$1\$2\$1.class" - delete "$buildDir/classes/kotlin/jmh/benchmarks/flow/misc/Numbers\$\$special\$\$inlined\$filter\$1\$1.class" -} - -jmhRunBytecodeGenerator.dependsOn(removeRedundantFiles) - -// It is better to use the following to run benchmarks, otherwise you may get unexpected errors: -// ./gradlew --no-daemon cleanJmhJar jmh -Pjmh="MyBenchmark" -jmh { - jmhVersion = '1.21' - duplicateClassesStrategy DuplicatesStrategy.INCLUDE - failOnError = true - resultFormat = 'CSV' - if (project.hasProperty('jmh')) { - include = ".*" + project.jmh + ".*" - } -// includeTests = false -} - -jmhJar { - baseName 'benchmarks' - classifier = null - version = null - destinationDir = file("$rootDir") -} - -dependencies { - compile "org.openjdk.jmh:jmh-core:1.21" - compile "io.projectreactor:reactor-core:$reactor_vesion" - compile 'io.reactivex.rxjava2:rxjava:2.1.9' - compile "com.github.akarnokd:rxjava2-extensions:0.20.8" - - compile "org.openjdk.jmh:jmh-core:1.21" - compile 'com.typesafe.akka:akka-actor_2.12:2.5.0' - compile project(':kotlinx-coroutines-core') - // add jmh dependency on main - jmh sourceSets.main.runtimeClasspath -} diff --git a/benchmarks/build.gradle.kts b/benchmarks/build.gradle.kts new file mode 100644 index 0000000000..b60dcbc8f4 --- /dev/null +++ b/benchmarks/build.gradle.kts @@ -0,0 +1,67 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +@file:Suppress("UnstableApiUsage") + +import me.champeau.gradle.* +import org.jetbrains.kotlin.gradle.tasks.* + +plugins { + id("net.ltgt.apt") + id("com.github.johnrengelman.shadow") + id("me.champeau.gradle.jmh") apply false +} + +repositories { + maven("https://repo.typesafe.com/typesafe/releases/") +} + +java { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 +} + +apply(plugin="me.champeau.gradle.jmh") + +tasks.named("compileJmhKotlin") { + kotlinOptions { + jvmTarget = "1.8" + freeCompilerArgs += "-Xjvm-default=enable" + } +} + + + +// It is better to use the following to run benchmarks, otherwise you may get unexpected errors: +// ./gradlew --no-daemon cleanJmhJar jmh -Pjmh="MyBenchmark" +extensions.configure("jmh") { + jmhVersion = "1.26" + duplicateClassesStrategy = DuplicatesStrategy.INCLUDE + failOnError = true + resultFormat = "CSV" + project.findProperty("jmh")?.also { + include = listOf(".*$it.*") + } +// includeTests = false +} + +tasks.named("jmhJar") { + archiveBaseName by "benchmarks" + archiveClassifier by null + archiveVersion by null + destinationDirectory.file("$rootDir") +} + +dependencies { + compile("org.openjdk.jmh:jmh-core:1.26") + compile("io.projectreactor:reactor-core:${version("reactor")}") + compile("io.reactivex.rxjava2:rxjava:2.1.9") + compile("com.github.akarnokd:rxjava2-extensions:0.20.8") + + compile("com.typesafe.akka:akka-actor_2.12:2.5.0") + compile(project(":kotlinx-coroutines-core")) + + // add jmh dependency on main + "jmhImplementation"(sourceSets.main.get().runtimeClasspath) +} diff --git a/benchmarks/src/jmh/kotlin/benchmarks/ChannelSinkBenchmark.kt b/benchmarks/src/jmh/kotlin/benchmarks/ChannelSinkBenchmark.kt index 9c7f38a6f9..6c5b623191 100644 --- a/benchmarks/src/jmh/kotlin/benchmarks/ChannelSinkBenchmark.kt +++ b/benchmarks/src/jmh/kotlin/benchmarks/ChannelSinkBenchmark.kt @@ -50,4 +50,22 @@ open class ChannelSinkBenchmark { for (i in start until (start + count)) send(i) } + + // Migrated from deprecated operators, are good only for stressing channels + + private fun ReceiveChannel.filter(context: CoroutineContext = Dispatchers.Unconfined, predicate: suspend (E) -> Boolean): ReceiveChannel = + GlobalScope.produce(context, onCompletion = { cancel() }) { + for (e in this@filter) { + if (predicate(e)) send(e) + } + } + + private suspend inline fun ReceiveChannel.fold(initial: R, operation: (acc: R, E) -> R): R { + var accumulator = initial + consumeEach { + accumulator = operation(accumulator, it) + } + return accumulator + } } + diff --git a/benchmarks/src/jmh/kotlin/benchmarks/flow/CombineFlowsBenchmark.kt b/benchmarks/src/jmh/kotlin/benchmarks/flow/CombineFlowsBenchmark.kt new file mode 100644 index 0000000000..4725ceda91 --- /dev/null +++ b/benchmarks/src/jmh/kotlin/benchmarks/flow/CombineFlowsBenchmark.kt @@ -0,0 +1,34 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package benchmarks.flow + +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* +import org.openjdk.jmh.annotations.* +import java.util.concurrent.* + +@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) +@Fork(value = 1) +@BenchmarkMode(Mode.Throughput) +@OutputTimeUnit(TimeUnit.MILLISECONDS) +@State(Scope.Benchmark) +open class CombineFlowsBenchmark { + + @Param("10", "100", "1000") + private var size = 10 + + @Benchmark + fun combine() = runBlocking { + combine((1 until size).map { flowOf(it) }) { a -> a}.collect() + } + + @Benchmark + fun combineTransform() = runBlocking { + val list = (1 until size).map { flowOf(it) }.toList() + combineTransform((1 until size).map { flowOf(it) }) { emit(it) }.collect() + } +} + diff --git a/benchmarks/src/jmh/kotlin/benchmarks/flow/CombineTwoFlowsBenchmark.kt b/benchmarks/src/jmh/kotlin/benchmarks/flow/CombineTwoFlowsBenchmark.kt new file mode 100644 index 0000000000..f7fbc6cf23 --- /dev/null +++ b/benchmarks/src/jmh/kotlin/benchmarks/flow/CombineTwoFlowsBenchmark.kt @@ -0,0 +1,47 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package benchmarks.flow + +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* +import kotlinx.coroutines.flow.internal.* +import org.openjdk.jmh.annotations.* +import java.util.concurrent.* + +@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) +@Fork(value = 1) +@BenchmarkMode(Mode.Throughput) +@OutputTimeUnit(TimeUnit.MILLISECONDS) +@State(Scope.Benchmark) +open class CombineTwoFlowsBenchmark { + + @Param("100", "100000", "1000000") + private var size = 100000 + + @Benchmark + fun combinePlain() = runBlocking { + val flow = (1 until size.toLong()).asFlow() + flow.combine(flow) { a, b -> a + b }.collect() + } + + @Benchmark + fun combineTransform() = runBlocking { + val flow = (1 until size.toLong()).asFlow() + flow.combineTransform(flow) { a, b -> emit(a + b) }.collect() + } + + @Benchmark + fun combineVararg() = runBlocking { + val flow = (1 until size.toLong()).asFlow() + combine(listOf(flow, flow)) { arr -> arr[0] + arr[1] }.collect() + } + + @Benchmark + fun combineTransformVararg() = runBlocking { + val flow = (1 until size.toLong()).asFlow() + combineTransform(listOf(flow, flow)) { arr -> emit(arr[0] + arr[1]) }.collect() + } +} diff --git a/benchmarks/src/jmh/kotlin/benchmarks/flow/FlowFlattenMergeBenchmark.kt b/benchmarks/src/jmh/kotlin/benchmarks/flow/FlowFlattenMergeBenchmark.kt index 3fff2697cc..149898881b 100644 --- a/benchmarks/src/jmh/kotlin/benchmarks/flow/FlowFlattenMergeBenchmark.kt +++ b/benchmarks/src/jmh/kotlin/benchmarks/flow/FlowFlattenMergeBenchmark.kt @@ -8,7 +8,7 @@ import benchmarks.common.* import kotlinx.coroutines.* import kotlinx.coroutines.flow.* import org.openjdk.jmh.annotations.* -import java.util.concurrent.* +import java.util.concurrent.TimeUnit /** * Benchmark to measure performance of [kotlinx.coroutines.flow.FlowKt.flattenMerge]. diff --git a/benchmarks/src/jmh/kotlin/benchmarks/flow/NumbersBenchmark.kt b/benchmarks/src/jmh/kotlin/benchmarks/flow/NumbersBenchmark.kt index 0cb31056bb..8453f5c7f9 100644 --- a/benchmarks/src/jmh/kotlin/benchmarks/flow/NumbersBenchmark.kt +++ b/benchmarks/src/jmh/kotlin/benchmarks/flow/NumbersBenchmark.kt @@ -11,7 +11,8 @@ import io.reactivex.functions.* import kotlinx.coroutines.* import kotlinx.coroutines.flow.* import org.openjdk.jmh.annotations.* -import java.util.concurrent.* +import java.util.concurrent.TimeUnit +import java.util.concurrent.Callable @Warmup(iterations = 7, time = 1, timeUnit = TimeUnit.SECONDS) @Measurement(iterations = 7, time = 1, timeUnit = TimeUnit.SECONDS) @@ -76,14 +77,14 @@ open class NumbersBenchmark { @Benchmark fun zipRx() { - val numbers = rxNumbers().take(natural.toLong()) + val numbers = rxNumbers().take(natural) val first = numbers .filter { it % 2L != 0L } .map { it * it } val second = numbers .filter { it % 2L == 0L } .map { it * it } - first.zipWith(second, BiFunction { v1, v2 -> v1 + v2 }).filter { it % 3 == 0L }.count() + first.zipWith(second, { v1, v2 -> v1 + v2 }).filter { it % 3 == 0L }.count() .blockingGet() } @@ -97,7 +98,7 @@ open class NumbersBenchmark { @Benchmark fun transformationsRx(): Long { - return rxNumbers().take(natural.toLong()) + return rxNumbers().take(natural) .filter { it % 2L != 0L } .map { it * it } .filter { (it + 1) % 3 == 0L }.count() diff --git a/benchmarks/src/jmh/kotlin/benchmarks/flow/TakeBenchmark.kt b/benchmarks/src/jmh/kotlin/benchmarks/flow/TakeBenchmark.kt index 1c469a69b9..1e12e2c33e 100644 --- a/benchmarks/src/jmh/kotlin/benchmarks/flow/TakeBenchmark.kt +++ b/benchmarks/src/jmh/kotlin/benchmarks/flow/TakeBenchmark.kt @@ -7,7 +7,7 @@ package benchmarks.flow import kotlinx.coroutines.* import kotlinx.coroutines.flow.* import org.openjdk.jmh.annotations.* -import java.util.concurrent.* +import java.util.concurrent.TimeUnit import java.util.concurrent.CancellationException import kotlin.coroutines.* import kotlin.coroutines.intrinsics.* diff --git a/benchmarks/src/jmh/kotlin/benchmarks/flow/TakeWhileBenchmark.kt b/benchmarks/src/jmh/kotlin/benchmarks/flow/TakeWhileBenchmark.kt new file mode 100644 index 0000000000..fd3d3cdb96 --- /dev/null +++ b/benchmarks/src/jmh/kotlin/benchmarks/flow/TakeWhileBenchmark.kt @@ -0,0 +1,68 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") + +package benchmarks.flow + +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* +import kotlinx.coroutines.flow.internal.* +import org.openjdk.jmh.annotations.* +import java.util.concurrent.TimeUnit + +@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) +@Fork(value = 1) +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@State(Scope.Benchmark) +open class TakeWhileBenchmark { + @Param("1", "10", "100", "1000") + private var size: Int = 0 + + private suspend inline fun Flow.consume() = + filter { it % 2L != 0L } + .map { it * it }.count() + + @Benchmark + fun baseline() = runBlocking { + (0L until size).asFlow().consume() + } + + @Benchmark + fun takeWhileDirect() = runBlocking { + (0L..Long.MAX_VALUE).asFlow().takeWhileDirect { it < size }.consume() + } + + @Benchmark + fun takeWhileViaCollectWhile() = runBlocking { + (0L..Long.MAX_VALUE).asFlow().takeWhileViaCollectWhile { it < size }.consume() + } + + // Direct implementation by checking predicate and throwing AbortFlowException + private fun Flow.takeWhileDirect(predicate: suspend (T) -> Boolean): Flow = unsafeFlow { + try { + collect { value -> + if (predicate(value)) emit(value) + else throw AbortFlowException(this) + } + } catch (e: AbortFlowException) { + e.checkOwnership(owner = this) + } + } + + // Essentially the same code, but reusing the logic via collectWhile function + private fun Flow.takeWhileViaCollectWhile(predicate: suspend (T) -> Boolean): Flow = unsafeFlow { + // This return is needed to work around a bug in JS BE: KT-39227 + return@unsafeFlow collectWhile { value -> + if (predicate(value)) { + emit(value) + true + } else { + false + } + } + } +} diff --git a/benchmarks/src/jmh/kotlin/benchmarks/flow/scrabble/FlowPlaysScrabbleBase.kt b/benchmarks/src/jmh/kotlin/benchmarks/flow/scrabble/FlowPlaysScrabbleBase.kt index 9e39b43b8b..3501bdfed3 100644 --- a/benchmarks/src/jmh/kotlin/benchmarks/flow/scrabble/FlowPlaysScrabbleBase.kt +++ b/benchmarks/src/jmh/kotlin/benchmarks/flow/scrabble/FlowPlaysScrabbleBase.kt @@ -9,7 +9,7 @@ import kotlinx.coroutines.flow.* import org.openjdk.jmh.annotations.* import java.lang.Long.max import java.util.* -import java.util.concurrent.* +import java.util.concurrent.TimeUnit import kotlin.math.* @Warmup(iterations = 7, time = 1, timeUnit = TimeUnit.SECONDS) diff --git a/benchmarks/src/jmh/kotlin/benchmarks/flow/scrabble/SaneFlowPlaysScrabble.kt b/benchmarks/src/jmh/kotlin/benchmarks/flow/scrabble/SaneFlowPlaysScrabble.kt index 0a4f69672f..ad97dfa373 100644 --- a/benchmarks/src/jmh/kotlin/benchmarks/flow/scrabble/SaneFlowPlaysScrabble.kt +++ b/benchmarks/src/jmh/kotlin/benchmarks/flow/scrabble/SaneFlowPlaysScrabble.kt @@ -9,7 +9,7 @@ import kotlinx.coroutines.flow.* import org.openjdk.jmh.annotations.* import java.lang.Long.* import java.util.* -import java.util.concurrent.* +import java.util.concurrent.TimeUnit @Warmup(iterations = 7, time = 1, timeUnit = TimeUnit.SECONDS) @Measurement(iterations = 7, time = 1, timeUnit = TimeUnit.SECONDS) diff --git a/benchmarks/src/jmh/kotlin/benchmarks/tailcall/SimpleChannel.kt b/benchmarks/src/jmh/kotlin/benchmarks/tailcall/SimpleChannel.kt index c217fcae91..d961dab8d9 100644 --- a/benchmarks/src/jmh/kotlin/benchmarks/tailcall/SimpleChannel.kt +++ b/benchmarks/src/jmh/kotlin/benchmarks/tailcall/SimpleChannel.kt @@ -70,12 +70,12 @@ class NonCancellableChannel : SimpleChannel() { } class CancellableChannel : SimpleChannel() { - override suspend fun suspendReceive(): Int = suspendAtomicCancellableCoroutine { + override suspend fun suspendReceive(): Int = suspendCancellableCoroutine { consumer = it.intercepted() COROUTINE_SUSPENDED } - override suspend fun suspendSend(element: Int) = suspendAtomicCancellableCoroutine { + override suspend fun suspendSend(element: Int) = suspendCancellableCoroutine { enqueuedValue = element producer = it.intercepted() COROUTINE_SUSPENDED @@ -84,13 +84,13 @@ class CancellableChannel : SimpleChannel() { class CancellableReusableChannel : SimpleChannel() { @Suppress("INVISIBLE_MEMBER") - override suspend fun suspendReceive(): Int = suspendAtomicCancellableCoroutineReusable { + override suspend fun suspendReceive(): Int = suspendCancellableCoroutineReusable { consumer = it.intercepted() COROUTINE_SUSPENDED } @Suppress("INVISIBLE_MEMBER") - override suspend fun suspendSend(element: Int) = suspendAtomicCancellableCoroutineReusable { + override suspend fun suspendSend(element: Int) = suspendCancellableCoroutineReusable { enqueuedValue = element producer = it.intercepted() COROUTINE_SUSPENDED diff --git a/benchmarks/src/main/kotlin/benchmarks/common/BenchmarkUtils.kt b/benchmarks/src/main/kotlin/benchmarks/common/BenchmarkUtils.kt index 0057573bc6..27bc6b7d85 100644 --- a/benchmarks/src/main/kotlin/benchmarks/common/BenchmarkUtils.kt +++ b/benchmarks/src/main/kotlin/benchmarks/common/BenchmarkUtils.kt @@ -6,7 +6,7 @@ package benchmarks.common import java.util.concurrent.* -fun doGeomDistrWork(work: Int) { +public fun doGeomDistrWork(work: Int) { // We use geometric distribution here. We also checked on macbook pro 13" (2017) that the resulting work times // are distributed geometrically, see https://github.com/Kotlin/kotlinx.coroutines/pull/1464#discussion_r355705325 val p = 1.0 / work diff --git a/build.gradle b/build.gradle index 87aac2a1e0..79c7f3553e 100644 --- a/build.gradle +++ b/build.gradle @@ -2,26 +2,19 @@ * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ import org.jetbrains.kotlin.konan.target.HostManager +import org.gradle.util.VersionNumber +apply plugin: 'jdk-convention' apply from: rootProject.file("gradle/experimental.gradle") def rootModule = "kotlinx.coroutines" def coreModule = "kotlinx-coroutines-core" // Not applicable for Kotlin plugin -def sourceless = ['kotlinx.coroutines', 'site', 'kotlinx-coroutines-bom', 'publication-validator'] -def internal = ['kotlinx.coroutines', 'site', 'benchmarks', 'js-stub', 'stdlib-stubs', 'publication-validator'] +def sourceless = ['kotlinx.coroutines', 'site', 'kotlinx-coroutines-bom', 'integration-testing'] +def internal = ['kotlinx.coroutines', 'site', 'benchmarks', 'js-stub', 'stdlib-stubs', 'integration-testing'] // Not published def unpublished = internal + ['example-frontend-js', 'android-unit-tests'] -static def platformOf(project) { - def name = project.name - if (name.endsWith("-js")) return "js" - if (name.endsWith("-common") || name.endsWith("-native")) { - throw IllegalStateException("$name platform is not supported") - } - return "jvm" -} - buildscript { /* * These property group is used to build kotlinx.coroutines against Kotlin compiler snapshot. @@ -53,13 +46,20 @@ buildscript { if (using_snapshot_version) { repositories { mavenLocal() - maven { url "https://oss.sonatype.org/content/repositories/snapshots" } } } repositories { jcenter() - maven { url "https://kotlin.bintray.com/kotlinx" } + maven { + url "https://kotlin.bintray.com/kotlinx" + credentials { + username = project.hasProperty('bintrayUser') ? project.property('bintrayUser') : System.getenv('BINTRAY_USER') ?: "" + password = project.hasProperty('bintrayApiKey') ? project.property('bintrayApiKey') : System.getenv('BINTRAY_API_KEY') ?: "" + } + } + // Future replacement for kotlin-dev, with cache redirector + maven { url "https://cache-redirector.jetbrains.com/maven.pkg.jetbrains.space/kotlin/p/kotlin/dev" } maven { url "https://kotlin.bintray.com/kotlin-dev" credentials { @@ -78,18 +78,22 @@ buildscript { classpath "org.jetbrains.kotlinx:atomicfu-gradle-plugin:$atomicfu_version" classpath "org.jetbrains.kotlinx:kotlinx-knit:$knit_version" classpath "com.moowork.gradle:gradle-node-plugin:$gradle_node_version" - classpath "org.openjfx:javafx-plugin:$javafx_plugin_version" classpath "org.jetbrains.kotlinx:binary-compatibility-validator:$binary_compatibility_validator_version" // JMH plugins classpath "com.github.jengelman.gradle.plugins:shadow:5.1.0" - classpath "me.champeau.gradle:jmh-gradle-plugin:0.5.0-rc-2" - classpath "net.ltgt.gradle:gradle-apt-plugin:0.21" } + + CacheRedirector.configureBuildScript(buildscript, rootProject) } import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType +// todo:KLUDGE: Hierarchical project structures are not fully supported in IDEA, enable only for a regular built +if (!Idea.active) { + ext.set("kotlin.mpp.enableGranularSourceSetsMetadata", "true") +} + // todo:KLUDGE: This is needed to workaround dependency resolution between Java and MPP modules def configureKotlinJvmPlatform(configuration) { configuration.attributes.attribute(KotlinPlatformType.attribute, KotlinPlatformType.jvm) @@ -137,13 +141,13 @@ apiValidation { if (build_snapshot_train) { ignoredProjects.remove("site") ignoredProjects.remove("example-frontend-js") + ignoredProjects.add("kotlinx-coroutines-core") } ignoredPackages += "kotlinx.coroutines.internal" } // Configure repositories allprojects { - String projectName = it.name repositories { /* * google should be first in the repository list because some of the play services @@ -151,6 +155,8 @@ allprojects { */ google() jcenter() + // Future replacement for kotlin-dev, with cache redirector + maven { url "https://cache-redirector.jetbrains.com/maven.pkg.jetbrains.space/kotlin/p/kotlin/dev" } maven { url "https://kotlin.bintray.com/kotlin-dev" credentials { @@ -159,14 +165,21 @@ allprojects { } } maven { url "https://kotlin.bintray.com/kotlin-eap" } - maven { url "https://kotlin.bintray.com/kotlinx" } + maven { + url "https://kotlin.bintray.com/kotlinx" + credentials { + username = project.hasProperty('bintrayUser') ? project.property('bintrayUser') : System.getenv('BINTRAY_USER') ?: "" + password = project.hasProperty('bintrayApiKey') ? project.property('bintrayApiKey') : System.getenv('BINTRAY_API_KEY') ?: "" + } + } + mavenLocal() } } // Add dependency to core source sets. Core is configured in kx-core/build.gradle configure(subprojects.findAll { !sourceless.contains(it.name) && it.name != coreModule }) { evaluationDependsOn(":$coreModule") - def platform = platformOf(it) + def platform = PlatformKt.platformOf(it) apply from: rootProject.file("gradle/compile-${platform}.gradle") dependencies { // See comment below for rationale, it will be replaced with "project" dependency @@ -203,6 +216,8 @@ if (build_snapshot_train) { exclude '**/*definitely/not/kotlinx*' // Disable because of KT-11567 in 1.4 exclude '**/*CasesPublicAPITest*' + // Kotlin + exclude '**/*PrecompiledDebugProbesTest*' } } @@ -227,7 +242,7 @@ if (build_snapshot_train) { * but publishing plugin does not re-read artifact names for kotlin-jvm projects, so renaming is not applied in pom files * for JVM-only projects. * - * We artificially replace "project" dependency with "module" one to have proper names in pom files, but then substitute it + * We artificially replace "project" dependency with "module" one to have proper names in pom files, but then substitute it * to have out "project" dependency back. */ configure(subprojects.findAll { it.name != coreModule && it.name != rootModule }) { @@ -239,7 +254,14 @@ configure(subprojects.findAll { it.name != coreModule && it.name != rootModule } } // Redefine source sets because we are not using 'kotlin/main/fqn' folder convention -configure(subprojects.findAll { !sourceless.contains(it.name) && it.name != "benchmarks" && it.name != coreModule }) { +configure(subprojects.findAll { + !sourceless.contains(it.name) && + it.name != "benchmarks" && + it.name != coreModule && + it.name != "example-frontend-js" +}) { + // Pure JS and pure MPP doesn't have this notion and are configured separately + // TODO detect it via platformOf and migrate benchmarks to the same scheme sourceSets { main.kotlin.srcDirs = ['src'] test.kotlin.srcDirs = ['test'] @@ -275,24 +297,15 @@ configure(subprojects.findAll { !unpublished.contains(it.name) }) { // Report Kotlin compiler version when building project println("Using Kotlin compiler version: $org.jetbrains.kotlin.config.KotlinCompilerVersion.VERSION") -// --------------- Publish only from under JDK11+ --------------- -task checkJdkForPublish { - doFirst { - String javaVersion = System.properties["java.version"] - int i = javaVersion.indexOf('.') - int javaVersionMajor = (i < 0 ? javaVersion : javaVersion.substring(0, i)).toInteger() - if (javaVersionMajor < 11) { - throw new GradleException("Project can be build for publishing only under JDK 11+, but found ${javaVersion}") - } - } +// --------------- Cache redirector --------------- + +allprojects { + CacheRedirector.configure(project) } // --------------- Configure sub-projects that are published --------------- -def publishTasks = getTasksByName("publish", true) + getTasksByName("publishNpm", true) -publishTasks.each { - it.dependsOn checkJdkForPublish -} +def publishTasks = getTasksByName("publish", true) + getTasksByName("publishNpm", true) task deploy(dependsOn: publishTasks) diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts new file mode 100644 index 0000000000..adcbd90fe1 --- /dev/null +++ b/buildSrc/build.gradle.kts @@ -0,0 +1,50 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +import java.util.* + +plugins { + `kotlin-dsl` +} + +val cacheRedirectorEnabled = System.getenv("CACHE_REDIRECTOR")?.toBoolean() == true +val buildSnapshotTrain = properties["build_snapshot_train"]?.toString()?.toBoolean() == true + +repositories { + if (cacheRedirectorEnabled) { + maven("https://cache-redirector.jetbrains.com/plugins.gradle.org/m2") + maven("https://cache-redirector.jetbrains.com/dl.bintray.com/kotlin/kotlin-eap") + maven("https://cache-redirector.jetbrains.com/dl.bintray.com/kotlin/kotlin-dev") + } else { + maven("https://plugins.gradle.org/m2") + maven("https://dl.bintray.com/kotlin/kotlin-eap") + maven("https://dl.bintray.com/kotlin/kotlin-dev") + } + + if (buildSnapshotTrain) { + mavenLocal() + } +} + +kotlinDslPluginOptions { + experimentalWarning.set(false) +} + +val props = Properties().apply { + file("../gradle.properties").inputStream().use { load(it) } +} + +fun version(target: String): String { + // Intercept reading from properties file + if (target == "kotlin") { + val snapshotVersion = properties["kotlin_snapshot_version"] + if (snapshotVersion != null) return snapshotVersion.toString() + } + return props.getProperty("${target}_version") +} + +dependencies { + implementation(kotlin("gradle-plugin", version("kotlin"))) + implementation("org.jetbrains.dokka:dokka-gradle-plugin:${version("dokka")}") +} diff --git a/buildSrc/settings.gradle.kts b/buildSrc/settings.gradle.kts new file mode 100644 index 0000000000..a6da8fdbc1 --- /dev/null +++ b/buildSrc/settings.gradle.kts @@ -0,0 +1,18 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ +pluginManagement { + val build_snapshot_train: String? by settings + repositories { + val cacheRedirectorEnabled = System.getenv("CACHE_REDIRECTOR")?.toBoolean() == true + if (cacheRedirectorEnabled) { + println("Redirecting repositories for buildSrc buildscript") + maven("https://cache-redirector.jetbrains.com/plugins.gradle.org/m2") + } else { + maven("https://plugins.gradle.org/m2") + } + if (build_snapshot_train?.toBoolean() == true) { + mavenLocal() + } + } +} diff --git a/buildSrc/src/main/kotlin/CacheRedirector.kt b/buildSrc/src/main/kotlin/CacheRedirector.kt new file mode 100644 index 0000000000..7cf01d8e76 --- /dev/null +++ b/buildSrc/src/main/kotlin/CacheRedirector.kt @@ -0,0 +1,152 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +import org.gradle.api.* +import org.gradle.api.artifacts.dsl.* +import org.gradle.api.artifacts.repositories.* +import org.gradle.api.initialization.dsl.* +import java.net.* + +/** + * Enabled via environment variable, so that it can be reliably accessed from any piece of the build script, + * including buildSrc within TeamCity CI. + */ +private val cacheRedirectorEnabled = System.getenv("CACHE_REDIRECTOR")?.toBoolean() == true + +/** + * The list of repositories supported by cache redirector should be synced with the list at https://cache-redirector.jetbrains.com/redirects_generated.html + * To add a repository to the list create an issue in ADM project (example issue https://youtrack.jetbrains.com/issue/IJI-149) + */ +private val mirroredUrls = listOf( + "https://cdn.azul.com/zulu/bin", + "https://clojars.org/repo", + "https://dl.bintray.com/d10xa/maven", + "https://dl.bintray.com/groovy/maven", + "https://dl.bintray.com/jetbrains/maven-patched", + "https://dl.bintray.com/jetbrains/scala-plugin-deps", + "https://dl.bintray.com/kodein-framework/Kodein-DI", + "https://dl.bintray.com/konsoletyper/teavm", + "https://dl.bintray.com/kotlin/kotlin-dev", + "https://dl.bintray.com/kotlin/kotlin-eap", + "https://dl.bintray.com/kotlin/kotlinx.html", + "https://dl.bintray.com/kotlin/kotlinx", + "https://dl.bintray.com/kotlin/ktor", + "https://dl.bintray.com/scalacenter/releases", + "https://dl.bintray.com/scalamacros/maven", + "https://dl.bintray.com/kotlin/exposed", + "https://dl.bintray.com/cy6ergn0m/maven", + "https://dl.bintray.com/kotlin/kotlin-js-wrappers", + "https://dl.google.com/android/repository", + "https://dl.google.com/dl/android/maven2", + "https://dl.google.com/dl/android/studio/ide-zips", + "https://dl.google.com/go", + "https://download.jetbrains.com", + "https://jcenter.bintray.com", + "https://jetbrains.bintray.com/dekaf", + "https://jetbrains.bintray.com/intellij-jbr", + "https://jetbrains.bintray.com/intellij-jdk", + "https://jetbrains.bintray.com/intellij-plugin-service", + "https://jetbrains.bintray.com/intellij-terraform", + "https://jetbrains.bintray.com/intellij-third-party-dependencies", + "https://jetbrains.bintray.com/jediterm", + "https://jetbrains.bintray.com/kotlin-native-dependencies", + "https://jetbrains.bintray.com/markdown", + "https://jetbrains.bintray.com/teamcity-rest-client", + "https://jetbrains.bintray.com/test-discovery", + "https://jetbrains.bintray.com/wormhole", + "https://jitpack.io", + "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev", + "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap", + "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/eap", + "https://kotlin.bintray.com/dukat", + "https://kotlin.bintray.com/kotlin-dependencies", + "https://oss.sonatype.org/content/repositories/releases", + "https://oss.sonatype.org/content/repositories/snapshots", + "https://oss.sonatype.org/content/repositories/staging", + "https://packages.confluent.io/maven/", + "https://plugins.gradle.org/m2", + "https://plugins.jetbrains.com/maven", + "https://repo1.maven.org/maven2", + "https://repo.grails.org/grails/core", + "https://repo.jenkins-ci.org/releases", + "https://repo.maven.apache.org/maven2", + "https://repo.spring.io/milestone", + "https://repo.typesafe.com/typesafe/ivy-releases", + "https://services.gradle.org", + "https://www.exasol.com/artifactory/exasol-releases", + "https://www.myget.org/F/intellij-go-snapshots/maven", + "https://www.myget.org/F/rd-model-snapshots/maven", + "https://www.myget.org/F/rd-snapshots/maven", + "https://www.python.org/ftp", + "https://www.jetbrains.com/intellij-repository/nightly", + "https://www.jetbrains.com/intellij-repository/releases", + "https://www.jetbrains.com/intellij-repository/snapshots" +) + +private val aliases = mapOf( + "https://repo.maven.apache.org/maven2" to "https://repo1.maven.org/maven2", // Maven Central + "https://kotlin.bintray.com/kotlin-dev" to "https://dl.bintray.com/kotlin/kotlin-dev", + "https://kotlin.bintray.com/kotlin-eap" to "https://dl.bintray.com/kotlin/kotlin-eap", + "https://kotlin.bintray.com/kotlinx" to "https://dl.bintray.com/kotlin/kotlinx" +) + +private fun URI.toCacheRedirectorUri() = URI("https://cache-redirector.jetbrains.com/$host/$path") + +private fun URI.maybeRedirect(): URI? { + val url = toString().trimEnd('/') + val dealiasedUrl = aliases.getOrDefault(url, url) + + return if (mirroredUrls.any { dealiasedUrl.startsWith(it) }) { + URI(dealiasedUrl).toCacheRedirectorUri() + } else { + null + } +} + +private fun URI.isCachedOrLocal() = scheme == "file" || + host == "cache-redirector.jetbrains.com" || + host == "teamcity.jetbrains.com" || + host == "buildserver.labs.intellij.net" + +private fun Project.checkRedirectUrl(url: URI, containerName: String): URI { + val redirected = url.maybeRedirect() + if (redirected == null && !url.isCachedOrLocal()) { + val msg = "Repository $url in $containerName should be cached with cache-redirector" + val details = "Using non cached repository may lead to download failures in CI builds." + + " Check buildSrc/src/main/kotlin/CacheRedirector.kt for details." + logger.warn("WARNING - $msg\n$details") + } + return if (cacheRedirectorEnabled) redirected ?: url else url +} + +private fun Project.checkRedirect(repositories: RepositoryHandler, containerName: String) { + if (cacheRedirectorEnabled) { + logger.info("Redirecting repositories for $containerName") + } + for (repository in repositories) { + when (repository) { + is MavenArtifactRepository -> repository.url = checkRedirectUrl(repository.url, containerName) + is IvyArtifactRepository -> repository.url = checkRedirectUrl(repository.url, containerName) + } + } +} + +// Used from Groovy scripts +object CacheRedirector { + /** + * Substitutes repositories in buildScript { } block. + */ + @JvmStatic + fun ScriptHandler.configureBuildScript(rootProject: Project) { + rootProject.checkRedirect(repositories, "${rootProject.displayName} buildscript") + } + + /** + * Substitutes repositories in a project. + */ + @JvmStatic + fun Project.configure() { + checkRedirect(repositories, displayName) + } +} diff --git a/buildSrc/src/main/kotlin/Dokka.kt b/buildSrc/src/main/kotlin/Dokka.kt new file mode 100644 index 0000000000..dd5f1ea48d --- /dev/null +++ b/buildSrc/src/main/kotlin/Dokka.kt @@ -0,0 +1,26 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +import org.gradle.api.Project +import org.gradle.kotlin.dsl.delegateClosureOf +import org.gradle.kotlin.dsl.withType +import org.jetbrains.dokka.DokkaConfiguration.ExternalDocumentationLink.Builder +import org.jetbrains.dokka.gradle.DokkaTask +import java.io.File +import java.net.URL + +/** + * Package-list by external URL for documentation generation. + */ +fun Project.externalDocumentationLink( + url: String, + packageList: File = projectDir.resolve("package.list") +) { + tasks.withType().configureEach { + externalDocumentationLink(delegateClosureOf { + this.url = URL(url) + packageListUrl = packageList.toPath().toUri().toURL() + }) + } +} diff --git a/buildSrc/src/main/kotlin/Idea.kt b/buildSrc/src/main/kotlin/Idea.kt new file mode 100644 index 0000000000..615b8aad74 --- /dev/null +++ b/buildSrc/src/main/kotlin/Idea.kt @@ -0,0 +1,5 @@ +object Idea { + @JvmStatic // for Gradle + val active: Boolean + get() = System.getProperty("idea.active") == "true" +} diff --git a/buildSrc/src/main/kotlin/MavenCentral.kt b/buildSrc/src/main/kotlin/MavenCentral.kt new file mode 100644 index 0000000000..3efaf33f1c --- /dev/null +++ b/buildSrc/src/main/kotlin/MavenCentral.kt @@ -0,0 +1,37 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +@file:Suppress("UnstableApiUsage") + +import org.gradle.api.Project +import org.gradle.api.publish.maven.MavenPom + +// Pom configuration + +fun MavenPom.configureMavenCentralMetadata(project: Project) { + name by project.name + description by "Coroutines support libraries for Kotlin" + url by "https://github.com/Kotlin/kotlinx.coroutines" + + licenses { + license { + name by "The Apache Software License, Version 2.0" + url by "https://www.apache.org/licenses/LICENSE-2.0.txt" + distribution by "repo" + } + } + + developers { + developer { + id by "JetBrains" + name by "JetBrains Team" + organization by "JetBrains" + organizationUrl by "https://www.jetbrains.com" + } + } + + scm { + url by "https://github.com/Kotlin/kotlinx.coroutines" + } +} diff --git a/buildSrc/src/main/kotlin/Platform.kt b/buildSrc/src/main/kotlin/Platform.kt new file mode 100644 index 0000000000..b667a138a8 --- /dev/null +++ b/buildSrc/src/main/kotlin/Platform.kt @@ -0,0 +1,9 @@ +import org.gradle.api.Project + +// Use from Groovy for now +fun platformOf(project: Project): String = + when (project.name.substringAfterLast("-")) { + "js" -> "js" + "common", "native" -> throw IllegalStateException("${project.name} platform is not supported") + else -> "jvm" + } diff --git a/buildSrc/src/main/kotlin/Projects.kt b/buildSrc/src/main/kotlin/Projects.kt new file mode 100644 index 0000000000..109311e876 --- /dev/null +++ b/buildSrc/src/main/kotlin/Projects.kt @@ -0,0 +1,4 @@ +import org.gradle.api.Project + +fun Project.version(target: String): String = + property("${target}_version") as String diff --git a/buildSrc/src/main/kotlin/Properties.kt b/buildSrc/src/main/kotlin/Properties.kt new file mode 100644 index 0000000000..a0968ee699 --- /dev/null +++ b/buildSrc/src/main/kotlin/Properties.kt @@ -0,0 +1,11 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +@file:Suppress("UnstableApiUsage") + +import org.gradle.api.provider.* + +infix fun Property.by(value: T) { + set(value) +} diff --git a/buildSrc/src/main/kotlin/RunR8.kt b/buildSrc/src/main/kotlin/RunR8.kt new file mode 100644 index 0000000000..d9eba79bd4 --- /dev/null +++ b/buildSrc/src/main/kotlin/RunR8.kt @@ -0,0 +1,52 @@ +import org.gradle.api.tasks.InputFile +import org.gradle.api.tasks.InputFiles +import org.gradle.api.tasks.JavaExec +import org.gradle.api.tasks.OutputDirectory +import org.gradle.api.tasks.bundling.Zip +import org.gradle.kotlin.dsl.get +import org.gradle.kotlin.dsl.named +import java.io.File + +/* + * Task used by our ui/android tests to test minification results + * and keep track of size of the binary. + * TODO move back to kotlinx-coroutines-android when it's migrated to the kts + */ +open class RunR8 : JavaExec() { + + @OutputDirectory + lateinit var outputDex: File + + @InputFile + lateinit var inputConfig: File + + @InputFile + val inputConfigCommon: File = File("testdata/r8-test-common.pro") + + @InputFiles + val jarFile: File = project.tasks.named("jar").get().archivePath + + init { + classpath = project.configurations["r8"] + main = "com.android.tools.r8.R8" + } + + override fun exec() { + // Resolve classpath only during execution + val arguments = mutableListOf( + "--release", + "--no-desugaring", + "--output", outputDex.absolutePath, + "--pg-conf", inputConfig.absolutePath + ) + arguments.addAll(project.configurations["runtimeClasspath"].files.map { it.absolutePath }) + arguments.add(jarFile.absolutePath) + + args = arguments + + project.delete(outputDex) + outputDex.mkdirs() + + super.exec() + } +} diff --git a/buildSrc/src/main/kotlin/UnpackAar.kt b/buildSrc/src/main/kotlin/UnpackAar.kt new file mode 100644 index 0000000000..c7d0b53d04 --- /dev/null +++ b/buildSrc/src/main/kotlin/UnpackAar.kt @@ -0,0 +1,32 @@ +import org.gradle.api.artifacts.transform.InputArtifact +import org.gradle.api.artifacts.transform.TransformAction +import org.gradle.api.artifacts.transform.TransformOutputs +import org.gradle.api.artifacts.transform.TransformParameters +import org.gradle.api.file.FileSystemLocation +import org.gradle.api.provider.Provider +import java.io.File +import java.nio.file.Files +import java.util.zip.ZipEntry +import java.util.zip.ZipFile + +// TODO move back to kotlinx-coroutines-play-services when it's migrated to the kts +@Suppress("UnstableApiUsage") +abstract class UnpackAar : TransformAction { + @get:InputArtifact + abstract val inputArtifact: Provider + + override fun transform(outputs: TransformOutputs) { + ZipFile(inputArtifact.get().asFile).use { zip -> + zip.entries().asSequence() + .filter { !it.isDirectory } + .filter { it.name.endsWith(".jar") } + .forEach { zip.unzip(it, outputs.file(it.name)) } + } + } +} + +private fun ZipFile.unzip(entry: ZipEntry, output: File) { + getInputStream(entry).use { + Files.copy(it, output.toPath()) + } +} diff --git a/buildSrc/src/main/kotlin/jdk-convention.gradle.kts b/buildSrc/src/main/kotlin/jdk-convention.gradle.kts new file mode 100644 index 0000000000..8bc1abf17d --- /dev/null +++ b/buildSrc/src/main/kotlin/jdk-convention.gradle.kts @@ -0,0 +1,10 @@ +import org.gradle.api.JavaVersion + +if (!JavaVersion.current().isJava11Compatible) { + val message = "Project required JDK 11+, but found ${JavaVersion.current()}" + if (Idea.active) { + logger.error(message) + } else { + throw GradleException(message) + } +} diff --git a/coroutines-guide.md b/coroutines-guide.md index bebd0d0e3c..09cfb93cab 100644 --- a/coroutines-guide.md +++ b/coroutines-guide.md @@ -20,6 +20,7 @@ The main coroutines guide has moved to the [docs folder](docs/coroutines-guide.m * [Closing resources with `finally`](docs/cancellation-and-timeouts.md#closing-resources-with-finally) * [Run non-cancellable block](docs/cancellation-and-timeouts.md#run-non-cancellable-block) * [Timeout](docs/cancellation-and-timeouts.md#timeout) + * [Asynchronous timeout and resources](docs/cancellation-and-timeouts.md#asynchronous-timeout-and-resources) * [Composing Suspending Functions](docs/composing-suspending-functions.md#composing-suspending-functions) * [Sequential by default](docs/composing-suspending-functions.md#sequential-by-default) @@ -32,6 +33,8 @@ The main coroutines guide has moved to the [docs folder](docs/coroutines-guide.m * [Dispatchers and threads](docs/coroutine-context-and-dispatchers.md#dispatchers-and-threads) * [Unconfined vs confined dispatcher](docs/coroutine-context-and-dispatchers.md#unconfined-vs-confined-dispatcher) * [Debugging coroutines and threads](docs/coroutine-context-and-dispatchers.md#debugging-coroutines-and-threads) + * [Debugging with IDEA](docs/coroutine-context-and-dispatchers.md#debugging-with-idea) + * [Debugging using logging](docs/coroutine-context-and-dispatchers.md#debugging-using-logging) * [Jumping between threads](docs/coroutine-context-and-dispatchers.md#jumping-between-threads) * [Job in the context](docs/coroutine-context-and-dispatchers.md#job-in-the-context) * [Children of a coroutine](docs/coroutine-context-and-dispatchers.md#children-of-a-coroutine) @@ -47,7 +50,7 @@ The main coroutines guide has moved to the [docs folder](docs/coroutines-guide.m * [Suspending functions](docs/flow.md#suspending-functions) * [Flows](docs/flow.md#flows) * [Flows are cold](docs/flow.md#flows-are-cold) - * [Flow cancellation](docs/flow.md#flow-cancellation) + * [Flow cancellation basics](docs/flow.md#flow-cancellation-basics) * [Flow builders](docs/flow.md#flow-builders) * [Intermediate flow operators](docs/flow.md#intermediate-flow-operators) * [Transform operator](docs/flow.md#transform-operator) @@ -76,9 +79,11 @@ The main coroutines guide has moved to the [docs folder](docs/coroutines-guide.m * [Flow completion](docs/flow.md#flow-completion) * [Imperative finally block](docs/flow.md#imperative-finally-block) * [Declarative handling](docs/flow.md#declarative-handling) - * [Upstream exceptions only](docs/flow.md#upstream-exceptions-only) + * [Successful completion](docs/flow.md#successful-completion) * [Imperative versus declarative](docs/flow.md#imperative-versus-declarative) * [Launching flow](docs/flow.md#launching-flow) + * [Flow cancellation checks](docs/flow.md#flow-cancellation-checks) + * [Making busy flow cancellable](docs/flow.md#making-busy-flow-cancellable) * [Flow and Reactive Streams](docs/flow.md#flow-and-reactive-streams) * [Channels](docs/channels.md#channels) diff --git a/docs/basics.md b/docs/basics.md index c5e931c58a..8aca23a18c 100644 --- a/docs/basics.md +++ b/docs/basics.md @@ -41,7 +41,7 @@ fun main() { -> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-01.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-01.kt). You will see the following result: @@ -57,24 +57,24 @@ They are launched with [launch] _coroutine builder_ in a context of some [Corout Here we are launching a new coroutine in the [GlobalScope], meaning that the lifetime of the new coroutine is limited only by the lifetime of the whole application. -You can achieve the same result replacing -`GlobalScope.launch { ... }` with `thread { ... }` and `delay(...)` with `Thread.sleep(...)`. +You can achieve the same result by replacing +`GlobalScope.launch { ... }` with `thread { ... }`, and `delay(...)` with `Thread.sleep(...)`. Try it (don't forget to import `kotlin.concurrent.thread`). -If you start by replacing `GlobalScope.launch` by `thread`, the compiler produces the following error: +If you start by replacing `GlobalScope.launch` with `thread`, the compiler produces the following error: ``` Error: Kotlin: Suspend functions are only allowed to be called from a coroutine or another suspend function ``` -That is because [delay] is a special _suspending function_ that does not block a thread, but _suspends_ -coroutine and it can be only used from a coroutine. +That is because [delay] is a special _suspending function_ that does not block a thread, but _suspends_ the +coroutine, and it can be only used from a coroutine. ### Bridging blocking and non-blocking worlds The first example mixes _non-blocking_ `delay(...)` and _blocking_ `Thread.sleep(...)` in the same code. It is easy to lose track of which one is blocking and which one is not. -Let's be explicit about blocking using [runBlocking] coroutine builder: +Let's be explicit about blocking using the [runBlocking] coroutine builder:
@@ -95,7 +95,7 @@ fun main() {
-> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-02.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-02.kt). ### Scope builder -In addition to the coroutine scope provided by different builders, it is possible to declare your own scope using -[coroutineScope] builder. It creates a coroutine scope and does not complete until all launched children -complete. -[runBlocking] and [coroutineScope] may look similar because they both wait for its body and all its children to complete. -The main difference between these two is that the [runBlocking] method _blocks_ the current thread for waiting, -while [coroutineScope] just suspends, releasing the underlying thread for other usages. -Because of that difference, [runBlocking] is a regular function and [coroutineScope] is a suspending function. +In addition to the coroutine scope provided by different builders, it is possible to declare your own scope using the +[coroutineScope][_coroutineScope] builder. It creates a coroutine scope and does not complete until all launched children complete. + +[runBlocking] and [coroutineScope][_coroutineScope] may look similar because they both wait for their body and all its children to complete. +The main difference is that the [runBlocking] method _blocks_ the current thread for waiting, +while [coroutineScope][_coroutineScope] just suspends, releasing the underlying thread for other usages. +Because of that difference, [runBlocking] is a regular function and [coroutineScope][_coroutineScope] is a suspending function. It can be demonstrated by the following example: @@ -271,7 +271,7 @@ fun main() = runBlocking { // this: CoroutineScope -> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-06.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-06.kt). -Note that right after "Task from coroutine scope" message, while waiting for nested launch, - "Task from runBlocking" is executed and printed, though coroutineScope is not completed yet. +Note that right after the "Task from coroutine scope" message (while waiting for nested launch) + "Task from runBlocking" is executed and printed — even though the [coroutineScope][_coroutineScope] is not completed yet. ### Extract function refactoring Let's extract the block of code inside `launch { ... }` into a separate function. When you -perform "Extract function" refactoring on this code you get a new function with `suspend` modifier. -That is your first _suspending function_. Suspending functions can be used inside coroutines +perform "Extract function" refactoring on this code, you get a new function with the `suspend` modifier. +This is your first _suspending function_. Suspending functions can be used inside coroutines just like regular functions, but their additional feature is that they can, in turn, -use other suspending functions, like `delay` in this example, to _suspend_ execution of a coroutine. +use other suspending functions (like `delay` in this example) to _suspend_ execution of a coroutine.
@@ -310,7 +310,7 @@ suspend fun doWorld() {
-> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-07.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-07.kt). -It launches 100K coroutines and, after a second, each coroutine prints a dot. +It launches 100K coroutines and, after 5 seconds, each coroutine prints a dot. + Now, try that with threads. What would happen? (Most likely your code will produce some sort of out-of-memory error) ### Global coroutines are like daemon threads @@ -379,7 +380,7 @@ fun main() = runBlocking { -> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-09.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-09.kt). You can run and see that it prints three lines and terminates: @@ -402,7 +403,7 @@ Active coroutines that were launched in [GlobalScope] do not keep the process al [runBlocking]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/run-blocking.html [Job]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/index.html [Job.join]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/join.html -[coroutineScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/coroutine-scope.html +[_coroutineScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/coroutine-scope.html [CoroutineScope()]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope.html diff --git a/docs/cancellation-and-timeouts.md b/docs/cancellation-and-timeouts.md index b51c45c941..b296bde493 100644 --- a/docs/cancellation-and-timeouts.md +++ b/docs/cancellation-and-timeouts.md @@ -11,6 +11,7 @@ * [Closing resources with `finally`](#closing-resources-with-finally) * [Run non-cancellable block](#run-non-cancellable-block) * [Timeout](#timeout) + * [Asynchronous timeout and resources](#asynchronous-timeout-and-resources) @@ -49,7 +50,7 @@ fun main() = runBlocking { -> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-01.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-01.kt). It produces the following output: @@ -104,7 +105,7 @@ fun main() = runBlocking { -> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-02.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-02.kt). Run it to see that it continues to print "I'm sleeping" even after cancellation until the job completes by itself after five iterations. @@ -156,7 +157,7 @@ fun main() = runBlocking { -> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-03.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-03.kt). As you can see, now this loop is cancelled. [isActive] is an extension property available inside the coroutine via the [CoroutineScope] object. @@ -203,7 +204,7 @@ fun main() = runBlocking { -> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-04.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-04.kt). Both [join][Job.join] and [cancelAndJoin] wait for all finalization actions to complete, so the example above produces the following output: @@ -259,7 +260,7 @@ fun main() = runBlocking { -> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-05.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-05.kt). +### Asynchronous timeout and resources + + + +The timeout event in [withTimeout] is asynchronous with respect to the code running in its block and may happen at any time, +even right before the return from inside of the timeout block. Keep this in mind if you open or acquire some +resource inside the block that needs closing or release outside of the block. + +For example, here we imitate a closeable resource with the `Resource` class, that simply keeps track of how many times +it was created by incrementing the `acquired` counter and decrementing this counter from its `close` function. +Let us run a lot of coroutines with the small timeout try acquire this resource from inside +of the `withTimeout` block after a bit of delay and release it from outside. + +
+ +```kotlin +import kotlinx.coroutines.* + +//sampleStart +var acquired = 0 + +class Resource { + init { acquired++ } // Acquire the resource + fun close() { acquired-- } // Release the resource +} + +fun main() { + runBlocking { + repeat(100_000) { // Launch 100K coroutines + launch { + val resource = withTimeout(60) { // Timeout of 60 ms + delay(50) // Delay for 50 ms + Resource() // Acquire a resource and return it from withTimeout block + } + resource.close() // Release the resource + } + } + } + // Outside of runBlocking all coroutines have completed + println(acquired) // Print the number of resources still acquired +} +//sampleEnd +``` + +
+ +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-08.kt). + + + +If you run the above code you'll see that it does not always print zero, though it may depend on the timings +of your machine you may need to tweak timeouts in this example to actually see non-zero values. + +> Note, that incrementing and decrementing `acquired` counter here from 100K coroutines is completely safe, +> since it always happens from the same main thread. More on that will be explained in the next chapter +> on coroutine context. + +To workaround this problem you can store a reference to the resource in the variable as opposed to returning it +from the `withTimeout` block. + +
+ +```kotlin +import kotlinx.coroutines.* + +var acquired = 0 + +class Resource { + init { acquired++ } // Acquire the resource + fun close() { acquired-- } // Release the resource +} + +fun main() { +//sampleStart + runBlocking { + repeat(100_000) { // Launch 100K coroutines + launch { + var resource: Resource? = null // Not acquired yet + try { + withTimeout(60) { // Timeout of 60 ms + delay(50) // Delay for 50 ms + resource = Resource() // Store a resource to the variable if acquired + } + // We can do something else with the resource here + } finally { + resource?.close() // Release the resource if it was acquired + } + } + } + } + // Outside of runBlocking all coroutines have completed + println(acquired) // Print the number of resources still acquired +//sampleEnd +} +``` + +
+ +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-09.kt). + +This example always prints zero. Resources do not leak. + + + [launch]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/launch.html diff --git a/docs/channels.md b/docs/channels.md index 6769b8dc52..d1a4bac05b 100644 --- a/docs/channels.md +++ b/docs/channels.md @@ -52,7 +52,7 @@ fun main() = runBlocking { -> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-channel-01.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-channel-01.kt). The output of this code is: @@ -99,7 +99,7 @@ fun main() = runBlocking { -> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-channel-02.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-channel-02.kt). diff --git a/docs/coroutine-context-and-dispatchers.md b/docs/coroutine-context-and-dispatchers.md index e379842443..36e049db89 100644 --- a/docs/coroutine-context-and-dispatchers.md +++ b/docs/coroutine-context-and-dispatchers.md @@ -8,6 +8,8 @@ * [Dispatchers and threads](#dispatchers-and-threads) * [Unconfined vs confined dispatcher](#unconfined-vs-confined-dispatcher) * [Debugging coroutines and threads](#debugging-coroutines-and-threads) + * [Debugging with IDEA](#debugging-with-idea) + * [Debugging using logging](#debugging-using-logging) * [Jumping between threads](#jumping-between-threads) * [Job in the context](#job-in-the-context) * [Children of a coroutine](#children-of-a-coroutine) @@ -65,7 +67,7 @@ fun main() = runBlocking { -> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-context-01.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-context-01.kt). It produces the following output (maybe in different order): @@ -129,7 +131,7 @@ fun main() = runBlocking { -> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-context-02.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-context-02.kt). Produces the output: @@ -155,8 +157,34 @@ The unconfined dispatcher should not be used in general code. Coroutines can suspend on one thread and resume on another thread. Even with a single-threaded dispatcher it might be hard to -figure out what the coroutine was doing, where, and when. The common approach to debugging applications with -threads is to print the thread name in the log file on each log statement. This feature is universally supported +figure out what the coroutine was doing, where, and when if you don't have special tooling. + +#### Debugging with IDEA + +The Coroutine Debugger of the Kotlin plugin simplifies debugging coroutines in IntelliJ IDEA. + +> Debugging works for versions 1.3.8 or later of `kotlinx-coroutines-core`. + +The **Debug** tool window contains the **Coroutines** tab. In this tab, you can find information about both currently running and suspended coroutines. +The coroutines are grouped by the dispatcher they are running on. + +![Debugging coroutines](images/coroutine-idea-debugging-1.png) + +With the coroutine debugger, you can: +* Check the state of each coroutine. +* See the values of local and captured variables for both running and suspended coroutines. +* See a full coroutine creation stack, as well as a call stack inside the coroutine. The stack includes all frames with +variable values, even those that would be lost during standard debugging. +* Get a full report that contains the state of each coroutine and its stack. To obtain it, right-click inside the **Coroutines** tab, and then click **Get Coroutines Dump**. + +To start coroutine debugging, you just need to set breakpoints and run the application in debug mode. + +Learn more about coroutines debugging in the [tutorial](https://kotlinlang.org/docs/tutorials/coroutines/debug-coroutines-with-idea.html). + +#### Debugging using logging + +Another approach to debugging applications with +threads without Coroutine Debugger is to print the thread name in the log file on each log statement. This feature is universally supported by logging frameworks. When using coroutines, the thread name alone does not give much of a context, so `kotlinx.coroutines` includes debugging facilities to make it easier. @@ -186,7 +214,7 @@ fun main() = runBlocking { -> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-context-03.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-context-03.kt). There are three coroutines. The main coroutine (#1) inside `runBlocking` and two coroutines computing the deferred values `a` (#2) and `b` (#3). @@ -238,7 +266,7 @@ fun main() { -> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-context-04.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-context-04.kt). It demonstrates several new techniques. One is using [runBlocking] with an explicitly specified context, and the other one is using the [withContext] function to change the context of a coroutine while still staying in the @@ -274,7 +302,7 @@ fun main() = runBlocking { -> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-context-05.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-context-05.kt). In the [debug mode](#debugging-coroutines-and-threads), it outputs something like this: @@ -332,7 +360,7 @@ fun main() = runBlocking { -> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-context-06.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-context-06.kt). The output of this code is: @@ -375,7 +403,7 @@ fun main() = runBlocking { -> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-context-07.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-context-07.kt). The result is going to be: @@ -427,7 +455,7 @@ fun main() = runBlocking(CoroutineName("main")) { -> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-context-08.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-context-08.kt). The output it produces with `-Dkotlinx.coroutines.debug` JVM option is similar to: @@ -462,7 +490,7 @@ fun main() = runBlocking { -> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-context-09.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-context-09.kt). The output of this code with the `-Dkotlinx.coroutines.debug` JVM option is: @@ -501,21 +529,8 @@ class Activity { -Alternatively, we can implement the [CoroutineScope] interface in this `Activity` class. The best way to do it is -to use delegation with default factory functions. -We also can combine the desired dispatcher (we used [Dispatchers.Default] in this example) with the scope: - -
- -```kotlin - class Activity : CoroutineScope by CoroutineScope(Dispatchers.Default) { - // to be continued ... -``` - -
- -Now, we can launch coroutines in the scope of this `Activity` without having to explicitly -specify their context. For the demo, we launch ten coroutines that delay for a different time: +Now, we can launch coroutines in the scope of this `Activity` using the defined `scope`. +For the demo, we launch ten coroutines that delay for a different time:
@@ -524,7 +539,7 @@ specify their context. For the demo, we launch ten coroutines that delay for a d fun doSomething() { // launch ten coroutines for a demo, each working for a different time repeat(10) { i -> - launch { + mainScope.launch { delay((i + 1) * 200L) // variable delay 200ms, 400ms, ... etc println("Coroutine $i is done") } @@ -544,21 +559,19 @@ of the activity no more messages are printed, even if we wait a little longer.
```kotlin -import kotlin.coroutines.* import kotlinx.coroutines.* -class Activity : CoroutineScope by CoroutineScope(Dispatchers.Default) { - +class Activity { + private val mainScope = CoroutineScope(Dispatchers.Default) // use Default for test purposes + fun destroy() { - cancel() // Extension on CoroutineScope + mainScope.cancel() } - // to be continued ... - // class Activity continues fun doSomething() { // launch ten coroutines for a demo, each working for a different time repeat(10) { i -> - launch { + mainScope.launch { delay((i + 1) * 200L) // variable delay 200ms, 400ms, ... etc println("Coroutine $i is done") } @@ -581,7 +594,7 @@ fun main() = runBlocking {
-> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-context-10.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-context-10.kt). The output of this example is: @@ -597,6 +610,9 @@ Destroying activity! As you can see, only the first two coroutines print a message and the others are cancelled by a single invocation of `job.cancel()` in `Activity.destroy()`. +> Note, that Android has first-party support for coroutine scope in all entities with the lifecycle. +See [the corresponding documentation](https://developer.android.com/topic/libraries/architecture/coroutines#lifecyclescope). + ### Thread-local data Sometimes it is convenient to have an ability to pass some thread-local data to or between coroutines. @@ -632,12 +648,12 @@ fun main() = runBlocking {
-> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-context-11.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-context-11.kt). In this example we launch a new coroutine in a background thread pool using [Dispatchers.Default], so it works on a different thread from the thread pool, but it still has the value of the thread local variable that we specified using `threadLocal.asContextElement(value = "launch")`, -no matter on what thread the coroutine is executed. +no matter which thread the coroutine is executed on. Thus, the output (with [debug](#debugging-coroutines-and-threads)) is: ```text @@ -664,7 +680,7 @@ stored in a thread-local variable. However, in this case you are fully responsib potentially concurrent modifications to the variable in this mutable box. For advanced usage, for example for integration with logging MDC, transactional contexts or any other libraries -which internally use thread-locals for passing data, see documentation of the [ThreadContextElement] interface +which internally use thread-locals for passing data, see the documentation of the [ThreadContextElement] interface that should be implemented. diff --git a/docs/coroutines-guide.md b/docs/coroutines-guide.md index e3f18d208e..2d15a7bbff 100644 --- a/docs/coroutines-guide.md +++ b/docs/coroutines-guide.md @@ -10,7 +10,7 @@ coroutine-enabled primitives that this guide covers, including `launch`, `async` This is a guide on core features of `kotlinx.coroutines` with a series of examples, divided up into different topics. -In order to use coroutines as well as follow the examples in this guide, you need to add a dependency on `kotlinx-coroutines-core` module as explained +In order to use coroutines as well as follow the examples in this guide, you need to add a dependency on the `kotlinx-coroutines-core` module as explained [in the project README](../README.md#using-in-your-projects). ## Table of contents diff --git a/docs/exception-handling.md b/docs/exception-handling.md index 08e63ea994..a3070213d1 100644 --- a/docs/exception-handling.md +++ b/docs/exception-handling.md @@ -18,22 +18,22 @@ ## Exception Handling - This section covers exception handling and cancellation on exceptions. -We already know that cancelled coroutine throws [CancellationException] in suspension points and that it -is ignored by coroutines machinery. But what happens if an exception is thrown during cancellation or multiple children of the same -coroutine throw an exception? +We already know that a cancelled coroutine throws [CancellationException] in suspension points and that it +is ignored by the coroutines' machinery. Here we look at what happens if an exception is thrown during cancellation or multiple children of the same +coroutine throw an exception. ### Exception propagation -Coroutine builders come in two flavors: propagating exceptions automatically ([launch] and [actor]) or +Coroutine builders come in two flavors: propagating exceptions automatically ([launch] and [actor]) or exposing them to users ([async] and [produce]). -The former treat exceptions as unhandled, similar to Java's `Thread.uncaughtExceptionHandler`, -while the latter are relying on the user to consume the final +When these builders are used to create a _root_ coroutine, that is not a _child_ of another coroutine, +the former builders treat exceptions as **uncaught** exceptions, similar to Java's `Thread.uncaughtExceptionHandler`, +while the latter are relying on the user to consume the final exception, for example via [await][Deferred.await] or [receive][ReceiveChannel.receive] ([produce] and [receive][ReceiveChannel.receive] are covered later in [Channels](https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/channels.md) section). -It can be demonstrated by a simple example that creates coroutines in the [GlobalScope]: +It can be demonstrated by a simple example that creates root coroutines using the [GlobalScope]:
@@ -41,13 +41,13 @@ It can be demonstrated by a simple example that creates coroutines in the [Globa import kotlinx.coroutines.* fun main() = runBlocking { - val job = GlobalScope.launch { + val job = GlobalScope.launch { // root coroutine with launch println("Throwing exception from launch") throw IndexOutOfBoundsException() // Will be printed to the console by Thread.defaultUncaughtExceptionHandler } job.join() println("Joined failed job") - val deferred = GlobalScope.async { + val deferred = GlobalScope.async { // root coroutine with async println("Throwing exception from async") throw ArithmeticException() // Nothing is printed, relying on user to call await } @@ -62,7 +62,7 @@ fun main() = runBlocking {
-> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-exceptions-01.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-exceptions-01.kt). The output of this code is (with [debug](https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/coroutine-context-and-dispatchers.md#debugging-coroutines-and-threads)): @@ -78,9 +78,13 @@ Caught ArithmeticException ### CoroutineExceptionHandler -But what if one does not want to print all exceptions to the console? -[CoroutineExceptionHandler] context element is used as generic `catch` block of coroutine where custom logging or exception handling may take place. -It is similar to using [`Thread.uncaughtExceptionHandler`](https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#setUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)). +It is possible to customize the default behavior of printing **uncaught** exceptions to the console. +[CoroutineExceptionHandler] context element on a _root_ coroutine can be used as generic `catch` block for +this root coroutine and all its children where custom exception handling may take place. +It is similar to [`Thread.uncaughtExceptionHandler`](https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#setUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)). +You cannot recover from the exception in the `CoroutineExceptionHandler`. The coroutine had already completed +with the corresponding exception when the handler is called. Normally, the handler is used to +log the exception, show some kind of error message, terminate, and/or restart the application. On JVM it is possible to redefine global exception handler for all coroutines by registering [CoroutineExceptionHandler] via [`ServiceLoader`](https://docs.oracle.com/javase/8/docs/api/java/util/ServiceLoader.html). @@ -89,8 +93,15 @@ Global exception handler is similar to which is used when no more specific handlers are registered. On Android, `uncaughtExceptionPreHandler` is installed as a global coroutine exception handler. -[CoroutineExceptionHandler] is invoked only on exceptions which are not expected to be handled by the user, -so registering it in [async] builder and the like of it has no effect. +`CoroutineExceptionHandler` is invoked only on **uncaught** exceptions — exceptions that were not handled in any other way. +In particular, all _children_ coroutines (coroutines created in the context of another [Job]) delegate handling of +their exceptions to their parent coroutine, which also delegates to the parent, and so on until the root, +so the `CoroutineExceptionHandler` installed in their context is never used. +In addition to that, [async] builder always catches all exceptions and represents them in the resulting [Deferred] object, +so its `CoroutineExceptionHandler` has no effect either. + +> Coroutines running in supervision scope do not propagate exceptions to their parent and are +excluded from this rule. A further [Supervision](#supervision) section of this document gives more details.
@@ -100,12 +111,12 @@ import kotlinx.coroutines.* fun main() = runBlocking { //sampleStart val handler = CoroutineExceptionHandler { _, exception -> - println("Caught $exception") + println("CoroutineExceptionHandler got $exception") } - val job = GlobalScope.launch(handler) { + val job = GlobalScope.launch(handler) { // root coroutine, running in GlobalScope throw AssertionError() } - val deferred = GlobalScope.async(handler) { + val deferred = GlobalScope.async(handler) { // also root, but async instead of launch throw ArithmeticException() // Nothing will be printed, relying on user to call deferred.await() } joinAll(job, deferred) @@ -115,19 +126,19 @@ fun main() = runBlocking {
-> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-exceptions-02.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-exceptions-02.kt). The output of this code is: ```text -Caught java.lang.AssertionError +CoroutineExceptionHandler got java.lang.AssertionError ``` ### Cancellation and exceptions -Cancellation is tightly bound with exceptions. Coroutines internally use `CancellationException` for cancellation, these +Cancellation is closely related to exceptions. Coroutines internally use `CancellationException` for cancellation, these exceptions are ignored by all handlers, so they should be used only as the source of additional debug information, which can be obtained by `catch` block. When a coroutine is cancelled using [Job.cancel], it terminates, but it does not cancel its parent. @@ -161,7 +172,7 @@ fun main() = runBlocking { -> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-exceptions-03.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-exceptions-03.kt). The output of this code is: @@ -175,15 +186,17 @@ Parent is not cancelled If a coroutine encounters an exception other than `CancellationException`, it cancels its parent with that exception. This behaviour cannot be overridden and is used to provide stable coroutines hierarchies for -[structured concurrency](https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/composing-suspending-functions.md#structured-concurrency-with-async) which do not depend on -[CoroutineExceptionHandler] implementation. -The original exception is handled by the parent when all its children terminate. +[structured concurrency](https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/composing-suspending-functions.md#structured-concurrency-with-async). +[CoroutineExceptionHandler] implementation is not used for child coroutines. -> This also a reason why, in these examples, [CoroutineExceptionHandler] is always installed to a coroutine +> In these examples [CoroutineExceptionHandler] is always installed to a coroutine that is created in [GlobalScope]. It does not make sense to install an exception handler to a coroutine that is launched in the scope of the main [runBlocking], since the main coroutine is going to be always cancelled when its child completes with exception despite the installed handler. +The original exception is handled by the parent only when all its children terminate, +which is demonstrated by the following example. +
```kotlin @@ -192,7 +205,7 @@ import kotlinx.coroutines.* fun main() = runBlocking { //sampleStart val handler = CoroutineExceptionHandler { _, exception -> - println("Caught $exception") + println("CoroutineExceptionHandler got $exception") } val job = GlobalScope.launch(handler) { launch { // the first child @@ -219,7 +232,7 @@ fun main() = runBlocking {
-> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-exceptions-04.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-exceptions-04.kt). The output of this code is: @@ -227,22 +240,15 @@ The output of this code is: Second child throws an exception Children are cancelled, but exception is not handled until all children terminate The first child finished its non cancellable block -Caught java.lang.ArithmeticException +CoroutineExceptionHandler got java.lang.ArithmeticException ``` ### Exceptions aggregation -What happens if multiple children of a coroutine throw an exception? -The general rule is "the first exception wins", so the first thrown exception is exposed to the handler. -But that may cause lost exceptions, for example if coroutine throws an exception in its `finally` block. -So, additional exceptions are suppressed. - -> One of the solutions would have been to report each exception separately, -but then [Deferred.await] should have had the same mechanism to avoid behavioural inconsistency and this -would cause implementation details of a coroutines (whether it had delegated parts of its work to its children or not) -to leak to its exception handler. - +When multiple children of a coroutine fail with an exception, the +general rule is "the first exception wins", so the first exception gets handled. +All additional exceptions that happen after the first one are attached to the first exception as suppressed ones. -> Note, this mechanism currently works only on Java version 1.7+. -Limitation on JS and Native is temporary and will be fixed in the future. +> Note that this mechanism currently only works on Java version 1.7+. +The JS and Native restrictions are temporary and will be lifted in the future. -Cancellation exceptions are transparent and unwrapped by default: +Cancellation exceptions are transparent and are unwrapped by default:
@@ -304,13 +310,13 @@ import java.io.* fun main() = runBlocking { //sampleStart val handler = CoroutineExceptionHandler { _, exception -> - println("Caught original $exception") + println("CoroutineExceptionHandler got $exception") } val job = GlobalScope.launch(handler) { - val inner = launch { + val inner = launch { // all this stack of coroutines will get cancelled launch { launch { - throw IOException() + throw IOException() // the original exception } } } @@ -318,7 +324,7 @@ fun main() = runBlocking { inner.join() } catch (e: CancellationException) { println("Rethrowing CancellationException with original cause") - throw e + throw e // cancellation exception is rethrown, yet the original IOException gets to the handler } } job.join() @@ -328,32 +334,33 @@ fun main() = runBlocking {
-> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-exceptions-06.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-exceptions-06.kt). The output of this code is: ```text Rethrowing CancellationException with original cause -Caught original java.io.IOException +CoroutineExceptionHandler got java.io.IOException ``` ### Supervision As we have studied before, cancellation is a bidirectional relationship propagating through the whole -coroutines hierarchy. But what if unidirectional cancellation is required? +hierarchy of coroutines. Let us take a look at the case when unidirectional cancellation is required. A good example of such a requirement is a UI component with the job defined in its scope. If any of the UI's child tasks have failed, it is not always necessary to cancel (effectively kill) the whole UI component, -but if UI component is destroyed (and its job is cancelled), then it is necessary to fail all child jobs as their results are no longer required. +but if UI component is destroyed (and its job is cancelled), then it is necessary to fail all child jobs as their results are no longer needed. -Another example is a server process that spawns several children jobs and needs to _supervise_ -their execution, tracking their failures and restarting just those children jobs that had failed. +Another example is a server process that spawns multiple child jobs and needs to _supervise_ +their execution, tracking their failures and only restarting the failed ones. #### Supervision job -For these purposes [SupervisorJob][SupervisorJob()] can be used. It is similar to a regular [Job][Job()] with the only exception that cancellation is propagated -only downwards. It is easy to demonstrate with an example: +The [SupervisorJob][SupervisorJob()] can be used for these purposes. +It is similar to a regular [Job][Job()] with the only exception that cancellation is propagated +only downwards. This can easily be demonstrated using the following example:
@@ -365,24 +372,24 @@ fun main() = runBlocking { with(CoroutineScope(coroutineContext + supervisor)) { // launch the first child -- its exception is ignored for this example (don't do this in practice!) val firstChild = launch(CoroutineExceptionHandler { _, _ -> }) { - println("First child is failing") - throw AssertionError("First child is cancelled") + println("The first child is failing") + throw AssertionError("The first child is cancelled") } // launch the second child val secondChild = launch { firstChild.join() // Cancellation of the first child is not propagated to the second child - println("First child is cancelled: ${firstChild.isCancelled}, but second one is still active") + println("The first child is cancelled: ${firstChild.isCancelled}, but the second one is still active") try { delay(Long.MAX_VALUE) } finally { // But cancellation of the supervisor is propagated - println("Second child is cancelled because supervisor is cancelled") + println("The second child is cancelled because the supervisor was cancelled") } } // wait until the first child fails & completes firstChild.join() - println("Cancelling supervisor") + println("Cancelling the supervisor") supervisor.cancel() secondChild.join() } @@ -391,24 +398,24 @@ fun main() = runBlocking {
-> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-supervision-01.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-supervision-01.kt). The output of this code is: ```text -First child is failing -First child is cancelled: true, but second one is still active -Cancelling supervisor -Second child is cancelled because supervisor is cancelled +The first child is failing +The first child is cancelled: true, but the second one is still active +Cancelling the supervisor +The second child is cancelled because the supervisor was cancelled ``` #### Supervision scope -For *scoped* concurrency [supervisorScope] can be used instead of [coroutineScope] for the same purpose. It propagates cancellation -only in one direction and cancels all children only if it has failed itself. It also waits for all children before completion -just like [coroutineScope] does. +Instead of [coroutineScope][_coroutineScope], we can use [supervisorScope][_supervisorScope] for _scoped_ concurrency. It propagates the cancellation +in one direction only and cancels all its children only if it failed itself. It also waits for all children before completion +just like [coroutineScope][_coroutineScope] does.
@@ -421,42 +428,45 @@ fun main() = runBlocking { supervisorScope { val child = launch { try { - println("Child is sleeping") + println("The child is sleeping") delay(Long.MAX_VALUE) } finally { - println("Child is cancelled") + println("The child is cancelled") } } // Give our child a chance to execute and print using yield yield() - println("Throwing exception from scope") + println("Throwing an exception from the scope") throw AssertionError() } } catch(e: AssertionError) { - println("Caught assertion error") + println("Caught an assertion error") } } ```
-> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-supervision-02.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-supervision-02.kt). The output of this code is: ```text -Child is sleeping -Throwing exception from scope -Child is cancelled -Caught assertion error +The child is sleeping +Throwing an exception from the scope +The child is cancelled +Caught an assertion error ``` #### Exceptions in supervised coroutines Another crucial difference between regular and supervisor jobs is exception handling. -Every child should handle its exceptions by itself via exception handling mechanisms. -This difference comes from the fact that child's failure is not propagated to the parent. +Every child should handle its exceptions by itself via the exception handling mechanism. +This difference comes from the fact that child's failure does not propagate to the parent. +It means that coroutines launched directly inside the [supervisorScope][_supervisorScope] _do_ use the [CoroutineExceptionHandler] +that is installed in their scope in the same way as root coroutines do +(see the [CoroutineExceptionHandler](#coroutineexceptionhandler) section for details).
@@ -466,30 +476,30 @@ import kotlinx.coroutines.* fun main() = runBlocking { val handler = CoroutineExceptionHandler { _, exception -> - println("Caught $exception") + println("CoroutineExceptionHandler got $exception") } supervisorScope { val child = launch(handler) { - println("Child throws an exception") + println("The child throws an exception") throw AssertionError() } - println("Scope is completing") + println("The scope is completing") } - println("Scope is completed") + println("The scope is completed") } ```
-> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-supervision-03.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-supervision-03.kt). The output of this code is: ```text -Scope is completing -Child throws an exception -Caught java.lang.AssertionError -Scope is completed +The scope is completing +The child throws an exception +CoroutineExceptionHandler got java.lang.AssertionError +The scope is completed ``` @@ -501,12 +511,14 @@ Scope is completed [Deferred.await]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-deferred/await.html [GlobalScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-global-scope/index.html [CoroutineExceptionHandler]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-exception-handler/index.html +[Job]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/index.html +[Deferred]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-deferred/index.html [Job.cancel]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/cancel.html [runBlocking]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/run-blocking.html [SupervisorJob()]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-supervisor-job.html [Job()]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job.html -[supervisorScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/supervisor-scope.html -[coroutineScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/coroutine-scope.html +[_coroutineScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/coroutine-scope.html +[_supervisorScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/supervisor-scope.html [actor]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.channels/actor.html [produce]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.channels/produce.html diff --git a/docs/flow.md b/docs/flow.md index 705f338b20..4374e7aa86 100644 --- a/docs/flow.md +++ b/docs/flow.md @@ -10,7 +10,7 @@ * [Suspending functions](#suspending-functions) * [Flows](#flows) * [Flows are cold](#flows-are-cold) - * [Flow cancellation](#flow-cancellation) + * [Flow cancellation basics](#flow-cancellation-basics) * [Flow builders](#flow-builders) * [Intermediate flow operators](#intermediate-flow-operators) * [Transform operator](#transform-operator) @@ -39,31 +39,33 @@ * [Flow completion](#flow-completion) * [Imperative finally block](#imperative-finally-block) * [Declarative handling](#declarative-handling) - * [Upstream exceptions only](#upstream-exceptions-only) + * [Successful completion](#successful-completion) * [Imperative versus declarative](#imperative-versus-declarative) * [Launching flow](#launching-flow) + * [Flow cancellation checks](#flow-cancellation-checks) + * [Making busy flow cancellable](#making-busy-flow-cancellable) * [Flow and Reactive Streams](#flow-and-reactive-streams) ## Asynchronous Flow -Suspending functions asynchronously returns a single value, but how can we return +A suspending function asynchronously returns a single value, but how can we return multiple asynchronously computed values? This is where Kotlin Flows come in. ### Representing multiple values Multiple values can be represented in Kotlin using [collections]. -For example, we can have a function `foo()` that returns a [List] +For example, we can have a `simple` function that returns a [List] of three numbers and then print them all using [forEach]:
```kotlin -fun foo(): List = listOf(1, 2, 3) +fun simple(): List = listOf(1, 2, 3) fun main() { - foo().forEach { value -> println(value) } + simple().forEach { value -> println(value) } } ``` @@ -89,7 +91,7 @@ If we are computing the numbers with some CPU-consuming blocking code
```kotlin -fun foo(): Sequence = sequence { // sequence builder +fun simple(): Sequence = sequence { // sequence builder for (i in 1..3) { Thread.sleep(100) // pretend we are computing it yield(i) // yield next value @@ -97,7 +99,7 @@ fun foo(): Sequence = sequence { // sequence builder } fun main() { - foo().forEach { value -> println(value) } + simple().forEach { value -> println(value) } } ``` @@ -116,7 +118,7 @@ This code outputs the same numbers, but it waits 100ms before printing each one. #### Suspending functions However, this computation blocks the main thread that is running the code. -When these values are computed by asynchronous code we can mark the function `foo` with a `suspend` modifier, +When these values are computed by asynchronous code we can mark the `simple` function with a `suspend` modifier, so that it can perform its work without blocking and return the result as a list:
@@ -125,13 +127,13 @@ so that it can perform its work without blocking and return the result as a list import kotlinx.coroutines.* //sampleStart -suspend fun foo(): List { +suspend fun simple(): List { delay(1000) // pretend we are doing something asynchronous here return listOf(1, 2, 3) } fun main() = runBlocking { - foo().forEach { value -> println(value) } + simple().forEach { value -> println(value) } } //sampleEnd ``` @@ -151,7 +153,7 @@ This code prints the numbers after waiting for a second. #### Flows Using the `List` result type, means we can only return all the values at once. To represent -the stream of values that are being asynchronously computed, we can use a [`Flow`][Flow] type just like we would the `Sequence` type for synchronously computed values: +the stream of values that are being asynchronously computed, we can use a [`Flow`][Flow] type just like we would use the `Sequence` type for synchronously computed values:
@@ -160,7 +162,7 @@ import kotlinx.coroutines.* import kotlinx.coroutines.flow.* //sampleStart -fun foo(): Flow = flow { // flow builder +fun simple(): Flow = flow { // flow builder for (i in 1..3) { delay(100) // pretend we are doing something useful here emit(i) // emit next value @@ -176,7 +178,7 @@ fun main() = runBlocking { } } // Collect the flow - foo().collect { value -> println(value) } + simple().collect { value -> println(value) } } //sampleEnd ``` @@ -201,18 +203,18 @@ I'm not blocked 3 Notice the following differences in the code with the [Flow] from the earlier examples: -* A builder function for [Flow] type is called [flow]. +* A builder function for [Flow] type is called [flow][_flow]. * Code inside the `flow { ... }` builder block can suspend. -* The function `foo()` is no longer marked with `suspend` modifier. +* The `simple` function is no longer marked with `suspend` modifier. * Values are _emitted_ from the flow using [emit][FlowCollector.emit] function. * Values are _collected_ from the flow using [collect][collect] function. -> We can replace [delay] with `Thread.sleep` in the body of `foo`'s `flow { ... }` and see that the main +> We can replace [delay] with `Thread.sleep` in the body of `simple`'s `flow { ... }` and see that the main thread is blocked in this case. ### Flows are cold -Flows are _cold_ streams similar to sequences — the code inside a [flow] builder does not +Flows are _cold_ streams similar to sequences — the code inside a [flow][_flow] builder does not run until the flow is collected. This becomes clear in the following example:
@@ -222,7 +224,7 @@ import kotlinx.coroutines.* import kotlinx.coroutines.flow.* //sampleStart -fun foo(): Flow = flow { +fun simple(): Flow = flow { println("Flow started") for (i in 1..3) { delay(100) @@ -231,8 +233,8 @@ fun foo(): Flow = flow { } fun main() = runBlocking { - println("Calling foo...") - val flow = foo() + println("Calling simple function...") + val flow = simple() println("Calling collect...") flow.collect { value -> println(value) } println("Calling collect again...") @@ -248,7 +250,7 @@ fun main() = runBlocking { Which prints: ```text -Calling foo... +Calling simple function... Calling collect... Flow started 1 @@ -263,16 +265,14 @@ Flow started -This is a key reason the `foo()` function (which returns a flow) is not marked with `suspend` modifier. -By itself, `foo()` returns quickly and does not wait for anything. The flow starts every time it is collected, +This is a key reason the `simple` function (which returns a flow) is not marked with `suspend` modifier. +By itself, `simple()` call returns quickly and does not wait for anything. The flow starts every time it is collected, that is why we see "Flow started" when we call `collect` again. -### Flow cancellation - -Flow adheres to the general cooperative cancellation of coroutines. However, flow infrastructure does not introduce -additional cancellation points. It is fully transparent for cancellation. As usual, flow collection can be -cancelled when the flow is suspended in a cancellable suspending function (like [delay]), and cannot be cancelled otherwise. +### Flow cancellation basics +Flow adheres to the general cooperative cancellation of coroutines. As usual, flow collection can be +cancelled when the flow is suspended in a cancellable suspending function (like [delay]). The following example shows how the flow gets cancelled on a timeout when running in a [withTimeoutOrNull] block and stops executing its code: @@ -283,7 +283,7 @@ import kotlinx.coroutines.* import kotlinx.coroutines.flow.* //sampleStart -fun foo(): Flow = flow { +fun simple(): Flow = flow { for (i in 1..3) { delay(100) println("Emitting $i") @@ -293,7 +293,7 @@ fun foo(): Flow = flow { fun main() = runBlocking { withTimeoutOrNull(250) { // Timeout after 250ms - foo().collect { value -> println(value) } + simple().collect { value -> println(value) } } println("Done") } @@ -304,7 +304,7 @@ fun main() = runBlocking { > You can get the full code from [here](../kotlinx-coroutines-core/jvm/test/guide/example-flow-06.kt). -Notice how only two numbers get emitted by the flow in `foo()` function, producing the following output: +Notice how only two numbers get emitted by the flow in the `simple` function, producing the following output: ```text Emitting 1 @@ -316,6 +316,8 @@ Done +See [Flow cancellation checks](#flow-cancellation-checks) section for more details. + ### Flow builders The `flow { ... }` builder from the previous examples is the most basic one. There are other builders for @@ -590,14 +592,14 @@ Filter 5 ### Flow context Collection of a flow always happens in the context of the calling coroutine. For example, if there is -a `foo` flow, then the following code runs in the context specified -by the author of this code, regardless of the implementation details of the `foo` flow: +a `simple` flow, then the following code runs in the context specified +by the author of this code, regardless of the implementation details of the `simple` flow:
```kotlin withContext(context) { - foo.collect { value -> + simple().collect { value -> println(value) // run in the specified context } } @@ -610,7 +612,7 @@ withContext(context) { This property of a flow is called _context preservation_. So, by default, code in the `flow { ... }` builder runs in the context that is provided by a collector -of the corresponding flow. For example, consider the implementation of `foo` that prints the thread +of the corresponding flow. For example, consider the implementation of a `simple` function that prints the thread it is called on and emits three numbers:
@@ -622,15 +624,15 @@ import kotlinx.coroutines.flow.* fun log(msg: String) = println("[${Thread.currentThread().name}] $msg") //sampleStart -fun foo(): Flow = flow { - log("Started foo flow") +fun simple(): Flow = flow { + log("Started simple flow") for (i in 1..3) { emit(i) } } fun main() = runBlocking { - foo().collect { value -> log("Collected $value") } + simple().collect { value -> log("Collected $value") } } //sampleEnd ``` @@ -642,7 +644,7 @@ fun main() = runBlocking { Running this code produces: ```text -[main @coroutine#1] Started foo flow +[main @coroutine#1] Started simple flow [main @coroutine#1] Collected 1 [main @coroutine#1] Collected 2 [main @coroutine#1] Collected 3 @@ -650,7 +652,7 @@ Running this code produces: -Since `foo().collect` is called from the main thread, the body of `foo`'s flow is also called in the main thread. +Since `simple().collect` is called from the main thread, the body of `simple`'s flow is also called in the main thread. This is the perfect default for fast-running or asynchronous code that does not care about the execution context and does not block the caller. @@ -670,7 +672,7 @@ import kotlinx.coroutines.* import kotlinx.coroutines.flow.* //sampleStart -fun foo(): Flow = flow { +fun simple(): Flow = flow { // The WRONG way to change context for CPU-consuming code in flow builder kotlinx.coroutines.withContext(Dispatchers.Default) { for (i in 1..3) { @@ -681,7 +683,7 @@ fun foo(): Flow = flow { } fun main() = runBlocking { - foo().collect { value -> println(value) } + simple().collect { value -> println(value) } } //sampleEnd ``` @@ -695,7 +697,7 @@ This code produces the following exception: ```text Exception in thread "main" java.lang.IllegalStateException: Flow invariant is violated: Flow was collected in [CoroutineId(1), "coroutine#1":BlockingCoroutine{Active}@5511c7f8, BlockingEventLoop@2eac3323], - but emission happened in [CoroutineId(1), "coroutine#1":DispatchedCoroutine{Active}@2dae0000, DefaultDispatcher]. + but emission happened in [CoroutineId(1), "coroutine#1":DispatchedCoroutine{Active}@2dae0000, Dispatchers.Default]. Please refer to 'flow' documentation or use 'flowOn' instead at ... ``` @@ -717,7 +719,7 @@ import kotlinx.coroutines.flow.* fun log(msg: String) = println("[${Thread.currentThread().name}] $msg") //sampleStart -fun foo(): Flow = flow { +fun simple(): Flow = flow { for (i in 1..3) { Thread.sleep(100) // pretend we are computing it in CPU-consuming way log("Emitting $i") @@ -726,7 +728,7 @@ fun foo(): Flow = flow { }.flowOn(Dispatchers.Default) // RIGHT way to change context for CPU-consuming code in flow builder fun main() = runBlocking { - foo().collect { value -> + simple().collect { value -> log("Collected $value") } } @@ -757,7 +759,7 @@ creates another coroutine for an upstream flow when it has to change the [Corout Running different parts of a flow in different coroutines can be helpful from the standpoint of the overall time it takes to collect the flow, especially when long-running asynchronous operations are involved. For example, consider a case when -the emission by `foo()` flow is slow, taking 100 ms to produce an element; and collector is also slow, +the emission by a `simple` flow is slow, taking 100 ms to produce an element; and collector is also slow, taking 300 ms to process an element. Let's see how long it takes to collect such a flow with three numbers:
@@ -768,7 +770,7 @@ import kotlinx.coroutines.flow.* import kotlin.system.* //sampleStart -fun foo(): Flow = flow { +fun simple(): Flow = flow { for (i in 1..3) { delay(100) // pretend we are asynchronously waiting 100 ms emit(i) // emit next value @@ -777,7 +779,7 @@ fun foo(): Flow = flow { fun main() = runBlocking { val time = measureTimeMillis { - foo().collect { value -> + simple().collect { value -> delay(300) // pretend we are processing it for 300 ms println(value) } @@ -802,7 +804,7 @@ Collected in 1220 ms -We can use a [buffer] operator on a flow to run emitting code of `foo()` concurrently with collecting code, +We can use a [buffer] operator on a flow to run emitting code of the `simple` flow concurrently with collecting code, as opposed to running them sequentially:
@@ -812,7 +814,7 @@ import kotlinx.coroutines.* import kotlinx.coroutines.flow.* import kotlin.system.* -fun foo(): Flow = flow { +fun simple(): Flow = flow { for (i in 1..3) { delay(100) // pretend we are asynchronously waiting 100 ms emit(i) // emit next value @@ -822,7 +824,7 @@ fun foo(): Flow = flow { fun main() = runBlocking { //sampleStart val time = measureTimeMillis { - foo() + simple() .buffer() // buffer emissions, don't wait .collect { value -> delay(300) // pretend we are processing it for 300 ms @@ -867,7 +869,7 @@ import kotlinx.coroutines.* import kotlinx.coroutines.flow.* import kotlin.system.* -fun foo(): Flow = flow { +fun simple(): Flow = flow { for (i in 1..3) { delay(100) // pretend we are asynchronously waiting 100 ms emit(i) // emit next value @@ -877,7 +879,7 @@ fun foo(): Flow = flow { fun main() = runBlocking { //sampleStart val time = measureTimeMillis { - foo() + simple() .conflate() // conflate emissions, don't process each one .collect { value -> delay(300) // pretend we are processing it for 300 ms @@ -918,7 +920,7 @@ import kotlinx.coroutines.* import kotlinx.coroutines.flow.* import kotlin.system.* -fun foo(): Flow = flow { +fun simple(): Flow = flow { for (i in 1..3) { delay(100) // pretend we are asynchronously waiting 100 ms emit(i) // emit next value @@ -928,7 +930,7 @@ fun foo(): Flow = flow { fun main() = runBlocking { //sampleStart val time = measureTimeMillis { - foo() + simple() .collectLatest { value -> // cancel & restart on the latest value println("Collecting $value") delay(300) // pretend we are processing it for 300 ms @@ -1110,7 +1112,7 @@ Now if we have a flow of three integers and call `requestFlow` for each of them Then we end up with a flow of flows (`Flow>`) that needs to be _flattened_ into a single flow for further processing. Collections and sequences have [flatten][Sequence.flatten] and [flatMap][Sequence.flatMap] -operators for this. However, due the asynchronous nature of flows they call for different _modes_ of flattening, +operators for this. However, due to the asynchronous nature of flows they call for different _modes_ of flattening, as such, there is a family of flattening operators on flows. #### flatMapConcat @@ -1279,7 +1281,7 @@ import kotlinx.coroutines.* import kotlinx.coroutines.flow.* //sampleStart -fun foo(): Flow = flow { +fun simple(): Flow = flow { for (i in 1..3) { println("Emitting $i") emit(i) // emit next value @@ -1288,7 +1290,7 @@ fun foo(): Flow = flow { fun main() = runBlocking { try { - foo().collect { value -> + simple().collect { value -> println(value) check(value <= 1) { "Collected $value" } } @@ -1329,7 +1331,7 @@ import kotlinx.coroutines.* import kotlinx.coroutines.flow.* //sampleStart -fun foo(): Flow = +fun simple(): Flow = flow { for (i in 1..3) { println("Emitting $i") @@ -1343,7 +1345,7 @@ fun foo(): Flow = fun main() = runBlocking { try { - foo().collect { value -> println(value) } + simple().collect { value -> println(value) } } catch (e: Throwable) { println("Caught $e") } @@ -1390,7 +1392,7 @@ For example, let us emit the text on catching an exception: import kotlinx.coroutines.* import kotlinx.coroutines.flow.* -fun foo(): Flow = +fun simple(): Flow = flow { for (i in 1..3) { println("Emitting $i") @@ -1404,7 +1406,7 @@ fun foo(): Flow = fun main() = runBlocking { //sampleStart - foo() + simple() .catch { e -> emit("Caught $e") } // emit on exception .collect { value -> println(value) } //sampleEnd @@ -1437,7 +1439,7 @@ import kotlinx.coroutines.* import kotlinx.coroutines.flow.* //sampleStart -fun foo(): Flow = flow { +fun simple(): Flow = flow { for (i in 1..3) { println("Emitting $i") emit(i) @@ -1445,7 +1447,7 @@ fun foo(): Flow = flow { } fun main() = runBlocking { - foo() + simple() .catch { e -> println("Caught $e") } // does not catch downstream exceptions .collect { value -> check(value <= 1) { "Collected $value" } @@ -1461,13 +1463,15 @@ fun main() = runBlocking { A "Caught ..." message is not printed despite there being a `catch` operator: - +``` + + #### Catching declaratively @@ -1481,7 +1485,7 @@ be triggered by a call to `collect()` without parameters: import kotlinx.coroutines.* import kotlinx.coroutines.flow.* -fun foo(): Flow = flow { +fun simple(): Flow = flow { for (i in 1..3) { println("Emitting $i") emit(i) @@ -1490,7 +1494,7 @@ fun foo(): Flow = flow { fun main() = runBlocking { //sampleStart - foo() + simple() .onEach { value -> check(value <= 1) { "Collected $value" } println(value) @@ -1508,12 +1512,14 @@ fun main() = runBlocking { Now we can see that a "Caught ..." message is printed and so we can catch all the exceptions without explicitly using a `try/catch` block: - +``` + + ### Flow completion @@ -1532,11 +1538,11 @@ import kotlinx.coroutines.* import kotlinx.coroutines.flow.* //sampleStart -fun foo(): Flow = (1..3).asFlow() +fun simple(): Flow = (1..3).asFlow() fun main() = runBlocking { try { - foo().collect { value -> println(value) } + simple().collect { value -> println(value) } } finally { println("Done") } @@ -1548,7 +1554,7 @@ fun main() = runBlocking { > You can get the full code from [here](../kotlinx-coroutines-core/jvm/test/guide/example-flow-31.kt). -This code prints three numbers produced by the `foo()` flow followed by a "Done" string: +This code prints three numbers produced by the `simple` flow followed by a "Done" string: ```text 1 @@ -1572,11 +1578,11 @@ The previous example can be rewritten using an [onCompletion] operator and produ import kotlinx.coroutines.* import kotlinx.coroutines.flow.* -fun foo(): Flow = (1..3).asFlow() +fun simple(): Flow = (1..3).asFlow() fun main() = runBlocking { //sampleStart - foo() + simple() .onCompletion { println("Done") } .collect { value -> println(value) } //sampleEnd @@ -1595,7 +1601,7 @@ Done The key advantage of [onCompletion] is a nullable `Throwable` parameter of the lambda that can be used to determine whether the flow collection was completed normally or exceptionally. In the following -example the `foo()` flow throws an exception after emitting the number 1: +example the `simple` flow throws an exception after emitting the number 1:
@@ -1604,13 +1610,13 @@ import kotlinx.coroutines.* import kotlinx.coroutines.flow.* //sampleStart -fun foo(): Flow = flow { +fun simple(): Flow = flow { emit(1) throw RuntimeException() } fun main() = runBlocking { - foo() + simple() .onCompletion { cause -> if (cause != null) println("Flow completed exceptionally") } .catch { cause -> println("Caught exception") } .collect { value -> println(value) } @@ -1635,10 +1641,10 @@ The [onCompletion] operator, unlike [catch], does not handle the exception. As w example code, the exception still flows downstream. It will be delivered to further `onCompletion` operators and can be handled with a `catch` operator. -#### Upstream exceptions only +#### Successful completion -Just like the [catch] operator, [onCompletion] only sees exceptions coming from upstream and does not -see downstream exceptions. For example, run the following code: +Another difference with [catch] operator is that [onCompletion] sees all exceptions and receives +a `null` exception only on successful completion of the upstream flow (without cancellation or failure).
@@ -1647,10 +1653,10 @@ import kotlinx.coroutines.* import kotlinx.coroutines.flow.* //sampleStart -fun foo(): Flow = (1..3).asFlow() +fun simple(): Flow = (1..3).asFlow() fun main() = runBlocking { - foo() + simple() .onCompletion { cause -> println("Flow completed with $cause") } .collect { value -> check(value <= 1) { "Collected $value" } @@ -1664,11 +1670,11 @@ fun main() = runBlocking { > You can get the full code from [here](../kotlinx-coroutines-core/jvm/test/guide/example-flow-34.kt). -We can see the completion cause is null, yet collection failed with exception: +We can see the completion cause is not null, because the flow was aborted due to downstream exception: ```text 1 -Flow completed with null +Flow completed with java.lang.IllegalStateException: Collected 2 Exception in thread "main" java.lang.IllegalStateException: Collected 2 ``` @@ -1777,6 +1783,127 @@ as cancellation and structured concurrency serve this purpose. Note that [launchIn] also returns a [Job], which can be used to [cancel][Job.cancel] the corresponding flow collection coroutine only without cancelling the whole scope or to [join][Job.join] it. +### Flow cancellation checks + +For convenience, the [flow][_flow] builder performs additional [ensureActive] checks for cancellation on each emitted value. +It means that a busy loop emitting from a `flow { ... }` is cancellable: + +
+ +```kotlin +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* + +//sampleStart +fun foo(): Flow = flow { + for (i in 1..5) { + println("Emitting $i") + emit(i) + } +} + +fun main() = runBlocking { + foo().collect { value -> + if (value == 3) cancel() + println(value) + } +} +//sampleEnd +``` + +
+ +> You can get the full code from [here](../kotlinx-coroutines-core/jvm/test/guide/example-flow-37.kt). + +We get only numbers up to 3 and a [CancellationException] after trying to emit number 4: + +```text +Emitting 1 +1 +Emitting 2 +2 +Emitting 3 +3 +Emitting 4 +Exception in thread "main" kotlinx.coroutines.JobCancellationException: BlockingCoroutine was cancelled; job="coroutine#1":BlockingCoroutine{Cancelled}@6d7b4f4c +``` + + + +However, most other flow operators do not do additional cancellation checks on their own for performance reasons. +For example, if you use [IntRange.asFlow] extension to write the same busy loop and don't suspend anywhere, +then there are no checks for cancellation: + +
+ +```kotlin +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* + +//sampleStart +fun main() = runBlocking { + (1..5).asFlow().collect { value -> + if (value == 3) cancel() + println(value) + } +} +//sampleEnd +``` + +
+ +> You can get the full code from [here](../kotlinx-coroutines-core/jvm/test/guide/example-flow-38.kt). + +All numbers from 1 to 5 are collected and cancellation gets detected only before return from `runBlocking`: + +```text +1 +2 +3 +4 +5 +Exception in thread "main" kotlinx.coroutines.JobCancellationException: BlockingCoroutine was cancelled; job="coroutine#1":BlockingCoroutine{Cancelled}@3327bd23 +``` + + + +#### Making busy flow cancellable + +In the case where you have a busy loop with coroutines you must explicitly check for cancellation. +You can add `.onEach { currentCoroutineContext().ensureActive() }`, but there is a ready-to-use +[cancellable] operator provided to do that: + +
+ +```kotlin +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* + +//sampleStart +fun main() = runBlocking { + (1..5).asFlow().cancellable().collect { value -> + if (value == 3) cancel() + println(value) + } +} +//sampleEnd +``` + +
+ +> You can get the full code from [here](../kotlinx-coroutines-core/jvm/test/guide/example-flow-39.kt). + +With the `cancellable` operator only the numbers from 1 to 3 are collected: + +```text +1 +2 +3 +Exception in thread "main" kotlinx.coroutines.JobCancellationException: BlockingCoroutine was cancelled; job="coroutine#1":BlockingCoroutine{Cancelled}@5ec0a365 +``` + + + ### Flow and Reactive Streams For those who are familiar with [Reactive Streams](https://www.reactive-streams.org/) or reactive frameworks such as RxJava and project Reactor, @@ -1786,7 +1913,7 @@ Indeed, its design was inspired by Reactive Streams and its various implementati be Kotlin and suspension friendly and respect structured concurrency. Achieving this goal would be impossible without reactive pioneers and their tremendous work. You can read the complete story in [Reactive Streams and Kotlin Flows](https://medium.com/@elizarov/reactive-streams-and-kotlin-flows-bfd12772cda4) article. While being different, conceptually, Flow *is* a reactive stream and it is possible to convert it to the reactive (spec and TCK compliant) Publisher and vice versa. -Such converters are provided by `kotlinx.coroutines` out-of-the-box and can be found in corresponding reactive modules (`kotlinx-coroutines-reactive` for Reactive Streams, `kotlinx-coroutines-reactor` for Project Reactor and `kotlinx-coroutines-rx2` for RxJava2). +Such converters are provided by `kotlinx.coroutines` out-of-the-box and can be found in corresponding reactive modules (`kotlinx-coroutines-reactive` for Reactive Streams, `kotlinx-coroutines-reactor` for Project Reactor and `kotlinx-coroutines-rx2`/`kotlinx-coroutines-rx3` for RxJava2/RxJava3). Integration modules include conversions from and to `Flow`, integration with Reactor's `Context` and suspension-friendly ways to work with various reactive entities. @@ -1813,9 +1940,11 @@ Integration modules include conversions from and to `Flow`, integration with Rea [Job]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/index.html [Job.cancel]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/cancel.html [Job.join]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/join.html +[ensureActive]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/ensure-active.html +[CancellationException]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-cancellation-exception/index.html [Flow]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html -[flow]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/flow.html +[_flow]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/flow.html [FlowCollector.emit]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow-collector/emit.html [collect]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/collect.html [flowOf]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/flow-of.html @@ -1845,4 +1974,6 @@ Integration modules include conversions from and to `Flow`, integration with Rea [catch]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/catch.html [onCompletion]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/on-completion.html [launchIn]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/launch-in.html +[IntRange.asFlow]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/kotlin.ranges.-int-range/as-flow.html +[cancellable]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/cancellable.html diff --git a/docs/images/coroutine-idea-debugging-1.png b/docs/images/coroutine-idea-debugging-1.png new file mode 100644 index 0000000000..0afe992515 Binary files /dev/null and b/docs/images/coroutine-idea-debugging-1.png differ diff --git a/docs/knit.properties b/docs/knit.properties index ab2508a114..2028ecb416 100644 --- a/docs/knit.properties +++ b/docs/knit.properties @@ -4,19 +4,7 @@ knit.package=kotlinx.coroutines.guide knit.dir=../kotlinx-coroutines-core/jvm/test/guide/ -knit.pattern=example-[a-zA-Z0-9-]+-##\\.kt -knit.include=knit.code.include test.package=kotlinx.coroutines.guide.test test.dir=../kotlinx-coroutines-core/jvm/test/guide/test/ -test.template=knit.test.template -# Various test validation modes and their corresponding methods from TestUtil -test.mode.=verifyLines -test.mode.STARTS_WITH=verifyLinesStartWith -test.mode.ARBITRARY_TIME=verifyLinesArbitraryTime -test.mode.FLEXIBLE_TIME=verifyLinesFlexibleTime -test.mode.FLEXIBLE_THREAD=verifyLinesFlexibleThread -test.mode.LINES_START_UNORDERED=verifyLinesStartUnordered -test.mode.LINES_START=verifyLinesStart -test.mode.EXCEPTION=verifyExceptions \ No newline at end of file diff --git a/docs/knit.test.template b/docs/knit.test.template index a912555a43..727493c662 100644 --- a/docs/knit.test.template +++ b/docs/knit.test.template @@ -5,6 +5,7 @@ // This file was automatically generated from ${file.name} by Knit tool. Do not edit. package ${test.package} +import kotlinx.coroutines.knit.* import org.junit.Test class ${test.name} { diff --git a/docs/select-expression.md b/docs/select-expression.md index 5809e7b93e..f0e5ae4681 100644 --- a/docs/select-expression.md +++ b/docs/select-expression.md @@ -125,7 +125,7 @@ fun main() = runBlocking {
-> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-select-01.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-select-01.kt). The result of this code is: @@ -220,7 +220,7 @@ fun main() = runBlocking {
-> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-select-02.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-select-02.kt). The result of this code is quite interesting, so we'll analyze it in mode detail: @@ -310,7 +310,7 @@ fun main() = runBlocking {
-> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-select-03.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-select-03.kt). So let us see what happens: @@ -403,7 +403,7 @@ fun main() = runBlocking {
-> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-select-04.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-select-04.kt). The output is: @@ -522,7 +522,7 @@ fun main() = runBlocking {
-> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-select-05.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-select-05.kt). The result of this code: diff --git a/docs/shared-mutable-state-and-concurrency.md b/docs/shared-mutable-state-and-concurrency.md index 1a3c406472..8b83ad0b20 100644 --- a/docs/shared-mutable-state-and-concurrency.md +++ b/docs/shared-mutable-state-and-concurrency.md @@ -24,7 +24,7 @@ but others are unique. ### The problem -Let us launch a hundred coroutines all doing the same action thousand times. +Let us launch a hundred coroutines all doing the same action a thousand times. We'll also measure their completion time for further comparisons:
@@ -90,7 +90,7 @@ fun main() = runBlocking {
-> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-sync-01.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-sync-01.kt). @@ -144,7 +144,7 @@ fun main() = runBlocking {
-> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-sync-02.kt). +> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-sync-02.kt). concurrent ---> common + ^ + ios \ | + macos | ---> nativeDarwin ---> native --+ + tvos | ^ + watchos / | + | + linux \ ---> nativeOther -------+ + mingw / + + ========================================================================== */ + +project.ext.sourceSetSuffixes = ["Main", "Test"] + +void defineSourceSet(newName, dependsOn, includedInPred) { + for (suffix in project.ext.sourceSetSuffixes) { + def newSS = kotlin.sourceSets.maybeCreate(newName + suffix) + for (dep in dependsOn) { + newSS.dependsOn(kotlin.sourceSets[dep + suffix]) + } + for (curSS in kotlin.sourceSets) { + def curName = curSS.name + if (curName.endsWith(suffix)) { + def prefix = curName.substring(0, curName.length() - suffix.length()) + if (includedInPred(prefix)) curSS.dependsOn(newSS) + } + } + } +} + +static boolean isNativeDarwin(String name) { return ["ios", "macos", "tvos", "watchos"].any { name.startsWith(it) } } +static boolean isNativeOther(String name) { return ["linux", "mingw"].any { name.startsWith(it) } } + +defineSourceSet("concurrent", ["common"]) { it in ["jvm", "native"] } +defineSourceSet("nativeDarwin", ["native"]) { isNativeDarwin(it) } +defineSourceSet("nativeOther", ["native"]) { isNativeOther(it) } + +/* ========================================================================== */ + /* * All platform plugins and configuration magic happens here instead of build.gradle * because JMV-only projects depend on core, thus core should always be initialized before configuration. @@ -18,7 +65,7 @@ kotlin { configure(sourceSets) { def srcDir = name.endsWith('Main') ? 'src' : 'test' def platform = name[0..-5] - kotlin.srcDir "$platform/$srcDir" + kotlin.srcDirs = ["$platform/$srcDir"] if (name == "jvmMain") { resources.srcDirs = ["$platform/resources"] } else if (name == "jvmTest") { @@ -31,12 +78,18 @@ kotlin { } configure(targets) { - def targetName = it.name - compilations.all { compilation -> - def compileTask = tasks.getByName(compilation.compileKotlinTaskName) - // binary compatibility support - if (targetName.contains("jvm") && compilation.compilationName == "main") { - compileTask.kotlinOptions.freeCompilerArgs += ["-Xdump-declarations-to=${buildDir}/visibilities.json"] + // Configure additional binaries and test runs -- one for each OS + if (["macos", "linux", "mingw"].any { name.startsWith(it) }) { + binaries { + // Test for memory leaks using a special entry point that does not exit but returns from main + binaries.getTest("DEBUG").freeCompilerArgs += ["-e", "kotlinx.coroutines.mainNoExit"] + // Configure a separate test where code runs in background + test("background", [org.jetbrains.kotlin.gradle.plugin.mpp.NativeBuildType.DEBUG]) { + freeCompilerArgs += ["-e", "kotlinx.coroutines.mainBackground"] + } + } + testRuns { + background { setExecutionSourceFrom(binaries.backgroundDebugTest) } } } } @@ -54,12 +107,52 @@ compileKotlinMetadata { } } +// :KLUDGE: Idea.active: This is needed to workaround resolve problems after importing this project to IDEA +def configureNativeSourceSetPreset(name, preset) { + def hostMainCompilation = project.kotlin.targetFromPreset(preset).compilations.main + // Look for platform libraries in "implementation" for default source set + def implementationConfiguration = configurations[hostMainCompilation.defaultSourceSet.implementationMetadataConfigurationName] + // Now find the libraries: Finds platform libs & stdlib, but platform declarations are still not resolved due to IDE bugs + def hostNativePlatformLibs = files( + provider { + implementationConfiguration.findAll { + it.path.endsWith(".klib") || it.absolutePath.contains("klib${File.separator}platform") || it.absolutePath.contains("stdlib") + } + } + ) + // Add all those dependencies + for (suffix in sourceSetSuffixes) { + configure(kotlin.sourceSets[name + suffix]) { + dependencies.add(implementationMetadataConfigurationName, hostNativePlatformLibs) + } + } +} + +// :KLUDGE: Idea.active: Configure platform libraries for native source sets when working in IDEA +if (Idea.active) { + def manager = project.ext.hostManager + def linuxPreset = kotlin.presets.linuxX64 + def macosPreset = kotlin.presets.macosX64 + // linux should be always available (cross-compilation capable) -- use it as default + assert manager.isEnabled(linuxPreset.konanTarget) + // use macOS libs for nativeDarwin if available + def macosAvailable = manager.isEnabled(macosPreset.konanTarget) + // configure source sets + configureNativeSourceSetPreset("native", linuxPreset) + configureNativeSourceSetPreset("nativeOther", linuxPreset) + configureNativeSourceSetPreset("nativeDarwin", macosAvailable ? macosPreset : linuxPreset) +} + kotlin.sourceSets { + jvmMain.dependencies { + compileOnly "com.google.android:annotations:4.1.1.4" + } + jvmTest.dependencies { api "org.jetbrains.kotlinx:lincheck:$lincheck_version" api "org.jetbrains.kotlinx:kotlinx-knit-test:$knit_version" api "com.esotericsoftware:kryo:4.0.0" - implementation project (":android-unit-tests") + implementation project(":android-unit-tests") } } @@ -86,12 +179,19 @@ jvmTest { enableAssertions = true systemProperty 'java.security.manager', 'kotlinx.coroutines.TestSecurityManager' // 'stress' is required to be able to run all subpackage tests like ":jvmTests --tests "*channels*" -Pstress=true" - if (!project.ext.ideaActive && rootProject.properties['stress'] == null) { + if (!Idea.active && rootProject.properties['stress'] == null) { exclude '**/*StressTest.*' } systemProperty 'kotlinx.coroutines.scheduler.keep.alive.sec', '100000' // any unpark problem hangs test } +jvmJar { + manifest { + attributes "Premain-Class": "kotlinx.coroutines.debug.AgentPremain" + attributes "Can-Retransform-Classes": "true" + } +} + task jvmStressTest(type: Test, dependsOn: compileTestKotlinJvm) { classpath = files { jvmTest.classpath } testClassesDirs = files { jvmTest.testClassesDirs } @@ -101,6 +201,8 @@ task jvmStressTest(type: Test, dependsOn: compileTestKotlinJvm) { enableAssertions = true testLogging.showStandardStreams = true systemProperty 'kotlinx.coroutines.scheduler.keep.alive.sec', '100000' // any unpark problem hangs test + systemProperty 'kotlinx.coroutines.semaphore.segmentSize', '2' + systemProperty 'kotlinx.coroutines.semaphore.maxSpinCycles', '10' } task jdk16Test(type: Test, dependsOn: [compileTestKotlinJvm, checkJdk16]) { @@ -111,6 +213,7 @@ task jdk16Test(type: Test, dependsOn: [compileTestKotlinJvm, checkJdk16]) { exclude '**/*LCStressTest.*' // lin-check tests use LinChecker which needs JDK8 exclude '**/exceptions/**' // exceptions tests check suppressed exception which needs JDK8 exclude '**/ExceptionsGuideTest.*' + exclude '**/RunInterruptibleStressTest.*' // fails on JDK 1.6 due to JDK bug } // Run these tests only during nightly stress test diff --git a/kotlinx-coroutines-core/common/src/AbstractCoroutine.kt b/kotlinx-coroutines-core/common/src/AbstractCoroutine.kt index 22111f0ce2..742c9670b7 100644 --- a/kotlinx-coroutines-core/common/src/AbstractCoroutine.kt +++ b/kotlinx-coroutines-core/common/src/AbstractCoroutine.kt @@ -113,7 +113,7 @@ public abstract class AbstractCoroutine( afterResume(state) } - protected open fun afterResume(state: Any?) = afterCompletion(state) + protected open fun afterResume(state: Any?): Unit = afterCompletion(state) internal final override fun handleOnCompletionException(exception: Throwable) { handleCoroutineException(context, exception) diff --git a/kotlinx-coroutines-core/common/src/Await.kt b/kotlinx-coroutines-core/common/src/Await.kt index dd1e1771f2..7189349024 100644 --- a/kotlinx-coroutines-core/common/src/Await.kt +++ b/kotlinx-coroutines-core/common/src/Await.kt @@ -5,6 +5,7 @@ package kotlinx.coroutines import kotlinx.atomicfu.* +import kotlinx.coroutines.channels.* import kotlin.coroutines.* /** @@ -18,6 +19,8 @@ import kotlin.coroutines.* * This suspending function is cancellable. * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, * this function immediately resumes with [CancellationException]. + * There is a **prompt cancellation guarantee**. If the job was cancelled while this function was + * suspended, it will not resume successfully. See [suspendCancellableCoroutine] documentation for low-level details. */ public suspend fun awaitAll(vararg deferreds: Deferred): List = if (deferreds.isEmpty()) emptyList() else AwaitAll(deferreds).await() @@ -33,6 +36,8 @@ public suspend fun awaitAll(vararg deferreds: Deferred): List = * This suspending function is cancellable. * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, * this function immediately resumes with [CancellationException]. + * There is a **prompt cancellation guarantee**. If the job was cancelled while this function was + * suspended, it will not resume successfully. See [suspendCancellableCoroutine] documentation for low-level details. */ public suspend fun Collection>.awaitAll(): List = if (isEmpty()) emptyList() else AwaitAll(toTypedArray()).await() @@ -41,8 +46,11 @@ public suspend fun Collection>.awaitAll(): List = * Suspends current coroutine until all given jobs are complete. * This method is semantically equivalent to joining all given jobs one by one with `jobs.forEach { it.join() }`. * - * This suspending function is cancellable. If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, + * This suspending function is cancellable. + * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, * this function immediately resumes with [CancellationException]. + * There is a **prompt cancellation guarantee**. If the job was cancelled while this function was + * suspended, it will not resume successfully. See [suspendCancellableCoroutine] documentation for low-level details. */ public suspend fun joinAll(vararg jobs: Job): Unit = jobs.forEach { it.join() } @@ -50,8 +58,11 @@ public suspend fun joinAll(vararg jobs: Job): Unit = jobs.forEach { it.join() } * Suspends current coroutine until all given jobs are complete. * This method is semantically equivalent to joining all given jobs one by one with `forEach { it.join() }`. * - * This suspending function is cancellable. If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, + * This suspending function is cancellable. + * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, * this function immediately resumes with [CancellationException]. + * There is a **prompt cancellation guarantee**. If the job was cancelled while this function was + * suspended, it will not resume successfully. See [suspendCancellableCoroutine] documentation for low-level details. */ public suspend fun Collection.joinAll(): Unit = forEach { it.join() } diff --git a/kotlinx-coroutines-core/common/src/Builders.common.kt b/kotlinx-coroutines-core/common/src/Builders.common.kt index 7dd1b174ee..b7deaccb72 100644 --- a/kotlinx-coroutines-core/common/src/Builders.common.kt +++ b/kotlinx-coroutines-core/common/src/Builders.common.kt @@ -4,6 +4,7 @@ @file:JvmMultifileClass @file:JvmName("BuildersKt") +@file:OptIn(ExperimentalContracts::class) package kotlinx.coroutines @@ -11,6 +12,7 @@ import kotlinx.atomicfu.* import kotlinx.coroutines.internal.* import kotlinx.coroutines.intrinsics.* import kotlinx.coroutines.selects.* +import kotlin.contracts.* import kotlin.coroutines.* import kotlin.coroutines.intrinsics.* import kotlin.jvm.* @@ -127,38 +129,44 @@ private class LazyDeferredCoroutine( * This function uses dispatcher from the new context, shifting execution of the [block] into the * different thread if a new dispatcher is specified, and back to the original dispatcher * when it completes. Note that the result of `withContext` invocation is - * dispatched into the original context in a cancellable way, which means that if the original [coroutineContext], - * in which `withContext` was invoked, is cancelled by the time its dispatcher starts to execute the code, + * dispatched into the original context in a cancellable way with a **prompt cancellation guarantee**, + * which means that if the original [coroutineContext], in which `withContext` was invoked, + * is cancelled by the time its dispatcher starts to execute the code, * it discards the result of `withContext` and throws [CancellationException]. */ public suspend fun withContext( context: CoroutineContext, block: suspend CoroutineScope.() -> T -): T = suspendCoroutineUninterceptedOrReturn sc@ { uCont -> - // compute new context - val oldContext = uCont.context - val newContext = oldContext + context - // always check for cancellation of new context - newContext.checkCompletion() - // FAST PATH #1 -- new context is the same as the old one - if (newContext === oldContext) { - val coroutine = ScopeCoroutine(newContext, uCont) - return@sc coroutine.startUndispatchedOrReturn(coroutine, block) +): T { + contract { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - // FAST PATH #2 -- the new dispatcher is the same as the old one (something else changed) - // `equals` is used by design (see equals implementation is wrapper context like ExecutorCoroutineDispatcher) - if (newContext[ContinuationInterceptor] == oldContext[ContinuationInterceptor]) { - val coroutine = UndispatchedCoroutine(newContext, uCont) - // There are changes in the context, so this thread needs to be updated - withCoroutineContext(newContext, null) { + return suspendCoroutineUninterceptedOrReturn sc@ { uCont -> + // compute new context + val oldContext = uCont.context + val newContext = oldContext + context + // always check for cancellation of new context + newContext.checkCompletion() + // FAST PATH #1 -- new context is the same as the old one + if (newContext === oldContext) { + val coroutine = ScopeCoroutine(newContext, uCont) return@sc coroutine.startUndispatchedOrReturn(coroutine, block) } + // FAST PATH #2 -- the new dispatcher is the same as the old one (something else changed) + // `equals` is used by design (see equals implementation is wrapper context like ExecutorCoroutineDispatcher) + if (newContext[ContinuationInterceptor] == oldContext[ContinuationInterceptor]) { + val coroutine = UndispatchedCoroutine(newContext, uCont) + // There are changes in the context, so this thread needs to be updated + withCoroutineContext(newContext, null) { + return@sc coroutine.startUndispatchedOrReturn(coroutine, block) + } + } + // SLOW PATH -- use new dispatcher + val coroutine = DispatchedCoroutine(newContext, uCont) + coroutine.initParentJob() + block.startCoroutineCancellable(coroutine, coroutine) + coroutine.getResult() } - // SLOW PATH -- use new dispatcher - val coroutine = DispatchedCoroutine(newContext, uCont) - coroutine.initParentJob() - block.startCoroutineCancellable(coroutine, coroutine) - coroutine.getResult() } /** @@ -167,7 +175,6 @@ public suspend fun withContext( * * This inline function calls [withContext]. */ -@ExperimentalCoroutinesApi public suspend inline operator fun CoroutineDispatcher.invoke( noinline block: suspend CoroutineScope.() -> T ): T = withContext(this, block) diff --git a/kotlinx-coroutines-core/common/src/CancellableContinuation.kt b/kotlinx-coroutines-core/common/src/CancellableContinuation.kt index fd5cd083e2..7d9315afbf 100644 --- a/kotlinx-coroutines-core/common/src/CancellableContinuation.kt +++ b/kotlinx-coroutines-core/common/src/CancellableContinuation.kt @@ -15,6 +15,8 @@ import kotlin.coroutines.intrinsics.* * When the [cancel] function is explicitly invoked, this continuation immediately resumes with a [CancellationException] or * the specified cancel cause. * + * An instance of `CancellableContinuation` is created by the [suspendCancellableCoroutine] function. + * * Cancellable continuation has three states (as subset of [Job] states): * * | **State** | [isActive] | [isCompleted] | [isCancelled] | @@ -24,14 +26,12 @@ import kotlin.coroutines.intrinsics.* * | _Canceled_ (final _completed_ state)| `false` | `true` | `true` | * * Invocation of [cancel] transitions this continuation from _active_ to _cancelled_ state, while - * invocation of [resume] or [resumeWithException] transitions it from _active_ to _resumed_ state. + * invocation of [Continuation.resume] or [Continuation.resumeWithException] transitions it from _active_ to _resumed_ state. * * A [cancelled][isCancelled] continuation implies that it is [completed][isCompleted]. * - * Invocation of [resume] or [resumeWithException] in _resumed_ state produces an [IllegalStateException]. - * Invocation of [resume] in _cancelled_ state is ignored (it is a trivial race between resume from the continuation owner and - * outer job's cancellation, and the cancellation wins). - * Invocation of [resumeWithException] in _cancelled_ state triggers exception handling of the passed exception. + * Invocation of [Continuation.resume] or [Continuation.resumeWithException] in _resumed_ state produces an [IllegalStateException], + * but is ignored in _cancelled_ state. * * ``` * +-----------+ resume +---------+ @@ -43,7 +43,6 @@ import kotlin.coroutines.intrinsics.* * +-----------+ * | Cancelled | * +-----------+ - * * ``` */ public interface CancellableContinuation : Continuation { @@ -78,6 +77,14 @@ public interface CancellableContinuation : Continuation { @InternalCoroutinesApi public fun tryResume(value: T, idempotent: Any? = null): Any? + /** + * Same as [tryResume] but with [onCancellation] handler that called if and only if the value is not + * delivered to the caller because of the dispatch in the process, so that atomicity delivery + * guaranteed can be provided by having a cancellation fallback. + */ + @InternalCoroutinesApi + public fun tryResume(value: T, idempotent: Any?, onCancellation: ((cause: Throwable) -> Unit)?): Any? + /** * Tries to resume this continuation with the specified [exception] and returns a non-null object token if successful, * or `null` otherwise (it was already resumed or cancelled). When a non-null object is returned, @@ -100,14 +107,8 @@ public interface CancellableContinuation : Continuation { * Legacy function that turned on cancellation behavior in [suspendCancellableCoroutine] before kotlinx.coroutines 1.1.0. * This function does nothing and is left only for binary compatibility with old compiled code. * - * @suppress **Deprecated**: This function is no longer used. - * It is left for binary compatibility with code compiled before kotlinx.coroutines 1.1.0. + * @suppress **This is unstable API and it is subject to change.** */ - @Deprecated( - level = DeprecationLevel.HIDDEN, - message = "This function is no longer used. " + - "It is left for binary compatibility with code compiled before kotlinx.coroutines 1.1.0. " - ) @InternalCoroutinesApi public fun initCancellability() @@ -118,8 +119,8 @@ public interface CancellableContinuation : Continuation { public fun cancel(cause: Throwable? = null): Boolean /** - * Registers a [handler] to be **synchronously** invoked on cancellation (regular or exceptional) of this continuation. - * When the continuation is already cancelled, the handler will be immediately invoked + * Registers a [handler] to be **synchronously** invoked on [cancellation][cancel] (regular or exceptional) of this continuation. + * When the continuation is already cancelled, the handler is immediately invoked * with the cancellation exception. Otherwise, the handler will be invoked as soon as this * continuation is cancelled. * @@ -128,7 +129,15 @@ public interface CancellableContinuation : Continuation { * processed as an uncaught exception in the context of the current coroutine * (see [CoroutineExceptionHandler]). * - * At most one [handler] can be installed on a continuation. + * At most one [handler] can be installed on a continuation. Attempt to call `invokeOnCancellation` second + * time produces [IllegalStateException]. + * + * This handler is also called when this continuation [resumes][Continuation.resume] normally (with a value) and then + * is cancelled while waiting to be dispatched. More generally speaking, this handler is called whenever + * the caller of [suspendCancellableCoroutine] is getting a [CancellationException]. + * + * A typical example for `invokeOnCancellation` usage is given in + * the documentation for the [suspendCancellableCoroutine] function. * * **Note**: Implementation of `CompletionHandler` must be fast, non-blocking, and thread-safe. * This `handler` can be invoked concurrently with the surrounding code. @@ -171,7 +180,7 @@ public interface CancellableContinuation : Continuation { * (see [CoroutineExceptionHandler]). * * This function shall be used when resuming with a resource that must be closed by - * code that called the corresponding suspending function, e.g.: + * code that called the corresponding suspending function, for example: * * ``` * continuation.resume(resource) { @@ -179,70 +188,156 @@ public interface CancellableContinuation : Continuation { * } * ``` * + * A more complete example and further details are given in + * the documentation for the [suspendCancellableCoroutine] function. + * * **Note**: The [onCancellation] handler must be fast, non-blocking, and thread-safe. * It can be invoked concurrently with the surrounding code. * There is no guarantee on the execution context of its invocation. */ - @ExperimentalCoroutinesApi // since 1.2.0, tentatively graduates in 1.3.0 - public fun resume(value: T, onCancellation: (cause: Throwable) -> Unit) + @ExperimentalCoroutinesApi // since 1.2.0 + public fun resume(value: T, onCancellation: ((cause: Throwable) -> Unit)?) } /** * Suspends the coroutine like [suspendCoroutine], but providing a [CancellableContinuation] to - * the [block]. This function throws a [CancellationException] if the coroutine is cancelled or completed while suspended. - */ -public suspend inline fun suspendCancellableCoroutine( - crossinline block: (CancellableContinuation) -> Unit -): T = - suspendCoroutineUninterceptedOrReturn { uCont -> - val cancellable = CancellableContinuationImpl(uCont.intercepted(), resumeMode = MODE_CANCELLABLE) - // NOTE: Before version 1.1.0 the following invocation was inlined here, so invocation of this - // method indicates that the code was compiled by kotlinx.coroutines < 1.1.0 - // cancellable.initCancellability() - block(cancellable) - cancellable.getResult() - } - -/** - * Suspends the coroutine like [suspendCancellableCoroutine], but with *atomic cancellation*. + * the [block]. This function throws a [CancellationException] if the [Job] of the coroutine is + * cancelled or completed while it is suspended. * - * When the suspended function throws a [CancellationException], it means that the continuation was not resumed. - * As a side-effect of atomic cancellation, a thread-bound coroutine (to some UI thread, for example) may - * continue to execute even after it was cancelled from the same thread in the case when the continuation - * was already resumed and was posted for execution to the thread's queue. + * A typical use of this function is to suspend a coroutine while waiting for a result + * from a single-shot callback API and to return the result to the caller. + * For multi-shot callback APIs see [callbackFlow][kotlinx.coroutines.flow.callbackFlow]. * - * @suppress **This an internal API and should not be used from general code.** + * ``` + * suspend fun awaitCallback(): T = suspendCancellableCoroutine { continuation -> + * val callback = object : Callback { // Implementation of some callback interface + * override fun onCompleted(value: T) { + * // Resume coroutine with a value provided by the callback + * continuation.resume(value) + * } + * override fun onApiError(cause: Throwable) { + * // Resume coroutine with an exception provided by the callback + * continuation.resumeWithException(cause) + * } + * } + * // Register callback with an API + * api.register(callback) + * // Remove callback on cancellation + * continuation.invokeOnCancellation { api.unregister(callback) } + * // At this point the coroutine is suspended by suspendCancellableCoroutine until callback fires + * } + * ``` + * + * > The callback `register`/`unregister` methods provided by an external API must be thread-safe, because + * > `invokeOnCancellation` block can be called at any time due to asynchronous nature of cancellation, even + * > concurrently with the call of the callback. + * + * ### Prompt cancellation guarantee + * + * This function provides **prompt cancellation guarantee**. + * If the [Job] of the current coroutine was cancelled while this function was suspended it will not resume + * successfully. + * + * The cancellation of the coroutine's job is generally asynchronous with respect to the suspended coroutine. + * The suspended coroutine is resumed with the call it to its [Continuation.resumeWith] member function or to + * [resume][Continuation.resume] extension function. + * However, when coroutine is resumed, it does not immediately start executing, but is passed to its + * [CoroutineDispatcher] to schedule its execution when dispatcher's resources become available for execution. + * The job's cancellation can happen both before, after, and concurrently with the call to `resume`. In any + * case, prompt cancellation guarantees that the the coroutine will not resume its code successfully. + * + * If the coroutine was resumed with an exception (for example, using [Continuation.resumeWithException] extension + * function) and cancelled, then the resulting exception of the `suspendCancellableCoroutine` function is determined + * by whichever action (exceptional resume or cancellation) that happened first. + * + * ### Returning resources from a suspended coroutine + * + * As a result of a prompt cancellation guarantee, when a closeable resource + * (like open file or a handle to another native resource) is returned from a suspended coroutine as a value + * it can be lost when the coroutine is cancelled. In order to ensure that the resource can be properly closed + * in this case, the [CancellableContinuation] interface provides two functions. + * + * * [invokeOnCancellation][CancellableContinuation.invokeOnCancellation] installs a handler that is called + * whenever a suspend coroutine is being cancelled. In addition to the example at the beginning, it can be + * used to ensure that a resource that was opened before the call to + * `suspendCancellableCoroutine` or in its body is closed in case of cancellation. + * + * ``` + * suspendCancellableCoroutine { continuation -> + * val resource = openResource() // Opens some resource + * continuation.invokeOnCancellation { + * resource.close() // Ensures the resource is closed on cancellation + * } + * // ... + * } + * ``` + * + * * [resume(value) { ... }][CancellableContinuation.resume] method on a [CancellableContinuation] takes + * an optional `onCancellation` block. It can be used when resuming with a resource that must be closed by + * the code that called the corresponding suspending function. + * + * ``` + * suspendCancellableCoroutine { continuation -> + * val callback = object : Callback { // Implementation of some callback interface + * // A callback provides a reference to some closeable resource + * override fun onCompleted(resource: T) { + * // Resume coroutine with a value provided by the callback and ensure the resource is closed in case + * // when the coroutine is cancelled before the caller gets a reference to the resource. + * continuation.resume(resource) { + * resource.close() // Close the resource on cancellation + * } + * } + * // ... + * } + * ``` + * + * ### Implementation details and custom continuation interceptors + * + * The prompt cancellation guarantee is the result of a coordinated implementation inside `suspendCancellableCoroutine` + * function and the [CoroutineDispatcher] class. The coroutine dispatcher checks for the status of the [Job] immediately + * before continuing its normal execution and aborts this normal execution, calling all the corresponding + * cancellation handlers, if the job was cancelled. + * + * If a custom implementation of [ContinuationInterceptor] is used in a coroutine's context that does not extend + * [CoroutineDispatcher] class, then there is no prompt cancellation guarantee. A custom continuation interceptor + * can resume execution of a previously suspended coroutine even if its job was already cancelled. */ -@InternalCoroutinesApi -public suspend inline fun suspendAtomicCancellableCoroutine( +public suspend inline fun suspendCancellableCoroutine( crossinline block: (CancellableContinuation) -> Unit ): T = suspendCoroutineUninterceptedOrReturn { uCont -> - val cancellable = CancellableContinuationImpl(uCont.intercepted(), resumeMode = MODE_ATOMIC_DEFAULT) + val cancellable = CancellableContinuationImpl(uCont.intercepted(), resumeMode = MODE_CANCELLABLE) + /* + * For non-atomic cancellation we setup parent-child relationship immediately + * in case when `block` blocks the current thread (e.g. Rx2 with trampoline scheduler), but + * properly supports cancellation. + */ + cancellable.initCancellability() block(cancellable) cancellable.getResult() } /** - * Suspends coroutine similar to [suspendAtomicCancellableCoroutine], but an instance of [CancellableContinuationImpl] is reused if possible. + * Suspends the coroutine similar to [suspendCancellableCoroutine], but an instance of + * [CancellableContinuationImpl] is reused. */ -internal suspend inline fun suspendAtomicCancellableCoroutineReusable( +internal suspend inline fun suspendCancellableCoroutineReusable( crossinline block: (CancellableContinuation) -> Unit ): T = suspendCoroutineUninterceptedOrReturn { uCont -> - val cancellable = getOrCreateCancellableContinuation(uCont.intercepted()) - block(cancellable) - cancellable.getResult() - } + val cancellable = getOrCreateCancellableContinuation(uCont.intercepted()) + block(cancellable) + cancellable.getResult() +} internal fun getOrCreateCancellableContinuation(delegate: Continuation): CancellableContinuationImpl { // If used outside of our dispatcher if (delegate !is DispatchedContinuation) { - return CancellableContinuationImpl(delegate, resumeMode = MODE_ATOMIC_DEFAULT) + return CancellableContinuationImpl(delegate, MODE_CANCELLABLE_REUSABLE) } /* * Attempt to claim reusable instance. * - * suspendAtomicCancellableCoroutineReusable { // <- claimed + * suspendCancellableCoroutineReusable { // <- claimed * // Any asynchronous cancellation is "postponed" while this block * // is being executed * } // postponed cancellation is checked here. @@ -253,26 +348,13 @@ internal fun getOrCreateCancellableContinuation(delegate: Continuation): * thus leaking CC instance for indefinite time. * 2) Continuation was cancelled. Then we should prevent any further reuse and bail out. */ - return delegate.claimReusableCancellableContinuation()?.takeIf { it.resetState() } - ?: return CancellableContinuationImpl(delegate, MODE_ATOMIC_DEFAULT) + return delegate.claimReusableCancellableContinuation()?.takeIf { it.resetStateReusable() } + ?: return CancellableContinuationImpl(delegate, MODE_CANCELLABLE_REUSABLE) } /** - * @suppress **Deprecated** - */ -@Deprecated( - message = "holdCancellability parameter is deprecated and is no longer used", - replaceWith = ReplaceWith("suspendAtomicCancellableCoroutine(block)") -) -@InternalCoroutinesApi -public suspend inline fun suspendAtomicCancellableCoroutine( - holdCancellability: Boolean = false, - crossinline block: (CancellableContinuation) -> Unit -): T = - suspendAtomicCancellableCoroutine(block) - -/** - * Removes the specified [node] on cancellation. + * Removes the specified [node] on cancellation. This function assumes that this node is already + * removed on successful resume and does not try to remove it if the continuation is cancelled during dispatch. */ internal fun CancellableContinuation<*>.removeOnCancellation(node: LockFreeLinkedListNode) = invokeOnCancellation(handler = RemoveOnCancel(node).asHandler) @@ -288,12 +370,12 @@ internal fun CancellableContinuation<*>.removeOnCancellation(node: LockFreeLinke * @suppress **This an internal API and should not be used from general code.** */ @InternalCoroutinesApi -public fun CancellableContinuation<*>.disposeOnCancellation(handle: DisposableHandle) = +public fun CancellableContinuation<*>.disposeOnCancellation(handle: DisposableHandle): Unit = invokeOnCancellation(handler = DisposeOnCancel(handle).asHandler) // --------------- implementation details --------------- -private class RemoveOnCancel(private val node: LockFreeLinkedListNode) : CancelHandler() { +private class RemoveOnCancel(private val node: LockFreeLinkedListNode) : BeforeResumeCancelHandler() { override fun invoke(cause: Throwable?) { node.remove() } override fun toString() = "RemoveOnCancel[$node]" } diff --git a/kotlinx-coroutines-core/common/src/CancellableContinuationImpl.kt b/kotlinx-coroutines-core/common/src/CancellableContinuationImpl.kt index 1f67dd3c6c..cdb1b78882 100644 --- a/kotlinx-coroutines-core/common/src/CancellableContinuationImpl.kt +++ b/kotlinx-coroutines-core/common/src/CancellableContinuationImpl.kt @@ -27,13 +27,17 @@ internal open class CancellableContinuationImpl( final override val delegate: Continuation, resumeMode: Int ) : DispatchedTask(resumeMode), CancellableContinuation, CoroutineStackFrame { + init { + assert { resumeMode != MODE_UNINITIALIZED } // invalid mode for CancellableContinuationImpl + } + public override val context: CoroutineContext = delegate.context /* * Implementation notes * - * AbstractContinuation is a subset of Job with following limitations: - * 1) It can have only cancellation listeners + * CancellableContinuationImpl is a subset of Job with following limitations: + * 1) It can have only cancellation listener (no "on cancelling") * 2) It always invokes cancellation listener if it's cancelled (no 'invokeImmediately') * 3) It can have at most one cancellation listener * 4) Its cancellation listeners cannot be deregistered @@ -82,21 +86,23 @@ internal open class CancellableContinuationImpl( public override val isCancelled: Boolean get() = state is CancelledContinuation public override fun initCancellability() { - // This method does nothing. Leftover for binary compatibility with old compiled code + setupCancellation() } private fun isReusable(): Boolean = delegate is DispatchedContinuation<*> && delegate.isReusable(this) /** - * Resets cancellability state in order to [suspendAtomicCancellableCoroutineReusable] to work. - * Invariant: used only by [suspendAtomicCancellableCoroutineReusable] in [REUSABLE_CLAIMED] state. + * Resets cancellability state in order to [suspendCancellableCoroutineReusable] to work. + * Invariant: used only by [suspendCancellableCoroutineReusable] in [REUSABLE_CLAIMED] state. */ - @JvmName("resetState") // Prettier stack traces - internal fun resetState(): Boolean { + @JvmName("resetStateReusable") // Prettier stack traces + internal fun resetStateReusable(): Boolean { + assert { resumeMode == MODE_CANCELLABLE_REUSABLE } // invalid mode for CancellableContinuationImpl assert { parentHandle !== NonDisposableHandle } val state = _state.value assert { state !is NotCompleted } - if (state is CompletedIdempotentResult) { + if (state is CompletedContinuation && state.idempotentResume != null) { + // Cannot reuse continuation that was resumed with idempotent marker detachChild() return false } @@ -107,13 +113,13 @@ internal open class CancellableContinuationImpl( /** * Setups parent cancellation and checks for postponed cancellation in the case of reusable continuations. - * It is only invoked from an internal [getResult] function. + * It is only invoked from an internal [getResult] function for reusable continuations + * and from [suspendCancellableCoroutine] to establish a cancellation before registering CC anywhere. */ private fun setupCancellation() { if (checkCompleted()) return if (parentHandle !== null) return // fast path 2 -- was already initialized val parent = delegate.context[Job] ?: return // fast path 3 -- don't do anything without parent - parent.start() // make sure the parent is started val handle = parent.invokeOnCompletion( onCancelling = true, handler = ChildContinuation(parent, this).asHandler @@ -129,7 +135,7 @@ internal open class CancellableContinuationImpl( private fun checkCompleted(): Boolean { val completed = isCompleted - if (resumeMode != MODE_ATOMIC_DEFAULT) return completed // Do not check postponed cancellation for non-reusable continuations + if (!resumeMode.isReusableMode) return completed // Do not check postponed cancellation for non-reusable continuations val dispatched = delegate as? DispatchedContinuation<*> ?: return completed val cause = dispatched.checkPostponedCancellation(this) ?: return completed if (!completed) { @@ -146,10 +152,26 @@ internal open class CancellableContinuationImpl( override fun takeState(): Any? = state - override fun cancelResult(state: Any?, cause: Throwable) { - if (state is CompletedWithCancellation) { - invokeHandlerSafely { - state.onCancellation(cause) + // Note: takeState does not clear the state so we don't use takenState + // and we use the actual current state where in CAS-loop + override fun cancelCompletedResult(takenState: Any?, cause: Throwable): Unit = _state.loop { state -> + when (state) { + is NotCompleted -> error("Not completed") + is CompletedExceptionally -> return // already completed exception or cancelled, nothing to do + is CompletedContinuation -> { + check(!state.cancelled) { "Must be called at most once" } + val update = state.copy(cancelCause = cause) + if (_state.compareAndSet(state, update)) { + state.invokeHandlers(this, cause) + return // done + } + } + else -> { + // completed normally without marker class, promote to CompletedContinuation in case + // if invokeOnCancellation if called later + if (_state.compareAndSet(state, CompletedContinuation(state, cancelCause = cause))) { + return // done + } } } } @@ -158,7 +180,7 @@ internal open class CancellableContinuationImpl( * Attempt to postpone cancellation for reusable cancellable continuation */ private fun cancelLater(cause: Throwable): Boolean { - if (resumeMode != MODE_ATOMIC_DEFAULT) return false + if (!resumeMode.isReusableMode) return false val dispatched = (delegate as? DispatchedContinuation<*>) ?: return false return dispatched.postponeCancellation(cause) } @@ -170,10 +192,10 @@ internal open class CancellableContinuationImpl( val update = CancelledContinuation(this, cause, handled = state is CancelHandler) if (!_state.compareAndSet(state, update)) return@loop // retry on cas failure // Invoke cancel handler if it was present - if (state is CancelHandler) invokeHandlerSafely { state.invoke(cause) } + (state as? CancelHandler)?.let { callCancelHandler(it, cause) } // Complete state update detachChildIfNonResuable() - dispatchResume(mode = MODE_ATOMIC_DEFAULT) + dispatchResume(resumeMode) // no need for additional cancellation checks return true } } @@ -185,14 +207,36 @@ internal open class CancellableContinuationImpl( detachChildIfNonResuable() } - private inline fun invokeHandlerSafely(block: () -> Unit) { + private inline fun callCancelHandlerSafely(block: () -> Unit) { try { - block() + block() } catch (ex: Throwable) { // Handler should never fail, if it does -- it is an unhandled exception handleCoroutineException( context, - CompletionHandlerException("Exception in cancellation handler for $this", ex) + CompletionHandlerException("Exception in invokeOnCancellation handler for $this", ex) + ) + } + } + + private fun callCancelHandler(handler: CompletionHandler, cause: Throwable?) = + /* + * :KLUDGE: We have to invoke a handler in platform-specific way via `invokeIt` extension, + * because we play type tricks on Kotlin/JS and handler is not necessarily a function there + */ + callCancelHandlerSafely { handler.invokeIt(cause) } + + fun callCancelHandler(handler: CancelHandler, cause: Throwable?) = + callCancelHandlerSafely { handler.invoke(cause) } + + fun callOnCancellation(onCancellation: (cause: Throwable) -> Unit, cause: Throwable) { + try { + onCancellation.invoke(cause) + } catch (ex: Throwable) { + // Handler should never fail, if it does -- it is an unhandled exception + handleCoroutineException( + context, + CompletionHandlerException("Exception in resume onCancellation handler for $this", ex) ) } } @@ -231,64 +275,75 @@ internal open class CancellableContinuationImpl( val state = this.state if (state is CompletedExceptionally) throw recoverStackTrace(state.cause, this) // if the parent job was already cancelled, then throw the corresponding cancellation exception - // otherwise, there is a race is suspendCancellableCoroutine { cont -> ... } does cont.resume(...) + // otherwise, there is a race if suspendCancellableCoroutine { cont -> ... } does cont.resume(...) // before the block returns. This getResult would return a result as opposed to cancellation // exception that should have happened if the continuation is dispatched for execution later. - if (resumeMode == MODE_CANCELLABLE) { + if (resumeMode.isCancellableMode) { val job = context[Job] if (job != null && !job.isActive) { val cause = job.getCancellationException() - cancelResult(state, cause) + cancelCompletedResult(state, cause) throw recoverStackTrace(cause, this) } } return getSuccessfulResult(state) } - override fun resumeWith(result: Result) { + override fun resumeWith(result: Result) = resumeImpl(result.toState(this), resumeMode) - } - override fun resume(value: T, onCancellation: (cause: Throwable) -> Unit) { - val cancelled = resumeImpl(CompletedWithCancellation(value, onCancellation), resumeMode) - if (cancelled != null) { - // too late to resume (was cancelled) -- call handler - invokeHandlerSafely { - onCancellation(cancelled.cause) - } - } - } + override fun resume(value: T, onCancellation: ((cause: Throwable) -> Unit)?) = + resumeImpl(value, resumeMode, onCancellation) public override fun invokeOnCancellation(handler: CompletionHandler) { - var handleCache: CancelHandler? = null + val cancelHandler = makeCancelHandler(handler) _state.loop { state -> when (state) { is Active -> { - val node = handleCache ?: makeHandler(handler).also { handleCache = it } - if (_state.compareAndSet(state, node)) return // quit on cas success + if (_state.compareAndSet(state, cancelHandler)) return // quit on cas success } is CancelHandler -> multipleHandlersError(handler, state) - is CancelledContinuation -> { + is CompletedExceptionally -> { /* - * Continuation was already cancelled, invoke directly. + * Continuation was already cancelled or completed exceptionally. * NOTE: multiple invokeOnCancellation calls with different handlers are not allowed, - * so we check to make sure that handler was installed just once. + * so we check to make sure handler was installed just once. */ if (!state.makeHandled()) multipleHandlersError(handler, state) /* + * Call the handler only if it was cancelled (not called when completed exceptionally). * :KLUDGE: We have to invoke a handler in platform-specific way via `invokeIt` extension, * because we play type tricks on Kotlin/JS and handler is not necessarily a function there */ - invokeHandlerSafely { handler.invokeIt((state as? CompletedExceptionally)?.cause) } + if (state is CancelledContinuation) { + callCancelHandler(handler, (state as? CompletedExceptionally)?.cause) + } return } + is CompletedContinuation -> { + /* + * Continuation was already completed, and might already have cancel handler. + */ + if (state.cancelHandler != null) multipleHandlersError(handler, state) + // BeforeResumeCancelHandler does not need to be called on a completed continuation + if (cancelHandler is BeforeResumeCancelHandler) return + if (state.cancelled) { + // Was already cancelled while being dispatched -- invoke the handler directly + callCancelHandler(handler, state.cancelCause) + return + } + val update = state.copy(cancelHandler = cancelHandler) + if (_state.compareAndSet(state, update)) return // quit on cas success + } else -> { /* - * Continuation was already completed, do nothing. - * NOTE: multiple invokeOnCancellation calls with different handlers are not allowed, - * but we have no way to check that it was installed just once in this case. + * Continuation was already completed normally, but might get cancelled while being dispatched. + * Change its state to CompletedContinuation, unless we have BeforeResumeCancelHandler which + * does not need to be called in this case. */ - return + if (cancelHandler is BeforeResumeCancelHandler) return + val update = CompletedContinuation(state, cancelHandler = cancelHandler) + if (_state.compareAndSet(state, update)) return // quit on cas success } } } @@ -298,7 +353,7 @@ internal open class CancellableContinuationImpl( error("It's prohibited to register multiple handlers, tried to register $handler, already has $state") } - private fun makeHandler(handler: CompletionHandler): CancelHandler = + private fun makeCancelHandler(handler: CompletionHandler): CancelHandler = if (handler is CancelHandler) handler else InvokeOnCancel(handler) private fun dispatchResume(mode: Int) { @@ -307,15 +362,39 @@ internal open class CancellableContinuationImpl( dispatch(mode) } - // returns null when successfully dispatched resumed, CancelledContinuation if too late (was already cancelled) - private fun resumeImpl(proposedUpdate: Any?, resumeMode: Int): CancelledContinuation? { + private fun resumedState( + state: NotCompleted, + proposedUpdate: Any?, + resumeMode: Int, + onCancellation: ((cause: Throwable) -> Unit)?, + idempotent: Any? + ): Any? = when { + proposedUpdate is CompletedExceptionally -> { + assert { idempotent == null } // there are no idempotent exceptional resumes + assert { onCancellation == null } // only successful results can be cancelled + proposedUpdate + } + !resumeMode.isCancellableMode && idempotent == null -> proposedUpdate // cannot be cancelled in process, all is fine + onCancellation != null || (state is CancelHandler && state !is BeforeResumeCancelHandler) || idempotent != null -> + // mark as CompletedContinuation if special cases are present: + // Cancellation handlers that shall be called after resume or idempotent resume + CompletedContinuation(proposedUpdate, state as? CancelHandler, onCancellation, idempotent) + else -> proposedUpdate // simple case -- use the value directly + } + + private fun resumeImpl( + proposedUpdate: Any?, + resumeMode: Int, + onCancellation: ((cause: Throwable) -> Unit)? = null + ) { _state.loop { state -> when (state) { is NotCompleted -> { - if (!_state.compareAndSet(state, proposedUpdate)) return@loop // retry on cas failure + val update = resumedState(state, proposedUpdate, resumeMode, onCancellation, idempotent = null) + if (!_state.compareAndSet(state, update)) return@loop // retry on cas failure detachChildIfNonResuable() - dispatchResume(resumeMode) - return null + dispatchResume(resumeMode) // dispatch resume, but it might get cancelled in process + return // done } is CancelledContinuation -> { /* @@ -323,14 +402,48 @@ internal open class CancellableContinuationImpl( * because cancellation is asynchronous and may race with resume. * Racy exceptions will be lost, too. */ - if (state.makeResumed()) return state // tried to resume just once, but was cancelled + if (state.makeResumed()) { // check if trying to resume one (otherwise error) + // call onCancellation + onCancellation?.let { callOnCancellation(it, state.cause) } + return // done + } + } + } + alreadyResumedError(proposedUpdate) // otherwise, an error (second resume attempt) + } + } + + /** + * Similar to [tryResume], but does not actually completes resume (needs [completeResume] call). + * Returns [RESUME_TOKEN] when resumed, `null` when it was already resumed or cancelled. + */ + private fun tryResumeImpl( + proposedUpdate: Any?, + idempotent: Any?, + onCancellation: ((cause: Throwable) -> Unit)? + ): Symbol? { + _state.loop { state -> + when (state) { + is NotCompleted -> { + val update = resumedState(state, proposedUpdate, resumeMode, onCancellation, idempotent) + if (!_state.compareAndSet(state, update)) return@loop // retry on cas failure + detachChildIfNonResuable() + return RESUME_TOKEN + } + is CompletedContinuation -> { + return if (idempotent != null && state.idempotentResume === idempotent) { + assert { state.result == proposedUpdate } // "Non-idempotent resume" + RESUME_TOKEN // resumed with the same token -- ok + } else { + null // resumed with a different token or non-idempotent -- too late + } } + else -> return null // cannot resume -- not active anymore } - alreadyResumedError(proposedUpdate) // otherwise -- an error (second resume attempt) } } - private fun alreadyResumedError(proposedUpdate: Any?) { + private fun alreadyResumedError(proposedUpdate: Any?): Nothing { error("Already resumed, but proposed with update $proposedUpdate") } @@ -342,7 +455,7 @@ internal open class CancellableContinuationImpl( /** * Detaches from the parent. - * Invariant: used used from [CoroutineDispatcher.releaseInterceptedContinuation] iff [isReusable] is `true` + * Invariant: used from [CoroutineDispatcher.releaseInterceptedContinuation] iff [isReusable] is `true` */ internal fun detachChild() { val handle = parentHandle @@ -351,42 +464,14 @@ internal open class CancellableContinuationImpl( } // Note: Always returns RESUME_TOKEN | null - override fun tryResume(value: T, idempotent: Any?): Any? { - _state.loop { state -> - when (state) { - is NotCompleted -> { - val update: Any? = if (idempotent == null) value else - CompletedIdempotentResult(idempotent, value) - if (!_state.compareAndSet(state, update)) return@loop // retry on cas failure - detachChildIfNonResuable() - return RESUME_TOKEN - } - is CompletedIdempotentResult -> { - return if (state.idempotentResume === idempotent) { - assert { state.result === value } // "Non-idempotent resume" - RESUME_TOKEN - } else { - null - } - } - else -> return null // cannot resume -- not active anymore - } - } - } + override fun tryResume(value: T, idempotent: Any?): Any? = + tryResumeImpl(value, idempotent, onCancellation = null) - override fun tryResumeWithException(exception: Throwable): Any? { - _state.loop { state -> - when (state) { - is NotCompleted -> { - val update = CompletedExceptionally(exception) - if (!_state.compareAndSet(state, update)) return@loop // retry on cas failure - detachChildIfNonResuable() - return RESUME_TOKEN - } - else -> return null // cannot resume -- not active anymore - } - } - } + override fun tryResume(value: T, idempotent: Any?, onCancellation: ((cause: Throwable) -> Unit)?): Any? = + tryResumeImpl(value, idempotent, onCancellation) + + override fun tryResumeWithException(exception: Throwable): Any? = + tryResumeImpl(CompletedExceptionally(exception), idempotent = null, onCancellation = null) // note: token is always RESUME_TOKEN override fun completeResume(token: Any) { @@ -407,11 +492,15 @@ internal open class CancellableContinuationImpl( @Suppress("UNCHECKED_CAST") override fun getSuccessfulResult(state: Any?): T = when (state) { - is CompletedIdempotentResult -> state.result as T - is CompletedWithCancellation -> state.result as T + is CompletedContinuation -> state.result as T else -> state as T } + // The exceptional state in CancellableContinuationImpl is stored directly and it is not recovered yet. + // The stacktrace recovery is invoked here. + override fun getExceptionalResult(state: Any?): Throwable? = + super.getExceptionalResult(state)?.let { recoverStackTrace(it, delegate) } + // For nicer debugging public override fun toString(): String = "${nameString()}(${delegate.toDebugString()}){$state}@$hexAddress" @@ -428,8 +517,20 @@ private object Active : NotCompleted { override fun toString(): String = "Active" } +/** + * Base class for all [CancellableContinuation.invokeOnCancellation] handlers to avoid an extra instance + * on JVM, yet support JS where you cannot extend from a functional type. + */ internal abstract class CancelHandler : CancelHandlerBase(), NotCompleted +/** + * Base class for all [CancellableContinuation.invokeOnCancellation] handlers that don't need to be invoked + * if continuation is cancelled after resumption, during dispatch, because the corresponding resources + * were already released before calling `resume`. This cancel handler is called only before `resume`. + * It avoids allocation of [CompletedContinuation] instance during resume on JVM. + */ +internal abstract class BeforeResumeCancelHandler : CancelHandler() + // Wrapper for lambdas, for the performance sake CancelHandler can be subclassed directly private class InvokeOnCancel( // Clashes with InvokeOnCancellation private val handler: CompletionHandler @@ -440,17 +541,18 @@ private class InvokeOnCancel( // Clashes with InvokeOnCancellation override fun toString() = "InvokeOnCancel[${handler.classSimpleName}@$hexAddress]" } -private class CompletedIdempotentResult( - @JvmField val idempotentResume: Any?, - @JvmField val result: Any? -) { - override fun toString(): String = "CompletedIdempotentResult[$result]" -} - -private class CompletedWithCancellation( +// Completed with additional metadata +private data class CompletedContinuation( @JvmField val result: Any?, - @JvmField val onCancellation: (cause: Throwable) -> Unit + @JvmField val cancelHandler: CancelHandler? = null, // installed via invokeOnCancellation + @JvmField val onCancellation: ((cause: Throwable) -> Unit)? = null, // installed via resume block + @JvmField val idempotentResume: Any? = null, + @JvmField val cancelCause: Throwable? = null ) { - override fun toString(): String = "CompletedWithCancellation[$result]" -} + val cancelled: Boolean get() = cancelCause != null + fun invokeHandlers(cont: CancellableContinuationImpl<*>, cause: Throwable) { + cancelHandler?.let { cont.callCancelHandler(it, cause) } + onCancellation?.let { cont.callOnCancellation(it, cause) } + } +} diff --git a/kotlinx-coroutines-core/common/src/CompletableDeferred.kt b/kotlinx-coroutines-core/common/src/CompletableDeferred.kt index f6cf90d515..2f00847298 100644 --- a/kotlinx-coroutines-core/common/src/CompletableDeferred.kt +++ b/kotlinx-coroutines-core/common/src/CompletableDeferred.kt @@ -16,8 +16,11 @@ import kotlinx.coroutines.selects.* * * An instance of completable deferred can be created by `CompletableDeferred()` function in _active_ state. * - * All functions on this interface and on all interfaces derived from it are **thread-safe** and can + * All functions on this interface are **thread-safe** and can * be safely invoked from concurrent coroutines without external synchronization. + * + * **The `CompletableDeferred` interface is not stable for inheritance in 3rd party libraries**, + * as new methods might be added to this interface in the future, but is stable for use. */ public interface CompletableDeferred : Deferred { /** @@ -54,8 +57,8 @@ public interface CompletableDeferred : Deferred { * This function transitions this deferred in the same ways described by [CompletableDeferred.complete] and * [CompletableDeferred.completeExceptionally]. */ -@ExperimentalCoroutinesApi // since 1.3.2, tentatively until 1.4.0 -public fun CompletableDeferred.completeWith(result: Result) = result.fold({ complete(it) }, { completeExceptionally(it) }) +public fun CompletableDeferred.completeWith(result: Result): Boolean = + result.fold({ complete(it) }, { completeExceptionally(it) }) /** * Creates a [CompletableDeferred] in an _active_ state. diff --git a/kotlinx-coroutines-core/common/src/CompletableJob.kt b/kotlinx-coroutines-core/common/src/CompletableJob.kt index 4b4d16bc53..74a92e36e5 100644 --- a/kotlinx-coroutines-core/common/src/CompletableJob.kt +++ b/kotlinx-coroutines-core/common/src/CompletableJob.kt @@ -7,6 +7,12 @@ package kotlinx.coroutines /** * A job that can be completed using [complete()] function. * It is returned by [Job()][Job] and [SupervisorJob()][SupervisorJob] constructor functions. + * + * All functions on this interface are **thread-safe** and can + * be safely invoked from concurrent coroutines without external synchronization. + * + * **The `CompletableJob` interface is not stable for inheritance in 3rd party libraries**, + * as new methods might be added to this interface in the future, but is stable for use. */ public interface CompletableJob : Job { /** diff --git a/kotlinx-coroutines-core/common/src/CompletedExceptionally.kt b/kotlinx-coroutines-core/common/src/CompletionState.kt similarity index 78% rename from kotlinx-coroutines-core/common/src/CompletedExceptionally.kt rename to kotlinx-coroutines-core/common/src/CompletionState.kt index b426785bd7..f09aa3ccd9 100644 --- a/kotlinx-coroutines-core/common/src/CompletedExceptionally.kt +++ b/kotlinx-coroutines-core/common/src/CompletionState.kt @@ -9,10 +9,17 @@ import kotlinx.coroutines.internal.* import kotlin.coroutines.* import kotlin.jvm.* -internal fun Result.toState(): Any? = fold({ it }, { CompletedExceptionally(it) }) +internal fun Result.toState( + onCancellation: ((cause: Throwable) -> Unit)? = null +): Any? = fold( + onSuccess = { if (onCancellation != null) CompletedWithCancellation(it, onCancellation) else it }, + onFailure = { CompletedExceptionally(it) } +) -internal fun Result.toState(caller: CancellableContinuation<*>): Any? = fold({ it }, - { CompletedExceptionally(recoverStackTrace(it, caller)) }) +internal fun Result.toState(caller: CancellableContinuation<*>): Any? = fold( + onSuccess = { it }, + onFailure = { CompletedExceptionally(recoverStackTrace(it, caller)) } +) @Suppress("RESULT_CLASS_IN_RETURN_TYPE", "UNCHECKED_CAST") internal fun recoverResult(state: Any?, uCont: Continuation): Result = @@ -21,6 +28,11 @@ internal fun recoverResult(state: Any?, uCont: Continuation): Result = else Result.success(state as T) +internal data class CompletedWithCancellation( + @JvmField val result: Any?, + @JvmField val onCancellation: (cause: Throwable) -> Unit +) + /** * Class for an internal state of a job that was cancelled (completed exceptionally). * diff --git a/kotlinx-coroutines-core/common/src/CoroutineDispatcher.kt b/kotlinx-coroutines-core/common/src/CoroutineDispatcher.kt index fe4c263e18..ab1e814b8a 100644 --- a/kotlinx-coroutines-core/common/src/CoroutineDispatcher.kt +++ b/kotlinx-coroutines-core/common/src/CoroutineDispatcher.kt @@ -4,6 +4,7 @@ package kotlinx.coroutines +import kotlinx.coroutines.internal.* import kotlin.coroutines.* /** @@ -87,7 +88,7 @@ public abstract class CoroutineDispatcher : * @suppress **This an internal API and should not be used from general code.** */ @InternalCoroutinesApi - public open fun dispatchYield(context: CoroutineContext, block: Runnable) = dispatch(context, block) + public open fun dispatchYield(context: CoroutineContext, block: Runnable): Unit = dispatch(context, block) /** * Returns a continuation that wraps the provided [continuation], thus intercepting all resumptions. @@ -115,7 +116,7 @@ public abstract class CoroutineDispatcher : "The dispatcher to the right of `+` just replaces the dispatcher to the left.", level = DeprecationLevel.ERROR ) - public operator fun plus(other: CoroutineDispatcher) = other + public operator fun plus(other: CoroutineDispatcher): CoroutineDispatcher = other /** @suppress for nicer debugging */ override fun toString(): String = "$classSimpleName@$hexAddress" diff --git a/kotlinx-coroutines-core/common/src/CoroutineExceptionHandler.kt b/kotlinx-coroutines-core/common/src/CoroutineExceptionHandler.kt index cd7fd0d7ca..b49a6faa35 100644 --- a/kotlinx-coroutines-core/common/src/CoroutineExceptionHandler.kt +++ b/kotlinx-coroutines-core/common/src/CoroutineExceptionHandler.kt @@ -52,11 +52,38 @@ public inline fun CoroutineExceptionHandler(crossinline handler: (CoroutineConte } /** - * An optional element in the coroutine context to handle uncaught exceptions. + * An optional element in the coroutine context to handle **uncaught** exceptions. * - * Normally, uncaught exceptions can only result from coroutines created using the [launch][CoroutineScope.launch] builder. + * Normally, uncaught exceptions can only result from _root_ coroutines created using the [launch][CoroutineScope.launch] builder. + * All _children_ coroutines (coroutines created in the context of another [Job]) delegate handling of their + * exceptions to their parent coroutine, which also delegates to the parent, and so on until the root, + * so the `CoroutineExceptionHandler` installed in their context is never used. + * Coroutines running with [SupervisorJob] do not propagate exceptions to their parent and are treated like root coroutines. * A coroutine that was created using [async][CoroutineScope.async] always catches all its exceptions and represents them - * in the resulting [Deferred] object. + * in the resulting [Deferred] object, so it cannot result in uncaught exceptions. + * + * ### Handling coroutine exceptions + * + * `CoroutineExceptionHandler` is a last-resort mechanism for global "catch all" behavior. + * You cannot recover from the exception in the `CoroutineExceptionHandler`. The coroutine had already completed + * with the corresponding exception when the handler is called. Normally, the handler is used to + * log the exception, show some kind of error message, terminate, and/or restart the application. + * + * If you need to handle exception in a specific part of the code, it is recommended to use `try`/`catch` around + * the corresponding code inside your coroutine. This way you can prevent completion of the coroutine + * with the exception (exception is now _caught_), retry the operation, and/or take other arbitrary actions: + * + * ``` + * scope.launch { // launch child coroutine in a scope + * try { + * // do something + * } catch (e: Throwable) { + * // handle exception + * } + * } + * ``` + * + * ### Implementation details * * By default, when no handler is installed, uncaught exception are handled in the following way: * * If exception is [CancellationException] then it is ignored @@ -66,10 +93,7 @@ public inline fun CoroutineExceptionHandler(crossinline handler: (CoroutineConte * * Otherwise, all instances of [CoroutineExceptionHandler] found via [ServiceLoader] * * and current thread's [Thread.uncaughtExceptionHandler] are invoked. * - * [CoroutineExceptionHandler] can be invoked from an arbitrary dispatcher used by coroutines in the current job hierarchy. - * For example, if one has a `MainScope` and launches children of the scope in main and default dispatchers, then exception handler can - * be invoked either in main or in default dispatcher thread regardless of - * which particular dispatcher coroutine that has thrown an exception used. + * [CoroutineExceptionHandler] can be invoked from an arbitrary thread. */ public interface CoroutineExceptionHandler : CoroutineContext.Element { /** diff --git a/kotlinx-coroutines-core/common/src/CoroutineScope.kt b/kotlinx-coroutines-core/common/src/CoroutineScope.kt index a6b79bdb5a..0dde6c9352 100644 --- a/kotlinx-coroutines-core/common/src/CoroutineScope.kt +++ b/kotlinx-coroutines-core/common/src/CoroutineScope.kt @@ -1,46 +1,60 @@ /* * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ +@file:OptIn(ExperimentalContracts::class) package kotlinx.coroutines import kotlinx.coroutines.internal.* import kotlinx.coroutines.intrinsics.* +import kotlin.contracts.* import kotlin.coroutines.* import kotlin.coroutines.intrinsics.* /** - * Defines a scope for new coroutines. Every coroutine builder + * Defines a scope for new coroutines. Every **coroutine builder** (like [launch], [async], etc) * is an extension on [CoroutineScope] and inherits its [coroutineContext][CoroutineScope.coroutineContext] - * to automatically propagate both context elements and cancellation. + * to automatically propagate all its elements and cancellation. * * The best ways to obtain a standalone instance of the scope are [CoroutineScope()] and [MainScope()] factory functions. * Additional context elements can be appended to the scope using the [plus][CoroutineScope.plus] operator. * + * ### Convention for structured concurrency + * * Manual implementation of this interface is not recommended, implementation by delegation should be preferred instead. - * By convention, the [context of a scope][CoroutineScope.coroutineContext] should contain an instance of a [job][Job] to enforce structured concurrency. + * By convention, the [context of a scope][CoroutineScope.coroutineContext] should contain an instance of a + * [job][Job] to enforce the discipline of **structured concurrency** with propagation of cancellation. * - * Every coroutine builder (like [launch][CoroutineScope.launch], [async][CoroutineScope.async], etc) + * Every coroutine builder (like [launch], [async], etc) * and every scoping function (like [coroutineScope], [withContext], etc) provides _its own_ scope * with its own [Job] instance into the inner block of code it runs. * By convention, they all wait for all the coroutines inside their block to complete before completing themselves, - * thus enforcing the discipline of **structured concurrency**. + * thus enforcing the structured concurrency. See [Job] documentation for more details. * - * [CoroutineScope] should be implemented (or used as a field) on entities with a well-defined lifecycle that are responsible - * for launching children coroutines. Example of such entity on Android is Activity. - * Usage of this interface may look like this: + * ### Android usage + * + * Android has first-party support for coroutine scope in all entities with the lifecycle. + * See [the corresponding documentation](https://developer.android.com/topic/libraries/architecture/coroutines#lifecyclescope). + * + * ### Custom usage + * + * [CoroutineScope] should be implemented or declared as a property on entities with a well-defined lifecycle that are + * responsible for launching children coroutines, for example: * * ``` - * class MyActivity : AppCompatActivity(), CoroutineScope by MainScope() { - * override fun onDestroy() { - * cancel() // cancel is extension on CoroutineScope + * class MyUIClass { + * val scope = MainScope() // the scope of MyUIClass + * + * fun destroy() { // destroys an instance of MyUIClass + * scope.cancel() // cancels all coroutines launched in this scope + * // ... do the rest of cleanup here ... * } * * /* - * * Note how coroutine builders are scoped: if activity is destroyed or any of the launched coroutines + * * Note: if this instance is destroyed or any of the launched coroutines * * in this method throws an exception, then all nested coroutines are cancelled. * */ - * fun showSomeData() = launch { // <- extension on current activity, launched in the main thread + * fun showSomeData() = scope.launch { // launched in the main thread * // ... here we can use suspending functions or coroutine builders with other dispatchers * draw(data) // draw in the main thread * } @@ -171,11 +185,15 @@ public object GlobalScope : CoroutineScope { * or may throw a corresponding unhandled [Throwable] if there is any unhandled exception in this scope * (for example, from a crashed coroutine that was started with [launch][CoroutineScope.launch] in this scope). */ -public suspend fun coroutineScope(block: suspend CoroutineScope.() -> R): R = - suspendCoroutineUninterceptedOrReturn { uCont -> +public suspend fun coroutineScope(block: suspend CoroutineScope.() -> R): R { + contract { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + } + return suspendCoroutineUninterceptedOrReturn { uCont -> val coroutine = ScopeCoroutine(uCont.context, uCont) coroutine.startUndispatchedOrReturn(coroutine, block) } +} /** * Creates a [CoroutineScope] that wraps the given coroutine [context]. @@ -208,10 +226,10 @@ public fun CoroutineScope.cancel(message: String, cause: Throwable? = null): Uni /** * Ensures that current scope is [active][CoroutineScope.isActive]. - * Throws [IllegalStateException] if the context does not have a job in it. * * If the job is no longer active, throws [CancellationException]. * If the job was cancelled, thrown exception contains the original cancellation cause. + * This function does not do anything if there is no [Job] in the scope's [coroutineContext][CoroutineScope.coroutineContext]. * * This method is a drop-in replacement for the following code, but with more precise exception: * ``` @@ -219,5 +237,23 @@ public fun CoroutineScope.cancel(message: String, cause: Throwable? = null): Uni * throw CancellationException() * } * ``` + * + * @see CoroutineContext.ensureActive */ public fun CoroutineScope.ensureActive(): Unit = coroutineContext.ensureActive() + + +/** + * Returns the current [CoroutineContext] retrieved by using [kotlin.coroutines.coroutineContext]. + * This function is an alias to avoid name clash with [CoroutineScope.coroutineContext] in a receiver position: + * + * ``` + * launch { // this: CoroutineScope + * val flow = flow { + * coroutineContext // Resolves into the context of outer launch, which is incorrect, see KT-38033 + * currentCoroutineContext() // Retrieves actual context where the flow is collected + * } + * } + * ``` + */ +public suspend inline fun currentCoroutineContext(): CoroutineContext = coroutineContext diff --git a/kotlinx-coroutines-core/common/src/CoroutineStart.kt b/kotlinx-coroutines-core/common/src/CoroutineStart.kt index 1272ce7c3a..d5791c79fe 100644 --- a/kotlinx-coroutines-core/common/src/CoroutineStart.kt +++ b/kotlinx-coroutines-core/common/src/CoroutineStart.kt @@ -1,7 +1,7 @@ /* * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ - +@file:Suppress("NO_EXPLICIT_VISIBILITY_IN_API_MODE") package kotlinx.coroutines import kotlinx.coroutines.CoroutineStart.* @@ -55,7 +55,7 @@ public enum class CoroutineStart { * Cancellability of coroutine at suspension points depends on the particular implementation details of * suspending functions as in [DEFAULT]. */ - @ExperimentalCoroutinesApi + @ExperimentalCoroutinesApi // Since 1.0.0, no ETA on stability ATOMIC, /** @@ -71,7 +71,7 @@ public enum class CoroutineStart { * * **Note: This is an experimental api.** Execution semantics of coroutines may change in the future when this mode is used. */ - @ExperimentalCoroutinesApi + @ExperimentalCoroutinesApi // Since 1.0.0, no ETA on stability UNDISPATCHED; /** @@ -85,12 +85,12 @@ public enum class CoroutineStart { * @suppress **This an internal API and should not be used from general code.** */ @InternalCoroutinesApi - public operator fun invoke(block: suspend () -> T, completion: Continuation) = + public operator fun invoke(block: suspend () -> T, completion: Continuation): Unit = when (this) { - CoroutineStart.DEFAULT -> block.startCoroutineCancellable(completion) - CoroutineStart.ATOMIC -> block.startCoroutine(completion) - CoroutineStart.UNDISPATCHED -> block.startCoroutineUndispatched(completion) - CoroutineStart.LAZY -> Unit // will start lazily + DEFAULT -> block.startCoroutineCancellable(completion) + ATOMIC -> block.startCoroutine(completion) + UNDISPATCHED -> block.startCoroutineUndispatched(completion) + LAZY -> Unit // will start lazily } /** @@ -104,12 +104,12 @@ public enum class CoroutineStart { * @suppress **This an internal API and should not be used from general code.** */ @InternalCoroutinesApi - public operator fun invoke(block: suspend R.() -> T, receiver: R, completion: Continuation) = + public operator fun invoke(block: suspend R.() -> T, receiver: R, completion: Continuation): Unit = when (this) { - CoroutineStart.DEFAULT -> block.startCoroutineCancellable(receiver, completion) - CoroutineStart.ATOMIC -> block.startCoroutine(receiver, completion) - CoroutineStart.UNDISPATCHED -> block.startCoroutineUndispatched(receiver, completion) - CoroutineStart.LAZY -> Unit // will start lazily + DEFAULT -> block.startCoroutineCancellable(receiver, completion) + ATOMIC -> block.startCoroutine(receiver, completion) + UNDISPATCHED -> block.startCoroutineUndispatched(receiver, completion) + LAZY -> Unit // will start lazily } /** diff --git a/kotlinx-coroutines-core/common/src/Debug.common.kt b/kotlinx-coroutines-core/common/src/Debug.common.kt index 013b983a74..949b05c63f 100644 --- a/kotlinx-coroutines-core/common/src/Debug.common.kt +++ b/kotlinx-coroutines-core/common/src/Debug.common.kt @@ -27,7 +27,7 @@ internal expect fun assert(value: () -> Boolean) * Copy mechanism is used only on JVM, but it might be convenient to implement it in common exceptions, * so on JVM their stacktraces will be properly recovered. */ -@ExperimentalCoroutinesApi +@ExperimentalCoroutinesApi // Since 1.2.0, no ETA on stability public interface CopyableThrowable where T : Throwable, T : CopyableThrowable { /** diff --git a/kotlinx-coroutines-core/common/src/Deferred.kt b/kotlinx-coroutines-core/common/src/Deferred.kt index f05abbdb43..ff996756a3 100644 --- a/kotlinx-coroutines-core/common/src/Deferred.kt +++ b/kotlinx-coroutines-core/common/src/Deferred.kt @@ -30,6 +30,9 @@ import kotlinx.coroutines.selects.* * * All functions on this interface and on all interfaces derived from it are **thread-safe** and can * be safely invoked from concurrent coroutines without external synchronization. + * + * **`Deferred` interface and all its derived interfaces are not stable for inheritance in 3rd party libraries**, + * as new methods might be added to this interface in the future, but is stable for use. */ public interface Deferred : Job { @@ -40,6 +43,8 @@ public interface Deferred : Job { * This suspending function is cancellable. * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function * immediately resumes with [CancellationException]. + * There is a **prompt cancellation guarantee**. If the job was cancelled while this function was + * suspended, it will not resume successfully. See [suspendCancellableCoroutine] documentation for low-level details. * * This function can be used in [select] invocation with [onAwait] clause. * Use [isCompleted] to check for completion of this deferred value without waiting. diff --git a/kotlinx-coroutines-core/common/src/Delay.kt b/kotlinx-coroutines-core/common/src/Delay.kt index cc205ecaf0..aae623d5df 100644 --- a/kotlinx-coroutines-core/common/src/Delay.kt +++ b/kotlinx-coroutines-core/common/src/Delay.kt @@ -21,11 +21,14 @@ import kotlin.time.* public interface Delay { /** * Delays coroutine for a given time without blocking a thread and resumes it after a specified time. + * * This suspending function is cancellable. * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function * immediately resumes with [CancellationException]. + * There is a **prompt cancellation guarantee**. If the job was cancelled while this function was + * suspended, it will not resume successfully. See [suspendCancellableCoroutine] documentation for low-level details. */ - suspend fun delay(time: Long) { + public suspend fun delay(time: Long) { if (time <= 0) return // don't delay return suspendCancellableCoroutine { scheduleResumeAfterDelay(time, it) } } @@ -45,7 +48,7 @@ public interface Delay { * with(continuation) { resumeUndispatchedWith(Unit) } * ``` */ - fun scheduleResumeAfterDelay(timeMillis: Long, continuation: CancellableContinuation) + public fun scheduleResumeAfterDelay(timeMillis: Long, continuation: CancellableContinuation) /** * Schedules invocation of a specified [block] after a specified delay [timeMillis]. @@ -54,15 +57,56 @@ public interface Delay { * * This implementation uses a built-in single-threaded scheduled executor service. */ - fun invokeOnTimeout(timeMillis: Long, block: Runnable): DisposableHandle = - DefaultDelay.invokeOnTimeout(timeMillis, block) + public fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle = + DefaultDelay.invokeOnTimeout(timeMillis, block, context) } +/** + * Suspends until cancellation, in which case it will throw a [CancellationException]. + * + * This function returns [Nothing], so it can be used in any coroutine, + * regardless of the required return type. + * + * Usage example in callback adapting code: + * + * ```kotlin + * fun currentTemperature(): Flow = callbackFlow { + * val callback = SensorCallback { degreesCelsius: Double -> + * trySend(Temperature.celsius(degreesCelsius)) + * } + * try { + * registerSensorCallback(callback) + * awaitCancellation() // Suspends to keep getting updates until cancellation. + * } finally { + * unregisterSensorCallback(callback) + * } + * } + * ``` + * + * Usage example in (non declarative) UI code: + * + * ```kotlin + * suspend fun showStuffUntilCancelled(content: Stuff): Nothing { + * someSubView.text = content.title + * anotherSubView.text = content.description + * someView.visibleInScope { + * awaitCancellation() // Suspends so the view stays visible. + * } + * } + * ``` + */ +public suspend fun awaitCancellation(): Nothing = suspendCancellableCoroutine {} + /** * Delays coroutine for a given time without blocking a thread and resumes it after a specified time. + * * This suspending function is cancellable. * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function * immediately resumes with [CancellationException]. + * There is a **prompt cancellation guarantee**. If the job was cancelled while this function was + * suspended, it will not resume successfully. See [suspendCancellableCoroutine] documentation for low-level details. + * + * If you want to delay forever (until cancellation), consider using [awaitCancellation] instead. * * Note that delay can be used in [select] invocation with [onTimeout][SelectBuilder.onTimeout] clause. * @@ -72,22 +116,30 @@ public interface Delay { public suspend fun delay(timeMillis: Long) { if (timeMillis <= 0) return // don't delay return suspendCancellableCoroutine sc@ { cont: CancellableContinuation -> - cont.context.delay.scheduleResumeAfterDelay(timeMillis, cont) + // if timeMillis == Long.MAX_VALUE then just wait forever like awaitCancellation, don't schedule. + if (timeMillis < Long.MAX_VALUE) { + cont.context.delay.scheduleResumeAfterDelay(timeMillis, cont) + } } } /** * Delays coroutine for a given [duration] without blocking a thread and resumes it after the specified time. + * * This suspending function is cancellable. * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function * immediately resumes with [CancellationException]. + * There is a **prompt cancellation guarantee**. If the job was cancelled while this function was + * suspended, it will not resume successfully. See [suspendCancellableCoroutine] documentation for low-level details. + * + * If you want to delay forever (until cancellation), consider using [awaitCancellation] instead. * * Note that delay can be used in [select] invocation with [onTimeout][SelectBuilder.onTimeout] clause. * * Implementation note: how exactly time is tracked is an implementation detail of [CoroutineDispatcher] in the context. */ @ExperimentalTime -public suspend fun delay(duration: Duration) = delay(duration.toDelayMillis()) +public suspend fun delay(duration: Duration): Unit = delay(duration.toDelayMillis()) /** Returns [Delay] implementation of the given context */ internal val CoroutineContext.delay: Delay get() = get(ContinuationInterceptor) as? Delay ?: DefaultDelay diff --git a/kotlinx-coroutines-core/common/src/EventLoop.common.kt b/kotlinx-coroutines-core/common/src/EventLoop.common.kt index ba331e20df..69ea9fe312 100644 --- a/kotlinx-coroutines-core/common/src/EventLoop.common.kt +++ b/kotlinx-coroutines-core/common/src/EventLoop.common.kt @@ -52,7 +52,7 @@ internal abstract class EventLoop : CoroutineDispatcher() { */ public open fun processNextEvent(): Long { if (!processUnconfinedEvent()) return Long.MAX_VALUE - return nextTime + return 0 } protected open val isEmpty: Boolean get() = isUnconfinedQueueEmpty @@ -251,7 +251,7 @@ internal abstract class EventLoopImplBase: EventLoopImplPlatform(), Delay { override fun processNextEvent(): Long { // unconfined events take priority - if (processUnconfinedEvent()) return nextTime + if (processUnconfinedEvent()) return 0 // queue all delayed tasks that are due to be executed val delayed = _delayed.value if (delayed != null && !delayed.isEmpty) { @@ -269,7 +269,11 @@ internal abstract class EventLoopImplBase: EventLoopImplPlatform(), Delay { } } // then process one event from queue - dequeue()?.run() + val task = dequeue() + if (task != null) { + task.run() + return 0 + } return nextTime } diff --git a/kotlinx-coroutines-core/common/src/Job.kt b/kotlinx-coroutines-core/common/src/Job.kt index 4d4e37ee25..2e05635a29 100644 --- a/kotlinx-coroutines-core/common/src/Job.kt +++ b/kotlinx-coroutines-core/common/src/Job.kt @@ -19,17 +19,22 @@ import kotlin.jvm.* * culminates in its completion. * * Jobs can be arranged into parent-child hierarchies where cancellation - * of a parent leads to immediate cancellation of all its [children]. Failure or cancellation of a child - * with an exception other than [CancellationException] immediately cancels its parent. This way, a parent - * can [cancel] its own children (including all their children recursively) without cancelling itself. + * of a parent leads to immediate cancellation of all its [children] recursively. + * Failure of a child with an exception other than [CancellationException] immediately cancels its parent and, + * consequently, all its other children. This behavior can be customized using [SupervisorJob]. * - * The most basic instances of [Job] are created with [launch][CoroutineScope.launch] coroutine builder or with a - * `Job()` factory function. By default, a failure of any of the job's children leads to an immediate failure - * of its parent and cancellation of the rest of its children. This behavior can be customized using [SupervisorJob]. + * The most basic instances of `Job` interface are created like this: * - * Conceptually, an execution of the job does not produce a result value. Jobs are launched solely for their + * * **Coroutine job** is created with [launch][CoroutineScope.launch] coroutine builder. + * It runs a specified block of code and completes on completion of this block. + * * **[CompletableJob]** is created with a `Job()` factory function. + * It is completed by calling [CompletableJob.complete]. + * + * Conceptually, an execution of a job does not produce a result value. Jobs are launched solely for their * side-effects. See [Deferred] interface for a job that produces a result. * + * ### Job states + * * A job has the following states: * * | **State** | [isActive] | [isCompleted] | [isCancelled] | @@ -41,13 +46,23 @@ import kotlin.jvm.* * | _Cancelled_ (final state) | `false` | `true` | `true` | * | _Completed_ (final state) | `false` | `true` | `false` | * - * Usually, a job is created in _active_ state (it is created and started). However, coroutine builders - * that provide an optional `start` parameter create a coroutine in _new_ state when this parameter is set to + * Usually, a job is created in the _active_ state (it is created and started). However, coroutine builders + * that provide an optional `start` parameter create a coroutine in the _new_ state when this parameter is set to * [CoroutineStart.LAZY]. Such a job can be made _active_ by invoking [start] or [join]. * - * A job is _active_ while the coroutine is working. Failure of the job with exception makes it _cancelling_. + * A job is _active_ while the coroutine is working or until [CompletableJob] is completed, + * or until it fails or cancelled. + * + * Failure of an _active_ job with an exception makes it _cancelling_. * A job can be cancelled at any time with [cancel] function that forces it to transition to - * _cancelling_ state immediately. The job becomes _cancelled_ when it finishes executing its work. + * the _cancelling_ state immediately. The job becomes _cancelled_ when it finishes executing its work and + * all its children complete. + * + * Completion of an _active_ coroutine's body or a call to [CompletableJob.complete] transitions the job to + * the _completing_ state. It waits in the _completing_ state for all its children to complete before + * transitioning to the _completed_ state. + * Note that _completing_ state is purely internal to the job. For an outside observer a _completing_ job is still + * active, while internally it is waiting for its children. * * ``` * wait children @@ -67,19 +82,32 @@ import kotlin.jvm.* * [coroutineContext](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines/coroutine-context.html) * represents the coroutine itself. * - * A job can have a _parent_ job. A job with a parent is cancelled when its parent is cancelled. - * Parent job waits in _completing_ or _cancelling_ state for all its children to complete before finishing. - * Note that _completing_ state is purely internal to the job. For an outside observer a _completing_ job is still - * active, while internally it is waiting for its children. + * ### Cancellation cause + * + * A coroutine job is said to _complete exceptionally_ when its body throws an exception; + * a [CompletableJob] is completed exceptionally by calling [CompletableJob.completeExceptionally]. + * An exceptionally completed job is cancelled and the corresponding exception becomes the _cancellation cause_ of the job. * - * Normal cancellation of a job is distinguished from its failure by the type of its cancellation exception cause. - * If the cause of cancellation is [CancellationException], then the job is considered to be _cancelled normally_. - * This usually happens when [cancel] is invoked without additional parameters. If the cause of cancellation is - * a different exception, then the job is considered to have _failed_. This usually happens when the code of the job - * encounters some problem and throws an exception. + * Normal cancellation of a job is distinguished from its failure by the type of this exception that caused its cancellation. + * A coroutine that threw [CancellationException] is considered to be _cancelled normally_. + * If a cancellation cause is a different exception type, then the job is considered to have _failed_. + * When a job has _failed_, then its parent gets cancelled with the exception of the same type, + * thus ensuring transparency in delegating parts of the job to its children. + * + * Note, that [cancel] function on a job only accepts [CancellationException] as a cancellation cause, thus + * calling [cancel] always results in a normal cancellation of a job, which does not lead to cancellation + * of its parent. This way, a parent can [cancel] its own children (cancelling all their children recursively, too) + * without cancelling itself. + * + * ### Concurrency and synchronization * * All functions on this interface and on all interfaces derived from it are **thread-safe** and can * be safely invoked from concurrent coroutines without external synchronization. + * + * ### Not stable for inheritance + * + * **`Job` interface and all its derived interfaces are not stable for inheritance in 3rd party libraries**, + * as new methods might be added to this interface in the future, but is stable for use. */ public interface Job : CoroutineContext.Element { /** @@ -167,7 +195,7 @@ public interface Job : CoroutineContext.Element { * @suppress This method implements old version of JVM ABI. Use [cancel]. */ @Deprecated(level = DeprecationLevel.HIDDEN, message = "Since 1.2.0, binary compatibility with versions <= 1.1.x") - public fun cancel() = cancel(null) + public fun cancel(): Unit = cancel(null) /** * @suppress This method has bad semantics when cause is not a [CancellationException]. Use [cancel]. @@ -337,7 +365,7 @@ public interface Job : CoroutineContext.Element { "Job is a coroutine context element and `+` is a set-sum operator for coroutine contexts. " + "The job to the right of `+` just replaces the job the left of `+`.", level = DeprecationLevel.ERROR) - public operator fun plus(other: Job) = other + public operator fun plus(other: Job): Job = other } /** @@ -382,7 +410,7 @@ public interface DisposableHandle { */ @Suppress("FunctionName") @InternalCoroutinesApi -public inline fun DisposableHandle(crossinline block: () -> Unit) = +public inline fun DisposableHandle(crossinline block: () -> Unit): DisposableHandle = object : DisposableHandle { override fun dispose() { block() @@ -496,7 +524,7 @@ public fun Job.cancelChildren(cause: CancellationException? = null) { * @suppress This method implements old version of JVM ABI. Use [cancel]. */ @Deprecated(level = DeprecationLevel.HIDDEN, message = "Since 1.2.0, binary compatibility with versions <= 1.1.x") -public fun Job.cancelChildren() = cancelChildren(null) +public fun Job.cancelChildren(): Unit = cancelChildren(null) /** * @suppress This method has bad semantics when cause is not a [CancellationException]. Use [Job.cancelChildren]. @@ -539,7 +567,7 @@ public fun CoroutineContext.cancel(cause: CancellationException? = null) { * @suppress This method implements old version of JVM ABI. Use [CoroutineContext.cancel]. */ @Deprecated(level = DeprecationLevel.HIDDEN, message = "Since 1.2.0, binary compatibility with versions <= 1.1.x") -public fun CoroutineContext.cancel() = cancel(null) +public fun CoroutineContext.cancel(): Unit = cancel(null) /** * Ensures that current job is [active][Job.isActive]. @@ -559,10 +587,10 @@ public fun Job.ensureActive(): Unit { /** * Ensures that job in the current context is [active][Job.isActive]. - * Throws [IllegalStateException] if the context does not have a job in it. * * If the job is no longer active, throws [CancellationException]. * If the job was cancelled, thrown exception contains the original cancellation cause. + * This function does not do anything if there is no [Job] in the context, since such a coroutine cannot be cancelled. * * This method is a drop-in replacement for the following code, but with more precise exception: * ``` @@ -571,9 +599,8 @@ public fun Job.ensureActive(): Unit { * } * ``` */ -public fun CoroutineContext.ensureActive(): Unit { - val job = get(Job) ?: error("Context cannot be checked for liveness because it does not have a job: $this") - job.ensureActive() +public fun CoroutineContext.ensureActive() { + get(Job)?.ensureActive() } /** @@ -605,7 +632,16 @@ public fun CoroutineContext.cancelChildren(cause: CancellationException? = null) * @suppress This method implements old version of JVM ABI. Use [CoroutineContext.cancelChildren]. */ @Deprecated(level = DeprecationLevel.HIDDEN, message = "Since 1.2.0, binary compatibility with versions <= 1.1.x") -public fun CoroutineContext.cancelChildren() = cancelChildren(null) +public fun CoroutineContext.cancelChildren(): Unit = cancelChildren(null) + +/** + * Retrieves the current [Job] instance from the given [CoroutineContext] or + * throws [IllegalStateException] if no job is present in the context. + * + * This method is a short-cut for `coroutineContext[Job]!!` and should be used only when it is known in advance that + * the context does have instance of the job in it. + */ +public val CoroutineContext.job: Job get() = get(Job) ?: error("Current context doesn't contain Job in it: $this") /** * @suppress This method has bad semantics when cause is not a [CancellationException]. Use [CoroutineContext.cancelChildren]. diff --git a/kotlinx-coroutines-core/common/src/JobSupport.kt b/kotlinx-coroutines-core/common/src/JobSupport.kt index e52aaeaa8e..020d00a32c 100644 --- a/kotlinx-coroutines-core/common/src/JobSupport.kt +++ b/kotlinx-coroutines-core/common/src/JobSupport.kt @@ -652,7 +652,7 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren * Makes this [Job] cancelled with a specified [cause]. * It is used in [AbstractCoroutine]-derived classes when there is an internal failure. */ - public fun cancelCoroutine(cause: Throwable?) = cancelImpl(cause) + public fun cancelCoroutine(cause: Throwable?): Boolean = cancelImpl(cause) // cause is Throwable or ParentJob when cancelChild was invoked // returns true is exception was handled, false otherwise diff --git a/kotlinx-coroutines-core/common/src/MainCoroutineDispatcher.kt b/kotlinx-coroutines-core/common/src/MainCoroutineDispatcher.kt index 3f2ddcd69f..daba38f0fd 100644 --- a/kotlinx-coroutines-core/common/src/MainCoroutineDispatcher.kt +++ b/kotlinx-coroutines-core/common/src/MainCoroutineDispatcher.kt @@ -43,4 +43,27 @@ public abstract class MainCoroutineDispatcher : CoroutineDispatcher() { * [Dispatchers.Main] supports immediate execution for Android, JavaFx and Swing platforms. */ public abstract val immediate: MainCoroutineDispatcher + + /** + * Returns a name of this main dispatcher for debugging purposes. This implementation returns + * `Dispatchers.Main` or `Dispatchers.Main.immediate` if it is the same as the corresponding + * reference in [Dispatchers] or a short class-name representation with address otherwise. + */ + override fun toString(): String = toStringInternalImpl() ?: "$classSimpleName@$hexAddress" + + /** + * Internal method for more specific [toString] implementations. It returns non-null + * string if this dispatcher is set in the platform as the main one. + * @suppress + */ + @InternalCoroutinesApi + protected fun toStringInternalImpl(): String? { + val main = Dispatchers.Main + if (this === main) return "Dispatchers.Main" + val immediate = + try { main.immediate } + catch (e: UnsupportedOperationException) { null } + if (this === immediate) return "Dispatchers.Main.immediate" + return null + } } diff --git a/kotlinx-coroutines-core/common/src/Supervisor.kt b/kotlinx-coroutines-core/common/src/Supervisor.kt index 1991119053..542e4fef48 100644 --- a/kotlinx-coroutines-core/common/src/Supervisor.kt +++ b/kotlinx-coroutines-core/common/src/Supervisor.kt @@ -1,13 +1,14 @@ /* * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ - +@file:OptIn(ExperimentalContracts::class) @file:Suppress("DEPRECATION_ERROR") package kotlinx.coroutines import kotlinx.coroutines.internal.* import kotlinx.coroutines.intrinsics.* +import kotlin.contracts.* import kotlin.coroutines.* import kotlin.coroutines.intrinsics.* import kotlin.jvm.* @@ -47,11 +48,15 @@ public fun SupervisorJob0(parent: Job? = null) : Job = SupervisorJob(parent) * A failure of the scope itself (exception thrown in the [block] or cancellation) fails the scope with all its children, * but does not cancel parent job. */ -public suspend fun supervisorScope(block: suspend CoroutineScope.() -> R): R = - suspendCoroutineUninterceptedOrReturn { uCont -> +public suspend fun supervisorScope(block: suspend CoroutineScope.() -> R): R { + contract { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + } + return suspendCoroutineUninterceptedOrReturn { uCont -> val coroutine = SupervisorCoroutine(uCont.context, uCont) coroutine.startUndispatchedOrReturn(coroutine, block) } +} private class SupervisorJobImpl(parent: Job?) : JobImpl(parent) { override fun childCancelled(cause: Throwable): Boolean = false diff --git a/kotlinx-coroutines-core/common/src/Timeout.kt b/kotlinx-coroutines-core/common/src/Timeout.kt index 87fe733773..4bfff118e8 100644 --- a/kotlinx-coroutines-core/common/src/Timeout.kt +++ b/kotlinx-coroutines-core/common/src/Timeout.kt @@ -1,12 +1,14 @@ /* * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ +@file:OptIn(ExperimentalContracts::class) package kotlinx.coroutines import kotlinx.coroutines.internal.* import kotlinx.coroutines.intrinsics.* import kotlinx.coroutines.selects.* +import kotlin.contracts.* import kotlin.coroutines.* import kotlin.coroutines.intrinsics.* import kotlin.jvm.* @@ -22,11 +24,21 @@ import kotlin.time.* * The sibling function that does not throw an exception on timeout is [withTimeoutOrNull]. * Note that the timeout action can be specified for a [select] invocation with [onTimeout][SelectBuilder.onTimeout] clause. * - * Implementation note: how the time is tracked exactly is an implementation detail of the context's [CoroutineDispatcher]. + * **The timeout event is asynchronous with respect to the code running in the block** and may happen at any time, + * even right before the return from inside of the timeout [block]. Keep this in mind if you open or acquire some + * resource inside the [block] that needs closing or release outside of the block. + * See the + * [Asynchronous timeout and resources][https://kotlinlang.org/docs/reference/coroutines/cancellation-and-timeouts.html#asynchronous-timeout-and-resources] + * section of the coroutines guide for details. + * + * > Implementation note: how the time is tracked exactly is an implementation detail of the context's [CoroutineDispatcher]. * * @param timeMillis timeout time in milliseconds. */ public suspend fun withTimeout(timeMillis: Long, block: suspend CoroutineScope.() -> T): T { + contract { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + } if (timeMillis <= 0L) throw TimeoutCancellationException("Timed out immediately") return suspendCoroutineUninterceptedOrReturn { uCont -> setupTimeout(TimeoutCoroutine(timeMillis, uCont), block) @@ -43,11 +55,22 @@ public suspend fun withTimeout(timeMillis: Long, block: suspend CoroutineSco * The sibling function that does not throw an exception on timeout is [withTimeoutOrNull]. * Note that the timeout action can be specified for a [select] invocation with [onTimeout][SelectBuilder.onTimeout] clause. * - * Implementation note: how the time is tracked exactly is an implementation detail of the context's [CoroutineDispatcher]. + * **The timeout event is asynchronous with respect to the code running in the block** and may happen at any time, + * even right before the return from inside of the timeout [block]. Keep this in mind if you open or acquire some + * resource inside the [block] that needs closing or release outside of the block. + * See the + * [Asynchronous timeout and resources][https://kotlinlang.org/docs/reference/coroutines/cancellation-and-timeouts.html#asynchronous-timeout-and-resources] + * section of the coroutines guide for details. + * + * > Implementation note: how the time is tracked exactly is an implementation detail of the context's [CoroutineDispatcher]. */ @ExperimentalTime -public suspend fun withTimeout(timeout: Duration, block: suspend CoroutineScope.() -> T): T = - withTimeout(timeout.toDelayMillis(), block) +public suspend fun withTimeout(timeout: Duration, block: suspend CoroutineScope.() -> T): T { + contract { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + } + return withTimeout(timeout.toDelayMillis(), block) +} /** * Runs a given suspending block of code inside a coroutine with a specified [timeout][timeMillis] and returns @@ -59,7 +82,14 @@ public suspend fun withTimeout(timeout: Duration, block: suspend CoroutineSc * The sibling function that throws an exception on timeout is [withTimeout]. * Note that the timeout action can be specified for a [select] invocation with [onTimeout][SelectBuilder.onTimeout] clause. * - * Implementation note: how the time is tracked exactly is an implementation detail of the context's [CoroutineDispatcher]. + * **The timeout event is asynchronous with respect to the code running in the block** and may happen at any time, + * even right before the return from inside of the timeout [block]. Keep this in mind if you open or acquire some + * resource inside the [block] that needs closing or release outside of the block. + * See the + * [Asynchronous timeout and resources][https://kotlinlang.org/docs/reference/coroutines/cancellation-and-timeouts.html#asynchronous-timeout-and-resources] + * section of the coroutines guide for details. + * + * > Implementation note: how the time is tracked exactly is an implementation detail of the context's [CoroutineDispatcher]. * * @param timeMillis timeout time in milliseconds. */ @@ -92,7 +122,14 @@ public suspend fun withTimeoutOrNull(timeMillis: Long, block: suspend Corout * The sibling function that throws an exception on timeout is [withTimeout]. * Note that the timeout action can be specified for a [select] invocation with [onTimeout][SelectBuilder.onTimeout] clause. * - * Implementation note: how the time is tracked exactly is an implementation detail of the context's [CoroutineDispatcher]. + * **The timeout event is asynchronous with respect to the code running in the block** and may happen at any time, + * even right before the return from inside of the timeout [block]. Keep this in mind if you open or acquire some + * resource inside the [block] that needs closing or release outside of the block. + * See the + * [Asynchronous timeout and resources][https://kotlinlang.org/docs/reference/coroutines/cancellation-and-timeouts.html#asynchronous-timeout-and-resources] + * section of the coroutines guide for details. + * + * > Implementation note: how the time is tracked exactly is an implementation detail of the context's [CoroutineDispatcher]. */ @ExperimentalTime public suspend fun withTimeoutOrNull(timeout: Duration, block: suspend CoroutineScope.() -> T): T? = @@ -105,7 +142,7 @@ private fun setupTimeout( // schedule cancellation of this coroutine on time val cont = coroutine.uCont val context = cont.context - coroutine.disposeOnCompletion(context.delay.invokeOnTimeout(coroutine.time, coroutine)) + coroutine.disposeOnCompletion(context.delay.invokeOnTimeout(coroutine.time, coroutine, coroutine.context)) // restart the block using a new coroutine with a new job, // however, start it undispatched, because we already are in the proper context return coroutine.startUndispatchedOrReturnIgnoreTimeout(coroutine, block) diff --git a/kotlinx-coroutines-core/common/src/Unconfined.kt b/kotlinx-coroutines-core/common/src/Unconfined.kt index ce03a28765..a0997a51d3 100644 --- a/kotlinx-coroutines-core/common/src/Unconfined.kt +++ b/kotlinx-coroutines-core/common/src/Unconfined.kt @@ -26,7 +26,7 @@ internal object Unconfined : CoroutineDispatcher() { "isDispatchNeeded and dispatch calls.") } - override fun toString(): String = "Unconfined" + override fun toString(): String = "Dispatchers.Unconfined" } /** diff --git a/kotlinx-coroutines-core/common/src/Yield.kt b/kotlinx-coroutines-core/common/src/Yield.kt index e0af04ddb7..0d8bd3bc2f 100644 --- a/kotlinx-coroutines-core/common/src/Yield.kt +++ b/kotlinx-coroutines-core/common/src/Yield.kt @@ -4,6 +4,7 @@ package kotlinx.coroutines +import kotlinx.coroutines.internal.* import kotlin.coroutines.* import kotlin.coroutines.intrinsics.* @@ -13,6 +14,8 @@ import kotlin.coroutines.intrinsics.* * This suspending function is cancellable. * If the [Job] of the current coroutine is cancelled or completed when this suspending function is invoked or while * this function is waiting for dispatch, it resumes with a [CancellationException]. + * There is a **prompt cancellation guarantee**. If the job was cancelled while this function was + * suspended, it will not resume successfully. See [suspendCancellableCoroutine] documentation for low-level details. * * **Note**: This function always [checks for cancellation][ensureActive] even when it does not suspend. * diff --git a/kotlinx-coroutines-core/common/src/channels/AbstractChannel.kt b/kotlinx-coroutines-core/common/src/channels/AbstractChannel.kt index 8d078e49ca..8edd2b310c 100644 --- a/kotlinx-coroutines-core/common/src/channels/AbstractChannel.kt +++ b/kotlinx-coroutines-core/common/src/channels/AbstractChannel.kt @@ -16,7 +16,9 @@ import kotlin.native.concurrent.* /** * Abstract send channel. It is a base class for all send channel implementations. */ -internal abstract class AbstractSendChannel : SendChannel { +internal abstract class AbstractSendChannel( + @JvmField protected val onUndeliveredElement: OnUndeliveredElement? +) : SendChannel { /** @suppress **This is unstable API and it is subject to change.** */ protected val queue = LockFreeLinkedListHead() @@ -122,22 +124,6 @@ internal abstract class AbstractSendChannel : SendChannel { } } - /** - * @suppress **This is unstable API and it is subject to change.** - */ - protected fun describeSendConflated(element: E): AddLastDesc<*> = SendConflatedDesc(queue, element) - - private class SendConflatedDesc( - queue: LockFreeLinkedListHead, - element: E - ) : SendBufferedDesc(queue, element) { - override fun finishOnSuccess(affected: LockFreeLinkedListNode, next: LockFreeLinkedListNode) { - super.finishOnSuccess(affected, next) - // remove previous SendBuffered - (affected as? SendBuffered<*>)?.remove() - } - } - // ------ SendChannel ------ public final override val isClosedForSend: Boolean get() = closedForSend != null @@ -151,14 +137,6 @@ internal abstract class AbstractSendChannel : SendChannel { return sendSuspend(element) } - internal suspend fun sendFair(element: E) { - if (offerInternal(element) === OFFER_SUCCESS) { - yield() // Works only on fast path to properly work in sequential use-cases - return - } - return sendSuspend(element) - } - public final override fun offer(element: E): Boolean { val result = offerInternal(element) return when { @@ -167,24 +145,34 @@ internal abstract class AbstractSendChannel : SendChannel { // We should check for closed token on offer as well, otherwise offer won't be linearizable // in the face of concurrent close() // See https://github.com/Kotlin/kotlinx.coroutines/issues/359 - throw recoverStackTrace(helpCloseAndGetSendException(closedForSend ?: return false)) + throw recoverStackTrace(helpCloseAndGetSendException(element, closedForSend ?: return false)) + } + result is Closed<*> -> { + throw recoverStackTrace(helpCloseAndGetSendException(element, result)) } - result is Closed<*> -> throw recoverStackTrace(helpCloseAndGetSendException(result)) else -> error("offerInternal returned $result") } } - private fun helpCloseAndGetSendException(closed: Closed<*>): Throwable { + private fun helpCloseAndGetSendException(element: E, closed: Closed<*>): Throwable { // To ensure linearizablity we must ALWAYS help close the channel when we observe that it was closed // See https://github.com/Kotlin/kotlinx.coroutines/issues/1419 helpClose(closed) + // Element was not delivered -> cals onUndeliveredElement + onUndeliveredElement?.callUndeliveredElementCatchingException(element)?.let { + // If it crashes, add send exception as suppressed for better diagnostics + it.addSuppressed(closed.sendException) + throw it + } return closed.sendException } - private suspend fun sendSuspend(element: E): Unit = suspendAtomicCancellableCoroutineReusable sc@ { cont -> + private suspend fun sendSuspend(element: E): Unit = suspendCancellableCoroutineReusable sc@ { cont -> loop@ while (true) { if (isFullImpl) { - val send = SendElement(element, cont) + val send = if (onUndeliveredElement == null) + SendElement(element, cont) else + SendElementWithUndeliveredHandler(element, cont, onUndeliveredElement) val enqueueResult = enqueueSend(send) when { enqueueResult == null -> { // enqueued successfully @@ -192,7 +180,7 @@ internal abstract class AbstractSendChannel : SendChannel { return@sc } enqueueResult is Closed<*> -> { - cont.helpCloseAndResumeWithSendException(enqueueResult) + cont.helpCloseAndResumeWithSendException(element, enqueueResult) return@sc } enqueueResult === ENQUEUE_FAILED -> {} // try to offer instead @@ -209,7 +197,7 @@ internal abstract class AbstractSendChannel : SendChannel { } offerResult === OFFER_FAILED -> continue@loop offerResult is Closed<*> -> { - cont.helpCloseAndResumeWithSendException(offerResult) + cont.helpCloseAndResumeWithSendException(element, offerResult) return@sc } else -> error("offerInternal returned $offerResult") @@ -217,9 +205,15 @@ internal abstract class AbstractSendChannel : SendChannel { } } - private fun Continuation<*>.helpCloseAndResumeWithSendException(closed: Closed<*>) { + private fun Continuation<*>.helpCloseAndResumeWithSendException(element: E, closed: Closed<*>) { helpClose(closed) - resumeWithException(closed.sendException) + val sendException = closed.sendException + onUndeliveredElement?.callUndeliveredElementCatchingException(element)?.let { + it.addSuppressed(sendException) + resumeWithException(it) + return + } + resumeWithException(sendException) } /** @@ -391,7 +385,7 @@ internal abstract class AbstractSendChannel : SendChannel { select.disposeOnSelect(node) return } - enqueueResult is Closed<*> -> throw recoverStackTrace(helpCloseAndGetSendException(enqueueResult)) + enqueueResult is Closed<*> -> throw recoverStackTrace(helpCloseAndGetSendException(element, enqueueResult)) enqueueResult === ENQUEUE_FAILED -> {} // try to offer enqueueResult is Receive<*> -> {} // try to offer else -> error("enqueueSend returned $enqueueResult ") @@ -407,7 +401,7 @@ internal abstract class AbstractSendChannel : SendChannel { block.startCoroutineUnintercepted(receiver = this, completion = select.completion) return } - offerResult is Closed<*> -> throw recoverStackTrace(helpCloseAndGetSendException(offerResult)) + offerResult is Closed<*> -> throw recoverStackTrace(helpCloseAndGetSendException(element, offerResult)) else -> error("offerSelectInternal returned $offerResult") } } @@ -447,7 +441,7 @@ internal abstract class AbstractSendChannel : SendChannel { // ------ private ------ private class SendSelect( - override val pollResult: Any?, + override val pollResult: E, // E | Closed - the result pollInternal returns when it rendezvous with this node @JvmField val channel: AbstractSendChannel, @JvmField val select: SelectInstance, @JvmField val block: suspend (SendChannel) -> R @@ -456,11 +450,13 @@ internal abstract class AbstractSendChannel : SendChannel { select.trySelectOther(otherOp) as Symbol? // must return symbol override fun completeResumeSend() { - block.startCoroutine(receiver = channel, completion = select.completion) + block.startCoroutineCancellable(receiver = channel, completion = select.completion) } override fun dispose() { // invoked on select completion - remove() + if (!remove()) return + // if the node was successfully removed (meaning it was added but was not received) then element not delivered + undeliveredElement() } override fun resumeSendClosed(closed: Closed<*>) { @@ -468,6 +464,10 @@ internal abstract class AbstractSendChannel : SendChannel { select.resumeSelectWithException(closed.sendException) } + override fun undeliveredElement() { + channel.onUndeliveredElement?.callUndeliveredElement(pollResult, select.completion.context) + } + override fun toString(): String = "SendSelect@$hexAddress($pollResult)[$channel, $select]" } @@ -485,7 +485,9 @@ internal abstract class AbstractSendChannel : SendChannel { /** * Abstract send/receive channel. It is a base class for all channel implementations. */ -internal abstract class AbstractChannel : AbstractSendChannel(), Channel { +internal abstract class AbstractChannel( + onUndeliveredElement: OnUndeliveredElement? +) : AbstractSendChannel(onUndeliveredElement), Channel { // ------ extension points for buffered channels ------ /** @@ -517,6 +519,8 @@ internal abstract class AbstractChannel : AbstractSendChannel(), Channel : AbstractSendChannel(), Channel receiveSuspend(receiveMode: Int): R = suspendAtomicCancellableCoroutineReusable sc@ { cont -> - val receive = ReceiveElement(cont as CancellableContinuation, receiveMode) + private suspend fun receiveSuspend(receiveMode: Int): R = suspendCancellableCoroutineReusable sc@ { cont -> + val receive = if (onUndeliveredElement == null) + ReceiveElement(cont as CancellableContinuation, receiveMode) else + ReceiveElementWithUndeliveredHandler(cont as CancellableContinuation, receiveMode, onUndeliveredElement) while (true) { if (enqueueReceive(receive)) { removeReceiveOnCancel(cont, receive) @@ -577,7 +583,7 @@ internal abstract class AbstractChannel : AbstractSendChannel(), Channel : AbstractSendChannel(), Channel @@ -801,7 +812,7 @@ internal abstract class AbstractChannel : AbstractSendChannel(), Channel, receive: Receive<*>) = cont.invokeOnCancellation(handler = RemoveReceiveOnCancel(receive).asHandler) - private inner class RemoveReceiveOnCancel(private val receive: Receive<*>) : CancelHandler() { + private inner class RemoveReceiveOnCancel(private val receive: Receive<*>) : BeforeResumeCancelHandler() { override fun invoke(cause: Throwable?) { if (receive.remove()) onReceiveDequeued() @@ -809,7 +820,7 @@ internal abstract class AbstractChannel : AbstractSendChannel(), Channel(val channel: AbstractChannel) : ChannelIterator { + private class Itr(@JvmField val channel: AbstractChannel) : ChannelIterator { var result: Any? = POLL_FAILED // E | POLL_FAILED | Closed override suspend fun hasNext(): Boolean { @@ -830,7 +841,7 @@ internal abstract class AbstractChannel : AbstractSendChannel(), Channel + private suspend fun hasNextSuspend(): Boolean = suspendCancellableCoroutineReusable sc@ { cont -> val receive = ReceiveHasNext(this, cont) while (true) { if (channel.enqueueReceive(receive)) { @@ -848,7 +859,8 @@ internal abstract class AbstractChannel : AbstractSendChannel(), Channel : AbstractSendChannel(), Channel( + private open class ReceiveElement( @JvmField val cont: CancellableContinuation, @JvmField val receiveMode: Int ) : Receive() { @@ -876,9 +888,8 @@ internal abstract class AbstractChannel : AbstractSendChannel(), Channel value } - @Suppress("IMPLICIT_CAST_TO_ANY") override fun tryResumeReceive(value: E, otherOp: PrepareOp?): Symbol? { - val token = cont.tryResume(resumeValue(value), otherOp?.desc) ?: return null + val token = cont.tryResume(resumeValue(value), otherOp?.desc, resumeOnCancellationFun(value)) ?: return null assert { token === RESUME_TOKEN } // the only other possible result // We can call finishPrepare only after successful tryResume, so that only good affected node is saved otherOp?.finishPrepare() @@ -897,12 +908,22 @@ internal abstract class AbstractChannel : AbstractSendChannel(), Channel( + private class ReceiveElementWithUndeliveredHandler( + cont: CancellableContinuation, + receiveMode: Int, + @JvmField val onUndeliveredElement: OnUndeliveredElement + ) : ReceiveElement(cont, receiveMode) { + override fun resumeOnCancellationFun(value: E): ((Throwable) -> Unit)? = + onUndeliveredElement.bindCancellationFun(value, cont.context) + } + + private open class ReceiveHasNext( @JvmField val iterator: Itr, @JvmField val cont: CancellableContinuation ) : Receive() { override fun tryResumeReceive(value: E, otherOp: PrepareOp?): Symbol? { - val token = cont.tryResume(true, otherOp?.desc) ?: return null + val token = cont.tryResume(true, otherOp?.desc, resumeOnCancellationFun(value)) + ?: return null assert { token === RESUME_TOKEN } // the only other possible result // We can call finishPrepare only after successful tryResume, so that only good affected node is saved otherOp?.finishPrepare() @@ -922,13 +943,17 @@ internal abstract class AbstractChannel : AbstractSendChannel(), Channel Unit)? = + iterator.channel.onUndeliveredElement?.bindCancellationFun(value, cont.context) + override fun toString(): String = "ReceiveHasNext@$hexAddress" } @@ -943,16 +968,20 @@ internal abstract class AbstractChannel : AbstractSendChannel(), Channel) { if (!select.trySelect()) return when (receiveMode) { RECEIVE_THROWS_ON_CLOSE -> select.resumeSelectWithException(closed.receiveException) - RECEIVE_RESULT -> block.startCoroutine(ValueOrClosed.closed(closed.closeCause), select.completion) + RECEIVE_RESULT -> block.startCoroutineCancellable(ValueOrClosed.closed(closed.closeCause), select.completion) RECEIVE_NULL_ON_CLOSE -> if (closed.closeCause == null) { - block.startCoroutine(null, select.completion) + block.startCoroutineCancellable(null, select.completion) } else { select.resumeSelectWithException(closed.receiveException) } @@ -964,6 +993,9 @@ internal abstract class AbstractChannel : AbstractSendChannel(), Channel Unit)? = + channel.onUndeliveredElement?.bindCancellationFun(value, select.completion.context) + override fun toString(): String = "ReceiveSelect@$hexAddress[$select,receiveMode=$receiveMode]" } } @@ -975,23 +1007,27 @@ internal const val RECEIVE_RESULT = 2 @JvmField @SharedImmutable -internal val OFFER_SUCCESS: Any = Symbol("OFFER_SUCCESS") +internal val EMPTY = Symbol("EMPTY") // marker for Conflated & Buffered channels @JvmField @SharedImmutable -internal val OFFER_FAILED: Any = Symbol("OFFER_FAILED") +internal val OFFER_SUCCESS = Symbol("OFFER_SUCCESS") @JvmField @SharedImmutable -internal val POLL_FAILED: Any = Symbol("POLL_FAILED") +internal val OFFER_FAILED = Symbol("OFFER_FAILED") @JvmField @SharedImmutable -internal val ENQUEUE_FAILED: Any = Symbol("ENQUEUE_FAILED") +internal val POLL_FAILED = Symbol("POLL_FAILED") @JvmField @SharedImmutable -internal val HANDLER_INVOKED: Any = Symbol("ON_CLOSE_HANDLER_INVOKED") +internal val ENQUEUE_FAILED = Symbol("ENQUEUE_FAILED") + +@JvmField +@SharedImmutable +internal val HANDLER_INVOKED = Symbol("ON_CLOSE_HANDLER_INVOKED") internal typealias Handler = (Throwable?) -> Unit @@ -999,7 +1035,7 @@ internal typealias Handler = (Throwable?) -> Unit * Represents sending waiter in the queue. */ internal abstract class Send : LockFreeLinkedListNode() { - abstract val pollResult: Any? // E | Closed + abstract val pollResult: Any? // E | Closed - the result pollInternal returns when it rendezvous with this node // Returns: null - failure, // RETRY_ATOMIC for retry (only when otherOp != null), // RESUME_TOKEN on success (call completeResumeSend) @@ -1007,6 +1043,7 @@ internal abstract class Send : LockFreeLinkedListNode() { abstract fun tryResumeSend(otherOp: PrepareOp?): Symbol? abstract fun completeResumeSend() abstract fun resumeSendClosed(closed: Closed<*>) + open fun undeliveredElement() {} } /** @@ -1025,9 +1062,8 @@ internal interface ReceiveOrClosed { /** * Represents sender for a specific element. */ -@Suppress("UNCHECKED_CAST") -internal class SendElement( - override val pollResult: Any?, +internal open class SendElement( + override val pollResult: E, @JvmField val cont: CancellableContinuation ) : Send() { override fun tryResumeSend(otherOp: PrepareOp?): Symbol? { @@ -1037,9 +1073,27 @@ internal class SendElement( otherOp?.finishPrepare() // finish preparations return RESUME_TOKEN } + override fun completeResumeSend() = cont.completeResume(RESUME_TOKEN) override fun resumeSendClosed(closed: Closed<*>) = cont.resumeWithException(closed.sendException) - override fun toString(): String = "SendElement@$hexAddress($pollResult)" + override fun toString(): String = "$classSimpleName@$hexAddress($pollResult)" +} + +internal class SendElementWithUndeliveredHandler( + pollResult: E, + cont: CancellableContinuation, + @JvmField val onUndeliveredElement: OnUndeliveredElement +) : SendElement(pollResult, cont) { + override fun remove(): Boolean { + if (!super.remove()) return false + // if the node was successfully removed (meaning it was added but was not received) then we have undelivered element + undeliveredElement() + return true + } + + override fun undeliveredElement() { + onUndeliveredElement.callUndeliveredElement(pollResult, cont.context) + } } /** @@ -1064,6 +1118,7 @@ internal class Closed( internal abstract class Receive : LockFreeLinkedListNode(), ReceiveOrClosed { override val offerResult get() = OFFER_SUCCESS abstract fun resumeReceiveClosed(closed: Closed<*>) + open fun resumeOnCancellationFun(value: E): ((Throwable) -> Unit)? = null } @Suppress("NOTHING_TO_INLINE", "UNCHECKED_CAST") diff --git a/kotlinx-coroutines-core/common/src/channels/ArrayBroadcastChannel.kt b/kotlinx-coroutines-core/common/src/channels/ArrayBroadcastChannel.kt index 19334ea706..91b5473c41 100644 --- a/kotlinx-coroutines-core/common/src/channels/ArrayBroadcastChannel.kt +++ b/kotlinx-coroutines-core/common/src/channels/ArrayBroadcastChannel.kt @@ -28,7 +28,7 @@ internal class ArrayBroadcastChannel( * Buffer capacity. */ val capacity: Int -) : AbstractSendChannel(), BroadcastChannel { +) : AbstractSendChannel(null), BroadcastChannel { init { require(capacity >= 1) { "ArrayBroadcastChannel capacity must be at least 1, but $capacity was specified" } } @@ -180,6 +180,8 @@ internal class ArrayBroadcastChannel( this.tail = tail + 1 return@withLock // go out of lock to wakeup this sender } + // Too late, already cancelled, but we removed it from the queue and need to release resources. + // However, ArrayBroadcastChannel does not support onUndeliveredElement, so nothing to do } } } @@ -205,7 +207,7 @@ internal class ArrayBroadcastChannel( private class Subscriber( private val broadcastChannel: ArrayBroadcastChannel - ) : AbstractChannel(), ReceiveChannel { + ) : AbstractChannel(null), ReceiveChannel { private val subLock = ReentrantLock() private val _subHead = atomic(0L) @@ -218,13 +220,15 @@ internal class ArrayBroadcastChannel( override val isBufferAlwaysFull: Boolean get() = error("Should not be used") override val isBufferFull: Boolean get() = error("Should not be used") - override fun onCancelIdempotent(wasClosed: Boolean) { + override fun close(cause: Throwable?): Boolean { + val wasClosed = super.close(cause) if (wasClosed) { broadcastChannel.updateHead(removeSub = this) subLock.withLock { subHead = broadcastChannel.tail } } + return wasClosed } // returns true if subHead was updated and broadcast channel's head must be checked diff --git a/kotlinx-coroutines-core/common/src/channels/ArrayChannel.kt b/kotlinx-coroutines-core/common/src/channels/ArrayChannel.kt index e26579eff7..80cb8aa011 100644 --- a/kotlinx-coroutines-core/common/src/channels/ArrayChannel.kt +++ b/kotlinx-coroutines-core/common/src/channels/ArrayChannel.kt @@ -23,25 +23,31 @@ internal open class ArrayChannel( /** * Buffer capacity. */ - val capacity: Int -) : AbstractChannel() { + private val capacity: Int, + private val onBufferOverflow: BufferOverflow, + onUndeliveredElement: OnUndeliveredElement? +) : AbstractChannel(onUndeliveredElement) { init { + // This check is actually used by the Channel(...) constructor function which checks only for known + // capacities and calls ArrayChannel constructor for everything else. require(capacity >= 1) { "ArrayChannel capacity must be at least 1, but $capacity was specified" } } private val lock = ReentrantLock() + /* * Guarded by lock. * Allocate minimum of capacity and 16 to avoid excess memory pressure for large channels when it's not necessary. */ - private var buffer: Array = arrayOfNulls(min(capacity, 8)) + private var buffer: Array = arrayOfNulls(min(capacity, 8)).apply { fill(EMPTY) } + private var head: Int = 0 private val size = atomic(0) // Invariant: size <= capacity protected final override val isBufferAlwaysEmpty: Boolean get() = false protected final override val isBufferEmpty: Boolean get() = size.value == 0 protected final override val isBufferAlwaysFull: Boolean get() = false - protected final override val isBufferFull: Boolean get() = size.value == capacity + protected final override val isBufferFull: Boolean get() = size.value == capacity && onBufferOverflow == BufferOverflow.SUSPEND override val isFull: Boolean get() = lock.withLock { isFullImpl } override val isEmpty: Boolean get() = lock.withLock { isEmptyImpl } @@ -53,31 +59,26 @@ internal open class ArrayChannel( lock.withLock { val size = this.size.value closedForSend?.let { return it } - if (size < capacity) { - // tentatively put element to buffer - this.size.value = size + 1 // update size before checking queue (!!!) - // check for receivers that were waiting on empty queue - if (size == 0) { - loop@ while (true) { - receive = takeFirstReceiveOrPeekClosed() ?: break@loop // break when no receivers queued - if (receive is Closed) { - this.size.value = size // restore size - return receive!! - } - val token = receive!!.tryResumeReceive(element, null) - if (token != null) { - assert { token === RESUME_TOKEN } - this.size.value = size // restore size - return@withLock - } + // update size before checking queue (!!!) + updateBufferSize(size)?.let { return it } + // check for receivers that were waiting on empty queue + if (size == 0) { + loop@ while (true) { + receive = takeFirstReceiveOrPeekClosed() ?: break@loop // break when no receivers queued + if (receive is Closed) { + this.size.value = size // restore size + return receive!! + } + val token = receive!!.tryResumeReceive(element, null) + if (token != null) { + assert { token === RESUME_TOKEN } + this.size.value = size // restore size + return@withLock } } - ensureCapacity(size) - buffer[(head + size) % buffer.size] = element // actually queue element - return OFFER_SUCCESS } - // size == capacity: full - return OFFER_FAILED + enqueueElement(size, element) + return OFFER_SUCCESS } // breaks here if offer meets receiver receive!!.completeResumeReceive(element) @@ -90,41 +91,36 @@ internal open class ArrayChannel( lock.withLock { val size = this.size.value closedForSend?.let { return it } - if (size < capacity) { - // tentatively put element to buffer - this.size.value = size + 1 // update size before checking queue (!!!) - // check for receivers that were waiting on empty queue - if (size == 0) { - loop@ while (true) { - val offerOp = describeTryOffer(element) - val failure = select.performAtomicTrySelect(offerOp) - when { - failure == null -> { // offered successfully - this.size.value = size // restore size - receive = offerOp.result - return@withLock - } - failure === OFFER_FAILED -> break@loop // cannot offer -> Ok to queue to buffer - failure === RETRY_ATOMIC -> {} // retry - failure === ALREADY_SELECTED || failure is Closed<*> -> { - this.size.value = size // restore size - return failure - } - else -> error("performAtomicTrySelect(describeTryOffer) returned $failure") + // update size before checking queue (!!!) + updateBufferSize(size)?.let { return it } + // check for receivers that were waiting on empty queue + if (size == 0) { + loop@ while (true) { + val offerOp = describeTryOffer(element) + val failure = select.performAtomicTrySelect(offerOp) + when { + failure == null -> { // offered successfully + this.size.value = size // restore size + receive = offerOp.result + return@withLock + } + failure === OFFER_FAILED -> break@loop // cannot offer -> Ok to queue to buffer + failure === RETRY_ATOMIC -> {} // retry + failure === ALREADY_SELECTED || failure is Closed<*> -> { + this.size.value = size // restore size + return failure } + else -> error("performAtomicTrySelect(describeTryOffer) returned $failure") } } - // let's try to select sending this element to buffer - if (!select.trySelect()) { // :todo: move trySelect completion outside of lock - this.size.value = size // restore size - return ALREADY_SELECTED - } - ensureCapacity(size) - buffer[(head + size) % buffer.size] = element // actually queue element - return OFFER_SUCCESS } - // size == capacity: full - return OFFER_FAILED + // let's try to select sending this element to buffer + if (!select.trySelect()) { // :todo: move trySelect completion outside of lock + this.size.value = size // restore size + return ALREADY_SELECTED + } + enqueueElement(size, element) + return OFFER_SUCCESS } // breaks here if offer meets receiver receive!!.completeResumeReceive(element) @@ -135,6 +131,35 @@ internal open class ArrayChannel( super.enqueueSend(send) } + // Guarded by lock + // Result is `OFFER_SUCCESS | OFFER_FAILED | null` + private fun updateBufferSize(currentSize: Int): Symbol? { + if (currentSize < capacity) { + size.value = currentSize + 1 // tentatively put it into the buffer + return null // proceed + } + // buffer is full + return when (onBufferOverflow) { + BufferOverflow.SUSPEND -> OFFER_FAILED + BufferOverflow.DROP_LATEST -> OFFER_SUCCESS + BufferOverflow.DROP_OLDEST -> null // proceed, will drop oldest in enqueueElement + } + } + + // Guarded by lock + private fun enqueueElement(currentSize: Int, element: E) { + if (currentSize < capacity) { + ensureCapacity(currentSize) + buffer[(head + currentSize) % buffer.size] = element // actually queue element + } else { + // buffer is full + assert { onBufferOverflow == BufferOverflow.DROP_OLDEST } // the only way we can get here + buffer[head % buffer.size] = null // drop oldest element + buffer[(head + currentSize) % buffer.size] = element // actually queue element + head = (head + 1) % buffer.size + } + } + // Guarded by lock private fun ensureCapacity(currentSize: Int) { if (currentSize >= buffer.size) { @@ -143,6 +168,7 @@ internal open class ArrayChannel( for (i in 0 until currentSize) { newBuffer[i] = buffer[(head + i) % buffer.size] } + newBuffer.fill(EMPTY, currentSize, newSize) buffer = newBuffer head = 0 } @@ -172,6 +198,8 @@ internal open class ArrayChannel( replacement = send!!.pollResult break@loop } + // too late, already cancelled, but we removed it from the queue and need to notify on undelivered element + send!!.undeliveredElement() } } if (replacement !== POLL_FAILED && replacement !is Closed<*>) { @@ -254,17 +282,23 @@ internal open class ArrayChannel( // Note: this function is invoked when channel is already closed override fun onCancelIdempotent(wasClosed: Boolean) { // clear buffer first, but do not wait for it in helpers - if (wasClosed) { - lock.withLock { - repeat(size.value) { - buffer[head] = 0 - head = (head + 1) % buffer.size + val onUndeliveredElement = onUndeliveredElement + var undeliveredElementException: UndeliveredElementException? = null // first cancel exception, others suppressed + lock.withLock { + repeat(size.value) { + val value = buffer[head] + if (onUndeliveredElement != null && value !== EMPTY) { + @Suppress("UNCHECKED_CAST") + undeliveredElementException = onUndeliveredElement.callUndeliveredElementCatchingException(value as E, undeliveredElementException) } - size.value = 0 + buffer[head] = EMPTY + head = (head + 1) % buffer.size } + size.value = 0 } // then clean all queued senders super.onCancelIdempotent(wasClosed) + undeliveredElementException?.let { throw it } // throw cancel exception at the end if there was one } // ------ debug ------ diff --git a/kotlinx-coroutines-core/common/src/channels/Broadcast.kt b/kotlinx-coroutines-core/common/src/channels/Broadcast.kt index e2891c9500..0193ed06b2 100644 --- a/kotlinx-coroutines-core/common/src/channels/Broadcast.kt +++ b/kotlinx-coroutines-core/common/src/channels/Broadcast.kt @@ -10,7 +10,6 @@ import kotlinx.coroutines.channels.Channel.Factory.UNLIMITED import kotlinx.coroutines.intrinsics.* import kotlin.coroutines.* import kotlin.coroutines.intrinsics.* -import kotlin.native.concurrent.* /** * Broadcasts all elements of the channel. @@ -34,19 +33,21 @@ import kotlin.native.concurrent.* * * This function has an inappropriate result type of [BroadcastChannel] which provides * [send][BroadcastChannel.send] and [close][BroadcastChannel.close] operations that interfere with - * the broadcasting coroutine in hard-to-specify ways. It will be replaced with - * sharing operators on [Flow][kotlinx.coroutines.flow.Flow] in the future. + * the broadcasting coroutine in hard-to-specify ways. + * + * **Note: This API is obsolete.** It will be deprecated and replaced with the + * [Flow.shareIn][kotlinx.coroutines.flow.shareIn] operator when it becomes stable. * * @param start coroutine start option. The default value is [CoroutineStart.LAZY]. */ -fun ReceiveChannel.broadcast( +public fun ReceiveChannel.broadcast( capacity: Int = 1, start: CoroutineStart = CoroutineStart.LAZY ): BroadcastChannel { val scope = GlobalScope + Dispatchers.Unconfined + CoroutineExceptionHandler { _, _ -> } // We can run this coroutine in the context that ignores all exceptions, because of `onCompletion = consume()` // which passes all exceptions upstream to the source ReceiveChannel - return scope.broadcast(capacity = capacity, start = start, onCompletion = consumes()) { + return scope.broadcast(capacity = capacity, start = start, onCompletion = { cancelConsumed(it) }) { for (e in this@broadcast) { send(e) } diff --git a/kotlinx-coroutines-core/common/src/channels/BroadcastChannel.kt b/kotlinx-coroutines-core/common/src/channels/BroadcastChannel.kt index 312480f943..d356566f17 100644 --- a/kotlinx-coroutines-core/common/src/channels/BroadcastChannel.kt +++ b/kotlinx-coroutines-core/common/src/channels/BroadcastChannel.kt @@ -7,9 +7,9 @@ package kotlinx.coroutines.channels import kotlinx.coroutines.* -import kotlinx.coroutines.channels.Channel.Factory.CONFLATED import kotlinx.coroutines.channels.Channel.Factory.BUFFERED import kotlinx.coroutines.channels.Channel.Factory.CHANNEL_DEFAULT_CAPACITY +import kotlinx.coroutines.channels.Channel.Factory.CONFLATED import kotlinx.coroutines.channels.Channel.Factory.UNLIMITED /** @@ -20,9 +20,10 @@ import kotlinx.coroutines.channels.Channel.Factory.UNLIMITED * See `BroadcastChannel()` factory function for the description of available * broadcast channel implementations. * - * **Note: This is an experimental api.** It may be changed in the future updates. + * **Note: This API is obsolete.** It will be deprecated and replaced by [SharedFlow][kotlinx.coroutines.flow.SharedFlow] + * when it becomes stable. */ -@ExperimentalCoroutinesApi +@ExperimentalCoroutinesApi // not @ObsoleteCoroutinesApi to reduce burden for people who are still using it public interface BroadcastChannel : SendChannel { /** * Subscribes to this [BroadcastChannel] and returns a channel to receive elements from it. diff --git a/kotlinx-coroutines-core/common/src/channels/BufferOverflow.kt b/kotlinx-coroutines-core/common/src/channels/BufferOverflow.kt new file mode 100644 index 0000000000..a89c633fe6 --- /dev/null +++ b/kotlinx-coroutines-core/common/src/channels/BufferOverflow.kt @@ -0,0 +1,35 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.channels + +import kotlinx.coroutines.* + +/** + * A strategy for buffer overflow handling in [channels][Channel] and [flows][kotlinx.coroutines.flow.Flow] that + * controls what is going to be sacrificed on buffer overflow: + * + * * [SUSPEND] — the upstream that is [sending][SendChannel.send] or + * is [emitting][kotlinx.coroutines.flow.FlowCollector.emit] a value is **suspended** while the buffer is full. + * * [DROP_OLDEST] — drop **the oldest** value in the buffer on overflow, add the new value to the buffer, do not suspend. + * * [DROP_LATEST] — drop **the latest** value that is being added to the buffer right now on buffer overflow + * (so that buffer contents stay the same), do not suspend. + */ +public enum class BufferOverflow { + /** + * Suspend on buffer overflow. + */ + SUSPEND, + + /** + * Drop **the oldest** value in the buffer on overflow, add the new value to the buffer, do not suspend. + */ + DROP_OLDEST, + + /** + * Drop **the latest** value that is being added to the buffer right now on buffer overflow + * (so that buffer contents stay the same), do not suspend. + */ + DROP_LATEST +} diff --git a/kotlinx-coroutines-core/common/src/channels/Channel.kt b/kotlinx-coroutines-core/common/src/channels/Channel.kt index 8dff4ec2b7..72c08e1acd 100644 --- a/kotlinx-coroutines-core/common/src/channels/Channel.kt +++ b/kotlinx-coroutines-core/common/src/channels/Channel.kt @@ -7,15 +7,15 @@ package kotlinx.coroutines.channels import kotlinx.coroutines.* +import kotlinx.coroutines.channels.Channel.Factory.BUFFERED +import kotlinx.coroutines.channels.Channel.Factory.CHANNEL_DEFAULT_CAPACITY import kotlinx.coroutines.channels.Channel.Factory.CONFLATED import kotlinx.coroutines.channels.Channel.Factory.RENDEZVOUS import kotlinx.coroutines.channels.Channel.Factory.UNLIMITED -import kotlinx.coroutines.channels.Channel.Factory.BUFFERED -import kotlinx.coroutines.channels.Channel.Factory.CHANNEL_DEFAULT_CAPACITY -import kotlinx.coroutines.internal.systemProp +import kotlinx.coroutines.internal.* import kotlinx.coroutines.selects.* -import kotlin.jvm.* import kotlin.internal.* +import kotlin.jvm.* /** * Sender's interface to [Channel]. @@ -44,19 +44,17 @@ public interface SendChannel { * Sends the specified [element] to this channel, suspending the caller while the buffer of this channel is full * or if it does not exist, or throws an exception if the channel [is closed for `send`][isClosedForSend] (see [close] for details). * - * Note that closing a channel _after_ this function has suspended does not cause this suspended [send] invocation + * [Closing][close] a channel _after_ this function has suspended does not cause this suspended [send] invocation * to abort, because closing a channel is conceptually like sending a special "close token" over this channel. * All elements sent over the channel are delivered in first-in first-out order. The sent element * will be delivered to receivers before the close token. * * This suspending function is cancellable. If the [Job] of the current coroutine is cancelled or completed while this * function is suspended, this function immediately resumes with a [CancellationException]. - * - * *Cancellation of suspended `send` is atomic*: when this function - * throws a [CancellationException], it means that the [element] was not sent to this channel. - * As a side-effect of atomic cancellation, a thread-bound coroutine (to some UI thread, for example) may - * continue to execute even after it was cancelled from the same thread in the case when this `send` operation - * was already resumed and the continuation was posted for execution to the thread's queue. + * There is a **prompt cancellation guarantee**. If the job was cancelled while this function was + * suspended, it will not resume successfully. The `send` call can send the element to the channel, + * but then throw [CancellationException], thus an exception should not be treated as a failure to deliver the element. + * See "Undelivered elements" section in [Channel] documentation for details on handling undelivered elements. * * Note that this function does not check for cancellation when it is not suspended. * Use [yield] or [CoroutineScope.isActive] to periodically check for cancellation in tight loops if needed. @@ -81,6 +79,11 @@ public interface SendChannel { * in situations when `send` suspends. * * Throws an exception if the channel [is closed for `send`][isClosedForSend] (see [close] for details). + * + * When `offer` call returns `false` it guarantees that the element was not delivered to the consumer and it + * it does not call `onUndeliveredElement` that was installed for this channel. If the channel was closed, + * then it calls `onUndeliveredElement` before throwing an exception. + * See "Undelivered elements" section in [Channel] documentation for details on handling undelivered elements. */ public fun offer(element: E): Boolean @@ -170,12 +173,10 @@ public interface ReceiveChannel { * * This suspending function is cancellable. If the [Job] of the current coroutine is cancelled or completed while this * function is suspended, this function immediately resumes with a [CancellationException]. - * - * *Cancellation of suspended `receive` is atomic*: when this function - * throws a [CancellationException], it means that the element was not retrieved from this channel. - * As a side-effect of atomic cancellation, a thread-bound coroutine (to some UI thread, for example) may - * continue to execute even after it was cancelled from the same thread in the case when this `receive` operation - * was already resumed and the continuation was posted for execution to the thread's queue. + * There is a **prompt cancellation guarantee**. If the job was cancelled while this function was + * suspended, it will not resume successfully. The `receive` call can retrieve the element from the channel, + * but then throw [CancellationException], thus failing to deliver the element. + * See "Undelivered elements" section in [Channel] documentation for details on handling undelivered elements. * * Note that this function does not check for cancellation when it is not suspended. * Use [yield] or [CoroutineScope.isActive] to periodically check for cancellation in tight loops if needed. @@ -200,12 +201,10 @@ public interface ReceiveChannel { * * This suspending function is cancellable. If the [Job] of the current coroutine is cancelled or completed while this * function is suspended, this function immediately resumes with a [CancellationException]. - * - * *Cancellation of suspended `receive` is atomic*: when this function - * throws a [CancellationException], it means that the element was not retrieved from this channel. - * As a side-effect of atomic cancellation, a thread-bound coroutine (to some UI thread, for example) may - * continue to execute even after it was cancelled from the same thread in the case when this `receive` operation - * was already resumed and the continuation was posted for execution to the thread's queue. + * There is a **prompt cancellation guarantee**. If the job was cancelled while this function was + * suspended, it will not resume successfully. The `receiveOrNull` call can retrieve the element from the channel, + * but then throw [CancellationException], thus failing to deliver the element. + * See "Undelivered elements" section in [Channel] documentation for details on handling undelivered elements. * * Note that this function does not check for cancellation when it is not suspended. * Use [yield] or [CoroutineScope.isActive] to periodically check for cancellation in tight loops if needed. @@ -250,12 +249,10 @@ public interface ReceiveChannel { * * This suspending function is cancellable. If the [Job] of the current coroutine is cancelled or completed while this * function is suspended, this function immediately resumes with a [CancellationException]. - * - * *Cancellation of suspended `receive` is atomic*: when this function - * throws a [CancellationException], it means that the element was not retrieved from this channel. - * As a side-effect of atomic cancellation, a thread-bound coroutine (to some UI thread, for example) may - * continue to execute even after it was cancelled from the same thread in the case when this receive operation - * was already resumed and the continuation was posted for execution to the thread's queue. + * There is a **prompt cancellation guarantee**. If the job was cancelled while this function was + * suspended, it will not resume successfully. The `receiveOrClosed` call can retrieve the element from the channel, + * but then throw [CancellationException], thus failing to deliver the element. + * See "Undelivered elements" section in [Channel] documentation for details on handling undelivered elements. * * Note that this function does not check for cancellation when it is not suspended. * Use [yield] or [CoroutineScope.isActive] to periodically check for cancellation in tight loops if needed. @@ -314,7 +311,7 @@ public interface ReceiveChannel { * @suppress This method implements old version of JVM ABI. Use [cancel]. */ @Deprecated(level = DeprecationLevel.HIDDEN, message = "Since 1.2.0, binary compatibility with versions <= 1.1.x") - public fun cancel() = cancel(null) + public fun cancel(): Unit = cancel(null) /** * @suppress This method has bad semantics when cause is not a [CancellationException]. Use [cancel]. @@ -332,7 +329,7 @@ public interface ReceiveChannel { * @suppress *This is an internal API, do not use*: Inline classes ABI is not stable yet and * [KT-27524](https://youtrack.jetbrains.com/issue/KT-27524) needs to be fixed. */ -@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS") +@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS", "EXPERIMENTAL_FEATURE_WARNING") @InternalCoroutinesApi // until https://youtrack.jetbrains.com/issue/KT-27524 is fixed public inline class ValueOrClosed internal constructor(private val holder: Any?) { @@ -439,12 +436,10 @@ public interface ChannelIterator { * * This suspending function is cancellable. If the [Job] of the current coroutine is cancelled or completed while this * function is suspended, this function immediately resumes with a [CancellationException]. - * - * *Cancellation of suspended `receive` is atomic*: when this function - * throws a [CancellationException], it means that the element was not retrieved from this channel. - * As a side-effect of atomic cancellation, a thread-bound coroutine (to some UI thread, for example) may - * continue to execute even after it was cancelled from the same thread in the case when this receive operation - * was already resumed and the continuation was posted for execution to the thread's queue. + * There is a **prompt cancellation guarantee**. If the job was cancelled while this function was + * suspended, it will not resume successfully. The `hasNext` call can retrieve the element from the channel, + * but then throw [CancellationException], thus failing to deliver the element. + * See "Undelivered elements" section in [Channel] documentation for details on handling undelivered elements. * * Note that this function does not check for cancellation when it is not suspended. * Use [yield] or [CoroutineScope.isActive] to periodically check for cancellation in tight loops if needed. @@ -486,28 +481,108 @@ public interface ChannelIterator { * Conceptually, a channel is similar to Java's [BlockingQueue][java.util.concurrent.BlockingQueue], * but it has suspending operations instead of blocking ones and can be [closed][SendChannel.close]. * + * ### Creating channels + * * The `Channel(capacity)` factory function is used to create channels of different kinds depending on * the value of the `capacity` integer: * - * * When `capacity` is 0 — it creates a `RendezvousChannel`. + * * When `capacity` is 0 — it creates a _rendezvous_ channel. * This channel does not have any buffer at all. An element is transferred from the sender * to the receiver only when [send] and [receive] invocations meet in time (rendezvous), so [send] suspends * until another coroutine invokes [receive], and [receive] suspends until another coroutine invokes [send]. * - * * When `capacity` is [Channel.UNLIMITED] — it creates a `LinkedListChannel`. + * * When `capacity` is [Channel.UNLIMITED] — it creates a channel with effectively unlimited buffer. * This channel has a linked-list buffer of unlimited capacity (limited only by available memory). * [Sending][send] to this channel never suspends, and [offer] always returns `true`. * - * * When `capacity` is [Channel.CONFLATED] — it creates a `ConflatedChannel`. + * * When `capacity` is [Channel.CONFLATED] — it creates a _conflated_ channel * This channel buffers at most one element and conflates all subsequent `send` and `offer` invocations, * so that the receiver always gets the last element sent. - * Back-to-send sent elements are _conflated_ — only the last sent element is received, + * Back-to-send sent elements are conflated — only the last sent element is received, * while previously sent elements **are lost**. * [Sending][send] to this channel never suspends, and [offer] always returns `true`. * * * When `capacity` is positive but less than [UNLIMITED] — it creates an array-based channel with the specified capacity. * This channel has an array buffer of a fixed `capacity`. * [Sending][send] suspends only when the buffer is full, and [receiving][receive] suspends only when the buffer is empty. + * + * Buffered channels can be configured with an additional [`onBufferOverflow`][BufferOverflow] parameter. It controls the behaviour + * of the channel's [send][Channel.send] function on buffer overflow: + * + * * [SUSPEND][BufferOverflow.SUSPEND] — the default, suspend `send` on buffer overflow until there is + * free space in the buffer. + * * [DROP_OLDEST][BufferOverflow.DROP_OLDEST] — do not suspend the `send`, add the latest value to the buffer, + * drop the oldest one from the buffer. + * A channel with `capacity = 1` and `onBufferOverflow = DROP_OLDEST` is a _conflated_ channel. + * * [DROP_LATEST][BufferOverflow.DROP_LATEST] — do not suspend the `send`, drop the value that is being sent, + * keep the buffer contents intact. + * + * A non-default `onBufferOverflow` implicitly creates a channel with at least one buffered element and + * is ignored for a channel with unlimited buffer. It cannot be specified for `capacity = CONFLATED`, which + * is a shortcut by itself. + * + * ### Prompt cancellation guarantee + * + * All suspending functions with channels provide **prompt cancellation guarantee**. + * If the job was cancelled while send or receive function was suspended, it will not resume successfully, + * but throws a [CancellationException]. + * With a single-threaded [dispatcher][CoroutineDispatcher] like [Dispatchers.Main] this gives a + * guarantee that if a piece code running in this thread cancels a [Job], then a coroutine running this job cannot + * resume successfully and continue to run, ensuring a prompt response to its cancellation. + * + * > **Prompt cancellation guarantee** for channel operations was added since `kotlinx.coroutines` version `1.4.0` + * > and had replaced a channel-specific atomic-cancellation that was not consistent with other suspending functions. + * > The low-level mechanics of prompt cancellation are explained in [suspendCancellableCoroutine] function. + * + * ### Undelivered elements + * + * As a result of a prompt cancellation guarantee, when a closeable resource + * (like open file or a handle to another native resource) is transferred via channel from one coroutine to another + * it can fail to be delivered and will be lost if either send or receive operations are cancelled in transit. + * + * A `Channel()` constructor function has an `onUndeliveredElement` optional parameter. + * When `onUndeliveredElement` parameter is set, the corresponding function is called once for each element + * that was sent to the channel with the call to the [send][SendChannel.send] function but failed to be delivered, + * which can happen in the following cases: + * + * * When [send][SendChannel.send] operation throws an exception because it was cancelled before it had a chance to actually + * send the element or because the channel was [closed][SendChannel.close] or [cancelled][ReceiveChannel.cancel]. + * * When [offer][SendChannel.offer] operation throws an exception when + * the channel was [closed][SendChannel.close] or [cancelled][ReceiveChannel.cancel]. + * * When [receive][ReceiveChannel.receive], [receiveOrNull][ReceiveChannel.receiveOrNull], or [hasNext][ChannelIterator.hasNext] + * operation throws an exception when it had retrieved the element from the + * channel but was cancelled before the code following the receive call resumed. + * * The channel was [cancelled][ReceiveChannel.cancel], in which case `onUndeliveredElement` is called on every + * remaining element in the channel's buffer. + * + * Note, that `onUndeliveredElement` function is called synchronously in an arbitrary context. It should be fast, non-blocking, + * and should not throw exceptions. Any exception thrown by `onUndeliveredElement` is wrapped into an internal runtime + * exception which is either rethrown from the caller method or handed off to the exception handler in the current context + * (see [CoroutineExceptionHandler]) when one is available. + * + * A typical usage for `onDeliveredElement` is to close a resource that is being transferred via the channel. The + * following code pattern guarantees that opened resources are closed even if producer, consumer, and/or channel + * are cancelled. Resources are never lost. + * + * ``` + * // Create the channel with onUndeliveredElement block that closes a resource + * val channel = Channel(capacity) { resource -> resource.close() } + * + * // Producer code + * val resourceToSend = openResource() + * channel.send(resourceToSend) + * + * // Consumer code + * val resourceReceived = channel.receive() + * try { + * // work with received resource + * } finally { + * resourceReceived.close() + * } + * ``` + * + * > Note, that if you do any kind of work in between `openResource()` and `channel.send(...)`, then you should + * > ensure that resource gets closed in case this additional code fails. */ public interface Channel : SendChannel, ReceiveChannel { /** @@ -515,27 +590,28 @@ public interface Channel : SendChannel, ReceiveChannel { */ public companion object Factory { /** - * Requests a channel with an unlimited capacity buffer in the `Channel(...)` factory function + * Requests a channel with an unlimited capacity buffer in the `Channel(...)` factory function. */ - public const val UNLIMITED = Int.MAX_VALUE + public const val UNLIMITED: Int = Int.MAX_VALUE /** - * Requests a rendezvous channel in the `Channel(...)` factory function — a `RendezvousChannel` gets created. + * Requests a rendezvous channel in the `Channel(...)` factory function — a channel that does not have a buffer. */ - public const val RENDEZVOUS = 0 + public const val RENDEZVOUS: Int = 0 /** - * Requests a conflated channel in the `Channel(...)` factory function — a `ConflatedChannel` gets created. + * Requests a conflated channel in the `Channel(...)` factory function. This is a shortcut to creating + * a channel with [`onBufferOverflow = DROP_OLDEST`][BufferOverflow.DROP_OLDEST]. */ - public const val CONFLATED = -1 + public const val CONFLATED: Int = -1 /** - * Requests a buffered channel with the default buffer capacity in the `Channel(...)` factory function — - * an `ArrayChannel` gets created with the default capacity. - * The default capacity is 64 and can be overridden by setting - * [DEFAULT_BUFFER_PROPERTY_NAME] on JVM. + * Requests a buffered channel with the default buffer capacity in the `Channel(...)` factory function. + * The default capacity for a channel that [suspends][BufferOverflow.SUSPEND] on overflow + * is 64 and can be overridden by setting [DEFAULT_BUFFER_PROPERTY_NAME] on JVM. + * For non-suspending channels, a buffer of capacity 1 is used. */ - public const val BUFFERED = -2 + public const val BUFFERED: Int = -2 // only for internal use, cannot be used with Channel(...) internal const val OPTIONAL_CHANNEL = -3 @@ -544,7 +620,7 @@ public interface Channel : SendChannel, ReceiveChannel { * Name of the property that defines the default channel capacity when * [BUFFERED] is used as parameter in `Channel(...)` factory function. */ - public const val DEFAULT_BUFFER_PROPERTY_NAME = "kotlinx.coroutines.channels.defaultBuffer" + public const val DEFAULT_BUFFER_PROPERTY_NAME: String = "kotlinx.coroutines.channels.defaultBuffer" internal val CHANNEL_DEFAULT_CAPACITY = systemProp(DEFAULT_BUFFER_PROPERTY_NAME, 64, 1, UNLIMITED - 1 @@ -557,17 +633,48 @@ public interface Channel : SendChannel, ReceiveChannel { * See [Channel] interface documentation for details. * * @param capacity either a positive channel capacity or one of the constants defined in [Channel.Factory]. + * @param onBufferOverflow configures an action on buffer overflow (optional, defaults to + * a [suspending][BufferOverflow.SUSPEND] attempt to [send][Channel.send] a value, + * supported only when `capacity >= 0` or `capacity == Channel.BUFFERED`, + * implicitly creates a channel with at least one buffered element). + * @param onUndeliveredElement an optional function that is called when element was sent but was not delivered to the consumer. + * See "Undelivered elements" section in [Channel] documentation. * @throws IllegalArgumentException when [capacity] < -2 */ -public fun Channel(capacity: Int = RENDEZVOUS): Channel = +public fun Channel( + capacity: Int = RENDEZVOUS, + onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND, + onUndeliveredElement: ((E) -> Unit)? = null +): Channel = when (capacity) { - RENDEZVOUS -> RendezvousChannel() - UNLIMITED -> LinkedListChannel() - CONFLATED -> ConflatedChannel() - BUFFERED -> ArrayChannel(CHANNEL_DEFAULT_CAPACITY) - else -> ArrayChannel(capacity) + RENDEZVOUS -> { + if (onBufferOverflow == BufferOverflow.SUSPEND) + RendezvousChannel(onUndeliveredElement) // an efficient implementation of rendezvous channel + else + ArrayChannel(1, onBufferOverflow, onUndeliveredElement) // support buffer overflow with buffered channel + } + CONFLATED -> { + require(onBufferOverflow == BufferOverflow.SUSPEND) { + "CONFLATED capacity cannot be used with non-default onBufferOverflow" + } + ConflatedChannel(onUndeliveredElement) + } + UNLIMITED -> LinkedListChannel(onUndeliveredElement) // ignores onBufferOverflow: it has buffer, but it never overflows + BUFFERED -> ArrayChannel( // uses default capacity with SUSPEND + if (onBufferOverflow == BufferOverflow.SUSPEND) CHANNEL_DEFAULT_CAPACITY else 1, + onBufferOverflow, onUndeliveredElement + ) + else -> { + if (capacity == 1 && onBufferOverflow == BufferOverflow.DROP_OLDEST) + ConflatedChannel(onUndeliveredElement) // conflated implementation is more efficient but appears to work in the same way + else + ArrayChannel(capacity, onBufferOverflow, onUndeliveredElement) + } } +@Deprecated(level = DeprecationLevel.HIDDEN, message = "Since 1.4.0, binary compatibility with earlier versions") +public fun Channel(capacity: Int = RENDEZVOUS): Channel = Channel(capacity) + /** * Indicates an attempt to [send][SendChannel.send] to a [isClosedForSend][SendChannel.isClosedForSend] channel * that was closed without a cause. A _failed_ channel rethrows the original [close][SendChannel.close] cause diff --git a/kotlinx-coroutines-core/common/src/channels/ChannelCoroutine.kt b/kotlinx-coroutines-core/common/src/channels/ChannelCoroutine.kt index 3f53b48c53..a75d466199 100644 --- a/kotlinx-coroutines-core/common/src/channels/ChannelCoroutine.kt +++ b/kotlinx-coroutines-core/common/src/channels/ChannelCoroutine.kt @@ -34,9 +34,4 @@ internal open class ChannelCoroutine( _channel.cancel(exception) // cancel the channel cancelCoroutine(exception) // cancel the job } - - @Suppress("UNCHECKED_CAST") - suspend fun sendFair(element: E) { - (_channel as AbstractSendChannel).sendFair(element) - } } diff --git a/kotlinx-coroutines-core/common/src/channels/Channels.common.kt b/kotlinx-coroutines-core/common/src/channels/Channels.common.kt index 4a73d5d59d..398d5ca44b 100644 --- a/kotlinx-coroutines-core/common/src/channels/Channels.common.kt +++ b/kotlinx-coroutines-core/common/src/channels/Channels.common.kt @@ -3,7 +3,7 @@ */ @file:JvmMultifileClass @file:JvmName("ChannelsKt") -@file:Suppress("DEPRECATION") +@file:Suppress("DEPRECATION_ERROR") package kotlinx.coroutines.channels @@ -40,12 +40,9 @@ public inline fun BroadcastChannel.consume(block: ReceiveChannel.() * * This suspending function is cancellable. If the [Job] of the current coroutine is cancelled or completed while this * function is suspended, this function immediately resumes with [CancellationException]. - * - * *Cancellation of suspended receive is atomic* -- when this function - * throws [CancellationException] it means that the element was not retrieved from this channel. - * As a side-effect of atomic cancellation, a thread-bound coroutine (to some UI thread, for example) may - * continue to execute even after it was cancelled from the same thread in the case when this receive operation - * was already resumed and the continuation was posted for execution to the thread's queue. + * There is a **prompt cancellation guarantee**. If the job was cancelled while this function was + * suspended, it will not resume successfully. If the `receiveOrNull` call threw [CancellationException] there is no way + * to tell if some element was already received from the channel or not. See [Channel] documentation for details. * * Note, that this function does not check for cancellation when it is not suspended. * Use [yield] or [CoroutineScope.isActive] to periodically check for cancellation in tight loops if needed. @@ -55,7 +52,7 @@ public inline fun BroadcastChannel.consume(block: ReceiveChannel.() * to find bugs. */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER") -@ExperimentalCoroutinesApi // since 1.3.0, tentatively stable in 1.4.0 +@ExperimentalCoroutinesApi // since 1.3.0, tentatively stable in 1.4.x public suspend fun ReceiveChannel.receiveOrNull(): E? { @Suppress("DEPRECATION", "UNCHECKED_CAST") return (this as ReceiveChannel).receiveOrNull() @@ -71,7 +68,7 @@ public suspend fun ReceiveChannel.receiveOrNull(): E? { * these extensions do not accidentally confuse `null` value and a normally closed channel, leading to hard * to find bugs. **/ -@ExperimentalCoroutinesApi // since 1.3.0, tentatively stable in 1.4.0 +@ExperimentalCoroutinesApi // since 1.3.0, tentatively stable in 1.4.x public fun ReceiveChannel.onReceiveOrNull(): SelectClause1 { @Suppress("DEPRECATION", "UNCHECKED_CAST") return (this as ReceiveChannel).onReceiveOrNull @@ -84,7 +81,7 @@ public fun ReceiveChannel.onReceiveOrNull(): SelectClause1 { * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @ObsoleteCoroutinesApi -public suspend inline fun BroadcastChannel.consumeEach(action: (E) -> Unit) = +public suspend inline fun BroadcastChannel.consumeEach(action: (E) -> Unit): Unit = consume { for (element in this) action(element) } @@ -105,8 +102,8 @@ public suspend inline fun BroadcastChannel.consumeEach(action: (E) -> Uni * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public fun ReceiveChannel<*>.consumes(): CompletionHandler = { cause: Throwable? -> cancelConsumed(cause) @@ -128,8 +125,8 @@ internal fun ReceiveChannel<*>.cancelConsumed(cause: Throwable?) { * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public fun consumesAll(vararg channels: ReceiveChannel<*>): CompletionHandler = { cause: Throwable? -> @@ -153,7 +150,6 @@ public fun consumesAll(vararg channels: ReceiveChannel<*>): CompletionHandler = * * The operation is _terminal_. */ -@ExperimentalCoroutinesApi // since 1.3.0, tentatively graduates in 1.4.0 public inline fun ReceiveChannel.consume(block: ReceiveChannel.() -> R): R { var cause: Throwable? = null try { @@ -174,8 +170,7 @@ public inline fun ReceiveChannel.consume(block: ReceiveChannel.() - * The operation is _terminal_. * This function [consumes][ReceiveChannel.consume] all elements of the original [ReceiveChannel]. */ -@ExperimentalCoroutinesApi // since 1.3.0, tentatively graduates in 1.4.0 -public suspend inline fun ReceiveChannel.consumeEach(action: (E) -> Unit) = +public suspend inline fun ReceiveChannel.consumeEach(action: (E) -> Unit): Unit = consume { for (e in this) action(e) } @@ -190,8 +185,8 @@ public suspend inline fun ReceiveChannel.consumeEach(action: (E) -> Unit) * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.consumeEachIndexed(action: (IndexedValue) -> Unit) { var index = 0 @@ -210,8 +205,8 @@ public suspend inline fun ReceiveChannel.consumeEachIndexed(action: (Inde * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend fun ReceiveChannel.elementAt(index: Int): E = elementAtOrElse(index) { throw IndexOutOfBoundsException("ReceiveChannel doesn't contain element at index $index.") } @@ -226,8 +221,8 @@ public suspend fun ReceiveChannel.elementAt(index: Int): E = * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.elementAtOrElse(index: Int, defaultValue: (Int) -> E): E = consume { @@ -251,8 +246,8 @@ public suspend inline fun ReceiveChannel.elementAtOrElse(index: Int, defa * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend fun ReceiveChannel.elementAtOrNull(index: Int): E? = consume { @@ -276,8 +271,8 @@ public suspend fun ReceiveChannel.elementAtOrNull(index: Int): E? = * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.find(predicate: (E) -> Boolean): E? = firstOrNull(predicate) @@ -292,8 +287,8 @@ public suspend inline fun ReceiveChannel.find(predicate: (E) -> Boolean): * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.findLast(predicate: (E) -> Boolean): E? = lastOrNull(predicate) @@ -309,8 +304,8 @@ public suspend inline fun ReceiveChannel.findLast(predicate: (E) -> Boole * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend fun ReceiveChannel.first(): E = consume { @@ -331,8 +326,8 @@ public suspend fun ReceiveChannel.first(): E = * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.first(predicate: (E) -> Boolean): E { consumeEach { @@ -351,8 +346,8 @@ public suspend inline fun ReceiveChannel.first(predicate: (E) -> Boolean) * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend fun ReceiveChannel.firstOrNull(): E? = consume { @@ -372,8 +367,8 @@ public suspend fun ReceiveChannel.firstOrNull(): E? = * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.firstOrNull(predicate: (E) -> Boolean): E? { consumeEach { @@ -392,8 +387,8 @@ public suspend inline fun ReceiveChannel.firstOrNull(predicate: (E) -> Bo * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend fun ReceiveChannel.indexOf(element: E): Int { var index = 0 @@ -415,8 +410,8 @@ public suspend fun ReceiveChannel.indexOf(element: E): Int { * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.indexOfFirst(predicate: (E) -> Boolean): Int { var index = 0 @@ -438,8 +433,8 @@ public suspend inline fun ReceiveChannel.indexOfFirst(predicate: (E) -> B * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.indexOfLast(predicate: (E) -> Boolean): Int { var lastIndex = -1 @@ -463,8 +458,8 @@ public suspend inline fun ReceiveChannel.indexOfLast(predicate: (E) -> Bo * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend fun ReceiveChannel.last(): E = consume { @@ -488,8 +483,8 @@ public suspend fun ReceiveChannel.last(): E = * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.last(predicate: (E) -> Boolean): E { var last: E? = null @@ -515,8 +510,8 @@ public suspend inline fun ReceiveChannel.last(predicate: (E) -> Boolean): * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend fun ReceiveChannel.lastIndexOf(element: E): Int { var lastIndex = -1 @@ -539,8 +534,8 @@ public suspend fun ReceiveChannel.lastIndexOf(element: E): Int { * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend fun ReceiveChannel.lastOrNull(): E? = consume { @@ -563,8 +558,8 @@ public suspend fun ReceiveChannel.lastOrNull(): E? = * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.lastOrNull(predicate: (E) -> Boolean): E? { var last: E? = null @@ -586,8 +581,8 @@ public suspend inline fun ReceiveChannel.lastOrNull(predicate: (E) -> Boo * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend fun ReceiveChannel.single(): E = consume { @@ -610,8 +605,8 @@ public suspend fun ReceiveChannel.single(): E = * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.single(predicate: (E) -> Boolean): E { var single: E? = null @@ -638,8 +633,8 @@ public suspend inline fun ReceiveChannel.single(predicate: (E) -> Boolean * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend fun ReceiveChannel.singleOrNull(): E? = consume { @@ -662,8 +657,8 @@ public suspend fun ReceiveChannel.singleOrNull(): E? = * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.singleOrNull(predicate: (E) -> Boolean): E? { var single: E? = null @@ -689,8 +684,8 @@ public suspend inline fun ReceiveChannel.singleOrNull(predicate: (E) -> B * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public fun ReceiveChannel.drop(n: Int, context: CoroutineContext = Dispatchers.Unconfined): ReceiveChannel = GlobalScope.produce(context, onCompletion = consumes()) { @@ -717,8 +712,8 @@ public fun ReceiveChannel.drop(n: Int, context: CoroutineContext = Dispat * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public fun ReceiveChannel.dropWhile(context: CoroutineContext = Dispatchers.Unconfined, predicate: suspend (E) -> Boolean): ReceiveChannel = GlobalScope.produce(context, onCompletion = consumes()) { @@ -743,8 +738,8 @@ public fun ReceiveChannel.dropWhile(context: CoroutineContext = Dispatche * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public fun ReceiveChannel.filter(context: CoroutineContext = Dispatchers.Unconfined, predicate: suspend (E) -> Boolean): ReceiveChannel = GlobalScope.produce(context, onCompletion = consumes()) { @@ -765,8 +760,8 @@ public fun ReceiveChannel.filter(context: CoroutineContext = Dispatchers. * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public fun ReceiveChannel.filterIndexed(context: CoroutineContext = Dispatchers.Unconfined, predicate: suspend (index: Int, E) -> Boolean): ReceiveChannel = GlobalScope.produce(context, onCompletion = consumes()) { @@ -788,8 +783,8 @@ public fun ReceiveChannel.filterIndexed(context: CoroutineContext = Dispa * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun > ReceiveChannel.filterIndexedTo(destination: C, predicate: (index: Int, E) -> Boolean): C { consumeEachIndexed { (index, element) -> @@ -810,8 +805,8 @@ public suspend inline fun > ReceiveChannel.fil * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun > ReceiveChannel.filterIndexedTo(destination: C, predicate: (index: Int, E) -> Boolean): C { consumeEachIndexed { (index, element) -> @@ -830,8 +825,8 @@ public suspend inline fun > ReceiveChannel.filterIndexe * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public fun ReceiveChannel.filterNot(context: CoroutineContext = Dispatchers.Unconfined, predicate: suspend (E) -> Boolean): ReceiveChannel = filter(context) { !predicate(it) } @@ -846,8 +841,8 @@ public fun ReceiveChannel.filterNot(context: CoroutineContext = Dispatche * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) @Suppress("UNCHECKED_CAST") public fun ReceiveChannel.filterNotNull(): ReceiveChannel = @@ -863,8 +858,8 @@ public fun ReceiveChannel.filterNotNull(): ReceiveChannel = * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend fun > ReceiveChannel.filterNotNullTo(destination: C): C { consumeEach { @@ -883,8 +878,8 @@ public suspend fun > ReceiveChannel.fil * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend fun > ReceiveChannel.filterNotNullTo(destination: C): C { consumeEach { @@ -903,8 +898,8 @@ public suspend fun > ReceiveChannel.filterNotNul * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun > ReceiveChannel.filterNotTo(destination: C, predicate: (E) -> Boolean): C { consumeEach { @@ -923,8 +918,8 @@ public suspend inline fun > ReceiveChannel.fil * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun > ReceiveChannel.filterNotTo(destination: C, predicate: (E) -> Boolean): C { consumeEach { @@ -943,8 +938,8 @@ public suspend inline fun > ReceiveChannel.filterNotTo( * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun > ReceiveChannel.filterTo(destination: C, predicate: (E) -> Boolean): C { consumeEach { @@ -963,8 +958,8 @@ public suspend inline fun > ReceiveChannel.fil * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun > ReceiveChannel.filterTo(destination: C, predicate: (E) -> Boolean): C { consumeEach { @@ -983,8 +978,8 @@ public suspend inline fun > ReceiveChannel.filterTo(des * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public fun ReceiveChannel.take(n: Int, context: CoroutineContext = Dispatchers.Unconfined): ReceiveChannel = GlobalScope.produce(context, onCompletion = consumes()) { @@ -1009,8 +1004,8 @@ public fun ReceiveChannel.take(n: Int, context: CoroutineContext = Dispat * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public fun ReceiveChannel.takeWhile(context: CoroutineContext = Dispatchers.Unconfined, predicate: suspend (E) -> Boolean): ReceiveChannel = GlobalScope.produce(context, onCompletion = consumes()) { @@ -1035,8 +1030,8 @@ public fun ReceiveChannel.takeWhile(context: CoroutineContext = Dispatche * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.associate(transform: (E) -> Pair): Map = associateTo(LinkedHashMap(), transform) @@ -1056,8 +1051,8 @@ public suspend inline fun ReceiveChannel.associate(transform: (E) - * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.associateBy(keySelector: (E) -> K): Map = associateByTo(LinkedHashMap(), keySelector) @@ -1076,8 +1071,8 @@ public suspend inline fun ReceiveChannel.associateBy(keySelector: (E) * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.associateBy(keySelector: (E) -> K, valueTransform: (E) -> V): Map = associateByTo(LinkedHashMap(), keySelector, valueTransform) @@ -1096,8 +1091,8 @@ public suspend inline fun ReceiveChannel.associateBy(keySelector: ( * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun > ReceiveChannel.associateByTo(destination: M, keySelector: (E) -> K): M { consumeEach { @@ -1120,8 +1115,8 @@ public suspend inline fun > ReceiveChannel.a * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun > ReceiveChannel.associateByTo(destination: M, keySelector: (E) -> K, valueTransform: (E) -> V): M { consumeEach { @@ -1143,8 +1138,8 @@ public suspend inline fun > ReceiveChannel> ReceiveChannel.associateTo(destination: M, transform: (E) -> Pair): M { consumeEach { @@ -1164,8 +1159,8 @@ public suspend inline fun > ReceiveChannel> ReceiveChannel.toChannel(destination: C): C { consumeEach { @@ -1184,8 +1179,8 @@ public suspend fun > ReceiveChannel.toChannel(destinati * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend fun > ReceiveChannel.toCollection(destination: C): C { consumeEach { @@ -1213,8 +1208,8 @@ public suspend fun ReceiveChannel.toList(): List = * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend fun ReceiveChannel>.toMap(): Map = toMap(LinkedHashMap()) @@ -1229,8 +1224,8 @@ public suspend fun ReceiveChannel>.toMap(): Map = * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend fun > ReceiveChannel>.toMap(destination: M): M { consumeEach { @@ -1249,8 +1244,8 @@ public suspend fun > ReceiveChannel> * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend fun ReceiveChannel.toMutableList(): MutableList = toCollection(ArrayList()) @@ -1267,8 +1262,8 @@ public suspend fun ReceiveChannel.toMutableList(): MutableList = * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend fun ReceiveChannel.toSet(): Set = this.toMutableSet() @@ -1283,8 +1278,8 @@ public suspend fun ReceiveChannel.toSet(): Set = * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public fun ReceiveChannel.flatMap(context: CoroutineContext = Dispatchers.Unconfined, transform: suspend (E) -> ReceiveChannel): ReceiveChannel = GlobalScope.produce(context, onCompletion = consumes()) { @@ -1306,8 +1301,8 @@ public fun ReceiveChannel.flatMap(context: CoroutineContext = Dispatch * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.groupBy(keySelector: (E) -> K): Map> = groupByTo(LinkedHashMap(), keySelector) @@ -1326,8 +1321,8 @@ public suspend inline fun ReceiveChannel.groupBy(keySelector: (E) -> K * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.groupBy(keySelector: (E) -> K, valueTransform: (E) -> V): Map> = groupByTo(LinkedHashMap(), keySelector, valueTransform) @@ -1345,8 +1340,8 @@ public suspend inline fun ReceiveChannel.groupBy(keySelector: (E) - * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun >> ReceiveChannel.groupByTo(destination: M, keySelector: (E) -> K): M { consumeEach { @@ -1371,8 +1366,8 @@ public suspend inline fun >> ReceiveCh * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun >> ReceiveChannel.groupByTo(destination: M, keySelector: (E) -> K, valueTransform: (E) -> V): M { consumeEach { @@ -1391,8 +1386,8 @@ public suspend inline fun >> Receiv * This function [consumes][ReceiveChannel.consume] all elements of the original [ReceiveChannel]. */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public fun ReceiveChannel.map(context: CoroutineContext = Dispatchers.Unconfined, transform: suspend (E) -> R): ReceiveChannel = GlobalScope.produce(context, onCompletion = consumes()) { @@ -1414,8 +1409,8 @@ public fun ReceiveChannel.map(context: CoroutineContext = Dispatchers. * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public fun ReceiveChannel.mapIndexed(context: CoroutineContext = Dispatchers.Unconfined, transform: suspend (index: Int, E) -> R): ReceiveChannel = GlobalScope.produce(context, onCompletion = consumes()) { @@ -1438,8 +1433,8 @@ public fun ReceiveChannel.mapIndexed(context: CoroutineContext = Dispa * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public fun ReceiveChannel.mapIndexedNotNull(context: CoroutineContext = Dispatchers.Unconfined, transform: suspend (index: Int, E) -> R?): ReceiveChannel = mapIndexed(context, transform).filterNotNull() @@ -1457,8 +1452,8 @@ public fun ReceiveChannel.mapIndexedNotNull(context: CoroutineCo * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun > ReceiveChannel.mapIndexedNotNullTo(destination: C, transform: (index: Int, E) -> R?): C { consumeEachIndexed { (index, element) -> @@ -1480,8 +1475,8 @@ public suspend inline fun > ReceiveChann * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun > ReceiveChannel.mapIndexedNotNullTo(destination: C, transform: (index: Int, E) -> R?): C { consumeEachIndexed { (index, element) -> @@ -1503,8 +1498,8 @@ public suspend inline fun > ReceiveChannel.map * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun > ReceiveChannel.mapIndexedTo(destination: C, transform: (index: Int, E) -> R): C { var index = 0 @@ -1527,8 +1522,8 @@ public suspend inline fun > ReceiveChannel. * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun > ReceiveChannel.mapIndexedTo(destination: C, transform: (index: Int, E) -> R): C { var index = 0 @@ -1549,8 +1544,8 @@ public suspend inline fun > ReceiveChannel.mapIndexe * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public fun ReceiveChannel.mapNotNull(context: CoroutineContext = Dispatchers.Unconfined, transform: suspend (E) -> R?): ReceiveChannel = map(context, transform).filterNotNull() @@ -1566,8 +1561,8 @@ public fun ReceiveChannel.mapNotNull(context: CoroutineContext = * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun > ReceiveChannel.mapNotNullTo(destination: C, transform: (E) -> R?): C { consumeEach { @@ -1587,8 +1582,8 @@ public suspend inline fun > ReceiveChann * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun > ReceiveChannel.mapNotNullTo(destination: C, transform: (E) -> R?): C { consumeEach { @@ -1608,8 +1603,8 @@ public suspend inline fun > ReceiveChannel.map * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun > ReceiveChannel.mapTo(destination: C, transform: (E) -> R): C { consumeEach { @@ -1629,8 +1624,8 @@ public suspend inline fun > ReceiveChannel. * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun > ReceiveChannel.mapTo(destination: C, transform: (E) -> R): C { consumeEach { @@ -1649,8 +1644,8 @@ public suspend inline fun > ReceiveChannel.mapTo(des * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public fun ReceiveChannel.withIndex(context: CoroutineContext = Dispatchers.Unconfined): ReceiveChannel> = GlobalScope.produce(context, onCompletion = consumes()) { @@ -1672,8 +1667,8 @@ public fun ReceiveChannel.withIndex(context: CoroutineContext = Dispatche * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public fun ReceiveChannel.distinct(): ReceiveChannel = this.distinctBy { it } @@ -1691,8 +1686,8 @@ public fun ReceiveChannel.distinct(): ReceiveChannel = * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public fun ReceiveChannel.distinctBy(context: CoroutineContext = Dispatchers.Unconfined, selector: suspend (E) -> K): ReceiveChannel = GlobalScope.produce(context, onCompletion = consumes()) { @@ -1718,8 +1713,8 @@ public fun ReceiveChannel.distinctBy(context: CoroutineContext = Dispa * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend fun ReceiveChannel.toMutableSet(): MutableSet = toCollection(LinkedHashSet()) @@ -1734,8 +1729,8 @@ public suspend fun ReceiveChannel.toMutableSet(): MutableSet = * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.all(predicate: (E) -> Boolean): Boolean { consumeEach { @@ -1754,8 +1749,8 @@ public suspend inline fun ReceiveChannel.all(predicate: (E) -> Boolean): * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend fun ReceiveChannel.any(): Boolean = consume { @@ -1772,8 +1767,8 @@ public suspend fun ReceiveChannel.any(): Boolean = * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.any(predicate: (E) -> Boolean): Boolean { consumeEach { @@ -1792,8 +1787,8 @@ public suspend inline fun ReceiveChannel.any(predicate: (E) -> Boolean): * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend fun ReceiveChannel.count(): Int { var count = 0 @@ -1811,8 +1806,8 @@ public suspend fun ReceiveChannel.count(): Int { * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.count(predicate: (E) -> Boolean): Int { var count = 0 @@ -1832,8 +1827,8 @@ public suspend inline fun ReceiveChannel.count(predicate: (E) -> Boolean) * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.fold(initial: R, operation: (acc: R, E) -> R): R { var accumulator = initial @@ -1856,8 +1851,8 @@ public suspend inline fun ReceiveChannel.fold(initial: R, operation: ( * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.foldIndexed(initial: R, operation: (index: Int, acc: R, E) -> R): R { var index = 0 @@ -1878,8 +1873,8 @@ public suspend inline fun ReceiveChannel.foldIndexed(initial: R, opera * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun > ReceiveChannel.maxBy(selector: (E) -> R): E? = consume { @@ -1908,8 +1903,8 @@ public suspend inline fun > ReceiveChannel.maxBy(selecto * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend fun ReceiveChannel.maxWith(comparator: Comparator): E? = consume { @@ -1933,8 +1928,8 @@ public suspend fun ReceiveChannel.maxWith(comparator: Comparator): * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun > ReceiveChannel.minBy(selector: (E) -> R): E? = consume { @@ -1963,8 +1958,8 @@ public suspend inline fun > ReceiveChannel.minBy(selecto * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend fun ReceiveChannel.minWith(comparator: Comparator): E? = consume { @@ -1988,8 +1983,8 @@ public suspend fun ReceiveChannel.minWith(comparator: Comparator): * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend fun ReceiveChannel.none(): Boolean = consume { @@ -2006,8 +2001,8 @@ public suspend fun ReceiveChannel.none(): Boolean = * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.none(predicate: (E) -> Boolean): Boolean { consumeEach { @@ -2026,8 +2021,8 @@ public suspend inline fun ReceiveChannel.none(predicate: (E) -> Boolean): * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.reduce(operation: (acc: S, E) -> S): S = consume { @@ -2053,8 +2048,8 @@ public suspend inline fun ReceiveChannel.reduce(operation: (acc: S * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.reduceIndexed(operation: (index: Int, acc: S, E) -> S): S = consume { @@ -2078,8 +2073,8 @@ public suspend inline fun ReceiveChannel.reduceIndexed(operation: * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.sumBy(selector: (E) -> Int): Int { var sum = 0 @@ -2099,8 +2094,8 @@ public suspend inline fun ReceiveChannel.sumBy(selector: (E) -> Int): Int * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.sumByDouble(selector: (E) -> Double): Double { var sum = 0.0 @@ -2120,8 +2115,8 @@ public suspend inline fun ReceiveChannel.sumByDouble(selector: (E) -> Dou * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public fun ReceiveChannel.requireNoNulls(): ReceiveChannel = map { it ?: throw IllegalArgumentException("null element found in $this.") } @@ -2138,8 +2133,8 @@ public fun ReceiveChannel.requireNoNulls(): ReceiveChannel = * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public suspend inline fun ReceiveChannel.partition(predicate: (E) -> Boolean): Pair, List> { val first = ArrayList() @@ -2165,8 +2160,8 @@ public suspend inline fun ReceiveChannel.partition(predicate: (E) -> Bool * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public infix fun ReceiveChannel.zip(other: ReceiveChannel): ReceiveChannel> = zip(other) { t1, t2 -> t1 to t2 } @@ -2181,8 +2176,8 @@ public infix fun ReceiveChannel.zip(other: ReceiveChannel): Receive * See [issue #254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). */ @Deprecated( - message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4", - level = DeprecationLevel.WARNING + message = "Channel operators are deprecated in favour of Flow and will be removed in 1.4.x", + level = DeprecationLevel.ERROR ) public fun ReceiveChannel.zip(other: ReceiveChannel, context: CoroutineContext = Dispatchers.Unconfined, transform: (a: E, b: R) -> V): ReceiveChannel = GlobalScope.produce(context, onCompletion = consumesAll(this, other)) { diff --git a/kotlinx-coroutines-core/common/src/channels/ConflatedBroadcastChannel.kt b/kotlinx-coroutines-core/common/src/channels/ConflatedBroadcastChannel.kt index 4990c933ec..5986dae3d4 100644 --- a/kotlinx-coroutines-core/common/src/channels/ConflatedBroadcastChannel.kt +++ b/kotlinx-coroutines-core/common/src/channels/ConflatedBroadcastChannel.kt @@ -10,7 +10,6 @@ import kotlinx.coroutines.internal.* import kotlinx.coroutines.intrinsics.* import kotlinx.coroutines.selects.* import kotlin.jvm.* -import kotlin.native.concurrent.* /** * Broadcasts the most recently sent element (aka [value]) to all [openSubscription] subscribers. @@ -27,9 +26,10 @@ import kotlin.native.concurrent.* * [opening][openSubscription] and [closing][ReceiveChannel.cancel] subscription takes O(N) time, where N is the * number of subscribers. * - * **Note: This API is experimental.** It may be changed in the future updates. + * **Note: This API is obsolete.** It will be deprecated and replaced by [StateFlow][kotlinx.coroutines.flow.StateFlow] + * when it becomes stable. */ -@ExperimentalCoroutinesApi +@ExperimentalCoroutinesApi // not @ObsoleteCoroutinesApi to reduce burden for people who are still using it public class ConflatedBroadcastChannel() : BroadcastChannel { /** * Creates an instance of this class that already holds a value. @@ -37,7 +37,7 @@ public class ConflatedBroadcastChannel() : BroadcastChannel { * It is as a shortcut to creating an instance with a default constructor and * immediately sending an element: `ConflatedBroadcastChannel().apply { offer(value) }`. */ - constructor(value: E) : this() { + public constructor(value: E) : this() { _state.lazySet(State(value, null)) } @@ -47,9 +47,7 @@ public class ConflatedBroadcastChannel() : BroadcastChannel { private val onCloseHandler = atomic(null) private companion object { - @SharedImmutable private val CLOSED = Closed(null) - @SharedImmutable private val UNDEFINED = Symbol("UNDEFINED") private val INITIAL_STATE = State(UNDEFINED, null) } @@ -284,7 +282,7 @@ public class ConflatedBroadcastChannel() : BroadcastChannel { private class Subscriber( private val broadcastChannel: ConflatedBroadcastChannel - ) : ConflatedChannel(), ReceiveChannel { + ) : ConflatedChannel(null), ReceiveChannel { override fun onCancelIdempotent(wasClosed: Boolean) { if (wasClosed) { diff --git a/kotlinx-coroutines-core/common/src/channels/ConflatedChannel.kt b/kotlinx-coroutines-core/common/src/channels/ConflatedChannel.kt index 399019c3ee..75e421c6e7 100644 --- a/kotlinx-coroutines-core/common/src/channels/ConflatedChannel.kt +++ b/kotlinx-coroutines-core/common/src/channels/ConflatedChannel.kt @@ -7,7 +7,6 @@ package kotlinx.coroutines.channels import kotlinx.coroutines.* import kotlinx.coroutines.internal.* import kotlinx.coroutines.selects.* -import kotlin.native.concurrent.* /** * Channel that buffers at most one element and conflates all subsequent `send` and `offer` invocations, @@ -18,7 +17,7 @@ import kotlin.native.concurrent.* * * This channel is created by `Channel(Channel.CONFLATED)` factory function invocation. */ -internal open class ConflatedChannel : AbstractChannel() { +internal open class ConflatedChannel(onUndeliveredElement: OnUndeliveredElement?) : AbstractChannel(onUndeliveredElement) { protected final override val isBufferAlwaysEmpty: Boolean get() = false protected final override val isBufferEmpty: Boolean get() = value === EMPTY protected final override val isBufferAlwaysFull: Boolean get() = false @@ -30,11 +29,6 @@ internal open class ConflatedChannel : AbstractChannel() { private var value: Any? = EMPTY - private companion object { - @SharedImmutable - private val EMPTY = Symbol("EMPTY") - } - // result is `OFFER_SUCCESS | Closed` protected override fun offerInternal(element: E): Any { var receive: ReceiveOrClosed? = null @@ -55,7 +49,7 @@ internal open class ConflatedChannel : AbstractChannel() { } } } - value = element + updateValueLocked(element)?.let { throw it } return OFFER_SUCCESS } // breaks here if offer meets receiver @@ -88,7 +82,7 @@ internal open class ConflatedChannel : AbstractChannel() { if (!select.trySelect()) { return ALREADY_SELECTED } - value = element + updateValueLocked(element)?.let { throw it } return OFFER_SUCCESS } // breaks here if offer meets receiver @@ -121,12 +115,20 @@ internal open class ConflatedChannel : AbstractChannel() { } protected override fun onCancelIdempotent(wasClosed: Boolean) { - if (wasClosed) { - lock.withLock { - value = EMPTY - } + var undeliveredElementException: UndeliveredElementException? = null // resource cancel exception + lock.withLock { + undeliveredElementException = updateValueLocked(EMPTY) } super.onCancelIdempotent(wasClosed) + undeliveredElementException?.let { throw it } // throw exception at the end if there was one + } + + private fun updateValueLocked(element: Any?): UndeliveredElementException? { + val old = value + val undeliveredElementException = if (old === EMPTY) null else + onUndeliveredElement?.callUndeliveredElementCatchingException(old as E) + value = element + return undeliveredElementException } override fun enqueueReceiveInternal(receive: Receive): Boolean = lock.withLock { diff --git a/kotlinx-coroutines-core/common/src/channels/LinkedListChannel.kt b/kotlinx-coroutines-core/common/src/channels/LinkedListChannel.kt index e66bbb2279..2f46421344 100644 --- a/kotlinx-coroutines-core/common/src/channels/LinkedListChannel.kt +++ b/kotlinx-coroutines-core/common/src/channels/LinkedListChannel.kt @@ -17,7 +17,7 @@ import kotlinx.coroutines.selects.* * * @suppress **This an internal API and should not be used from general code.** */ -internal open class LinkedListChannel : AbstractChannel() { +internal open class LinkedListChannel(onUndeliveredElement: OnUndeliveredElement?) : AbstractChannel(onUndeliveredElement) { protected final override val isBufferAlwaysEmpty: Boolean get() = true protected final override val isBufferEmpty: Boolean get() = true protected final override val isBufferAlwaysFull: Boolean get() = false diff --git a/kotlinx-coroutines-core/common/src/channels/Produce.kt b/kotlinx-coroutines-core/common/src/channels/Produce.kt index 24fd399bb7..10a15e2a93 100644 --- a/kotlinx-coroutines-core/common/src/channels/Produce.kt +++ b/kotlinx-coroutines-core/common/src/channels/Produce.kt @@ -22,12 +22,16 @@ public interface ProducerScope : CoroutineScope, SendChannel { * All the [SendChannel] functions on this interface delegate to * the channel instance returned by this property. */ - val channel: SendChannel + public val channel: SendChannel } /** * Suspends the current coroutine until the channel is either [closed][SendChannel.close] or [cancelled][ReceiveChannel.cancel] - * and invokes the given [block] before resuming the coroutine. This suspending function is cancellable. + * and invokes the given [block] before resuming the coroutine. + * + * This suspending function is cancellable. + * There is a **prompt cancellation guarantee**. If the job was cancelled while this function was + * suspended, it will not resume successfully. See [suspendCancellableCoroutine] documentation for low-level details. * * Note that when the producer channel is cancelled, this function resumes with a cancellation exception. * Therefore, in case of cancellation, no code after the call to this function will be executed. @@ -91,13 +95,8 @@ public fun CoroutineScope.produce( context: CoroutineContext = EmptyCoroutineContext, capacity: Int = 0, @BuilderInference block: suspend ProducerScope.() -> Unit -): ReceiveChannel { - val channel = Channel(capacity) - val newContext = newCoroutineContext(context) - val coroutine = ProducerCoroutine(newContext, channel) - coroutine.start(CoroutineStart.DEFAULT, coroutine, block) - return coroutine -} +): ReceiveChannel = + produce(context, capacity, BufferOverflow.SUSPEND, CoroutineStart.DEFAULT, onCompletion = null, block = block) /** * **This is an internal API and should not be used from general code.** @@ -118,8 +117,19 @@ public fun CoroutineScope.produce( start: CoroutineStart = CoroutineStart.DEFAULT, onCompletion: CompletionHandler? = null, @BuilderInference block: suspend ProducerScope.() -> Unit +): ReceiveChannel = + produce(context, capacity, BufferOverflow.SUSPEND, start, onCompletion, block) + +// Internal version of produce that is maximally flexible, but is not exposed through public API (too many params) +internal fun CoroutineScope.produce( + context: CoroutineContext = EmptyCoroutineContext, + capacity: Int = 0, + onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND, + start: CoroutineStart = CoroutineStart.DEFAULT, + onCompletion: CompletionHandler? = null, + @BuilderInference block: suspend ProducerScope.() -> Unit ): ReceiveChannel { - val channel = Channel(capacity) + val channel = Channel(capacity, onBufferOverflow) val newContext = newCoroutineContext(context) val coroutine = ProducerCoroutine(newContext, channel) if (onCompletion != null) coroutine.invokeOnCompletion(handler = onCompletion) diff --git a/kotlinx-coroutines-core/common/src/channels/RendezvousChannel.kt b/kotlinx-coroutines-core/common/src/channels/RendezvousChannel.kt index 700f50908c..857a97938f 100644 --- a/kotlinx-coroutines-core/common/src/channels/RendezvousChannel.kt +++ b/kotlinx-coroutines-core/common/src/channels/RendezvousChannel.kt @@ -4,6 +4,8 @@ package kotlinx.coroutines.channels +import kotlinx.coroutines.internal.* + /** * Rendezvous channel. This channel does not have any buffer at all. An element is transferred from sender * to receiver only when [send] and [receive] invocations meet in time (rendezvous), so [send] suspends @@ -13,7 +15,7 @@ package kotlinx.coroutines.channels * * This implementation is fully lock-free. **/ -internal open class RendezvousChannel : AbstractChannel() { +internal open class RendezvousChannel(onUndeliveredElement: OnUndeliveredElement?) : AbstractChannel(onUndeliveredElement) { protected final override val isBufferAlwaysEmpty: Boolean get() = true protected final override val isBufferEmpty: Boolean get() = true protected final override val isBufferAlwaysFull: Boolean get() = true diff --git a/kotlinx-coroutines-core/common/src/flow/Builders.kt b/kotlinx-coroutines-core/common/src/flow/Builders.kt index 4157576aae..7d84cd2105 100644 --- a/kotlinx-coroutines-core/common/src/flow/Builders.kt +++ b/kotlinx-coroutines-core/common/src/flow/Builders.kt @@ -16,9 +16,11 @@ import kotlin.jvm.* import kotlinx.coroutines.flow.internal.unsafeFlow as flow /** - * Creates a flow from the given suspendable [block]. + * Creates a _cold_ flow from the given suspendable [block]. + * The flow being _cold_ means that the [block] is called every time a terminal operator is applied to the resulting flow. * * Example of usage: + * * ``` * fun fibonacci(): Flow = flow { * var x = BigInteger.ZERO @@ -34,8 +36,12 @@ import kotlinx.coroutines.flow.internal.unsafeFlow as flow * fibonacci().take(100).collect { println(it) } * ``` * + * Emissions from [flow] builder are [cancellable] by default — each call to [emit][FlowCollector.emit] + * also calls [ensureActive][CoroutineContext.ensureActive]. + * * `emit` should happen strictly in the dispatchers of the [block] in order to preserve the flow context. * For example, the following code will result in an [IllegalStateException]: + * * ``` * flow { * emit(1) // Ok @@ -44,24 +50,20 @@ import kotlinx.coroutines.flow.internal.unsafeFlow as flow * } * } * ``` + * * If you want to switch the context of execution of a flow, use the [flowOn] operator. */ public fun flow(@BuilderInference block: suspend FlowCollector.() -> Unit): Flow = SafeFlow(block) // Named anonymous object -private class SafeFlow(private val block: suspend FlowCollector.() -> Unit) : Flow { - override suspend fun collect(collector: FlowCollector) { - val safeCollector = SafeCollector(collector, coroutineContext) - try { - safeCollector.block() - } finally { - safeCollector.releaseIntercepted() - } +private class SafeFlow(private val block: suspend FlowCollector.() -> Unit) : AbstractFlow() { + override suspend fun collectSafely(collector: FlowCollector) { + collector.block() } } /** - * Creates a flow that produces a single value from the given functional type. + * Creates a _cold_ flow that produces a single value from the given functional type. */ @FlowPreview public fun (() -> T).asFlow(): Flow = flow { @@ -69,8 +71,10 @@ public fun (() -> T).asFlow(): Flow = flow { } /** - * Creates a flow that produces a single value from the given functional type. + * Creates a _cold_ flow that produces a single value from the given functional type. + * * Example of usage: + * * ``` * suspend fun remoteCall(): R = ... * fun remoteCallFlow(): Flow = ::remoteCall.asFlow() @@ -82,7 +86,7 @@ public fun (suspend () -> T).asFlow(): Flow = flow { } /** - * Creates a flow that produces values from the given iterable. + * Creates a _cold_ flow that produces values from the given iterable. */ public fun Iterable.asFlow(): Flow = flow { forEach { value -> @@ -91,7 +95,7 @@ public fun Iterable.asFlow(): Flow = flow { } /** - * Creates a flow that produces values from the given iterator. + * Creates a _cold_ flow that produces values from the given iterator. */ public fun Iterator.asFlow(): Flow = flow { forEach { value -> @@ -100,7 +104,7 @@ public fun Iterator.asFlow(): Flow = flow { } /** - * Creates a flow that produces values from the given sequence. + * Creates a _cold_ flow that produces values from the given sequence. */ public fun Sequence.asFlow(): Flow = flow { forEach { value -> @@ -112,6 +116,7 @@ public fun Sequence.asFlow(): Flow = flow { * Creates a flow that produces values from the specified `vararg`-arguments. * * Example of usage: + * * ``` * flowOf(1, 2, 3) * ``` @@ -123,7 +128,7 @@ public fun flowOf(vararg elements: T): Flow = flow { } /** - * Creates flow that produces the given [value]. + * Creates a flow that produces the given [value]. */ public fun flowOf(value: T): Flow = flow { /* @@ -143,7 +148,9 @@ private object EmptyFlow : Flow { } /** - * Creates a flow that produces values from the given array. + * Creates a _cold_ flow that produces values from the given array. + * The flow being _cold_ means that the array components are read every time a terminal operator is applied + * to the resulting flow. */ public fun Array.asFlow(): Flow = flow { forEach { value -> @@ -152,7 +159,9 @@ public fun Array.asFlow(): Flow = flow { } /** - * Creates a flow that produces values from the array. + * Creates a _cold_ flow that produces values from the array. + * The flow being _cold_ means that the array components are read every time a terminal operator is applied + * to the resulting flow. */ public fun IntArray.asFlow(): Flow = flow { forEach { value -> @@ -161,7 +170,9 @@ public fun IntArray.asFlow(): Flow = flow { } /** - * Creates a flow that produces values from the array. + * Creates a _cold_ flow that produces values from the given array. + * The flow being _cold_ means that the array components are read every time a terminal operator is applied + * to the resulting flow. */ public fun LongArray.asFlow(): Flow = flow { forEach { value -> @@ -193,8 +204,8 @@ public fun LongRange.asFlow(): Flow = flow { @FlowPreview @Deprecated( message = "Use channelFlow with awaitClose { } instead of flowViaChannel and invokeOnClose { }.", - level = DeprecationLevel.WARNING -) + level = DeprecationLevel.ERROR +) // To be removed in 1.4.x @Suppress("DeprecatedCallableAddReplaceWith") public fun flowViaChannel( bufferSize: Int = BUFFERED, @@ -207,7 +218,7 @@ public fun flowViaChannel( } /** - * Creates an instance of the cold [Flow] with elements that are sent to a [SendChannel] + * Creates an instance of a _cold_ [Flow] with elements that are sent to a [SendChannel] * provided to the builder's [block] of code via [ProducerScope]. It allows elements to be * produced by code that is running in a different context or concurrently. * The resulting flow is _cold_, which means that [block] is called every time a terminal operator @@ -255,7 +266,7 @@ public fun channelFlow(@BuilderInference block: suspend ProducerScope.() ChannelFlowBuilder(block) /** - * Creates an instance of the cold [Flow] with elements that are sent to a [SendChannel] + * Creates an instance of a _cold_ [Flow] with elements that are sent to a [SendChannel] * provided to the builder's [block] of code via [ProducerScope]. It allows elements to be * produced by code that is running in a different context or concurrently. * @@ -282,11 +293,12 @@ public fun channelFlow(@BuilderInference block: suspend ProducerScope.() * Adjacent applications of [callbackFlow], [flowOn], [buffer], [produceIn], and [broadcastIn] are * always fused so that only one properly configured channel is used for execution. * - * Example of usage: + * Example of usage that converts a multi-short callback API to a flow. + * For single-shot callbacks use [suspendCancellableCoroutine]. * * ``` * fun flowFrom(api: CallbackBasedApi): Flow = callbackFlow { - * val callback = object : Callback { // implementation of some callback interface + * val callback = object : Callback { // Implementation of some callback interface * override fun onNextValue(value: T) { * // To avoid blocking you can configure channel capacity using * // either buffer(Channel.CONFLATED) or buffer(Channel.UNLIMITED) to avoid overfill @@ -310,6 +322,10 @@ public fun channelFlow(@BuilderInference block: suspend ProducerScope.() * awaitClose { api.unregister(callback) } * } * ``` + * + * > The callback `register`/`unregister` methods provided by an external API must be thread-safe, because + * > `awaitClose` block can be called at any time due to asynchronous nature of cancellation, even + * > concurrently with the call of the callback. */ @ExperimentalCoroutinesApi public fun callbackFlow(@BuilderInference block: suspend ProducerScope.() -> Unit): Flow = CallbackFlowBuilder(block) @@ -318,10 +334,11 @@ public fun callbackFlow(@BuilderInference block: suspend ProducerScope.() private open class ChannelFlowBuilder( private val block: suspend ProducerScope.() -> Unit, context: CoroutineContext = EmptyCoroutineContext, - capacity: Int = BUFFERED -) : ChannelFlow(context, capacity) { - override fun create(context: CoroutineContext, capacity: Int): ChannelFlow = - ChannelFlowBuilder(block, context, capacity) + capacity: Int = BUFFERED, + onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND +) : ChannelFlow(context, capacity, onBufferOverflow) { + override fun create(context: CoroutineContext, capacity: Int, onBufferOverflow: BufferOverflow): ChannelFlow = + ChannelFlowBuilder(block, context, capacity, onBufferOverflow) override suspend fun collectTo(scope: ProducerScope) = block(scope) @@ -333,8 +350,9 @@ private open class ChannelFlowBuilder( private class CallbackFlowBuilder( private val block: suspend ProducerScope.() -> Unit, context: CoroutineContext = EmptyCoroutineContext, - capacity: Int = BUFFERED -) : ChannelFlowBuilder(block, context, capacity) { + capacity: Int = BUFFERED, + onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND +) : ChannelFlowBuilder(block, context, capacity, onBufferOverflow) { override suspend fun collectTo(scope: ProducerScope) { super.collectTo(scope) @@ -354,6 +372,6 @@ private class CallbackFlowBuilder( } } - override fun create(context: CoroutineContext, capacity: Int): ChannelFlow = - CallbackFlowBuilder(block, context, capacity) + override fun create(context: CoroutineContext, capacity: Int, onBufferOverflow: BufferOverflow): ChannelFlow = + CallbackFlowBuilder(block, context, capacity, onBufferOverflow) } diff --git a/kotlinx-coroutines-core/common/src/flow/Channels.kt b/kotlinx-coroutines-core/common/src/flow/Channels.kt index e3a64b9568..762cdcad1b 100644 --- a/kotlinx-coroutines-core/common/src/flow/Channels.kt +++ b/kotlinx-coroutines-core/common/src/flow/Channels.kt @@ -20,11 +20,13 @@ import kotlinx.coroutines.flow.internal.unsafeFlow as flow * the channel afterwards. If you need to iterate over the channel without consuming it, * a regular `for` loop should be used instead. * + * Note, that emitting values from a channel into a flow is not atomic. A value that was received from the + * channel many not reach the flow collector if it was cancelled and will be lost. + * * This function provides a more efficient shorthand for `channel.consumeEach { value -> emit(value) }`. * See [consumeEach][ReceiveChannel.consumeEach]. */ -@ExperimentalCoroutinesApi // since version 1.3.0 -public suspend fun FlowCollector.emitAll(channel: ReceiveChannel) = +public suspend fun FlowCollector.emitAll(channel: ReceiveChannel): Unit = emitAllImpl(channel, consume = true) private suspend fun FlowCollector.emitAllImpl(channel: ReceiveChannel, consume: Boolean) { @@ -84,7 +86,6 @@ private suspend fun FlowCollector.emitAllImpl(channel: ReceiveChannel, * In particular, [produceIn] returns the original channel. * Calls to [flowOn] have generally no effect, unless [buffer] is used to explicitly request buffering. */ -@ExperimentalCoroutinesApi // since version 1.4.0 public fun ReceiveChannel.receiveAsFlow(): Flow = ChannelAsFlow(this, consume = false) /** @@ -106,7 +107,6 @@ public fun ReceiveChannel.receiveAsFlow(): Flow = ChannelAsFlow(this, * In particular, [produceIn] returns the original channel (but throws [IllegalStateException] on repeated calls). * Calls to [flowOn] have generally no effect, unless [buffer] is used to explicitly request buffering. */ -@ExperimentalCoroutinesApi // since version 1.3.0 public fun ReceiveChannel.consumeAsFlow(): Flow = ChannelAsFlow(this, consume = true) /** @@ -119,8 +119,9 @@ private class ChannelAsFlow( private val channel: ReceiveChannel, private val consume: Boolean, context: CoroutineContext = EmptyCoroutineContext, - capacity: Int = Channel.OPTIONAL_CHANNEL -) : ChannelFlow(context, capacity) { + capacity: Int = Channel.OPTIONAL_CHANNEL, + onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND +) : ChannelFlow(context, capacity, onBufferOverflow) { private val consumed = atomic(false) private fun markConsumed() { @@ -129,8 +130,11 @@ private class ChannelAsFlow( } } - override fun create(context: CoroutineContext, capacity: Int): ChannelFlow = - ChannelAsFlow(channel, consume, context, capacity) + override fun create(context: CoroutineContext, capacity: Int, onBufferOverflow: BufferOverflow): ChannelFlow = + ChannelAsFlow(channel, consume, context, capacity, onBufferOverflow) + + override fun dropChannelOperators(): Flow? = + ChannelAsFlow(channel, consume) override suspend fun collectTo(scope: ProducerScope) = SendingCollector(scope).emitAllImpl(channel, consume) // use efficient channel receiving code from emitAll @@ -157,7 +161,7 @@ private class ChannelAsFlow( } } - override fun additionalToStringProps(): String = "channel=$channel, " + override fun additionalToStringProps(): String = "channel=$channel" } /** @@ -184,8 +188,22 @@ public fun BroadcastChannel.asFlow(): Flow = flow { * Use [buffer] operator on the flow before calling `broadcastIn` to specify a value other than * default and to control what happens when data is produced faster than it is consumed, * that is to control backpressure behavior. + * + * ### Deprecated + * + * **This API is deprecated.** The [BroadcastChannel] provides a complex channel-like API for hot flows. + * [SharedFlow] is a easier-to-use and more flow-centric API for the same purposes, so using + * [shareIn] operator is preferred. It is not a direct replacement, so please + * study [shareIn] documentation to see what kind of shared flow fits your use-case. As a rule of thumb: + * + * * Replace `broadcastIn(scope)` and `broadcastIn(scope, CoroutineStart.LAZY)` with `shareIn(scope, 0, SharingStarted.Lazily)`. + * * Replace `broadcastIn(scope, CoroutineStart.DEFAULT)` with `shareIn(scope, 0, SharingStarted.Eagerly)`. */ -@FlowPreview +@Deprecated( + message = "Use shareIn operator and the resulting SharedFlow as a replacement for BroadcastChannel", + replaceWith = ReplaceWith("shareIn(scope, 0, SharingStarted.Lazily)"), + level = DeprecationLevel.WARNING +) public fun Flow.broadcastIn( scope: CoroutineScope, start: CoroutineStart = CoroutineStart.LAZY diff --git a/kotlinx-coroutines-core/common/src/flow/Flow.kt b/kotlinx-coroutines-core/common/src/flow/Flow.kt index 378b9f148e..19a5b43f31 100644 --- a/kotlinx-coroutines-core/common/src/flow/Flow.kt +++ b/kotlinx-coroutines-core/common/src/flow/Flow.kt @@ -9,8 +9,7 @@ import kotlinx.coroutines.flow.internal.* import kotlin.coroutines.* /** - * A cold asynchronous data stream that sequentially emits values - * and completes normally or with an exception. + * An asynchronous data stream that sequentially emits values and completes normally or with an exception. * * _Intermediate operators_ on the flow such as [map], [filter], [take], [zip], etc are functions that are * applied to the _upstream_ flow or flows and return a _downstream_ flow where further operators can be applied to. @@ -39,11 +38,12 @@ import kotlin.coroutines.* * with an exception for a few operations specifically designed to introduce concurrency into flow * execution such as [buffer] and [flatMapMerge]. See their documentation for details. * - * The `Flow` interface does not carry information whether a flow truly is a cold stream that can be collected repeatedly and - * triggers execution of the same code every time it is collected, or if it is a hot stream that emits different - * values from the same running source on each collection. However, conventionally flows represent cold streams. - * Transitions between hot and cold streams are supported via channels and the corresponding API: - * [channelFlow], [produceIn], [broadcastIn]. + * The `Flow` interface does not carry information whether a flow is a _cold_ stream that can be collected repeatedly and + * triggers execution of the same code every time it is collected, or if it is a _hot_ stream that emits different + * values from the same running source on each collection. Usually flows represent _cold_ streams, but + * there is a [SharedFlow] subtype that represents _hot_ streams. In addition to that, any flow can be turned + * into a _hot_ one by the [stateIn] and [shareIn] operators, or by converting the flow into a hot channel + * via the [produceIn] operator. * * ### Flow builders * @@ -55,6 +55,8 @@ import kotlin.coroutines.* * sequential calls to [emit][FlowCollector.emit] function. * * [channelFlow { ... }][channelFlow] builder function to construct arbitrary flows from * potentially concurrent calls to the [send][kotlinx.coroutines.channels.SendChannel.send] function. + * * [MutableStateFlow] and [MutableSharedFlow] define the corresponding constructor functions to create + * a _hot_ flow that can be directly updated. * * ### Flow constraints * @@ -156,6 +158,12 @@ import kotlin.coroutines.* * * Flow is [Reactive Streams](http://www.reactive-streams.org/) compliant, you can safely interop it with * reactive streams using [Flow.asPublisher] and [Publisher.asFlow] from `kotlinx-coroutines-reactive` module. + * + * ### Not stable for inheritance + * + * **The `Flow` interface is not stable for inheritance in 3rd party libraries**, as new methods + * might be added to this interface in the future, but is stable for use. + * Use the `flow { ... }` builder function to create an implementation. */ public interface Flow { /** @@ -195,7 +203,7 @@ public interface Flow { * ``` */ @FlowPreview -public abstract class AbstractFlow : Flow { +public abstract class AbstractFlow : Flow, CancellableFlow { @InternalCoroutinesApi public final override suspend fun collect(collector: FlowCollector) { diff --git a/kotlinx-coroutines-core/common/src/flow/Migration.kt b/kotlinx-coroutines-core/common/src/flow/Migration.kt index 16bde89f44..11969a48fa 100644 --- a/kotlinx-coroutines-core/common/src/flow/Migration.kt +++ b/kotlinx-coroutines-core/common/src/flow/Migration.kt @@ -4,13 +4,11 @@ @file:JvmMultifileClass @file:JvmName("FlowKt") -@file:Suppress("unused", "DeprecatedCallableAddReplaceWith", "UNUSED_PARAMETER") +@file:Suppress("unused", "DeprecatedCallableAddReplaceWith", "UNUSED_PARAMETER", "NO_EXPLICIT_RETURN_TYPE_IN_API_MODE") package kotlinx.coroutines.flow import kotlinx.coroutines.* -import kotlinx.coroutines.flow.internal.* -import kotlinx.coroutines.flow.internal.unsafeFlow import kotlin.coroutines.* import kotlin.jvm.* @@ -22,7 +20,7 @@ import kotlin.jvm.* * Deprecated functions also are moved here when they renamed. The difference is that they have * a body with their implementation while pure stubs have [noImpl]. */ -private fun noImpl(): Nothing = +internal fun noImpl(): Nothing = throw UnsupportedOperationException("Not implemented, should not be called") /** @@ -99,7 +97,7 @@ public fun Flow.publishOn(context: CoroutineContext): Flow = noImpl() * Opposed to subscribeOn, it it **possible** to use multiple `flowOn` operators in the one flow * @suppress */ -@Deprecated(message = "Use flowOn instead", level = DeprecationLevel.ERROR) +@Deprecated(message = "Use 'flowOn' instead", level = DeprecationLevel.ERROR) public fun Flow.subscribeOn(context: CoroutineContext): Flow = noImpl() /** @@ -151,7 +149,7 @@ public fun Flow.onErrorResumeNext(fallback: Flow): Flow = noImpl() * @suppress */ @Deprecated( - message = "Use launchIn with onEach, onCompletion and catch operators instead", + message = "Use 'launchIn' with 'onEach', 'onCompletion' and 'catch' instead", level = DeprecationLevel.ERROR ) public fun Flow.subscribe(): Unit = noImpl() @@ -161,7 +159,7 @@ public fun Flow.subscribe(): Unit = noImpl() * @suppress */ @Deprecated( - message = "Use launchIn with onEach, onCompletion and catch operators instead", + message = "Use 'launchIn' with 'onEach', 'onCompletion' and 'catch' instead", level = DeprecationLevel.ERROR )public fun Flow.subscribe(onEach: suspend (T) -> Unit): Unit = noImpl() @@ -170,7 +168,7 @@ public fun Flow.subscribe(): Unit = noImpl() * @suppress */ @Deprecated( - message = "Use launchIn with onEach, onCompletion and catch operators instead", + message = "Use 'launchIn' with 'onEach', 'onCompletion' and 'catch' instead", level = DeprecationLevel.ERROR )public fun Flow.subscribe(onEach: suspend (T) -> Unit, onError: suspend (Throwable) -> Unit): Unit = noImpl() @@ -181,7 +179,7 @@ public fun Flow.subscribe(): Unit = noImpl() */ @Deprecated( level = DeprecationLevel.ERROR, - message = "Flow analogue is named flatMapConcat", + message = "Flow analogue is 'flatMapConcat'", replaceWith = ReplaceWith("flatMapConcat(mapper)") ) public fun Flow.flatMap(mapper: suspend (T) -> Flow): Flow = noImpl() @@ -369,10 +367,10 @@ public fun Flow.combineLatest(other: Flow, transform: suspen message = "Flow analogue of 'combineLatest' is 'combine'", replaceWith = ReplaceWith("combine(this, other, other2, transform)") ) -public inline fun Flow.combineLatest( +public fun Flow.combineLatest( other: Flow, other2: Flow, - crossinline transform: suspend (T1, T2, T3) -> R + transform: suspend (T1, T2, T3) -> R ) = combine(this, other, other2, transform) @Deprecated( @@ -380,11 +378,11 @@ public inline fun Flow.combineLatest( message = "Flow analogue of 'combineLatest' is 'combine'", replaceWith = ReplaceWith("combine(this, other, other2, other3, transform)") ) -public inline fun Flow.combineLatest( +public fun Flow.combineLatest( other: Flow, other2: Flow, other3: Flow, - crossinline transform: suspend (T1, T2, T3, T4) -> R + transform: suspend (T1, T2, T3, T4) -> R ) = combine(this, other, other2, other3, transform) @Deprecated( @@ -392,12 +390,12 @@ public inline fun Flow.combineLatest( message = "Flow analogue of 'combineLatest' is 'combine'", replaceWith = ReplaceWith("combine(this, other, other2, other3, transform)") ) -public inline fun Flow.combineLatest( +public fun Flow.combineLatest( other: Flow, other2: Flow, other3: Flow, other4: Flow, - crossinline transform: suspend (T1, T2, T3, T4, T5) -> R + transform: suspend (T1, T2, T3, T4, T5) -> R ): Flow = combine(this, other, other2, other3, other4, transform) /** @@ -430,3 +428,57 @@ public fun Flow.delayEach(timeMillis: Long): Flow = onEach { delay(tim replaceWith = ReplaceWith("this.flatMapLatest(transform)") ) public fun Flow.switchMap(transform: suspend (value: T) -> Flow): Flow = flatMapLatest(transform) + +@Deprecated( + level = DeprecationLevel.WARNING, // Since 1.3.8, was experimental when deprecated + message = "'scanReduce' was renamed to 'runningReduce' to be consistent with Kotlin standard library", + replaceWith = ReplaceWith("runningReduce(operation)") +) +public fun Flow.scanReduce(operation: suspend (accumulator: T, value: T) -> T): Flow = runningReduce(operation) + +@Deprecated( + level = DeprecationLevel.ERROR, + message = "Flow analogue of 'publish()' is 'shareIn'. \n" + + "publish().connect() is the default strategy (no extra call is needed), \n" + + "publish().autoConnect() translates to 'started = SharingStared.Lazily' argument, \n" + + "publish().refCount() translates to 'started = SharingStared.WhileSubscribed()' argument.", + replaceWith = ReplaceWith("this.shareIn(scope, 0)") +) +public fun Flow.publish(): Flow = noImpl() + +@Deprecated( + level = DeprecationLevel.ERROR, + message = "Flow analogue of 'publish(bufferSize)' is 'buffer' followed by 'shareIn'. \n" + + "publish().connect() is the default strategy (no extra call is needed), \n" + + "publish().autoConnect() translates to 'started = SharingStared.Lazily' argument, \n" + + "publish().refCount() translates to 'started = SharingStared.WhileSubscribed()' argument.", + replaceWith = ReplaceWith("this.buffer(bufferSize).shareIn(scope, 0)") +) +public fun Flow.publish(bufferSize: Int): Flow = noImpl() + +@Deprecated( + level = DeprecationLevel.ERROR, + message = "Flow analogue of 'replay()' is 'shareIn' with unlimited replay. \n" + + "replay().connect() is the default strategy (no extra call is needed), \n" + + "replay().autoConnect() translates to 'started = SharingStared.Lazily' argument, \n" + + "replay().refCount() translates to 'started = SharingStared.WhileSubscribed()' argument.", + replaceWith = ReplaceWith("this.shareIn(scope, Int.MAX_VALUE)") +) +public fun Flow.replay(): Flow = noImpl() + +@Deprecated( + level = DeprecationLevel.ERROR, + message = "Flow analogue of 'replay(bufferSize)' is 'shareIn' with the specified replay parameter. \n" + + "replay().connect() is the default strategy (no extra call is needed), \n" + + "replay().autoConnect() translates to 'started = SharingStared.Lazily' argument, \n" + + "replay().refCount() translates to 'started = SharingStared.WhileSubscribed()' argument.", + replaceWith = ReplaceWith("this.shareIn(scope, bufferSize)") +) +public fun Flow.replay(bufferSize: Int): Flow = noImpl() + +@Deprecated( + level = DeprecationLevel.ERROR, + message = "Flow analogue of 'cache()' is 'shareIn' with unlimited replay and 'started = SharingStared.Lazily' argument'", + replaceWith = ReplaceWith("this.shareIn(scope, Int.MAX_VALUE, started = SharingStared.Lazily)") +) +public fun Flow.cache(): Flow = noImpl() diff --git a/kotlinx-coroutines-core/common/src/flow/SharedFlow.kt b/kotlinx-coroutines-core/common/src/flow/SharedFlow.kt new file mode 100644 index 0000000000..427041a7bb --- /dev/null +++ b/kotlinx-coroutines-core/common/src/flow/SharedFlow.kt @@ -0,0 +1,658 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.flow + +import kotlinx.coroutines.* +import kotlinx.coroutines.channels.* +import kotlinx.coroutines.flow.internal.* +import kotlinx.coroutines.internal.* +import kotlin.coroutines.* +import kotlin.jvm.* +import kotlin.native.concurrent.* + +/** + * A _hot_ [Flow] that shares emitted values among all its collectors in a broadcast fashion, so that all collectors + * get all emitted values. A shared flow is called _hot_ because its active instance exists independently of the + * presence of collectors. This is opposed to a regular [Flow], such as defined by the [`flow { ... }`][flow] function, + * which is _cold_ and is started separately for each collector. + * + * **Shared flow never completes**. A call to [Flow.collect] on a shared flow never completes normally, and + * neither does a coroutine started by the [Flow.launchIn] function. An active collector of a shared flow is called a _subscriber_. + * + * A subscriber of a shared flow can be cancelled. This usually happens when the scope in which the coroutine is running + * is cancelled. A subscriber to a shared flow is always [cancellable][Flow.cancellable], and checks for + * cancellation before each emission. Note that most terminal operators like [Flow.toList] would also not complete, + * when applied to a shared flow, but flow-truncating operators like [Flow.take] and [Flow.takeWhile] can be used on a + * shared flow to turn it into a completing one. + * + * A [mutable shared flow][MutableSharedFlow] is created using the [MutableSharedFlow(...)] constructor function. + * Its state can be updated by [emitting][MutableSharedFlow.emit] values to it and performing other operations. + * See the [MutableSharedFlow] documentation for details. + * + * [SharedFlow] is useful for broadcasting events that happen inside an application to subscribers that can come and go. + * For example, the following class encapsulates an event bus that distributes events to all subscribers + * in a _rendezvous_ manner, suspending until all subscribers process each event: + * + * ``` + * class EventBus { + * private val _events = MutableSharedFlow() // private mutable shared flow + * val events = _events.asSharedFlow() // publicly exposed as read-only shared flow + * + * suspend fun produceEvent(event: Event) { + * _events.emit(event) // suspends until all subscribers receive it + * } + * } + * ``` + * + * As an alternative to the above usage with the `MutableSharedFlow(...)` constructor function, + * any _cold_ [Flow] can be converted to a shared flow using the [shareIn] operator. + * + * There is a specialized implementation of shared flow for the case where the most recent state value needs + * to be shared. See [StateFlow] for details. + * + * ### Replay cache and buffer + * + * A shared flow keeps a specific number of the most recent values in its _replay cache_. Every new subscriber first + * gets the values from the replay cache and then gets new emitted values. The maximum size of the replay cache is + * specified when the shared flow is created by the `replay` parameter. A snapshot of the current replay cache + * is available via the [replayCache] property and it can be reset with the [MutableSharedFlow.resetReplayCache] function. + * + * A replay cache also provides buffer for emissions to the shared flow, allowing slow subscribers to + * get values from the buffer without suspending emitters. The buffer space determines how much slow subscribers + * can lag from the fast ones. When creating a shared flow, additional buffer capacity beyond replay can be reserved + * using the `extraBufferCapacity` parameter. + * + * A shared flow with a buffer can be configured to avoid suspension of emitters on buffer overflow using + * the `onBufferOverflow` parameter, which is equal to one of the entries of the [BufferOverflow] enum. When a strategy other + * than [SUSPENDED][BufferOverflow.SUSPEND] is configured, emissions to the shared flow never suspend. + * + * ### SharedFlow vs BroadcastChannel + * + * Conceptually shared flow is similar to [BroadcastChannel][BroadcastChannel] + * and is designed to completely replace `BroadcastChannel` in the future. + * It has the following important differences: + * + * * `SharedFlow` is simpler, because it does not have to implement all the [Channel] APIs, which allows + * for faster and simpler implementation. + * * `SharedFlow` supports configurable replay and buffer overflow strategy. + * * `SharedFlow` has a clear separation into a read-only `SharedFlow` interface and a [MutableSharedFlow]. + * * `SharedFlow` cannot be closed like `BroadcastChannel` and can never represent a failure. + * All errors and completion signals should be explicitly _materialized_ if needed. + * + * To migrate [BroadcastChannel] usage to [SharedFlow], start by replacing usages of the `BroadcastChannel(capacity)` + * constructor with `MutableSharedFlow(0, extraBufferCapacity=capacity)` (broadcast channel does not replay + * values to new subscribers). Replace [send][BroadcastChannel.send] and [offer][BroadcastChannel.offer] calls + * with [emit][MutableStateFlow.emit] and [tryEmit][MutableStateFlow.tryEmit], and convert subscribers' code to flow operators. + * + * ### Concurrency + * + * All methods of shared flow are **thread-safe** and can be safely invoked from concurrent coroutines without + * external synchronization. + * + * ### Operator fusion + * + * Application of [flowOn][Flow.flowOn], [buffer] with [RENDEZVOUS][Channel.RENDEZVOUS] capacity, + * or [cancellable] operators to a shared flow has no effect. + * + * ### Implementation notes + * + * Shared flow implementation uses a lock to ensure thread-safety, but suspending collector and emitter coroutines are + * resumed outside of this lock to avoid dead-locks when using unconfined coroutines. Adding new subscribers + * has `O(1)` amortized cost, but emitting has `O(N)` cost, where `N` is the number of subscribers. + * + * ### Not stable for inheritance + * + * **The `SharedFlow` interface is not stable for inheritance in 3rd party libraries**, as new methods + * might be added to this interface in the future, but is stable for use. + * Use the `MutableSharedFlow(replay, ...)` constructor function to create an implementation. + */ +public interface SharedFlow : Flow { + /** + * A snapshot of the replay cache. + */ + public val replayCache: List +} + +/** + * A mutable [SharedFlow] that provides functions to [emit] values to the flow. + * An instance of `MutableSharedFlow` with the given configuration parameters can be created using `MutableSharedFlow(...)` + * constructor function. + * + * See the [SharedFlow] documentation for details on shared flows. + * + * `MutableSharedFlow` is a [SharedFlow] that also provides the abilities to [emit] a value, + * to [tryEmit] without suspension if possible, to track the [subscriptionCount], + * and to [resetReplayCache]. + * + * ### Concurrency + * + * All methods of shared flow are **thread-safe** and can be safely invoked from concurrent coroutines without + * external synchronization. + * + * ### Not stable for inheritance + * + * **The `MutableSharedFlow` interface is not stable for inheritance in 3rd party libraries**, as new methods + * might be added to this interface in the future, but is stable for use. + * Use the `MutableSharedFlow(...)` constructor function to create an implementation. + */ +public interface MutableSharedFlow : SharedFlow, FlowCollector { + /** + * Tries to emit a [value] to this shared flow without suspending. It returns `true` if the value was + * emitted successfully. When this function returns `false`, it means that the call to a plain [emit] + * function will suspend until there is a buffer space available. + * + * A shared flow configured with a [BufferOverflow] strategy other than [SUSPEND][BufferOverflow.SUSPEND] + * (either [DROP_OLDEST][BufferOverflow.DROP_OLDEST] or [DROP_LATEST][BufferOverflow.DROP_LATEST]) never + * suspends on [emit], and thus `tryEmit` to such a shared flow always returns `true`. + */ + public fun tryEmit(value: T): Boolean + + /** + * The number of subscribers (active collectors) to this shared flow. + * + * The integer in the resulting [StateFlow] is not negative and starts with zero for a freshly created + * shared flow. + * + * This state can be used to react to changes in the number of subscriptions to this shared flow. + * For example, if you need to call `onActive` when the first subscriber appears and `onInactive` + * when the last one disappears, you can set it up like this: + * + * ``` + * sharedFlow.subscriptionCount + * .map { count -> count > 0 } // map count into active/inactive flag + * .distinctUntilChanged() // only react to true<->false changes + * .onEach { isActive -> // configure an action + * if (isActive) onActive() else onInactive() + * } + * .launchIn(scope) // launch it + * ``` + */ + public val subscriptionCount: StateFlow + + /** + * Resets the [replayCache] of this shared flow to an empty state. + * New subscribers will be receiving only the values that were emitted after this call, + * while old subscribers will still be receiving previously buffered values. + * To reset a shared flow to an initial value, emit the value after this call. + * + * On a [MutableStateFlow], which always contains a single value, this function is not + * supported, and throws an [UnsupportedOperationException]. To reset a [MutableStateFlow] + * to an initial value, just update its [value][MutableStateFlow.value]. + * + * **Note: This is an experimental api.** This function may be removed or renamed in the future. + */ + @ExperimentalCoroutinesApi + public fun resetReplayCache() +} + +/** + * Creates a [MutableSharedFlow] with the given configuration parameters. + * + * This function throws [IllegalArgumentException] on unsupported values of parameters or combinations thereof. + * + * @param replay the number of values replayed to new subscribers (cannot be negative, defaults to zero). + * @param extraBufferCapacity the number of values buffered in addition to `replay`. + * [emit][MutableSharedFlow.emit] does not suspend while there is a buffer space remaining (optional, cannot be negative, defaults to zero). + * @param onBufferOverflow configures an action on buffer overflow (optional, defaults to + * [suspending][BufferOverflow.SUSPEND] attempts to [emit][MutableSharedFlow.emit] a value, + * supported only when `replay > 0` or `extraBufferCapacity > 0`). + */ +@Suppress("FunctionName", "UNCHECKED_CAST") +public fun MutableSharedFlow( + replay: Int = 0, + extraBufferCapacity: Int = 0, + onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND +): MutableSharedFlow { + require(replay >= 0) { "replay cannot be negative, but was $replay" } + require(extraBufferCapacity >= 0) { "extraBufferCapacity cannot be negative, but was $extraBufferCapacity" } + require(replay > 0 || extraBufferCapacity > 0 || onBufferOverflow == BufferOverflow.SUSPEND) { + "replay or extraBufferCapacity must be positive with non-default onBufferOverflow strategy $onBufferOverflow" + } + val bufferCapacity0 = replay + extraBufferCapacity + val bufferCapacity = if (bufferCapacity0 < 0) Int.MAX_VALUE else bufferCapacity0 // coerce to MAX_VALUE on overflow + return SharedFlowImpl(replay, bufferCapacity, onBufferOverflow) +} + +// ------------------------------------ Implementation ------------------------------------ + +private class SharedFlowSlot : AbstractSharedFlowSlot>() { + @JvmField + var index = -1L // current "to-be-emitted" index, -1 means the slot is free now + + @JvmField + var cont: Continuation? = null // collector waiting for new value + + override fun allocateLocked(flow: SharedFlowImpl<*>): Boolean { + if (index >= 0) return false // not free + index = flow.updateNewCollectorIndexLocked() + return true + } + + override fun freeLocked(flow: SharedFlowImpl<*>): Array?> { + assert { index >= 0 } + val oldIndex = index + index = -1L + cont = null // cleanup continuation reference + return flow.updateCollectorIndexLocked(oldIndex) + } +} + +private class SharedFlowImpl( + private val replay: Int, + private val bufferCapacity: Int, + private val onBufferOverflow: BufferOverflow +) : AbstractSharedFlow(), MutableSharedFlow, CancellableFlow, FusibleFlow { + /* + Logical structure of the buffer + + buffered values + /-----------------------\ + replayCache queued emitters + /----------\/----------------------\ + +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ + | | 1 | 2 | 3 | 4 | 5 | 6 | E | E | E | E | E | E | | | | + +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ + ^ ^ ^ ^ + | | | | + head | head + bufferSize head + totalSize + | | | + index of the slowest | index of the fastest + possible collector | possible collector + | | + | replayIndex == new collector's index + \---------------------- / + range of possible minCollectorIndex + + head == minOf(minCollectorIndex, replayIndex) // by definition + totalSize == bufferSize + queueSize // by definition + + INVARIANTS: + minCollectorIndex = activeSlots.minOf { it.index } ?: (head + bufferSize) + replayIndex <= head + bufferSize + */ + + // Stored state + private var buffer: Array? = null // allocated when needed, allocated size always power of two + private var replayIndex = 0L // minimal index from which new collector gets values + private var minCollectorIndex = 0L // minimal index of active collectors, equal to replayIndex if there are none + private var bufferSize = 0 // number of buffered values + private var queueSize = 0 // number of queued emitters + + // Computed state + private val head: Long get() = minOf(minCollectorIndex, replayIndex) + private val replaySize: Int get() = (head + bufferSize - replayIndex).toInt() + private val totalSize: Int get() = bufferSize + queueSize + private val bufferEndIndex: Long get() = head + bufferSize + private val queueEndIndex: Long get() = head + bufferSize + queueSize + + override val replayCache: List + get() = synchronized(this) { + val replaySize = this.replaySize + if (replaySize == 0) return emptyList() + val result = ArrayList(replaySize) + val buffer = buffer!! // must be allocated, because replaySize > 0 + @Suppress("UNCHECKED_CAST") + for (i in 0 until replaySize) result += buffer.getBufferAt(replayIndex + i) as T + result + } + + @Suppress("UNCHECKED_CAST") + override suspend fun collect(collector: FlowCollector) { + val slot = allocateSlot() + try { + if (collector is SubscribedFlowCollector) collector.onSubscription() + val collectorJob = currentCoroutineContext()[Job] + while (true) { + var newValue: Any? + while (true) { + newValue = tryTakeValue(slot) // attempt no-suspend fast path first + if (newValue !== NO_VALUE) break + awaitValue(slot) // await signal that the new value is available + } + collectorJob?.ensureActive() + collector.emit(newValue as T) + } + } finally { + freeSlot(slot) + } + } + + override fun tryEmit(value: T): Boolean { + var resumes: Array?> = EMPTY_RESUMES + val emitted = synchronized(this) { + if (tryEmitLocked(value)) { + resumes = findSlotsToResumeLocked(resumes) + true + } else { + false + } + } + for (cont in resumes) cont?.resume(Unit) + return emitted + } + + override suspend fun emit(value: T) { + if (tryEmit(value)) return // fast-path + emitSuspend(value) + } + + @Suppress("UNCHECKED_CAST") + private fun tryEmitLocked(value: T): Boolean { + // Fast path without collectors -> no buffering + if (nCollectors == 0) return tryEmitNoCollectorsLocked(value) // always returns true + // With collectors we'll have to buffer + // cannot emit now if buffer is full & blocked by slow collectors + if (bufferSize >= bufferCapacity && minCollectorIndex <= replayIndex) { + when (onBufferOverflow) { + BufferOverflow.SUSPEND -> return false // will suspend + BufferOverflow.DROP_LATEST -> return true // just drop incoming + BufferOverflow.DROP_OLDEST -> {} // force enqueue & drop oldest instead + } + } + enqueueLocked(value) + bufferSize++ // value was added to buffer + // drop oldest from the buffer if it became more than bufferCapacity + if (bufferSize > bufferCapacity) dropOldestLocked() + // keep replaySize not larger that needed + if (replaySize > replay) { // increment replayIndex by one + updateBufferLocked(replayIndex + 1, minCollectorIndex, bufferEndIndex, queueEndIndex) + } + return true + } + + private fun tryEmitNoCollectorsLocked(value: T): Boolean { + assert { nCollectors == 0 } + if (replay == 0) return true // no need to replay, just forget it now + enqueueLocked(value) // enqueue to replayCache + bufferSize++ // value was added to buffer + // drop oldest from the buffer if it became more than replay + if (bufferSize > replay) dropOldestLocked() + minCollectorIndex = head + bufferSize // a default value (max allowed) + return true + } + + private fun dropOldestLocked() { + buffer!!.setBufferAt(head, null) + bufferSize-- + val newHead = head + 1 + if (replayIndex < newHead) replayIndex = newHead + if (minCollectorIndex < newHead) correctCollectorIndexesOnDropOldest(newHead) + assert { head == newHead } // since head = minOf(minCollectorIndex, replayIndex) it should have updated + } + + private fun correctCollectorIndexesOnDropOldest(newHead: Long) { + forEachSlotLocked { slot -> + @Suppress("ConvertTwoComparisonsToRangeCheck") // Bug in JS backend + if (slot.index >= 0 && slot.index < newHead) { + slot.index = newHead // force move it up (this collector was too slow and missed the value at its index) + } + } + minCollectorIndex = newHead + } + + // enqueues item to buffer array, caller shall increment either bufferSize or queueSize + private fun enqueueLocked(item: Any?) { + val curSize = totalSize + val buffer = when (val curBuffer = buffer) { + null -> growBuffer(null, 0, 2) + else -> if (curSize >= curBuffer.size) growBuffer(curBuffer, curSize,curBuffer.size * 2) else curBuffer + } + buffer.setBufferAt(head + curSize, item) + } + + private fun growBuffer(curBuffer: Array?, curSize: Int, newSize: Int): Array { + check(newSize > 0) { "Buffer size overflow" } + val newBuffer = arrayOfNulls(newSize).also { buffer = it } + if (curBuffer == null) return newBuffer + val head = head + for (i in 0 until curSize) { + newBuffer.setBufferAt(head + i, curBuffer.getBufferAt(head + i)) + } + return newBuffer + } + + private suspend fun emitSuspend(value: T) = suspendCancellableCoroutine sc@{ cont -> + var resumes: Array?> = EMPTY_RESUMES + val emitter = synchronized(this) lock@{ + // recheck buffer under lock again (make sure it is really full) + if (tryEmitLocked(value)) { + cont.resume(Unit) + resumes = findSlotsToResumeLocked(resumes) + return@lock null + } + // add suspended emitter to the buffer + Emitter(this, head + totalSize, value, cont).also { + enqueueLocked(it) + queueSize++ // added to queue of waiting emitters + // synchronous shared flow might rendezvous with waiting emitter + if (bufferCapacity == 0) resumes = findSlotsToResumeLocked(resumes) + } + } + // outside of the lock: register dispose on cancellation + emitter?.let { cont.disposeOnCancellation(it) } + // outside of the lock: resume slots if needed + for (cont in resumes) cont?.resume(Unit) + } + + private fun cancelEmitter(emitter: Emitter) = synchronized(this) { + if (emitter.index < head) return // already skipped past this index + val buffer = buffer!! + if (buffer.getBufferAt(emitter.index) !== emitter) return // already resumed + buffer.setBufferAt(emitter.index, NO_VALUE) + cleanupTailLocked() + } + + internal fun updateNewCollectorIndexLocked(): Long { + val index = replayIndex + if (index < minCollectorIndex) minCollectorIndex = index + return index + } + + // Is called when a collector disappears or changes index, returns a list of continuations to resume after lock + internal fun updateCollectorIndexLocked(oldIndex: Long): Array?> { + assert { oldIndex >= minCollectorIndex } + if (oldIndex > minCollectorIndex) return EMPTY_RESUMES // nothing changes, it was not min + // start computing new minimal index of active collectors + val head = head + var newMinCollectorIndex = head + bufferSize + // take into account a special case of sync shared flow that can go past 1st queued emitter + if (bufferCapacity == 0 && queueSize > 0) newMinCollectorIndex++ + forEachSlotLocked { slot -> + @Suppress("ConvertTwoComparisonsToRangeCheck") // Bug in JS backend + if (slot.index >= 0 && slot.index < newMinCollectorIndex) newMinCollectorIndex = slot.index + } + assert { newMinCollectorIndex >= minCollectorIndex } // can only grow + if (newMinCollectorIndex <= minCollectorIndex) return EMPTY_RESUMES // nothing changes + // Compute new buffer size if we drop items we no longer need and no emitter is resumed: + // We must keep all the items from newMinIndex to the end of buffer + var newBufferEndIndex = bufferEndIndex // var to grow when waiters are resumed + val maxResumeCount = if (nCollectors > 0) { + // If we have collectors we can resume up to maxResumeCount waiting emitters + // a) queueSize -> that's how many waiting emitters we have + // b) bufferCapacity - newBufferSize0 -> that's how many we can afford to resume to add w/o exceeding bufferCapacity + val newBufferSize0 = (newBufferEndIndex - newMinCollectorIndex).toInt() + minOf(queueSize, bufferCapacity - newBufferSize0) + } else { + // If we don't have collectors anymore we must resume all waiting emitters + queueSize // that's how many waiting emitters we have (at most) + } + var resumes: Array?> = EMPTY_RESUMES + val newQueueEndIndex = newBufferEndIndex + queueSize + if (maxResumeCount > 0) { // collect emitters to resume if we have them + resumes = arrayOfNulls(maxResumeCount) + var resumeCount = 0 + val buffer = buffer!! + for (curEmitterIndex in newBufferEndIndex until newQueueEndIndex) { + val emitter = buffer.getBufferAt(curEmitterIndex) + if (emitter !== NO_VALUE) { + emitter as Emitter // must have Emitter class + resumes[resumeCount++] = emitter.cont + buffer.setBufferAt(curEmitterIndex, NO_VALUE) // make as canceled if we moved ahead + buffer.setBufferAt(newBufferEndIndex, emitter.value) + newBufferEndIndex++ + if (resumeCount >= maxResumeCount) break // enough resumed, done + } + } + } + // Compute new buffer size -> how many values we now actually have after resume + val newBufferSize1 = (newBufferEndIndex - head).toInt() + // Compute new replay size -> limit to replay the number of items we need, take into account that it can only grow + var newReplayIndex = maxOf(replayIndex, newBufferEndIndex - minOf(replay, newBufferSize1)) + // adjustment for synchronous case with cancelled emitter (NO_VALUE) + if (bufferCapacity == 0 && newReplayIndex < newQueueEndIndex && buffer!!.getBufferAt(newReplayIndex) == NO_VALUE) { + newBufferEndIndex++ + newReplayIndex++ + } + // Update buffer state + updateBufferLocked(newReplayIndex, newMinCollectorIndex, newBufferEndIndex, newQueueEndIndex) + // just in case we've moved all buffered emitters and have NO_VALUE's at the tail now + cleanupTailLocked() + // We need to waken up suspended collectors if any emitters were resumed here + if (resumes.isNotEmpty()) resumes = findSlotsToResumeLocked(resumes) + return resumes + } + + private fun updateBufferLocked( + newReplayIndex: Long, + newMinCollectorIndex: Long, + newBufferEndIndex: Long, + newQueueEndIndex: Long + ) { + // Compute new head value + val newHead = minOf(newMinCollectorIndex, newReplayIndex) + assert { newHead >= head } + // cleanup items we don't have to buffer anymore (because head is about to move) + for (index in head until newHead) buffer!!.setBufferAt(index, null) + // update all state variables to newly computed values + replayIndex = newReplayIndex + minCollectorIndex = newMinCollectorIndex + bufferSize = (newBufferEndIndex - newHead).toInt() + queueSize = (newQueueEndIndex - newBufferEndIndex).toInt() + // check our key invariants (just in case) + assert { bufferSize >= 0 } + assert { queueSize >= 0 } + assert { replayIndex <= this.head + bufferSize } + } + + // Removes all the NO_VALUE items from the end of the queue and reduces its size + private fun cleanupTailLocked() { + // If we have synchronous case, then keep one emitter queued + if (bufferCapacity == 0 && queueSize <= 1) return // return, don't clear it + val buffer = buffer!! + while (queueSize > 0 && buffer.getBufferAt(head + totalSize - 1) === NO_VALUE) { + queueSize-- + buffer.setBufferAt(head + totalSize, null) + } + } + + // returns NO_VALUE if cannot take value without suspension + private fun tryTakeValue(slot: SharedFlowSlot): Any? { + var resumes: Array?> = EMPTY_RESUMES + val value = synchronized(this) { + val index = tryPeekLocked(slot) + if (index < 0) { + NO_VALUE + } else { + val oldIndex = slot.index + val newValue = getPeekedValueLockedAt(index) + slot.index = index + 1 // points to the next index after peeked one + resumes = updateCollectorIndexLocked(oldIndex) + newValue + } + } + for (resume in resumes) resume?.resume(Unit) + return value + } + + // returns -1 if cannot peek value without suspension + private fun tryPeekLocked(slot: SharedFlowSlot): Long { + // return buffered value if possible + val index = slot.index + if (index < bufferEndIndex) return index + if (bufferCapacity > 0) return -1L // if there's a buffer, never try to rendezvous with emitters + // Synchronous shared flow (bufferCapacity == 0) tries to rendezvous + if (index > head) return -1L // ... but only with the first emitter (never look forward) + if (queueSize == 0) return -1L // nothing there to rendezvous with + return index // rendezvous with the first emitter + } + + private fun getPeekedValueLockedAt(index: Long): Any? = + when (val item = buffer!!.getBufferAt(index)) { + is Emitter -> item.value + else -> item + } + + private suspend fun awaitValue(slot: SharedFlowSlot): Unit = suspendCancellableCoroutine { cont -> + synchronized(this) lock@{ + val index = tryPeekLocked(slot) // recheck under this lock + if (index < 0) { + slot.cont = cont // Ok -- suspending + } else { + cont.resume(Unit) // has value, no need to suspend + return@lock + } + slot.cont = cont // suspend, waiting + } + } + + private fun findSlotsToResumeLocked(resumesIn: Array?>): Array?> { + var resumes: Array?> = resumesIn + var resumeCount = resumesIn.size + forEachSlotLocked loop@{ slot -> + val cont = slot.cont ?: return@loop // only waiting slots + if (tryPeekLocked(slot) < 0) return@loop // only slots that can peek a value + if (resumeCount >= resumes.size) resumes = resumes.copyOf(maxOf(2, 2 * resumes.size)) + resumes[resumeCount++] = cont + slot.cont = null // not waiting anymore + } + return resumes + } + + override fun createSlot() = SharedFlowSlot() + override fun createSlotArray(size: Int): Array = arrayOfNulls(size) + + override fun resetReplayCache() = synchronized(this) { + // Update buffer state + updateBufferLocked( + newReplayIndex = bufferEndIndex, + newMinCollectorIndex = minCollectorIndex, + newBufferEndIndex = bufferEndIndex, + newQueueEndIndex = queueEndIndex + ) + } + + override fun fuse(context: CoroutineContext, capacity: Int, onBufferOverflow: BufferOverflow) = + fuseSharedFlow(context, capacity, onBufferOverflow) + + private class Emitter( + @JvmField val flow: SharedFlowImpl<*>, + @JvmField var index: Long, + @JvmField val value: Any?, + @JvmField val cont: Continuation + ) : DisposableHandle { + override fun dispose() = flow.cancelEmitter(this) + } +} + +@SharedImmutable +@JvmField +internal val NO_VALUE = Symbol("NO_VALUE") + +private fun Array.getBufferAt(index: Long) = get(index.toInt() and (size - 1)) +private fun Array.setBufferAt(index: Long, item: Any?) = set(index.toInt() and (size - 1), item) + +internal fun SharedFlow.fuseSharedFlow( + context: CoroutineContext, + capacity: Int, + onBufferOverflow: BufferOverflow +): Flow { + // context is irrelevant for shared flow and making additional rendezvous is meaningless + // however, additional non-trivial buffering after shared flow could make sense for very slow subscribers + if ((capacity == Channel.RENDEZVOUS || capacity == Channel.OPTIONAL_CHANNEL) && onBufferOverflow == BufferOverflow.SUSPEND) { + return this + } + // Apply channel flow operator as usual + return ChannelFlowOperatorImpl(this, context, capacity, onBufferOverflow) +} diff --git a/kotlinx-coroutines-core/common/src/flow/SharingStarted.kt b/kotlinx-coroutines-core/common/src/flow/SharingStarted.kt new file mode 100644 index 0000000000..19e5fa36c7 --- /dev/null +++ b/kotlinx-coroutines-core/common/src/flow/SharingStarted.kt @@ -0,0 +1,210 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.flow + +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.internal.* +import kotlin.time.* + +/** + * A command emitted by [SharingStarted] implementations to control the sharing coroutine in + * the [shareIn] and [stateIn] operators. + */ +public enum class SharingCommand { + /** + * Starts sharing, launching collection of the upstream flow. + * + * Emitting this command again does not do anything. Emit [STOP] and then [START] to restart an + * upstream flow. + */ + START, + + /** + * Stops sharing, cancelling collection of the upstream flow. + */ + STOP, + + /** + * Stops sharing, cancelling collection of the upstream flow, and resets the [SharedFlow.replayCache] + * to its initial state. + * The [shareIn] operator calls [MutableSharedFlow.resetReplayCache]; + * the [stateIn] operator resets the value to its original `initialValue`. + */ + STOP_AND_RESET_REPLAY_CACHE +} + +/** + * A strategy for starting and stopping the sharing coroutine in [shareIn] and [stateIn] operators. + * + * This interface provides a set of built-in strategies: [Eagerly], [Lazily], [WhileSubscribed], and + * supports custom strategies by implementing this interface's [command] function. + * + * For example, it is possible to define a custom strategy that starts the upstream only when the number + * of subscribers exceeds the given `threshold` and make it an extension on [SharingStarted.Companion] so + * that it looks like a built-in strategy on the use-site: + * + * ``` + * fun SharingStarted.Companion.WhileSubscribedAtLeast(threshold: Int): SharingStarted = + * object : SharingStarted { + * override fun command(subscriptionCount: StateFlow): Flow = + * subscriptionCount + * .map { if (it >= threshold) SharingCommand.START else SharingCommand.STOP } + * } + * ``` + * + * ### Commands + * + * The `SharingStarted` strategy works by emitting [commands][SharingCommand] that control upstream flow from its + * [`command`][command] flow implementation function. Back-to-back emissions of the same command have no effect. + * Only emission of a different command has effect: + * + * * [START][SharingCommand.START] — the upstream flow is stared. + * * [STOP][SharingCommand.STOP] — the upstream flow is stopped. + * * [STOP_AND_RESET_REPLAY_CACHE][SharingCommand.STOP_AND_RESET_REPLAY_CACHE] — + * the upstream flow is stopped and the [SharedFlow.replayCache] is reset to its initial state. + * The [shareIn] operator calls [MutableSharedFlow.resetReplayCache]; + * the [stateIn] operator resets the value to its original `initialValue`. + * + * Initially, the upstream flow is stopped and is in the initial state, so the emission of additional + * [STOP][SharingCommand.STOP] and [STOP_AND_RESET_REPLAY_CACHE][SharingCommand.STOP_AND_RESET_REPLAY_CACHE] commands will + * have no effect. + * + * The completion of the `command` flow normally has no effect (the upstream flow keeps running if it was running). + * The failure of the `command` flow cancels the sharing coroutine and the upstream flow. + */ +public interface SharingStarted { + public companion object { + /** + * Sharing is started immediately and never stops. + */ + public val Eagerly: SharingStarted = StartedEagerly() + + /** + * Sharing is started when the first subscriber appears and never stops. + */ + public val Lazily: SharingStarted = StartedLazily() + + /** + * Sharing is started when the first subscriber appears, immediately stops when the last + * subscriber disappears (by default), keeping the replay cache forever (by default). + * + * It has the following optional parameters: + * + * * [stopTimeoutMillis] — configures a delay (in milliseconds) between the disappearance of the last + * subscriber and the stopping of the sharing coroutine. It defaults to zero (stop immediately). + * * [replayExpirationMillis] — configures a delay (in milliseconds) between the stopping of + * the sharing coroutine and the resetting of the replay cache (which makes the cache empty for the [shareIn] operator + * and resets the cached value to the original `initialValue` for the [stateIn] operator). + * It defaults to `Long.MAX_VALUE` (keep replay cache forever, never reset buffer). + * Use zero value to expire the cache immediately. + * + * This function throws [IllegalArgumentException] when either [stopTimeoutMillis] or [replayExpirationMillis] + * are negative. + */ + @Suppress("FunctionName") + public fun WhileSubscribed( + stopTimeoutMillis: Long = 0, + replayExpirationMillis: Long = Long.MAX_VALUE + ): SharingStarted = + StartedWhileSubscribed(stopTimeoutMillis, replayExpirationMillis) + } + + /** + * Transforms the [subscriptionCount][MutableSharedFlow.subscriptionCount] state of the shared flow into the + * flow of [commands][SharingCommand] that control the sharing coroutine. See the [SharingStarted] interface + * documentation for details. + */ + public fun command(subscriptionCount: StateFlow): Flow +} + +/** + * Sharing is started when the first subscriber appears, immediately stops when the last + * subscriber disappears (by default), keeping the replay cache forever (by default). + * + * It has the following optional parameters: + * + * * [stopTimeout] — configures a delay between the disappearance of the last + * subscriber and the stopping of the sharing coroutine. It defaults to zero (stop immediately). + * * [replayExpiration] — configures a delay between the stopping of + * the sharing coroutine and the resetting of the replay cache (which makes the cache empty for the [shareIn] operator + * and resets the cached value to the original `initialValue` for the [stateIn] operator). + * It defaults to [Duration.INFINITE] (keep replay cache forever, never reset buffer). + * Use [Duration.ZERO] value to expire the cache immediately. + * + * This function throws [IllegalArgumentException] when either [stopTimeout] or [replayExpiration] + * are negative. + */ +@Suppress("FunctionName") +@ExperimentalTime +public fun SharingStarted.Companion.WhileSubscribed( + stopTimeout: Duration = Duration.ZERO, + replayExpiration: Duration = Duration.INFINITE +): SharingStarted = + StartedWhileSubscribed(stopTimeout.toLongMilliseconds(), replayExpiration.toLongMilliseconds()) + +// -------------------------------- implementation -------------------------------- + +private class StartedEagerly : SharingStarted { + override fun command(subscriptionCount: StateFlow): Flow = + flowOf(SharingCommand.START) + override fun toString(): String = "SharingStarted.Eagerly" +} + +private class StartedLazily : SharingStarted { + override fun command(subscriptionCount: StateFlow): Flow = flow { + var started = false + subscriptionCount.collect { count -> + if (count > 0 && !started) { + started = true + emit(SharingCommand.START) + } + } + } + + override fun toString(): String = "SharingStarted.Lazily" +} + +private class StartedWhileSubscribed( + private val stopTimeout: Long, + private val replayExpiration: Long +) : SharingStarted { + init { + require(stopTimeout >= 0) { "stopTimeout($stopTimeout ms) cannot be negative" } + require(replayExpiration >= 0) { "replayExpiration($replayExpiration ms) cannot be negative" } + } + + override fun command(subscriptionCount: StateFlow): Flow = subscriptionCount + .transformLatest { count -> + if (count > 0) { + emit(SharingCommand.START) + } else { + delay(stopTimeout) + if (replayExpiration > 0) { + emit(SharingCommand.STOP) + delay(replayExpiration) + } + emit(SharingCommand.STOP_AND_RESET_REPLAY_CACHE) + } + } + .dropWhile { it != SharingCommand.START } // don't emit any STOP/RESET_BUFFER to start with, only START + .distinctUntilChanged() // just in case somebody forgets it, don't leak our multiple sending of START + + @OptIn(ExperimentalStdlibApi::class) + override fun toString(): String { + val params = buildList(2) { + if (stopTimeout > 0) add("stopTimeout=${stopTimeout}ms") + if (replayExpiration < Long.MAX_VALUE) add("replayExpiration=${replayExpiration}ms") + } + return "SharingStarted.WhileSubscribed(${params.joinToString()})" + } + + // equals & hashcode to facilitate testing, not documented in public contract + override fun equals(other: Any?): Boolean = + other is StartedWhileSubscribed && + stopTimeout == other.stopTimeout && + replayExpiration == other.replayExpiration + + override fun hashCode(): Int = stopTimeout.hashCode() * 31 + replayExpiration.hashCode() +} diff --git a/kotlinx-coroutines-core/common/src/flow/StateFlow.kt b/kotlinx-coroutines-core/common/src/flow/StateFlow.kt new file mode 100644 index 0000000000..a9a4ed3d24 --- /dev/null +++ b/kotlinx-coroutines-core/common/src/flow/StateFlow.kt @@ -0,0 +1,380 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.flow + +import kotlinx.atomicfu.* +import kotlinx.coroutines.* +import kotlinx.coroutines.channels.* +import kotlinx.coroutines.flow.internal.* +import kotlinx.coroutines.internal.* +import kotlin.coroutines.* +import kotlin.native.concurrent.* + +/** + * A [SharedFlow] that represents a read-only state with a single updatable data [value] that emits updates + * to the value to its collectors. A state flow is a _hot_ flow because its active instance exists independently + * of the presence of collectors. Its current value can be retrieved via the [value] property. + * + * **State flow never completes**. A call to [Flow.collect] on a state flow never completes normally, and + * neither does a coroutine started by the [Flow.launchIn] function. An active collector of a state flow is called a _subscriber_. + * + * A [mutable state flow][MutableStateFlow] is created using `MutableStateFlow(value)` constructor function with + * the initial value. The value of mutable state flow can be updated by setting its [value] property. + * Updates to the [value] are always [conflated][Flow.conflate]. So a slow collector skips fast updates, + * but always collects the most recently emitted value. + * + * [StateFlow] is useful as a data-model class to represent any kind of state. + * Derived values can be defined using various operators on the flows, with [combine] operator being especially + * useful to combine values from multiple state flows using arbitrary functions. + * + * For example, the following class encapsulates an integer state and increments its value on each call to `inc`: + * + * ``` + * class CounterModel { + * private val _counter = MutableStateFlow(0) // private mutable state flow + * val counter = _counter.asStateFlow() // publicly exposed as read-only state flow + * + * fun inc() { + * _counter.value++ + * } + * } + * ``` + * + * Having two instances of the above `CounterModel` class one can define the sum of their counters like this: + * + * ``` + * val aModel = CounterModel() + * val bModel = CounterModel() + * val sumFlow: Flow = aModel.counter.combine(bModel.counter) { a, b -> a + b } + * ``` + * + * As an alternative to the above usage with the `MutableStateFlow(...)` constructor function, + * any _cold_ [Flow] can be converted to a state flow using the [stateIn] operator. + * + * ### Strong equality-based conflation + * + * Values in state flow are conflated using [Any.equals] comparison in a similar way to + * [distinctUntilChanged] operator. It is used to conflate incoming updates + * to [value][MutableStateFlow.value] in [MutableStateFlow] and to suppress emission of the values to collectors + * when new value is equal to the previously emitted one. State flow behavior with classes that violate + * the contract for [Any.equals] is unspecified. + * + * ### State flow is a shared flow + * + * State flow is a special-purpose, high-performance, and efficient implementation of [SharedFlow] for the narrow, + * but widely used case of sharing a state. See the [SharedFlow] documentation for the basic rules, + * constraints, and operators that are applicable to all shared flows. + * + * State flow always has an initial value, replays one most recent value to new subscribers, does not buffer any + * more values, but keeps the last emitted one, and does not support [resetReplayCache][MutableSharedFlow.resetReplayCache]. + * A state flow behaves identically to a shared flow when it is created + * with the following parameters and the [distinctUntilChanged] operator is applied to it: + * + * ``` + * // MutableStateFlow(initialValue) is a shared flow with the following parameters: + * val shared = MutableSharedFlow( + * replay = 1, + * onBufferOverflow = BufferOverflow.DROP_OLDEST + * ) + * shared.tryEmit(initialValue) // emit the initial value + * val state = shared.distinctUntilChanged() // get StateFlow-like behavior + * ``` + * + * Use [SharedFlow] when you need a [StateFlow] with tweaks in its behavior such as extra buffering, replaying more + * values, or omitting the initial value. + * + * ### StateFlow vs ConflatedBroadcastChannel + * + * Conceptually, state flow is similar to [ConflatedBroadcastChannel] + * and is designed to completely replace `ConflatedBroadcastChannel` in the future. + * It has the following important differences: + * + * * `StateFlow` is simpler, because it does not have to implement all the [Channel] APIs, which allows + * for faster, garbage-free implementation, unlike `ConflatedBroadcastChannel` implementation that + * allocates objects on each emitted value. + * * `StateFlow` always has a value which can be safely read at any time via [value] property. + * Unlike `ConflatedBroadcastChannel`, there is no way to create a state flow without a value. + * * `StateFlow` has a clear separation into a read-only `StateFlow` interface and a [MutableStateFlow]. + * * `StateFlow` conflation is based on equality like [distinctUntilChanged] operator, + * unlike conflation in `ConflatedBroadcastChannel` that is based on reference identity. + * * `StateFlow` cannot be closed like `ConflatedBroadcastChannel` and can never represent a failure. + * All errors and completion signals should be explicitly _materialized_ if needed. + * + * `StateFlow` is designed to better cover typical use-cases of keeping track of state changes in time, taking + * more pragmatic design choices for the sake of convenience. + * + * To migrate [ConflatedBroadcastChannel] usage to [StateFlow], start by replacing usages of the `ConflatedBroadcastChannel()` + * constructor with `MutableStateFlow(initialValue)`, using `null` as an initial value if you don't have one. + * Replace [send][ConflatedBroadcastChannel.send] and [offer][ConflatedBroadcastChannel.offer] calls + * with updates to the state flow's [MutableStateFlow.value], and convert subscribers' code to flow operators. + * You can use the [filterNotNull] operator to mimic behavior of a `ConflatedBroadcastChannel` without initial value. + * + * ### Concurrency + * + * All methods of state flow are **thread-safe** and can be safely invoked from concurrent coroutines without + * external synchronization. + * + * ### Operator fusion + * + * Application of [flowOn][Flow.flowOn], [conflate][Flow.conflate], + * [buffer] with [CONFLATED][Channel.CONFLATED] or [RENDEZVOUS][Channel.RENDEZVOUS] capacity, + * [distinctUntilChanged][Flow.distinctUntilChanged], or [cancellable] operators to a state flow has no effect. + * + * ### Implementation notes + * + * State flow implementation is optimized for memory consumption and allocation-freedom. It uses a lock to ensure + * thread-safety, but suspending collector coroutines are resumed outside of this lock to avoid dead-locks when + * using unconfined coroutines. Adding new subscribers has `O(1)` amortized cost, but updating a [value] has `O(N)` + * cost, where `N` is the number of active subscribers. + * + * ### Not stable for inheritance + * + * **`The StateFlow` interface is not stable for inheritance in 3rd party libraries**, as new methods + * might be added to this interface in the future, but is stable for use. + * Use the `MutableStateFlow(value)` constructor function to create an implementation. + */ +public interface StateFlow : SharedFlow { + /** + * The current value of this state flow. + */ + public val value: T +} + +/** + * A mutable [StateFlow] that provides a setter for [value]. + * An instance of `MutableStateFlow` with the given initial `value` can be created using + * `MutableStateFlow(value)` constructor function. + * + * See the [StateFlow] documentation for details on state flows. + * + * ### Not stable for inheritance + * + * **The `MutableStateFlow` interface is not stable for inheritance in 3rd party libraries**, as new methods + * might be added to this interface in the future, but is stable for use. + * Use the `MutableStateFlow()` constructor function to create an implementation. + */ +public interface MutableStateFlow : StateFlow, MutableSharedFlow { + /** + * The current value of this state flow. + * + * Setting a value that is [equal][Any.equals] to the previous one does nothing. + */ + public override var value: T + + /** + * Atomically compares the current [value] with [expect] and sets it to [update] if it is equal to [expect]. + * The result is `true` if the [value] was set to [update] and `false` otherwise. + * + * This function use a regular comparison using [Any.equals]. If both [expect] and [update] are equal to the + * current [value], this function returns `true`, but it does not actually change the reference that is + * stored in the [value]. + */ + public fun compareAndSet(expect: T, update: T): Boolean +} + +/** + * Creates a [MutableStateFlow] with the given initial [value]. + */ +@Suppress("FunctionName") +public fun MutableStateFlow(value: T): MutableStateFlow = StateFlowImpl(value ?: NULL) + +// ------------------------------------ Implementation ------------------------------------ + +@SharedImmutable +private val NONE = Symbol("NONE") + +@SharedImmutable +private val PENDING = Symbol("PENDING") + +// StateFlow slots are allocated for its collectors +private class StateFlowSlot : AbstractSharedFlowSlot>() { + /** + * Each slot can have one of the following states: + * + * * `null` -- it is not used right now. Can [allocateLocked] to new collector. + * * `NONE` -- used by a collector, but neither suspended nor has pending value. + * * `PENDING` -- pending to process new value. + * * `CancellableContinuationImpl` -- suspended waiting for new value. + * + * It is important that default `null` value is used, because there can be a race between allocation + * of a new slot and trying to do [makePending] on this slot. + */ + private val _state = atomic(null) + + override fun allocateLocked(flow: StateFlowImpl<*>): Boolean { + // No need for atomic check & update here, since allocated happens under StateFlow lock + if (_state.value != null) return false // not free + _state.value = NONE // allocated + return true + } + + override fun freeLocked(flow: StateFlowImpl<*>): Array?> { + _state.value = null // free now + return EMPTY_RESUMES // nothing more to do + } + + @Suppress("UNCHECKED_CAST") + fun makePending() { + _state.loop { state -> + when { + state == null -> return // this slot is free - skip it + state === PENDING -> return // already pending, nothing to do + state === NONE -> { // mark as pending + if (_state.compareAndSet(state, PENDING)) return + } + else -> { // must be a suspend continuation state + // we must still use CAS here since continuation may get cancelled and free the slot at any time + if (_state.compareAndSet(state, NONE)) { + (state as CancellableContinuationImpl).resume(Unit) + return + } + } + } + } + } + + fun takePending(): Boolean = _state.getAndSet(NONE)!!.let { state -> + assert { state !is CancellableContinuationImpl<*> } + return state === PENDING + } + + @Suppress("UNCHECKED_CAST") + suspend fun awaitPending(): Unit = suspendCancellableCoroutine sc@ { cont -> + assert { _state.value !is CancellableContinuationImpl<*> } // can be NONE or PENDING + if (_state.compareAndSet(NONE, cont)) return@sc // installed continuation, waiting for pending + // CAS failed -- the only possible reason is that it is already in pending state now + assert { _state.value === PENDING } + cont.resume(Unit) + } +} + +private class StateFlowImpl( + initialState: Any // T | NULL +) : AbstractSharedFlow(), MutableStateFlow, CancellableFlow, FusibleFlow { + private val _state = atomic(initialState) // T | NULL + private var sequence = 0 // serializes updates, value update is in process when sequence is odd + + @Suppress("UNCHECKED_CAST") + public override var value: T + get() = NULL.unbox(_state.value) + set(value) { updateState(null, value ?: NULL) } + + override fun compareAndSet(expect: T, update: T): Boolean = + updateState(expect ?: NULL, update ?: NULL) + + private fun updateState(expectedState: Any?, newState: Any): Boolean { + var curSequence = 0 + var curSlots: Array? = this.slots // benign race, we will not use it + synchronized(this) { + val oldState = _state.value + if (expectedState != null && oldState != expectedState) return false // CAS support + if (oldState == newState) return true // Don't do anything if value is not changing, but CAS -> true + _state.value = newState + curSequence = sequence + if (curSequence and 1 == 0) { // even sequence means quiescent state flow (no ongoing update) + curSequence++ // make it odd + sequence = curSequence + } else { + // update is already in process, notify it, and return + sequence = curSequence + 2 // change sequence to notify, keep it odd + return true // updated + } + curSlots = slots // read current reference to collectors under lock + } + /* + Fire value updates outside of the lock to avoid deadlocks with unconfined coroutines. + Loop until we're done firing all the changes. This is a sort of simple flat combining that + ensures sequential firing of concurrent updates and avoids the storm of collector resumes + when updates happen concurrently from many threads. + */ + while (true) { + // Benign race on element read from array + curSlots?.forEach { + it?.makePending() + } + // check if the value was updated again while we were updating the old one + synchronized(this) { + if (sequence == curSequence) { // nothing changed, we are done + sequence = curSequence + 1 // make sequence even again + return true // done, updated + } + // reread everything for the next loop under the lock + curSequence = sequence + curSlots = slots + } + } + } + + override val replayCache: List + get() = listOf(value) + + override fun tryEmit(value: T): Boolean { + this.value = value + return true + } + + override suspend fun emit(value: T) { + this.value = value + } + + @Suppress("UNCHECKED_CAST") + override fun resetReplayCache() { + throw UnsupportedOperationException("MutableStateFlow.resetReplayCache is not supported") + } + + override suspend fun collect(collector: FlowCollector) { + val slot = allocateSlot() + try { + if (collector is SubscribedFlowCollector) collector.onSubscription() + val collectorJob = currentCoroutineContext()[Job] + var oldState: Any? = null // previously emitted T!! | NULL (null -- nothing emitted yet) + // The loop is arranged so that it starts delivering current value without waiting first + while (true) { + // Here the coroutine could have waited for a while to be dispatched, + // so we use the most recent state here to ensure the best possible conflation of stale values + val newState = _state.value + // always check for cancellation + collectorJob?.ensureActive() + // Conflate value emissions using equality + if (oldState == null || oldState != newState) { + collector.emit(NULL.unbox(newState)) + oldState = newState + } + // Note: if awaitPending is cancelled, then it bails out of this loop and calls freeSlot + if (!slot.takePending()) { // try fast-path without suspending first + slot.awaitPending() // only suspend for new values when needed + } + } + } finally { + freeSlot(slot) + } + } + + override fun createSlot() = StateFlowSlot() + override fun createSlotArray(size: Int): Array = arrayOfNulls(size) + + override fun fuse(context: CoroutineContext, capacity: Int, onBufferOverflow: BufferOverflow) = + fuseStateFlow(context, capacity, onBufferOverflow) +} + +internal fun MutableStateFlow.increment(delta: Int) { + while (true) { // CAS loop + val current = value + if (compareAndSet(current, current + delta)) return + } +} + +internal fun StateFlow.fuseStateFlow( + context: CoroutineContext, + capacity: Int, + onBufferOverflow: BufferOverflow +): Flow { + // state flow is always conflated so additional conflation does not have any effect + assert { capacity != Channel.CONFLATED } // should be desugared by callers + if ((capacity in 0..1 || capacity == Channel.BUFFERED) && onBufferOverflow == BufferOverflow.DROP_OLDEST) { + return this + } + return fuseSharedFlow(context, capacity, onBufferOverflow) +} diff --git a/kotlinx-coroutines-core/common/src/flow/internal/AbstractSharedFlow.kt b/kotlinx-coroutines-core/common/src/flow/internal/AbstractSharedFlow.kt new file mode 100644 index 0000000000..ccb5343084 --- /dev/null +++ b/kotlinx-coroutines-core/common/src/flow/internal/AbstractSharedFlow.kt @@ -0,0 +1,101 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.flow.internal + +import kotlinx.coroutines.flow.* +import kotlinx.coroutines.internal.* +import kotlin.coroutines.* +import kotlin.jvm.* +import kotlin.native.concurrent.* + +@JvmField +@SharedImmutable +internal val EMPTY_RESUMES = arrayOfNulls?>(0) + +internal abstract class AbstractSharedFlowSlot { + abstract fun allocateLocked(flow: F): Boolean + abstract fun freeLocked(flow: F): Array?> // returns continuations to resume after lock +} + +internal abstract class AbstractSharedFlow> : SynchronizedObject() { + @Suppress("UNCHECKED_CAST") + protected var slots: Array? = null // allocated when needed + private set + protected var nCollectors = 0 // number of allocated (!free) slots + private set + private var nextIndex = 0 // oracle for the next free slot index + private var _subscriptionCount: MutableStateFlow? = null // init on first need + + val subscriptionCount: StateFlow + get() = synchronized(this) { + // allocate under lock in sync with nCollectors variable + _subscriptionCount ?: MutableStateFlow(nCollectors).also { + _subscriptionCount = it + } + } + + protected abstract fun createSlot(): S + + protected abstract fun createSlotArray(size: Int): Array + + @Suppress("UNCHECKED_CAST") + protected fun allocateSlot(): S { + // Actually create slot under lock + var subscriptionCount: MutableStateFlow? = null + val slot = synchronized(this) { + val slots = when (val curSlots = slots) { + null -> createSlotArray(2).also { slots = it } + else -> if (nCollectors >= curSlots.size) { + curSlots.copyOf(2 * curSlots.size).also { slots = it } + } else { + curSlots + } + } + var index = nextIndex + var slot: S + while (true) { + slot = slots[index] ?: createSlot().also { slots[index] = it } + index++ + if (index >= slots.size) index = 0 + if ((slot as AbstractSharedFlowSlot).allocateLocked(this)) break // break when found and allocated free slot + } + nextIndex = index + nCollectors++ + subscriptionCount = _subscriptionCount // retrieve under lock if initialized + slot + } + // increments subscription count + subscriptionCount?.increment(1) + return slot + } + + @Suppress("UNCHECKED_CAST") + protected fun freeSlot(slot: S) { + // Release slot under lock + var subscriptionCount: MutableStateFlow? = null + val resumes = synchronized(this) { + nCollectors-- + subscriptionCount = _subscriptionCount // retrieve under lock if initialized + // Reset next index oracle if we have no more active collectors for more predictable behavior next time + if (nCollectors == 0) nextIndex = 0 + (slot as AbstractSharedFlowSlot).freeLocked(this) + } + /* + Resume suspended coroutines. + This can happens when the subscriber that was freed was a slow one and was holding up buffer. + When this subscriber was freed, previously queued emitted can now wake up and are resumed here. + */ + for (cont in resumes) cont?.resume(Unit) + // decrement subscription count + subscriptionCount?.increment(-1) + } + + protected inline fun forEachSlotLocked(block: (S) -> Unit) { + if (nCollectors == 0) return + slots?.forEach { slot -> + if (slot != null) block(slot) + } + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/common/src/flow/internal/ChannelFlow.kt b/kotlinx-coroutines-core/common/src/flow/internal/ChannelFlow.kt index 8a18bff30e..f3730cc7fa 100644 --- a/kotlinx-coroutines-core/common/src/flow/internal/ChannelFlow.kt +++ b/kotlinx-coroutines-core/common/src/flow/internal/ChannelFlow.kt @@ -16,17 +16,44 @@ internal fun Flow.asChannelFlow(): ChannelFlow = this as? ChannelFlow ?: ChannelFlowOperatorImpl(this) /** - * Operators that use channels extend this ChannelFlow and are always fused with each other. + * Operators that can fuse with **downstream** [buffer] and [flowOn] operators implement this interface. + * + * @suppress **This an internal API and should not be used from general code.** + */ +@InternalCoroutinesApi +public interface FusibleFlow : Flow { + /** + * This function is called by [flowOn] (with context) and [buffer] (with capacity) operators + * that are applied to this flow. Should not be used with [capacity] of [Channel.CONFLATED] + * (it shall be desugared to `capacity = 0, onBufferOverflow = DROP_OLDEST`). + */ + public fun fuse( + context: CoroutineContext = EmptyCoroutineContext, + capacity: Int = Channel.OPTIONAL_CHANNEL, + onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND + ): Flow +} + +/** + * Operators that use channels as their "output" extend this `ChannelFlow` and are always fused with each other. + * This class servers as a skeleton implementation of [FusibleFlow] and provides other cross-cutting + * methods like ability to [produceIn] and [broadcastIn] the corresponding flow, thus making it + * possible to directly use the backing channel if it exists (hence the `ChannelFlow` name). * * @suppress **This an internal API and should not be used from general code.** */ @InternalCoroutinesApi public abstract class ChannelFlow( // upstream context - @JvmField val context: CoroutineContext, + @JvmField public val context: CoroutineContext, // buffer capacity between upstream and downstream context - @JvmField val capacity: Int -) : Flow { + @JvmField public val capacity: Int, + // buffer overflow strategy + @JvmField public val onBufferOverflow: BufferOverflow +) : FusibleFlow { + init { + assert { capacity != Channel.CONFLATED } // CONFLATED must be desugared to 0, DROP_OLDEST by callers + } // shared code to create a suspend lambda from collectTo function in one place internal val collectToFun: suspend (ProducerScope) -> Unit @@ -35,38 +62,62 @@ public abstract class ChannelFlow( private val produceCapacity: Int get() = if (capacity == Channel.OPTIONAL_CHANNEL) Channel.BUFFERED else capacity - public fun update( - context: CoroutineContext = EmptyCoroutineContext, - capacity: Int = Channel.OPTIONAL_CHANNEL - ): ChannelFlow { + /** + * When this [ChannelFlow] implementation can work without a channel (supports [Channel.OPTIONAL_CHANNEL]), + * then it should return a non-null value from this function, so that a caller can use it without the effect of + * additional [flowOn] and [buffer] operators, by incorporating its + * [context], [capacity], and [onBufferOverflow] into its own implementation. + */ + public open fun dropChannelOperators(): Flow? = null + + public override fun fuse(context: CoroutineContext, capacity: Int, onBufferOverflow: BufferOverflow): Flow { + assert { capacity != Channel.CONFLATED } // CONFLATED must be desugared to (0, DROP_OLDEST) by callers // note: previous upstream context (specified before) takes precedence val newContext = context + this.context - val newCapacity = when { - this.capacity == Channel.OPTIONAL_CHANNEL -> capacity - capacity == Channel.OPTIONAL_CHANNEL -> this.capacity - this.capacity == Channel.BUFFERED -> capacity - capacity == Channel.BUFFERED -> this.capacity - this.capacity == Channel.CONFLATED -> Channel.CONFLATED - capacity == Channel.CONFLATED -> Channel.CONFLATED - else -> { - // sanity checks - assert { this.capacity >= 0 } - assert { capacity >= 0 } - // combine capacities clamping to UNLIMITED on overflow - val sum = this.capacity + capacity - if (sum >= 0) sum else Channel.UNLIMITED // unlimited on int overflow + val newCapacity: Int + val newOverflow: BufferOverflow + if (onBufferOverflow != BufferOverflow.SUSPEND) { + // this additional buffer never suspends => overwrite preceding buffering configuration + newCapacity = capacity + newOverflow = onBufferOverflow + } else { + // combine capacities, keep previous overflow strategy + newCapacity = when { + this.capacity == Channel.OPTIONAL_CHANNEL -> capacity + capacity == Channel.OPTIONAL_CHANNEL -> this.capacity + this.capacity == Channel.BUFFERED -> capacity + capacity == Channel.BUFFERED -> this.capacity + else -> { + // sanity checks + assert { this.capacity >= 0 } + assert { capacity >= 0 } + // combine capacities clamping to UNLIMITED on overflow + val sum = this.capacity + capacity + if (sum >= 0) sum else Channel.UNLIMITED // unlimited on int overflow + } } + newOverflow = this.onBufferOverflow } - if (newContext == this.context && newCapacity == this.capacity) return this - return create(newContext, newCapacity) + if (newContext == this.context && newCapacity == this.capacity && newOverflow == this.onBufferOverflow) + return this + return create(newContext, newCapacity, newOverflow) } - protected abstract fun create(context: CoroutineContext, capacity: Int): ChannelFlow + protected abstract fun create(context: CoroutineContext, capacity: Int, onBufferOverflow: BufferOverflow): ChannelFlow protected abstract suspend fun collectTo(scope: ProducerScope) - open fun broadcastImpl(scope: CoroutineScope, start: CoroutineStart): BroadcastChannel = - scope.broadcast(context, produceCapacity, start, block = collectToFun) + // broadcastImpl is used in broadcastIn operator which is obsolete and replaced by SharedFlow. + // BroadcastChannel does not support onBufferOverflow beyond simple conflation + public open fun broadcastImpl(scope: CoroutineScope, start: CoroutineStart): BroadcastChannel { + val broadcastCapacity = when (onBufferOverflow) { + BufferOverflow.SUSPEND -> produceCapacity + BufferOverflow.DROP_OLDEST -> Channel.CONFLATED + BufferOverflow.DROP_LATEST -> + throw IllegalArgumentException("Broadcast channel does not support BufferOverflow.DROP_LATEST") + } + return scope.broadcast(context, broadcastCapacity, start, block = collectToFun) + } /** * Here we use ATOMIC start for a reason (#1825). @@ -76,27 +127,34 @@ public abstract class ChannelFlow( * handlers, while the pipeline before does not, because it was cancelled during its dispatch. * Thus `onCompletion` and `finally` blocks won't be executed and it may lead to a different kinds of memory leaks. */ - open fun produceImpl(scope: CoroutineScope): ReceiveChannel = - scope.produce(context, produceCapacity, start = CoroutineStart.ATOMIC, block = collectToFun) + public open fun produceImpl(scope: CoroutineScope): ReceiveChannel = + scope.produce(context, produceCapacity, onBufferOverflow, start = CoroutineStart.ATOMIC, block = collectToFun) - override suspend fun collect(collector: FlowCollector) = + override suspend fun collect(collector: FlowCollector): Unit = coroutineScope { collector.emitAll(produceImpl(this)) } - open fun additionalToStringProps() = "" + protected open fun additionalToStringProps(): String? = null // debug toString - override fun toString(): String = - "$classSimpleName[${additionalToStringProps()}context=$context, capacity=$capacity]" + override fun toString(): String { + val props = ArrayList(4) + additionalToStringProps()?.let { props.add(it) } + if (context !== EmptyCoroutineContext) props.add("context=$context") + if (capacity != Channel.OPTIONAL_CHANNEL) props.add("capacity=$capacity") + if (onBufferOverflow != BufferOverflow.SUSPEND) props.add("onBufferOverflow=$onBufferOverflow") + return "$classSimpleName[${props.joinToString(", ")}]" + } } // ChannelFlow implementation that operates on another flow before it internal abstract class ChannelFlowOperator( - @JvmField val flow: Flow, + @JvmField protected val flow: Flow, context: CoroutineContext, - capacity: Int -) : ChannelFlow(context, capacity) { + capacity: Int, + onBufferOverflow: BufferOverflow +) : ChannelFlow(context, capacity, onBufferOverflow) { protected abstract suspend fun flowCollect(collector: FlowCollector) // Changes collecting context upstream to the specified newContext, while collecting in the original context @@ -131,14 +189,19 @@ internal abstract class ChannelFlowOperator( override fun toString(): String = "$flow -> ${super.toString()}" } -// Simple channel flow operator: flowOn, buffer, or their fused combination +/** + * Simple channel flow operator: [flowOn], [buffer], or their fused combination. + */ internal class ChannelFlowOperatorImpl( flow: Flow, context: CoroutineContext = EmptyCoroutineContext, - capacity: Int = Channel.OPTIONAL_CHANNEL -) : ChannelFlowOperator(flow, context, capacity) { - override fun create(context: CoroutineContext, capacity: Int): ChannelFlow = - ChannelFlowOperatorImpl(flow, context, capacity) + capacity: Int = Channel.OPTIONAL_CHANNEL, + onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND +) : ChannelFlowOperator(flow, context, capacity, onBufferOverflow) { + override fun create(context: CoroutineContext, capacity: Int, onBufferOverflow: BufferOverflow): ChannelFlow = + ChannelFlowOperatorImpl(flow, context, capacity, onBufferOverflow) + + override fun dropChannelOperators(): Flow? = flow override suspend fun flowCollect(collector: FlowCollector) = flow.collect(collector) @@ -161,19 +224,33 @@ private class UndispatchedContextCollector( private val emitRef: suspend (T) -> Unit = { downstream.emit(it) } // allocate suspend function ref once on creation override suspend fun emit(value: T): Unit = - withContextUndispatched(emitContext, countOrElement, emitRef, value) + withContextUndispatched(emitContext, value, countOrElement, emitRef) } // Efficiently computes block(value) in the newContext -private suspend fun withContextUndispatched( +internal suspend fun withContextUndispatched( newContext: CoroutineContext, + value: V, countOrElement: Any = threadContextElements(newContext), // can be precomputed for speed - block: suspend (V) -> T, value: V + block: suspend (V) -> T ): T = suspendCoroutineUninterceptedOrReturn { uCont -> withCoroutineContext(newContext, countOrElement) { - block.startCoroutineUninterceptedOrReturn(value, Continuation(newContext) { - uCont.resumeWith(it) - }) + block.startCoroutineUninterceptedOrReturn(value, StackFrameContinuation(uCont, newContext)) } } + +// Continuation that links the caller with uCont with walkable CoroutineStackFrame +private class StackFrameContinuation( + private val uCont: Continuation, override val context: CoroutineContext +) : Continuation, CoroutineStackFrame { + + override val callerFrame: CoroutineStackFrame? + get() = uCont as? CoroutineStackFrame + + override fun resumeWith(result: Result) { + uCont.resumeWith(result) + } + + override fun getStackTraceElement(): StackTraceElement? = null +} diff --git a/kotlinx-coroutines-core/common/src/flow/internal/Combine.kt b/kotlinx-coroutines-core/common/src/flow/internal/Combine.kt index 67da32c9f9..bbdebd08b9 100644 --- a/kotlinx-coroutines-core/common/src/flow/internal/Combine.kt +++ b/kotlinx-coroutines-core/common/src/flow/internal/Combine.kt @@ -9,133 +9,135 @@ import kotlinx.coroutines.* import kotlinx.coroutines.channels.* import kotlinx.coroutines.flow.* import kotlinx.coroutines.internal.* -import kotlinx.coroutines.selects.* +import kotlin.coroutines.* +import kotlin.coroutines.intrinsics.* -internal fun getNull(): Symbol = NULL // Workaround for JS BE bug - -internal suspend fun FlowCollector.combineTransformInternal( - first: Flow, second: Flow, - transform: suspend FlowCollector.(a: T1, b: T2) -> Unit -) { - coroutineScope { - val firstChannel = asFairChannel(first) - val secondChannel = asFairChannel(second) - var firstValue: Any? = null - var secondValue: Any? = null - var firstIsClosed = false - var secondIsClosed = false - while (!firstIsClosed || !secondIsClosed) { - select { - onReceive(firstIsClosed, firstChannel, { firstIsClosed = true }) { value -> - firstValue = value - if (secondValue !== null) { - transform(getNull().unbox(firstValue), getNull().unbox(secondValue) as T2) - } - } - - onReceive(secondIsClosed, secondChannel, { secondIsClosed = true }) { value -> - secondValue = value - if (firstValue !== null) { - transform(getNull().unbox(firstValue) as T1, getNull().unbox(secondValue) as T2) - } - } - } - } - } -} +private typealias Update = IndexedValue @PublishedApi internal suspend fun FlowCollector.combineInternal( flows: Array>, - arrayFactory: () -> Array, + arrayFactory: () -> Array?, // Array factory is required to workaround array typing on JVM transform: suspend FlowCollector.(Array) -> Unit -): Unit = coroutineScope { +): Unit = flowScope { // flow scope so any cancellation within the source flow will cancel the whole scope val size = flows.size - val channels = Array(size) { asFairChannel(flows[it]) } + if (size == 0) return@flowScope // bail-out for empty input val latestValues = arrayOfNulls(size) - val isClosed = Array(size) { false } - var nonClosed = size - var remainingNulls = size - // See flow.combine(other) for explanation. - while (nonClosed != 0) { - select { - for (i in 0 until size) { - onReceive(isClosed[i], channels[i], { isClosed[i] = true; --nonClosed }) { value -> - if (latestValues[i] == null) --remainingNulls - latestValues[i] = value - if (remainingNulls != 0) return@onReceive - val arguments = arrayFactory() - for (index in 0 until size) { - arguments[index] = NULL.unbox(latestValues[index]) - } - transform(arguments as Array) + latestValues.fill(UNINITIALIZED) // Smaller bytecode & faster that Array(size) { UNINITIALIZED } + val resultChannel = Channel(size) + val nonClosed = LocalAtomicInt(size) + var remainingAbsentValues = size + for (i in 0 until size) { + // Coroutine per flow that keeps track of its value and sends result to downstream + launch { + try { + flows[i].collect { value -> + resultChannel.send(Update(i, value)) + yield() // Emulate fairness, giving each flow chance to emit + } + } finally { + // Close the channel when there is no more flows + if (nonClosed.decrementAndGet() == 0) { + resultChannel.close() } } } } -} -private inline fun SelectBuilder.onReceive( - isClosed: Boolean, - channel: ReceiveChannel, - crossinline onClosed: () -> Unit, - noinline onReceive: suspend (value: Any) -> Unit -) { - if (isClosed) return - @Suppress("DEPRECATION") - channel.onReceiveOrNull { - // TODO onReceiveOrClosed when boxing issues are fixed - if (it === null) onClosed() - else onReceive(it) - } -} + /* + * Batch-receive optimization: read updates in batches, but bail-out + * as soon as we encountered two values from the same source + */ + val lastReceivedEpoch = ByteArray(size) + var currentEpoch: Byte = 0 + while (true) { + ++currentEpoch + // Start batch + // The very first receive in epoch should be suspending + var element = resultChannel.receiveOrNull() ?: break // Channel is closed, nothing to do here + while (true) { + val index = element.index + // Update values + val previous = latestValues[index] + latestValues[index] = element.value + if (previous === UNINITIALIZED) --remainingAbsentValues + // Check epoch + // Received the second value from the same flow in the same epoch -- bail out + if (lastReceivedEpoch[index] == currentEpoch) break + lastReceivedEpoch[index] = currentEpoch + element = resultChannel.poll() ?: break + } -// Channel has any type due to onReceiveOrNull. This will be fixed after receiveOrClosed -private fun CoroutineScope.asFairChannel(flow: Flow<*>): ReceiveChannel = produce { - val channel = channel as ChannelCoroutine - flow.collect { value -> - return@collect channel.sendFair(value ?: NULL) + // Process batch result if there is enough data + if (remainingAbsentValues == 0) { + /* + * If arrayFactory returns null, then we can avoid array copy because + * it's our own safe transformer that immediately deconstructs the array + */ + val results = arrayFactory() + if (results == null) { + transform(latestValues as Array) + } else { + (latestValues as Array).copyInto(results) + transform(results as Array) + } + } } } -internal fun zipImpl(flow: Flow, flow2: Flow, transform: suspend (T1, T2) -> R): Flow = unsafeFlow { - coroutineScope { - val first = asChannel(flow) - val second = asChannel(flow2) - /* - * This approach only works with rendezvous channel and is required to enforce correctness - * in the following scenario: - * ``` - * val f1 = flow { emit(1); delay(Long.MAX_VALUE) } - * val f2 = flowOf(1) - * f1.zip(f2) { ... } - * ``` - * - * Invariant: this clause is invoked only when all elements from the channel were processed (=> rendezvous restriction). - */ - (second as SendChannel<*>).invokeOnClose { - if (!first.isClosedForReceive) first.cancel(AbortFlowException(this@unsafeFlow)) - } +internal fun zipImpl(flow: Flow, flow2: Flow, transform: suspend (T1, T2) -> R): Flow = + unsafeFlow { + coroutineScope { + val second = produce { + flow2.collect { value -> + return@collect channel.send(value ?: NULL) + } + } - val otherIterator = second.iterator() - try { - first.consumeEach { value -> - if (!otherIterator.hasNext()) { - return@consumeEach + /* + * This approach only works with rendezvous channel and is required to enforce correctness + * in the following scenario: + * ``` + * val f1 = flow { emit(1); delay(Long.MAX_VALUE) } + * val f2 = flowOf(1) + * f1.zip(f2) { ... } + * ``` + * + * Invariant: this clause is invoked only when all elements from the channel were processed (=> rendezvous restriction). + */ + val collectJob = Job() + (second as SendChannel<*>).invokeOnClose { + // Optimization to avoid AFE allocation when the other flow is done + if (collectJob.isActive) collectJob.cancel(AbortFlowException(this@unsafeFlow)) + } + + try { + /* + * Non-trivial undispatched (because we are in the right context and there is no structured concurrency) + * hierarchy: + * -Outer coroutineScope that owns the whole zip process + * - First flow is collected by the child of coroutineScope, collectJob. + * So it can be safely cancelled as soon as the second flow is done + * - **But** the downstream MUST NOT be cancelled when the second flow is done, + * so we emit to downstream from coroutineScope job. + * Typically, such hierarchy requires coroutine for collector that communicates + * with coroutines scope via a channel, but it's way too expensive, so + * we are using this trick instead. + */ + val scopeContext = coroutineContext + val cnt = threadContextElements(scopeContext) + withContextUndispatched(coroutineContext + collectJob, Unit) { + flow.collect { value -> + withContextUndispatched(scopeContext, Unit, cnt) { + val otherValue = second.receiveOrNull() ?: throw AbortFlowException(this@unsafeFlow) + emit(transform(value, NULL.unbox(otherValue))) + } + } } - emit(transform(NULL.unbox(value), NULL.unbox(otherIterator.next()))) + } catch (e: AbortFlowException) { + e.checkOwnership(owner = this@unsafeFlow) + } finally { + if (!second.isClosedForReceive) second.cancel() } - } catch (e: AbortFlowException) { - e.checkOwnership(owner = this@unsafeFlow) - } finally { - if (!second.isClosedForReceive) second.cancel(AbortFlowException(this@unsafeFlow)) } } -} - -// Channel has any type due to onReceiveOrNull. This will be fixed after receiveOrClosed -private fun CoroutineScope.asChannel(flow: Flow<*>): ReceiveChannel = produce { - flow.collect { value -> - return@collect channel.send(value ?: NULL) - } -} diff --git a/kotlinx-coroutines-core/common/src/flow/internal/Merge.kt b/kotlinx-coroutines-core/common/src/flow/internal/Merge.kt index 798f38b8bd..530bcc1e5a 100644 --- a/kotlinx-coroutines-core/common/src/flow/internal/Merge.kt +++ b/kotlinx-coroutines-core/common/src/flow/internal/Merge.kt @@ -14,10 +14,11 @@ internal class ChannelFlowTransformLatest( private val transform: suspend FlowCollector.(value: T) -> Unit, flow: Flow, context: CoroutineContext = EmptyCoroutineContext, - capacity: Int = Channel.BUFFERED -) : ChannelFlowOperator(flow, context, capacity) { - override fun create(context: CoroutineContext, capacity: Int): ChannelFlow = - ChannelFlowTransformLatest(transform, flow, context, capacity) + capacity: Int = Channel.BUFFERED, + onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND +) : ChannelFlowOperator(flow, context, capacity, onBufferOverflow) { + override fun create(context: CoroutineContext, capacity: Int, onBufferOverflow: BufferOverflow): ChannelFlow = + ChannelFlowTransformLatest(transform, flow, context, capacity, onBufferOverflow) override suspend fun flowCollect(collector: FlowCollector) { assert { collector is SendingCollector } // So cancellation behaviour is not leaking into the downstream @@ -41,10 +42,11 @@ internal class ChannelFlowMerge( private val flow: Flow>, private val concurrency: Int, context: CoroutineContext = EmptyCoroutineContext, - capacity: Int = Channel.BUFFERED -) : ChannelFlow(context, capacity) { - override fun create(context: CoroutineContext, capacity: Int): ChannelFlow = - ChannelFlowMerge(flow, concurrency, context, capacity) + capacity: Int = Channel.BUFFERED, + onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND +) : ChannelFlow(context, capacity, onBufferOverflow) { + override fun create(context: CoroutineContext, capacity: Int, onBufferOverflow: BufferOverflow): ChannelFlow = + ChannelFlowMerge(flow, concurrency, context, capacity, onBufferOverflow) override fun produceImpl(scope: CoroutineScope): ReceiveChannel { return scope.flowProduce(context, capacity, block = collectToFun) @@ -72,17 +74,17 @@ internal class ChannelFlowMerge( } } - override fun additionalToStringProps(): String = - "concurrency=$concurrency, " + override fun additionalToStringProps(): String = "concurrency=$concurrency" } internal class ChannelLimitedFlowMerge( private val flows: Iterable>, context: CoroutineContext = EmptyCoroutineContext, - capacity: Int = Channel.BUFFERED -) : ChannelFlow(context, capacity) { - override fun create(context: CoroutineContext, capacity: Int): ChannelFlow = - ChannelLimitedFlowMerge(flows, context, capacity) + capacity: Int = Channel.BUFFERED, + onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND +) : ChannelFlow(context, capacity, onBufferOverflow) { + override fun create(context: CoroutineContext, capacity: Int, onBufferOverflow: BufferOverflow): ChannelFlow = + ChannelLimitedFlowMerge(flows, context, capacity, onBufferOverflow) override fun produceImpl(scope: CoroutineScope): ReceiveChannel { return scope.flowProduce(context, capacity, block = collectToFun) diff --git a/kotlinx-coroutines-core/common/src/flow/internal/NullSurrogate.kt b/kotlinx-coroutines-core/common/src/flow/internal/NullSurrogate.kt index 22e1957419..f20deb2d38 100644 --- a/kotlinx-coroutines-core/common/src/flow/internal/NullSurrogate.kt +++ b/kotlinx-coroutines-core/common/src/flow/internal/NullSurrogate.kt @@ -11,11 +11,20 @@ import kotlin.native.concurrent.* /** * This value is used a a surrogate `null` value when needed. * It should never leak to the outside world. + * Its usage typically are paired with [Symbol.unbox] usages. */ @JvmField @SharedImmutable internal val NULL = Symbol("NULL") +/** + * Symbol to indicate that the value is not yet initialized. + * It should never leak to the outside world. + */ +@JvmField +@SharedImmutable +internal val UNINITIALIZED = Symbol("UNINITIALIZED") + /* * Symbol used to indicate that the flow is complete. * It should never leak to the outside world. diff --git a/kotlinx-coroutines-core/common/src/flow/internal/SendingCollector.kt b/kotlinx-coroutines-core/common/src/flow/internal/SendingCollector.kt index 1620a2ac7c..c2abafd2fd 100644 --- a/kotlinx-coroutines-core/common/src/flow/internal/SendingCollector.kt +++ b/kotlinx-coroutines-core/common/src/flow/internal/SendingCollector.kt @@ -16,5 +16,5 @@ import kotlinx.coroutines.flow.* public class SendingCollector( private val channel: SendChannel ) : FlowCollector { - override suspend fun emit(value: T) = channel.send(value) + override suspend fun emit(value: T): Unit = channel.send(value) } diff --git a/kotlinx-coroutines-core/common/src/flow/operators/Context.kt b/kotlinx-coroutines-core/common/src/flow/operators/Context.kt index 4e6167f0cd..a6d6b76dae 100644 --- a/kotlinx-coroutines-core/common/src/flow/operators/Context.kt +++ b/kotlinx-coroutines-core/common/src/flow/operators/Context.kt @@ -60,13 +60,23 @@ import kotlin.jvm.* * Q : -->---------- [2A] -- [2B] -- [2C] -->-- // collect * ``` * - * When operator's code takes time to execute this decreases the total execution time of the flow. + * When the operator's code takes some time to execute, this decreases the total execution time of the flow. * A [channel][Channel] is used between the coroutines to send elements emitted by the coroutine `P` to * the coroutine `Q`. If the code before `buffer` operator (in the coroutine `P`) is faster than the code after * `buffer` operator (in the coroutine `Q`), then this channel will become full at some point and will suspend * the producer coroutine `P` until the consumer coroutine `Q` catches up. * The [capacity] parameter defines the size of this buffer. * + * ### Buffer overflow + * + * By default, the emitter is suspended when the buffer overflows, to let collector catch up. This strategy can be + * overridden with an optional [onBufferOverflow] parameter so that the emitter is never suspended. In this + * case, on buffer overflow either the oldest value in the buffer is dropped with the [DROP_OLDEST][BufferOverflow.DROP_OLDEST] + * strategy and the latest emitted value is added to the buffer, + * or the latest value that is being emitted is dropped with the [DROP_LATEST][BufferOverflow.DROP_LATEST] strategy, + * keeping the buffer intact. + * To implement either of the custom strategies, a buffer of at least one element is used. + * * ### Operator fusion * * Adjacent applications of [channelFlow], [flowOn], [buffer], [produceIn], and [broadcastIn] are @@ -76,9 +86,12 @@ import kotlin.jvm.* * which effectively requests a buffer of any size. Multiple requests with a specified buffer * size produce a buffer with the sum of the requested buffer sizes. * + * A `buffer` call with a non-default value of the [onBufferOverflow] parameter overrides all immediately preceding + * buffering operators, because it never suspends its upstream, and thus no upstream buffer would ever be used. + * * ### Conceptual implementation * - * The actual implementation of `buffer` is not trivial due to the fusing, but conceptually its + * The actual implementation of `buffer` is not trivial due to the fusing, but conceptually its basic * implementation is equivalent to the following code that can be written using [produce] * coroutine builder to produce a channel and [consumeEach][ReceiveChannel.consumeEach] extension to consume it: * @@ -96,25 +109,43 @@ import kotlin.jvm.* * * ### Conflation * - * Usage of this function with [capacity] of [Channel.CONFLATED][Channel.CONFLATED] is provided as a shortcut via - * [conflate] operator. See its documentation for details. + * Usage of this function with [capacity] of [Channel.CONFLATED][Channel.CONFLATED] is a shortcut to + * `buffer(onBufferOverflow = `[`BufferOverflow.DROP_OLDEST`][BufferOverflow.DROP_OLDEST]`)`, and is available via + * a separate [conflate] operator. See its documentation for details. * * @param capacity type/capacity of the buffer between coroutines. Allowed values are the same as in `Channel(...)` - * factory function: [BUFFERED][Channel.BUFFERED] (by default), [CONFLATED][Channel.CONFLATED], - * [RENDEZVOUS][Channel.RENDEZVOUS], [UNLIMITED][Channel.UNLIMITED] or a non-negative value indicating - * an explicitly requested size. + * factory function: [BUFFERED][Channel.BUFFERED] (by default), [CONFLATED][Channel.CONFLATED], + * [RENDEZVOUS][Channel.RENDEZVOUS], [UNLIMITED][Channel.UNLIMITED] or a non-negative value indicating + * an explicitly requested size. + * @param onBufferOverflow configures an action on buffer overflow (optional, defaults to + * [SUSPEND][BufferOverflow.SUSPEND], supported only when `capacity >= 0` or `capacity == Channel.BUFFERED`, + * implicitly creates a channel with at least one buffered element). */ -@ExperimentalCoroutinesApi -public fun Flow.buffer(capacity: Int = BUFFERED): Flow { +@Suppress("NAME_SHADOWING") +public fun Flow.buffer(capacity: Int = BUFFERED, onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND): Flow { require(capacity >= 0 || capacity == BUFFERED || capacity == CONFLATED) { "Buffer size should be non-negative, BUFFERED, or CONFLATED, but was $capacity" } - return if (this is ChannelFlow) - update(capacity = capacity) - else - ChannelFlowOperatorImpl(this, capacity = capacity) + require(capacity != CONFLATED || onBufferOverflow == BufferOverflow.SUSPEND) { + "CONFLATED capacity cannot be used with non-default onBufferOverflow" + } + // desugar CONFLATED capacity to (0, DROP_OLDEST) + var capacity = capacity + var onBufferOverflow = onBufferOverflow + if (capacity == CONFLATED) { + capacity = 0 + onBufferOverflow = BufferOverflow.DROP_OLDEST + } + // create a flow + return when (this) { + is FusibleFlow -> fuse(capacity = capacity, onBufferOverflow = onBufferOverflow) + else -> ChannelFlowOperatorImpl(this, capacity = capacity, onBufferOverflow = onBufferOverflow) + } } +@Deprecated(level = DeprecationLevel.HIDDEN, message = "Since 1.4.0, binary compatibility with earlier versions") +public fun Flow.buffer(capacity: Int = BUFFERED): Flow = buffer(capacity) + /** * Conflates flow emissions via conflated channel and runs collector in a separate coroutine. * The effect of this is that emitter is never suspended due to a slow collector, but collector @@ -139,15 +170,20 @@ public fun Flow.buffer(capacity: Int = BUFFERED): Flow { * assertEquals(listOf(1, 10, 20, 30), result) * ``` * - * Note that `conflate` operator is a shortcut for [buffer] with `capacity` of [Channel.CONFLATED][Channel.CONFLATED]. + * Note that `conflate` operator is a shortcut for [buffer] with `capacity` of [Channel.CONFLATED][Channel.CONFLATED], + * with is, in turn, a shortcut to a buffer that only keeps the latest element as + * created by `buffer(onBufferOverflow = `[`BufferOverflow.DROP_OLDEST`][BufferOverflow.DROP_OLDEST]`)`. * * ### Operator fusion * * Adjacent applications of `conflate`/[buffer], [channelFlow], [flowOn], [produceIn], and [broadcastIn] are * always fused so that only one properly configured channel is used for execution. * **Conflation takes precedence over `buffer()` calls with any other capacity.** + * + * Note that any instance of [StateFlow] already behaves as if `conflate` operator is + * applied to it, so applying `conflate` to a `StateFlow` has not effect. + * See [StateFlow] documentation on Operator Fusion. */ -@ExperimentalCoroutinesApi public fun Flow.conflate(): Flow = buffer(CONFLATED) /** @@ -170,13 +206,17 @@ public fun Flow.conflate(): Flow = buffer(CONFLATED) * * For more explanation of context preservation please refer to [Flow] documentation. * - * This operators retains a _sequential_ nature of flow if changing the context does not call for changing + * This operator retains a _sequential_ nature of flow if changing the context does not call for changing * the [dispatcher][CoroutineDispatcher]. Otherwise, if changing dispatcher is required, it collects * flow emissions in one coroutine that is run using a specified [context] and emits them from another coroutines * with the original collector's context using a channel with a [default][Channel.BUFFERED] buffer size * between two coroutines similarly to [buffer] operator, unless [buffer] operator is explicitly called * before or after `flowOn`, which requests buffering behavior and specifies channel size. * + * Note, that flows operating across different dispatchers might lose some in-flight elements when cancelled. + * In particular, this operator ensures that downstream flow does not resume on cancellation even if the element + * was already emitted by the upstream flow. + * * ### Operator fusion * * Adjacent applications of [channelFlow], [flowOn], [buffer], [produceIn], and [broadcastIn] are @@ -192,18 +232,51 @@ public fun Flow.conflate(): Flow = buffer(CONFLATED) * .flowOn(Dispatchers.Default) * ``` * + * Note that an instance of [SharedFlow] does not have an execution context by itself, + * so applying `flowOn` to a `SharedFlow` has not effect. See the [SharedFlow] documentation on Operator Fusion. + * * @throws [IllegalArgumentException] if provided context contains [Job] instance. */ -@ExperimentalCoroutinesApi public fun Flow.flowOn(context: CoroutineContext): Flow { checkFlowContext(context) return when { context == EmptyCoroutineContext -> this - this is ChannelFlow -> update(context = context) + this is FusibleFlow -> fuse(context = context) else -> ChannelFlowOperatorImpl(this, context = context) } } +/** + * Returns a flow which checks cancellation status on each emission and throws + * the corresponding cancellation cause if flow collector was cancelled. + * Note that [flow] builder and all implementations of [SharedFlow] are [cancellable] by default. + * + * This operator provides a shortcut for `.onEach { currentCoroutineContext().ensureActive() }`. + * See [ensureActive][CoroutineContext.ensureActive] for details. + */ +public fun Flow.cancellable(): Flow = + when (this) { + is CancellableFlow<*> -> this // Fast-path, already cancellable + else -> CancellableFlowImpl(this) + } + +/** + * Internal marker for flows that are [cancellable]. + */ +internal interface CancellableFlow : Flow + +/** + * Named implementation class for a flow that is defined by the [cancellable] function. + */ +private class CancellableFlowImpl(private val flow: Flow) : CancellableFlow { + override suspend fun collect(collector: FlowCollector) { + flow.collect { + currentCoroutineContext().ensureActive() + collector.emit(it) + } + } +} + /** * The operator that changes the context where all transformations applied to the given flow within a [builder] are executed. * This operator is context preserving and does not affect the context of the preceding and subsequent operations. @@ -252,7 +325,7 @@ public fun Flow.flowWith( * All builders are written using scoping and no global coroutines are launched, so it is safe not to provide explicit Job. * It is also necessary not to mess with cancellation if multiple flowWith are used. */ - val originalContext = coroutineContext.minusKey(Job) + val originalContext = currentCoroutineContext().minusKey(Job) val prepared = source.flowOn(originalContext).buffer(bufferSize) builder(prepared).flowOn(flowContext).buffer(bufferSize).collect { value -> return@collect emit(value) diff --git a/kotlinx-coroutines-core/common/src/flow/operators/Delay.kt b/kotlinx-coroutines-core/common/src/flow/operators/Delay.kt index 5f3c900c1b..c95b4be940 100644 --- a/kotlinx-coroutines-core/common/src/flow/operators/Delay.kt +++ b/kotlinx-coroutines-core/common/src/flow/operators/Delay.kt @@ -14,13 +14,30 @@ import kotlinx.coroutines.selects.* import kotlin.jvm.* import kotlin.time.* +/* Scaffolding for Knit code examples + + +*/ + /** * Returns a flow that mirrors the original flow, but filters out values * that are followed by the newer values within the given [timeout][timeoutMillis]. * The latest value is always emitted. * * Example: - * ``` + * + * ```kotlin * flow { * emit(1) * delay(90) @@ -33,51 +50,81 @@ import kotlin.time.* * emit(5) * }.debounce(1000) * ``` - * produces `3, 4, 5`. + * + * + * produces the following emissions + * + * ```text + * 3, 4, 5 + * ``` + * * * Note that the resulting flow does not emit anything as long as the original flow emits * items faster than every [timeoutMillis] milliseconds. */ @FlowPreview public fun Flow.debounce(timeoutMillis: Long): Flow { - require(timeoutMillis > 0) { "Debounce timeout should be positive" } - return scopedFlow { downstream -> - // Actually Any, KT-30796 - val values = produce(capacity = Channel.CONFLATED) { - collect { value -> send(value ?: NULL) } - } - var lastValue: Any? = null - while (lastValue !== DONE) { - select { - // Should be receiveOrClosed when boxing issues are fixed - values.onReceiveOrNull { - if (it == null) { - if (lastValue != null) downstream.emit(NULL.unbox(lastValue)) - lastValue = DONE - } else { - lastValue = it - } - } - - lastValue?.let { value -> - // set timeout when lastValue != null - onTimeout(timeoutMillis) { - lastValue = null // Consume the value - downstream.emit(NULL.unbox(value)) - } - } - } - } - } + require(timeoutMillis >= 0L) { "Debounce timeout should not be negative" } + if (timeoutMillis == 0L) return this + return debounceInternal { timeoutMillis } } /** * Returns a flow that mirrors the original flow, but filters out values - * that are followed by the newer values within the given [timeout]. + * that are followed by the newer values within the given [timeout][timeoutMillis]. * The latest value is always emitted. * + * A variation of [debounce] that allows specifying the timeout value dynamically. + * * Example: + * + * ```kotlin + * flow { + * emit(1) + * delay(90) + * emit(2) + * delay(90) + * emit(3) + * delay(1010) + * emit(4) + * delay(1010) + * emit(5) + * }.debounce { + * if (it == 1) { + * 0L + * } else { + * 1000L + * } + * } * ``` + * + * + * produces the following emissions + * + * ```text + * 1, 3, 4, 5 + * ``` + * + * + * Note that the resulting flow does not emit anything as long as the original flow emits + * items faster than every [timeoutMillis] milliseconds. + * + * @param timeoutMillis [T] is the emitted value and the return value is timeout in milliseconds. + */ +@FlowPreview +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +public fun Flow.debounce(timeoutMillis: (T) -> Long): Flow = + debounceInternal(timeoutMillis) + +/** + * Returns a flow that mirrors the original flow, but filters out values + * that are followed by the newer values within the given [timeout]. + * The latest value is always emitted. + * + * Example: + * + * ```kotlin * flow { * emit(1) * delay(90.milliseconds) @@ -90,29 +137,141 @@ public fun Flow.debounce(timeoutMillis: Long): Flow { * emit(5) * }.debounce(1000.milliseconds) * ``` - * produces `3, 4, 5`. + * + * + * produces the following emissions + * + * ```text + * 3, 4, 5 + * ``` + * * * Note that the resulting flow does not emit anything as long as the original flow emits * items faster than every [timeout] milliseconds. */ @ExperimentalTime @FlowPreview -public fun Flow.debounce(timeout: Duration): Flow = debounce(timeout.toDelayMillis()) +public fun Flow.debounce(timeout: Duration): Flow = + debounce(timeout.toDelayMillis()) /** - * Returns a flow that emits only the latest value emitted by the original flow during the given sampling [period][periodMillis]. + * Returns a flow that mirrors the original flow, but filters out values + * that are followed by the newer values within the given [timeout]. + * The latest value is always emitted. + * + * A variation of [debounce] that allows specifying the timeout value dynamically. * * Example: + * + * ```kotlin + * flow { + * emit(1) + * delay(90.milliseconds) + * emit(2) + * delay(90.milliseconds) + * emit(3) + * delay(1010.milliseconds) + * emit(4) + * delay(1010.milliseconds) + * emit(5) + * }.debounce { + * if (it == 1) { + * 0.milliseconds + * } else { + * 1000.milliseconds + * } + * } + * ``` + * + * + * produces the following emissions + * + * ```text + * 1, 3, 4, 5 * ``` + * + * + * Note that the resulting flow does not emit anything as long as the original flow emits + * items faster than every [timeout] unit. + * + * @param timeout [T] is the emitted value and the return value is timeout in [Duration]. + */ +@ExperimentalTime +@FlowPreview +@JvmName("debounceDuration") +@OptIn(kotlin.experimental.ExperimentalTypeInference::class) +@OverloadResolutionByLambdaReturnType +public fun Flow.debounce(timeout: (T) -> Duration): Flow = + debounceInternal { emittedItem -> + timeout(emittedItem).toDelayMillis() + } + +private fun Flow.debounceInternal(timeoutMillisSelector: (T) -> Long) : Flow = + scopedFlow { downstream -> + // Produce the values using the default (rendezvous) channel + // Note: the actual type is Any, KT-30796 + val values = produce { + collect { value -> send(value ?: NULL) } + } + // Now consume the values + var lastValue: Any? = null + while (lastValue !== DONE) { + var timeoutMillis = 0L // will be always computed when lastValue != null + // Compute timeout for this value + if (lastValue != null) { + timeoutMillis = timeoutMillisSelector(NULL.unbox(lastValue)) + require(timeoutMillis >= 0L) { "Debounce timeout should not be negative" } + if (timeoutMillis == 0L) { + downstream.emit(NULL.unbox(lastValue)) + lastValue = null // Consume the value + } + } + // assert invariant: lastValue != null implies timeoutMillis > 0 + assert { lastValue == null || timeoutMillis > 0 } + // wait for the next value with timeout + select { + // Set timeout when lastValue exists and is not consumed yet + if (lastValue != null) { + onTimeout(timeoutMillis) { + downstream.emit(NULL.unbox(lastValue)) + lastValue = null // Consume the value + } + } + // Should be receiveOrClosed when boxing issues are fixed + values.onReceiveOrNull { value -> + if (value == null) { + if (lastValue != null) downstream.emit(NULL.unbox(lastValue)) + lastValue = DONE + } else { + lastValue = value + } + } + } + } + } + +/** + * Returns a flow that emits only the latest value emitted by the original flow during the given sampling [period][periodMillis]. + * + * Example: + * + * ```kotlin * flow { * repeat(10) { * emit(it) - * delay(50) + * delay(110) * } - * }.sample(100) + * }.sample(200) + * ``` + * + * + * produces the following emissions + * + * ```text + * 1, 3, 5, 7, 9 * ``` - * produces `1, 3, 5, 7, 9`. - * + * + * * Note that the latest element is not emitted if it does not fit into the sampling window. */ @FlowPreview @@ -166,15 +325,23 @@ internal fun CoroutineScope.fixedPeriodTicker(delayMillis: Long, initialDelayMil * Returns a flow that emits only the latest value emitted by the original flow during the given sampling [period]. * * Example: - * ``` + * + * ```kotlin * flow { * repeat(10) { * emit(it) - * delay(50.milliseconds) + * delay(110.milliseconds) * } - * }.sample(100.milliseconds) + * }.sample(200.milliseconds) + * ``` + * + * + * produces the following emissions + * + * ```text + * 1, 3, 5, 7, 9 * ``` - * produces `1, 3, 5, 7, 9`. + * * * Note that the latest element is not emitted if it does not fit into the sampling window. */ diff --git a/kotlinx-coroutines-core/common/src/flow/operators/Distinct.kt b/kotlinx-coroutines-core/common/src/flow/operators/Distinct.kt index 53c4d6d2aa..1a34af776f 100644 --- a/kotlinx-coroutines-core/common/src/flow/operators/Distinct.kt +++ b/kotlinx-coroutines-core/common/src/flow/operators/Distinct.kt @@ -10,47 +10,77 @@ package kotlinx.coroutines.flow import kotlinx.coroutines.* import kotlinx.coroutines.flow.internal.* import kotlin.jvm.* -import kotlinx.coroutines.flow.internal.unsafeFlow as flow +import kotlin.native.concurrent.* /** * Returns flow where all subsequent repetitions of the same value are filtered out. + * + * Note that any instance of [StateFlow] already behaves as if `distinctUtilChanged` operator is + * applied to it, so applying `distinctUntilChanged` to a `StateFlow` has no effect. + * See [StateFlow] documentation on Operator Fusion. + * Also, repeated application of `distinctUntilChanged` operator on any flow has no effect. */ -@ExperimentalCoroutinesApi -public fun Flow.distinctUntilChanged(): Flow = distinctUntilChangedBy { it } +public fun Flow.distinctUntilChanged(): Flow = + when (this) { + is StateFlow<*> -> this // state flows are always distinct + else -> distinctUntilChangedBy(keySelector = defaultKeySelector, areEquivalent = defaultAreEquivalent) + } /** * Returns flow where all subsequent repetitions of the same value are filtered out, when compared * with each other via the provided [areEquivalent] function. + * + * Note that repeated application of `distinctUntilChanged` operator with the same parameter has no effect. */ -@FlowPreview +@Suppress("UNCHECKED_CAST") public fun Flow.distinctUntilChanged(areEquivalent: (old: T, new: T) -> Boolean): Flow = - distinctUntilChangedBy(keySelector = { it }, areEquivalent = areEquivalent) + distinctUntilChangedBy(keySelector = defaultKeySelector, areEquivalent = areEquivalent as (Any?, Any?) -> Boolean) /** * Returns flow where all subsequent repetitions of the same key are filtered out, where * key is extracted with [keySelector] function. + * + * Note that repeated application of `distinctUntilChanged` operator with the same parameter has no effect. */ -@FlowPreview public fun Flow.distinctUntilChangedBy(keySelector: (T) -> K): Flow = - distinctUntilChangedBy(keySelector = keySelector, areEquivalent = { old, new -> old == new }) + distinctUntilChangedBy(keySelector = keySelector, areEquivalent = defaultAreEquivalent) + +@SharedImmutable +private val defaultKeySelector: (Any?) -> Any? = { it } + +@SharedImmutable +private val defaultAreEquivalent: (Any?, Any?) -> Boolean = { old, new -> old == new } /** * Returns flow where all subsequent repetitions of the same key are filtered out, where * keys are extracted with [keySelector] function and compared with each other via the * provided [areEquivalent] function. + * + * NOTE: It is non-inline to share a single implementing class. */ -private inline fun Flow.distinctUntilChangedBy( - crossinline keySelector: (T) -> K, - crossinline areEquivalent: (old: K, new: K) -> Boolean -): Flow = - flow { +private fun Flow.distinctUntilChangedBy( + keySelector: (T) -> Any?, + areEquivalent: (old: Any?, new: Any?) -> Boolean +): Flow = when { + this is DistinctFlowImpl<*> && this.keySelector === keySelector && this.areEquivalent === areEquivalent -> this // same + else -> DistinctFlowImpl(this, keySelector, areEquivalent) +} + +private class DistinctFlowImpl( + private val upstream: Flow, + @JvmField val keySelector: (T) -> Any?, + @JvmField val areEquivalent: (old: Any?, new: Any?) -> Boolean +): Flow { + @InternalCoroutinesApi + override suspend fun collect(collector: FlowCollector) { var previousKey: Any? = NULL - collect { value -> + upstream.collect { value -> val key = keySelector(value) @Suppress("UNCHECKED_CAST") - if (previousKey === NULL || !areEquivalent(previousKey as K, key)) { + if (previousKey === NULL || !areEquivalent(previousKey, key)) { previousKey = key - emit(value) + collector.emit(value) } } } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/common/src/flow/operators/Emitters.kt b/kotlinx-coroutines-core/common/src/flow/operators/Emitters.kt index 9236fd26d8..244af9a7f5 100644 --- a/kotlinx-coroutines-core/common/src/flow/operators/Emitters.kt +++ b/kotlinx-coroutines-core/common/src/flow/operators/Emitters.kt @@ -10,20 +10,20 @@ package kotlinx.coroutines.flow import kotlinx.coroutines.* import kotlinx.coroutines.flow.internal.* -import kotlin.coroutines.* import kotlin.jvm.* // ------------------ WARNING ------------------ -// These emitting operators must use safe flow builder, because their allow +// These emitting operators must use safe flow builder, because they allow // user code to directly emit to the underlying FlowCollector. /** * Applies [transform] function to each value of the given flow. * - * The receiver of the [transform] is [FlowCollector] and thus `transform` is a - * generic function that may transform emitted element, skip it or emit it multiple times. + * The receiver of the `transform` is [FlowCollector] and thus `transform` is a + * flexible function that may transform emitted element, skip it or emit it multiple times. * - * This operator can be used as a building block for other operators, for example: + * This operator generalizes [filter] and [map] operators and + * can be used as a building block for other operators, for example: * * ``` * fun Flow.skipOddAndDuplicateEven(): Flow = transform { value -> @@ -34,7 +34,6 @@ import kotlin.jvm.* * } * ``` */ -@ExperimentalCoroutinesApi public inline fun Flow.transform( @BuilderInference crossinline transform: suspend FlowCollector.(value: T) -> Unit ): Flow = flow { // Note: safe flow is used here, because collector is exposed to transform on each operation @@ -56,9 +55,14 @@ internal inline fun Flow.unsafeTransform( } /** - * Invokes the given [action] when the this flow starts to be collected. + * Returns a flow that invokes the given [action] **before** this flow starts to be collected. * - * The receiver of the [action] is [FlowCollector] and thus `onStart` can emit additional elements. + * The [action] is called before the upstream flow is started, so if it is used with a [SharedFlow] + * there is **no guarantee** that emissions from the upstream flow that happen inside or immediately + * after this `onStart` action will be collected + * (see [onSubscription] for an alternative operator on shared flows). + * + * The receiver of the [action] is [FlowCollector], so `onStart` can emit additional elements. * For example: * * ``` @@ -67,11 +71,10 @@ internal inline fun Flow.unsafeTransform( * .collect { println(it) } // prints Begin, a, b, c * ``` */ -@ExperimentalCoroutinesApi // tentatively stable in 1.3.0 public fun Flow.onStart( action: suspend FlowCollector.() -> Unit ): Flow = unsafeFlow { // Note: unsafe flow is used here, but safe collector is used to invoke start action - val safeCollector = SafeCollector(this, coroutineContext) + val safeCollector = SafeCollector(this, currentCoroutineContext()) try { safeCollector.action() } finally { @@ -81,8 +84,8 @@ public fun Flow.onStart( } /** - * Invokes the given [action] when the given flow is completed or cancelled, using - * the exception from the upstream (if any) as cause parameter of [action]. + * Returns a flow that invokes the given [action] **after** the flow is completed or cancelled, passing + * the cancellation exception or failure as cause parameter of [action]. * * Conceptually, `onCompletion` is similar to wrapping the flow collection into a `finally` block, * for example the following imperative snippet: @@ -106,53 +109,89 @@ public fun Flow.onStart( * .collect() * ``` * - * This operator is *transparent* to exceptions that occur in downstream flow - * and does not observe exceptions that are thrown to cancel the flow, - * while any exception from the [action] will be thrown downstream. - * This behaviour can be demonstrated by the following example: + * Unlike [catch], this operator reports exception that occur both upstream and downstream + * and observe exceptions that are thrown to cancel the flow. Exception is empty if and only if + * the flow had fully completed successfully. Conceptually, the following code: + * + * ``` + * myFlow.collect { value -> + * println(value) + * } + * println("Completed successfully") + * ``` + * + * can be replaced with: * * ``` - * flow { emitData() } - * .map { computeOne(it) } - * .onCompletion { println(it) } // Can print exceptions from emitData and computeOne - * .map { computeTwo(it) } - * .onCompletion { println(it) } // Can print exceptions from emitData, computeOne, onCompletion and computeTwo + * myFlow + * .onEach { println(it) } + * .onCompletion { if (it == null) println("Completed successfully") } * .collect() * ``` * * The receiver of the [action] is [FlowCollector] and this operator can be used to emit additional - * elements at the end of the collection. For example: + * elements at the end **if it completed successfully**. For example: * * ``` * flowOf("a", "b", "c") * .onCompletion { emit("Done") } * .collect { println(it) } // prints a, b, c, Done * ``` + * + * In case of failure or cancellation, any attempt to emit additional elements throws the corresponding exception. + * Use [catch] if you need to suppress failure and replace it with emission of elements. */ -@ExperimentalCoroutinesApi // tentatively stable in 1.3.0 public fun Flow.onCompletion( action: suspend FlowCollector.(cause: Throwable?) -> Unit ): Flow = unsafeFlow { // Note: unsafe flow is used here, but safe collector is used to invoke completion action - val exception = try { - catchImpl(this) + try { + collect(this) } catch (e: Throwable) { /* - * Exception from the downstream. * Use throwing collector to prevent any emissions from the * completion sequence when downstream has failed, otherwise it may * lead to a non-sequential behaviour impossible with `finally` */ - ThrowingCollector(e).invokeSafely(action, null) + ThrowingCollector(e).invokeSafely(action, e) throw e } - // Exception from the upstream or normal completion - val safeCollector = SafeCollector(this, coroutineContext) + // Normal completion + val sc = SafeCollector(this, currentCoroutineContext()) try { - safeCollector.invokeSafely(action, exception) + sc.action(null) } finally { - safeCollector.releaseIntercepted() + sc.releaseIntercepted() + } +} + +/** + * Invokes the given [action] when this flow completes without emitting any elements. + * The receiver of the [action] is [FlowCollector], so `onEmpty` can emit additional elements. + * For example: + * + * ``` + * emptyFlow().onEmpty { + * emit(1) + * emit(2) + * }.collect { println(it) } // prints 1, 2 + * ``` + */ +public fun Flow.onEmpty( + action: suspend FlowCollector.() -> Unit +): Flow = unsafeFlow { + var isEmpty = true + collect { + isEmpty = false + emit(it) + } + if (isEmpty) { + val collector = SafeCollector(this, currentCoroutineContext()) + try { + collector.action() + } finally { + collector.releaseIntercepted() + } } - exception?.let { throw it } } private class ThrowingCollector(private val e: Throwable) : FlowCollector { @@ -161,12 +200,6 @@ private class ThrowingCollector(private val e: Throwable) : FlowCollector } } -// It was only released in 1.3.0-M2, remove in 1.4.0 -/** @suppress */ -@Deprecated(level = DeprecationLevel.HIDDEN, message = "binary compatibility with a version w/o FlowCollector receiver") -public fun Flow.onCompletion(action: suspend (cause: Throwable?) -> Unit) = - onCompletion { action(it) } - private suspend fun FlowCollector.invokeSafely( action: suspend FlowCollector.(cause: Throwable?) -> Unit, cause: Throwable? @@ -174,7 +207,7 @@ private suspend fun FlowCollector.invokeSafely( try { action(cause) } catch (e: Throwable) { - if (cause !== null) e.addSuppressedThrowable(cause) + if (cause !== null && cause !== e) e.addSuppressedThrowable(cause) throw e } } diff --git a/kotlinx-coroutines-core/common/src/flow/operators/Errors.kt b/kotlinx-coroutines-core/common/src/flow/operators/Errors.kt index ad77ba9b14..c73ded9eea 100644 --- a/kotlinx-coroutines-core/common/src/flow/operators/Errors.kt +++ b/kotlinx-coroutines-core/common/src/flow/operators/Errors.kt @@ -54,7 +54,6 @@ import kotlinx.coroutines.flow.internal.unsafeFlow as flow * retry an original flow use [retryWhen] operator that can retry the flow multiple times without * introducing ever-growing stack of suspending calls. */ -@ExperimentalCoroutinesApi // tentatively stable in 1.3.0 public fun Flow.catch(action: suspend FlowCollector.(cause: Throwable) -> Unit): Flow = flow { val exception = catchImpl(this) @@ -117,7 +116,6 @@ public fun Flow.onErrorCollect( * * @throws IllegalArgumentException when [retries] is not positive. */ -@ExperimentalCoroutinesApi // tentatively stable in 1.3.0 public fun Flow.retry( retries: Long = Long.MAX_VALUE, predicate: suspend (cause: Throwable) -> Boolean = { true } @@ -169,7 +167,6 @@ public fun Flow.retry( * * See [catch] for more details. */ -@ExperimentalCoroutinesApi // tentatively stable in 1.3.0 public fun Flow.retryWhen(predicate: suspend FlowCollector.(cause: Throwable, attempt: Long) -> Boolean): Flow = flow { var attempt = 0L diff --git a/kotlinx-coroutines-core/common/src/flow/operators/Limit.kt b/kotlinx-coroutines-core/common/src/flow/operators/Limit.kt index 988c22f489..1d7ffd1db6 100644 --- a/kotlinx-coroutines-core/common/src/flow/operators/Limit.kt +++ b/kotlinx-coroutines-core/common/src/flow/operators/Limit.kt @@ -10,13 +10,13 @@ package kotlinx.coroutines.flow import kotlinx.coroutines.* import kotlinx.coroutines.flow.internal.* import kotlin.jvm.* +import kotlinx.coroutines.flow.flow as safeFlow import kotlinx.coroutines.flow.internal.unsafeFlow as flow /** * Returns a flow that ignores first [count] elements. * Throws [IllegalArgumentException] if [count] is negative. */ -@ExperimentalCoroutinesApi public fun Flow.drop(count: Int): Flow { require(count >= 0) { "Drop count should be non-negative, but had $count" } return flow { @@ -30,7 +30,6 @@ public fun Flow.drop(count: Int): Flow { /** * Returns a flow containing all elements except first elements that satisfy the given predicate. */ -@ExperimentalCoroutinesApi public fun Flow.dropWhile(predicate: suspend (T) -> Boolean): Flow = flow { var matched = false collect { value -> @@ -48,13 +47,16 @@ public fun Flow.dropWhile(predicate: suspend (T) -> Boolean): Flow = f * When [count] elements are consumed, the original flow is cancelled. * Throws [IllegalArgumentException] if [count] is not positive. */ -@ExperimentalCoroutinesApi public fun Flow.take(count: Int): Flow { require(count > 0) { "Requested element count $count should be positive" } return flow { var consumed = 0 try { collect { value -> + // Note: this for take is not written via collectWhile on purpose. + // It checks condition first and then makes a tail-call to either emit or emitAbort. + // This way normal execution does not require a state machine, only a termination (emitAbort). + // See "TakeBenchmark" for comparision of different approaches. if (++consumed < count) { return@collect emit(value) } else { @@ -74,15 +76,67 @@ private suspend fun FlowCollector.emitAbort(value: T) { /** * Returns a flow that contains first elements satisfying the given [predicate]. + * + * Note, that the resulting flow does not contain the element on which the [predicate] returned `false`. + * See [transformWhile] for a more flexible operator. */ -@ExperimentalCoroutinesApi public fun Flow.takeWhile(predicate: suspend (T) -> Boolean): Flow = flow { - try { - collect { value -> - if (predicate(value)) emit(value) - else throw AbortFlowException(this) + // This return is needed to work around a bug in JS BE: KT-39227 + return@flow collectWhile { value -> + if (predicate(value)) { + emit(value) + true + } else { + false + } + } +} + +/** + * Applies [transform] function to each value of the given flow while this + * function returns `true`. + * + * The receiver of the `transformWhile` is [FlowCollector] and thus `transformWhile` is a + * flexible function that may transform emitted element, skip it or emit it multiple times. + * + * This operator generalizes [takeWhile] and can be used as a building block for other operators. + * For example, a flow of download progress messages can be completed when the + * download is done but emit this last message (unlike `takeWhile`): + * + * ``` + * fun Flow.completeWhenDone(): Flow = + * transformWhile { progress -> + * emit(progress) // always emit progress + * !progress.isDone() // continue while download is not done + * } + * } + * ``` + */ +@ExperimentalCoroutinesApi +public fun Flow.transformWhile( + @BuilderInference transform: suspend FlowCollector.(value: T) -> Boolean +): Flow = + safeFlow { // Note: safe flow is used here, because collector is exposed to transform on each operation + // This return is needed to work around a bug in JS BE: KT-39227 + return@safeFlow collectWhile { value -> + transform(value) + } + } + +// Internal building block for non-tailcalling flow-truncating operators +internal suspend inline fun Flow.collectWhile(crossinline predicate: suspend (value: T) -> Boolean) { + val collector = object : FlowCollector { + override suspend fun emit(value: T) { + // Note: we are checking predicate first, then throw. If the predicate does suspend (calls emit, for example) + // the the resulting code is never tail-suspending and produces a state-machine + if (!predicate(value)) { + throw AbortFlowException(this) + } } + } + try { + collect(collector) } catch (e: AbortFlowException) { - e.checkOwnership(owner = this) + e.checkOwnership(collector) } } diff --git a/kotlinx-coroutines-core/common/src/flow/operators/Lint.kt b/kotlinx-coroutines-core/common/src/flow/operators/Lint.kt new file mode 100644 index 0000000000..7a70fbf7f2 --- /dev/null +++ b/kotlinx-coroutines-core/common/src/flow/operators/Lint.kt @@ -0,0 +1,82 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +@file:Suppress("unused") + +package kotlinx.coroutines.flow + +import kotlinx.coroutines.* +import kotlin.coroutines.* + +/** + * Applying [cancellable][Flow.cancellable] to a [SharedFlow] has no effect. + * See the [SharedFlow] documentation on Operator Fusion. + */ +@Deprecated( + level = DeprecationLevel.ERROR, + message = "Applying 'cancellable' to a SharedFlow has no effect. See the SharedFlow documentation on Operator Fusion.", + replaceWith = ReplaceWith("this") +) +public fun SharedFlow.cancellable(): Flow = noImpl() + +/** + * Applying [flowOn][Flow.flowOn] to [SharedFlow] has no effect. + * See the [SharedFlow] documentation on Operator Fusion. + */ +@Deprecated( + level = DeprecationLevel.ERROR, + message = "Applying 'flowOn' to SharedFlow has no effect. See the SharedFlow documentation on Operator Fusion.", + replaceWith = ReplaceWith("this") +) +public fun SharedFlow.flowOn(context: CoroutineContext): Flow = noImpl() + +/** + * Applying [conflate][Flow.conflate] to [StateFlow] has no effect. + * See the [StateFlow] documentation on Operator Fusion. + */ +@Deprecated( + level = DeprecationLevel.ERROR, + message = "Applying 'conflate' to StateFlow has no effect. See the StateFlow documentation on Operator Fusion.", + replaceWith = ReplaceWith("this") +) +public fun StateFlow.conflate(): Flow = noImpl() + +/** + * Applying [distinctUntilChanged][Flow.distinctUntilChanged] to [StateFlow] has no effect. + * See the [StateFlow] documentation on Operator Fusion. + */ +@Deprecated( + level = DeprecationLevel.ERROR, + message = "Applying 'distinctUntilChanged' to StateFlow has no effect. See the StateFlow documentation on Operator Fusion.", + replaceWith = ReplaceWith("this") +) +public fun StateFlow.distinctUntilChanged(): Flow = noImpl() + +@Deprecated( + message = "isActive is resolved into the extension of outer CoroutineScope which is likely to be an error." + + "Use currentCoroutineContext().isActive or cancellable() operator instead " + + "or specify the receiver of isActive explicitly. " + + "Additionally, flow {} builder emissions are cancellable by default.", + level = DeprecationLevel.ERROR, + replaceWith = ReplaceWith("currentCoroutineContext().isActive") +) +public val FlowCollector<*>.isActive: Boolean + get() = noImpl() + +@Deprecated( + message = "cancel() is resolved into the extension of outer CoroutineScope which is likely to be an error." + + "Use currentCoroutineContext().cancel() instead or specify the receiver of cancel() explicitly", + level = DeprecationLevel.ERROR, + replaceWith = ReplaceWith("currentCoroutineContext().cancel(cause)") +) +public fun FlowCollector<*>.cancel(cause: CancellationException? = null): Unit = noImpl() + +@Deprecated( + message = "coroutineContext is resolved into the property of outer CoroutineScope which is likely to be an error." + + "Use currentCoroutineContext() instead or specify the receiver of coroutineContext explicitly", + level = DeprecationLevel.ERROR, + replaceWith = ReplaceWith("currentCoroutineContext()") +) +public val FlowCollector<*>.coroutineContext: CoroutineContext + get() = noImpl() \ No newline at end of file diff --git a/kotlinx-coroutines-core/common/src/flow/operators/Merge.kt b/kotlinx-coroutines-core/common/src/flow/operators/Merge.kt index 85fe90b91f..340d8e313e 100644 --- a/kotlinx-coroutines-core/common/src/flow/operators/Merge.kt +++ b/kotlinx-coroutines-core/common/src/flow/operators/Merge.kt @@ -19,14 +19,14 @@ import kotlinx.coroutines.flow.internal.unsafeFlow as flow * Name of the property that defines the value of [DEFAULT_CONCURRENCY]. */ @FlowPreview -public const val DEFAULT_CONCURRENCY_PROPERTY_NAME = "kotlinx.coroutines.flow.defaultConcurrency" +public const val DEFAULT_CONCURRENCY_PROPERTY_NAME: String = "kotlinx.coroutines.flow.defaultConcurrency" /** * Default concurrency limit that is used by [flattenMerge] and [flatMapMerge] operators. * It is 16 by default and can be changed on JVM using [DEFAULT_CONCURRENCY_PROPERTY_NAME] property. */ @FlowPreview -public val DEFAULT_CONCURRENCY = systemProp(DEFAULT_CONCURRENCY_PROPERTY_NAME, +public val DEFAULT_CONCURRENCY: Int = systemProp(DEFAULT_CONCURRENCY_PROPERTY_NAME, 16, 1, Int.MAX_VALUE ) diff --git a/kotlinx-coroutines-core/common/src/flow/operators/Share.kt b/kotlinx-coroutines-core/common/src/flow/operators/Share.kt new file mode 100644 index 0000000000..fe737a5bd1 --- /dev/null +++ b/kotlinx-coroutines-core/common/src/flow/operators/Share.kt @@ -0,0 +1,413 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +@file:JvmMultifileClass +@file:JvmName("FlowKt") + +package kotlinx.coroutines.flow + +import kotlinx.coroutines.* +import kotlinx.coroutines.channels.* +import kotlinx.coroutines.flow.internal.* +import kotlin.coroutines.* +import kotlin.jvm.* + +// -------------------------------- shareIn -------------------------------- + +/** + * Converts a _cold_ [Flow] into a _hot_ [SharedFlow] that is started in the given coroutine [scope], + * sharing emissions from a single running instance of the upstream flow with multiple downstream subscribers, + * and replaying a specified number of [replay] values to new subscribers. See the [SharedFlow] documentation + * for the general concepts of shared flows. + * + * The starting of the sharing coroutine is controlled by the [started] parameter. The following options + * are supported. + * + * * [Eagerly][SharingStarted.Eagerly] — the upstream flow is started even before the first subscriber appears. Note + * that in this case all values emitted by the upstream beyond the most recent values as specified by + * [replay] parameter **will be immediately discarded**. + * * [Lazily][SharingStarted.Lazily] — starts the upstream flow after the first subscriber appears, which guarantees + * that this first subscriber gets all the emitted values, while subsequent subscribers are only guaranteed to + * get the most recent [replay] values. The upstream flow continues to be active even when all subscribers + * disappear, but only the most recent [replay] values are cached without subscribers. + * * [WhileSubscribed()][SharingStarted.WhileSubscribed] — starts the upstream flow when the first subscriber + * appears, immediately stops when the last subscriber disappears, keeping the replay cache forever. + * It has additional optional configuration parameters as explained in its documentation. + * * A custom strategy can be supplied by implementing the [SharingStarted] interface. + * + * The `shareIn` operator is useful in situations when there is a _cold_ flow that is expensive to create and/or + * to maintain, but there are multiple subscribers that need to collect its values. For example, consider a + * flow of messages coming from a backend over the expensive network connection, taking a lot of + * time to establish. Conceptually, it might be implemented like this: + * + * ``` + * val backendMessages: Flow = flow { + * connectToBackend() // takes a lot of time + * try { + * while (true) { + * emit(receiveMessageFromBackend()) + * } + * } finally { + * disconnectFromBackend() + * } + * } + * ``` + * + * If this flow is directly used in the application, then every time it is collected a fresh connection is + * established, and it will take a while before messages start flowing. However, we can share a single connection + * and establish it eagerly like this: + * + * ``` + * val messages: SharedFlow = backendMessages.shareIn(scope, SharingStarted.Eagerly) + * ``` + * + * Now a single connection is shared between all collectors from `messages`, and there is a chance that the connection + * is already established by the time it is needed. + * + * ### Upstream completion and error handling + * + * **Normal completion of the upstream flow has no effect on subscribers**, and the sharing coroutine continues to run. If a + * a strategy like [SharingStarted.WhileSubscribed] is used, then the upstream can get restarted again. If a special + * action on upstream completion is needed, then an [onCompletion] operator can be used before the + * `shareIn` operator to emit a special value in this case, like this: + * + * ``` + * backendMessages + * .onCompletion { cause -> if (cause == null) emit(UpstreamHasCompletedMessage) } + * .shareIn(scope, SharingStarted.Eagerly) + * ``` + * + * Any exception in the upstream flow terminates the sharing coroutine without affecting any of the subscribers, + * and will be handled by the [scope] in which the sharing coroutine is launched. Custom exception handling + * can be configured by using the [catch] or [retry] operators before the `shareIn` operator. + * For example, to retry connection on any `IOException` with 1 second delay between attempts, use: + * + * ``` + * val messages = backendMessages + * .retry { e -> + * val shallRetry = e is IOException // other exception are bugs - handle them + * if (shallRetry) delay(1000) + * shallRetry + * } + * .shareIn(scope, SharingStarted.Eagerly) + * ``` + * + * ### Initial value + * + * When a special initial value is needed to signal to subscribers that the upstream is still loading the data, + * use the [onStart] operator on the upstream flow. For example: + * + * ``` + * backendMessages + * .onStart { emit(UpstreamIsStartingMessage) } + * .shareIn(scope, SharingStarted.Eagerly, 1) // replay one most recent message + * ``` + * + * ### Buffering and conflation + * + * The `shareIn` operator runs the upstream flow in a separate coroutine, and buffers emissions from upstream as explained + * in the [buffer] operator's description, using a buffer of [replay] size or the default (whichever is larger). + * This default buffering can be overridden with an explicit buffer configuration by preceding the `shareIn` call + * with [buffer] or [conflate], for example: + * + * * `buffer(0).shareIn(scope, started, 0)` — overrides the default buffer size and creates a [SharedFlow] without a buffer. + * Effectively, it configures sequential processing between the upstream emitter and subscribers, + * as the emitter is suspended until all subscribers process the value. Note, that the value is still immediately + * discarded when there are no subscribers. + * * `buffer(b).shareIn(scope, started, r)` — creates a [SharedFlow] with `replay = r` and `extraBufferCapacity = b`. + * * `conflate().shareIn(scope, started, r)` — creates a [SharedFlow] with `replay = r`, `onBufferOverflow = DROP_OLDEST`, + * and `extraBufferCapacity = 1` when `replay == 0` to support this strategy. + * + * ### Operator fusion + * + * Application of [flowOn][Flow.flowOn], [buffer] with [RENDEZVOUS][Channel.RENDEZVOUS] capacity, + * or [cancellable] operators to the resulting shared flow has no effect. + * + * ### Exceptions + * + * This function throws [IllegalArgumentException] on unsupported values of parameters or combinations thereof. + * + * @param scope the coroutine scope in which sharing is started. + * @param started the strategy that controls when sharing is started and stopped. + * @param replay the number of values replayed to new subscribers (cannot be negative, defaults to zero). + */ +public fun Flow.shareIn( + scope: CoroutineScope, + started: SharingStarted, + replay: Int = 0 +): SharedFlow { + val config = configureSharing(replay) + val shared = MutableSharedFlow( + replay = replay, + extraBufferCapacity = config.extraBufferCapacity, + onBufferOverflow = config.onBufferOverflow + ) + @Suppress("UNCHECKED_CAST") + scope.launchSharing(config.context, config.upstream, shared, started, NO_VALUE as T) + return shared.asSharedFlow() +} + +private class SharingConfig( + @JvmField val upstream: Flow, + @JvmField val extraBufferCapacity: Int, + @JvmField val onBufferOverflow: BufferOverflow, + @JvmField val context: CoroutineContext +) + +// Decomposes upstream flow to fuse with it when possible +private fun Flow.configureSharing(replay: Int): SharingConfig { + assert { replay >= 0 } + val defaultExtraCapacity = replay.coerceAtLeast(Channel.CHANNEL_DEFAULT_CAPACITY) - replay + // Combine with preceding buffer/flowOn and channel-using operators + if (this is ChannelFlow) { + // Check if this ChannelFlow can operate without a channel + val upstream = dropChannelOperators() + if (upstream != null) { // Yes, it can => eliminate the intermediate channel + return SharingConfig( + upstream = upstream, + extraBufferCapacity = when (capacity) { + Channel.OPTIONAL_CHANNEL, Channel.BUFFERED, 0 -> // handle special capacities + when { + onBufferOverflow == BufferOverflow.SUSPEND -> // buffer was configured with suspension + if (capacity == 0) 0 else defaultExtraCapacity // keep explicitly configured 0 or use default + replay == 0 -> 1 // no suspension => need at least buffer of one + else -> 0 // replay > 0 => no need for extra buffer beyond replay because we don't suspend + } + else -> capacity // otherwise just use the specified capacity as extra capacity + }, + onBufferOverflow = onBufferOverflow, + context = context + ) + } + } + // Add sharing operator on top with a default buffer + return SharingConfig( + upstream = this, + extraBufferCapacity = defaultExtraCapacity, + onBufferOverflow = BufferOverflow.SUSPEND, + context = EmptyCoroutineContext + ) +} + +// Launches sharing coroutine +private fun CoroutineScope.launchSharing( + context: CoroutineContext, + upstream: Flow, + shared: MutableSharedFlow, + started: SharingStarted, + initialValue: T +) { + launch(context) { // the single coroutine to rule the sharing + // Optimize common built-in started strategies + when { + started === SharingStarted.Eagerly -> { + // collect immediately & forever + upstream.collect(shared) + } + started === SharingStarted.Lazily -> { + // start collecting on the first subscriber - wait for it first + shared.subscriptionCount.first { it > 0 } + upstream.collect(shared) + } + else -> { + // other & custom strategies + started.command(shared.subscriptionCount) + .distinctUntilChanged() // only changes in command have effect + .collectLatest { // cancels block on new emission + when (it) { + SharingCommand.START -> upstream.collect(shared) // can be cancelled + SharingCommand.STOP -> { /* just cancel and do nothing else */ } + SharingCommand.STOP_AND_RESET_REPLAY_CACHE -> { + if (initialValue === NO_VALUE) { + shared.resetReplayCache() // regular shared flow -> reset cache + } else { + shared.tryEmit(initialValue) // state flow -> reset to initial value + } + } + } + } + } + } + } +} + +// -------------------------------- stateIn -------------------------------- + +/** + * Converts a _cold_ [Flow] into a _hot_ [StateFlow] that is started in the given coroutine [scope], + * sharing the most recently emitted value from a single running instance of the upstream flow with multiple + * downstream subscribers. See the [StateFlow] documentation for the general concepts of state flows. + * + * The starting of the sharing coroutine is controlled by the [started] parameter, as explained in the + * documentation for [shareIn] operator. + * + * The `stateIn` operator is useful in situations when there is a _cold_ flow that provides updates to the + * value of some state and is expensive to create and/or to maintain, but there are multiple subscribers + * that need to collect the most recent state value. For example, consider a + * flow of state updates coming from a backend over the expensive network connection, taking a lot of + * time to establish. Conceptually it might be implemented like this: + * + * ``` + * val backendState: Flow = flow { + * connectToBackend() // takes a lot of time + * try { + * while (true) { + * emit(receiveStateUpdateFromBackend()) + * } + * } finally { + * disconnectFromBackend() + * } + * } + * ``` + * + * If this flow is directly used in the application, then every time it is collected a fresh connection is + * established, and it will take a while before state updates start flowing. However, we can share a single connection + * and establish it eagerly like this: + * + * ``` + * val state: StateFlow = backendMessages.stateIn(scope, SharingStarted.Eagerly, State.LOADING) + * ``` + * + * Now, a single connection is shared between all collectors from `state`, and there is a chance that the connection + * is already established by the time it is needed. + * + * ### Upstream completion and error handling + * + * **Normal completion of the upstream flow has no effect on subscribers**, and the sharing coroutine continues to run. If a + * a strategy like [SharingStarted.WhileSubscribed] is used, then the upstream can get restarted again. If a special + * action on upstream completion is needed, then an [onCompletion] operator can be used before + * the `stateIn` operator to emit a special value in this case. See the [shareIn] operator's documentation for an example. + * + * Any exception in the upstream flow terminates the sharing coroutine without affecting any of the subscribers, + * and will be handled by the [scope] in which the sharing coroutine is launched. Custom exception handling + * can be configured by using the [catch] or [retry] operators before the `stateIn` operator, similarly to + * the [shareIn] operator. + * + * ### Operator fusion + * + * Application of [flowOn][Flow.flowOn], [conflate][Flow.conflate], + * [buffer] with [CONFLATED][Channel.CONFLATED] or [RENDEZVOUS][Channel.RENDEZVOUS] capacity, + * [distinctUntilChanged][Flow.distinctUntilChanged], or [cancellable] operators to a state flow has no effect. + * + * @param scope the coroutine scope in which sharing is started. + * @param started the strategy that controls when sharing is started and stopped. + * @param initialValue the initial value of the state flow. + * This value is also used when the state flow is reset using the [SharingStarted.WhileSubscribed] strategy + * with the `replayExpirationMillis` parameter. + */ +public fun Flow.stateIn( + scope: CoroutineScope, + started: SharingStarted, + initialValue: T +): StateFlow { + val config = configureSharing(1) + val state = MutableStateFlow(initialValue) + scope.launchSharing(config.context, config.upstream, state, started, initialValue) + return state.asStateFlow() +} + +/** + * Starts the upstream flow in a given [scope], suspends until the first value is emitted, and returns a _hot_ + * [StateFlow] of future emissions, sharing the most recently emitted value from this running instance of the upstream flow + * with multiple downstream subscribers. See the [StateFlow] documentation for the general concepts of state flows. + * + * @param scope the coroutine scope in which sharing is started. + */ +public suspend fun Flow.stateIn(scope: CoroutineScope): StateFlow { + val config = configureSharing(1) + val result = CompletableDeferred>() + scope.launchSharingDeferred(config.context, config.upstream, result) + return result.await() +} + +private fun CoroutineScope.launchSharingDeferred( + context: CoroutineContext, + upstream: Flow, + result: CompletableDeferred> +) { + launch(context) { + try { + var state: MutableStateFlow? = null + upstream.collect { value -> + state?.let { it.value = value } ?: run { + state = MutableStateFlow(value).also { + result.complete(it.asStateFlow()) + } + } + } + } catch (e: Throwable) { + // Notify the waiter that the flow has failed + result.completeExceptionally(e) + // But still cancel the scope where state was (not) produced + throw e + } + } +} + +// -------------------------------- asSharedFlow/asStateFlow -------------------------------- + +/** + * Represents this mutable shared flow as a read-only shared flow. + */ +public fun MutableSharedFlow.asSharedFlow(): SharedFlow = + ReadonlySharedFlow(this) + +/** + * Represents this mutable state flow as a read-only state flow. + */ +public fun MutableStateFlow.asStateFlow(): StateFlow = + ReadonlyStateFlow(this) + +private class ReadonlySharedFlow( + flow: SharedFlow +) : SharedFlow by flow, CancellableFlow, FusibleFlow { + override fun fuse(context: CoroutineContext, capacity: Int, onBufferOverflow: BufferOverflow) = + fuseSharedFlow(context, capacity, onBufferOverflow) +} + +private class ReadonlyStateFlow( + flow: StateFlow +) : StateFlow by flow, CancellableFlow, FusibleFlow { + override fun fuse(context: CoroutineContext, capacity: Int, onBufferOverflow: BufferOverflow) = + fuseStateFlow(context, capacity, onBufferOverflow) +} + +// -------------------------------- onSubscription -------------------------------- + +/** + * Returns a flow that invokes the given [action] **after** this shared flow starts to be collected + * (after the subscription is registered). + * + * The [action] is called before any value is emitted from the upstream + * flow to this subscription but after the subscription is established. It is guaranteed that all emissions to + * the upstream flow that happen inside or immediately after this `onSubscription` action will be + * collected by this subscription. + * + * The receiver of the [action] is [FlowCollector], so `onSubscription` can emit additional elements. + */ +public fun SharedFlow.onSubscription(action: suspend FlowCollector.() -> Unit): SharedFlow = + SubscribedSharedFlow(this, action) + +private class SubscribedSharedFlow( + private val sharedFlow: SharedFlow, + private val action: suspend FlowCollector.() -> Unit +) : SharedFlow by sharedFlow { + override suspend fun collect(collector: FlowCollector) = + sharedFlow.collect(SubscribedFlowCollector(collector, action)) +} + +internal class SubscribedFlowCollector( + private val collector: FlowCollector, + private val action: suspend FlowCollector.() -> Unit +) : FlowCollector by collector { + suspend fun onSubscription() { + val safeCollector = SafeCollector(collector, currentCoroutineContext()) + try { + safeCollector.action() + } finally { + safeCollector.releaseIntercepted() + } + if (collector is SubscribedFlowCollector) collector.onSubscription() + } +} diff --git a/kotlinx-coroutines-core/common/src/flow/operators/Transform.kt b/kotlinx-coroutines-core/common/src/flow/operators/Transform.kt index f446c80c23..e3552d2893 100644 --- a/kotlinx-coroutines-core/common/src/flow/operators/Transform.kt +++ b/kotlinx-coroutines-core/common/src/flow/operators/Transform.kt @@ -59,7 +59,6 @@ public inline fun Flow.mapNotNull(crossinline transform: suspend /** * Returns a flow that wraps each element into [IndexedValue], containing value and its index (starting from zero). */ -@ExperimentalCoroutinesApi public fun Flow.withIndex(): Flow> = flow { var index = 0 collect { value -> @@ -68,7 +67,7 @@ public fun Flow.withIndex(): Flow> = flow { } /** - * Returns a flow which performs the given [action] on each value of the original flow. + * Returns a flow that invokes the given [action] **before** each value of the upstream flow is emitted downstream. */ public fun Flow.onEach(action: suspend (T) -> Unit): Flow = transform { value -> action(value) @@ -101,12 +100,12 @@ public fun Flow.scan(initial: R, @BuilderInference operation: suspend * * For example: * ``` - * flowOf(1, 2, 3, 4).scanReduce { (v1, v2) -> v1 + v2 }.toList() + * flowOf(1, 2, 3, 4).runningReduce { (v1, v2) -> v1 + v2 }.toList() * ``` * will produce `[1, 3, 6, 10]` */ @ExperimentalCoroutinesApi -public fun Flow.scanReduce(operation: suspend (accumulator: T, value: T) -> T): Flow = flow { +public fun Flow.runningReduce(operation: suspend (accumulator: T, value: T) -> T): Flow = flow { var accumulator: Any? = NULL collect { value -> accumulator = if (accumulator === NULL) { diff --git a/kotlinx-coroutines-core/common/src/flow/operators/Zip.kt b/kotlinx-coroutines-core/common/src/flow/operators/Zip.kt index 03bddf8330..790c0895e4 100644 --- a/kotlinx-coroutines-core/common/src/flow/operators/Zip.kt +++ b/kotlinx-coroutines-core/common/src/flow/operators/Zip.kt @@ -8,7 +8,6 @@ package kotlinx.coroutines.flow -import kotlinx.coroutines.* import kotlinx.coroutines.flow.internal.* import kotlin.jvm.* import kotlinx.coroutines.flow.flow as safeFlow @@ -20,8 +19,8 @@ import kotlinx.coroutines.flow.internal.unsafeFlow as flow * * It can be demonstrated with the following example: * ``` - * val flow = flowOf(1, 2).delayEach(10) - * val flow2 = flowOf("a", "b", "c").delayEach(15) + * val flow = flowOf(1, 2).onEach { delay(10) } + * val flow2 = flowOf("a", "b", "c").onEach { delay(15) } * flow.combine(flow2) { i, s -> i.toString() + s }.collect { * println(it) // Will print "1a 2a 2b 2c" * } @@ -30,11 +29,8 @@ import kotlinx.coroutines.flow.internal.unsafeFlow as flow * This function is a shorthand for `flow.combineTransform(flow2) { a, b -> emit(transform(a, b)) } */ @JvmName("flowCombine") -@ExperimentalCoroutinesApi public fun Flow.combine(flow: Flow, transform: suspend (a: T1, b: T2) -> R): Flow = flow { - combineTransformInternal(this@combine, flow) { a, b -> - emit(transform(a, b)) - } + combineInternal(arrayOf(this@combine, flow), nullArrayFactory(), { emit(transform(it[0] as T1, it[1] as T2)) }) } /** @@ -43,8 +39,8 @@ public fun Flow.combine(flow: Flow, transform: suspend (a: T * * It can be demonstrated with the following example: * ``` - * val flow = flowOf(1, 2).delayEach(10) - * val flow2 = flowOf("a", "b", "c").delayEach(15) + * val flow = flowOf(1, 2).onEach { delay(10) } + * val flow2 = flowOf("a", "b", "c").onEach { delay(15) } * combine(flow, flow2) { i, s -> i.toString() + s }.collect { * println(it) // Will print "1a 2a 2b 2c" * } @@ -52,7 +48,6 @@ public fun Flow.combine(flow: Flow, transform: suspend (a: T * * This function is a shorthand for `combineTransform(flow, flow2) { a, b -> emit(transform(a, b)) } */ -@ExperimentalCoroutinesApi public fun combine(flow: Flow, flow2: Flow, transform: suspend (a: T1, b: T2) -> R): Flow = flow.combine(flow2, transform) @@ -74,14 +69,14 @@ public fun combine(flow: Flow, flow2: Flow, transform: suspe * ``` */ @JvmName("flowCombineTransform") -@ExperimentalCoroutinesApi public fun Flow.combineTransform( flow: Flow, @BuilderInference transform: suspend FlowCollector.(a: T1, b: T2) -> Unit -): Flow = safeFlow { - combineTransformInternal(this@combineTransform, flow) { a, b -> - transform(a, b) - } +): Flow = combineTransformUnsafe(this, flow) { args: Array<*> -> + transform( + args[0] as T1, + args[1] as T2 + ) } /** @@ -101,12 +96,11 @@ public fun Flow.combineTransform( * } * ``` */ -@ExperimentalCoroutinesApi public fun combineTransform( flow: Flow, flow2: Flow, @BuilderInference transform: suspend FlowCollector.(a: T1, b: T2) -> Unit -): Flow = combineTransform(flow, flow2) { args: Array<*> -> +): Flow = combineTransformUnsafe(flow, flow2) { args: Array<*> -> transform( args[0] as T1, args[1] as T2 @@ -117,13 +111,12 @@ public fun combineTransform( * Returns a [Flow] whose values are generated with [transform] function by combining * the most recently emitted values by each flow. */ -@ExperimentalCoroutinesApi -public inline fun combine( +public fun combine( flow: Flow, flow2: Flow, flow3: Flow, - @BuilderInference crossinline transform: suspend (T1, T2, T3) -> R -): Flow = combine(flow, flow2, flow3) { args: Array<*> -> + @BuilderInference transform: suspend (T1, T2, T3) -> R +): Flow = combineUnsafe(flow, flow2, flow3) { args: Array<*> -> transform( args[0] as T1, args[1] as T2, @@ -137,13 +130,12 @@ public inline fun combine( * The receiver of the [transform] is [FlowCollector] and thus `transform` is a * generic function that may transform emitted element, skip it or emit it multiple times. */ -@ExperimentalCoroutinesApi -public inline fun combineTransform( +public fun combineTransform( flow: Flow, flow2: Flow, flow3: Flow, - @BuilderInference crossinline transform: suspend FlowCollector.(T1, T2, T3) -> Unit -): Flow = combineTransform(flow, flow2, flow3) { args: Array<*> -> + @BuilderInference transform: suspend FlowCollector.(T1, T2, T3) -> Unit +): Flow = combineTransformUnsafe(flow, flow2, flow3) { args: Array<*> -> transform( args[0] as T1, args[1] as T2, @@ -155,13 +147,12 @@ public inline fun combineTransform( * Returns a [Flow] whose values are generated with [transform] function by combining * the most recently emitted values by each flow. */ -@ExperimentalCoroutinesApi -public inline fun combine( +public fun combine( flow: Flow, flow2: Flow, flow3: Flow, flow4: Flow, - crossinline transform: suspend (T1, T2, T3, T4) -> R + transform: suspend (T1, T2, T3, T4) -> R ): Flow = combine(flow, flow2, flow3, flow4) { args: Array<*> -> transform( args[0] as T1, @@ -177,14 +168,13 @@ public inline fun combine( * The receiver of the [transform] is [FlowCollector] and thus `transform` is a * generic function that may transform emitted element, skip it or emit it multiple times. */ -@ExperimentalCoroutinesApi -public inline fun combineTransform( +public fun combineTransform( flow: Flow, flow2: Flow, flow3: Flow, flow4: Flow, - @BuilderInference crossinline transform: suspend FlowCollector.(T1, T2, T3, T4) -> Unit -): Flow = combineTransform(flow, flow2, flow3, flow4) { args: Array<*> -> + @BuilderInference transform: suspend FlowCollector.(T1, T2, T3, T4) -> Unit +): Flow = combineTransformUnsafe(flow, flow2, flow3, flow4) { args: Array<*> -> transform( args[0] as T1, args[1] as T2, @@ -197,15 +187,14 @@ public inline fun combineTransform( * Returns a [Flow] whose values are generated with [transform] function by combining * the most recently emitted values by each flow. */ -@ExperimentalCoroutinesApi -public inline fun combine( +public fun combine( flow: Flow, flow2: Flow, flow3: Flow, flow4: Flow, flow5: Flow, - crossinline transform: suspend (T1, T2, T3, T4, T5) -> R -): Flow = combine(flow, flow2, flow3, flow4, flow5) { args: Array<*> -> + transform: suspend (T1, T2, T3, T4, T5) -> R +): Flow = combineUnsafe(flow, flow2, flow3, flow4, flow5) { args: Array<*> -> transform( args[0] as T1, args[1] as T2, @@ -221,15 +210,14 @@ public inline fun combine( * The receiver of the [transform] is [FlowCollector] and thus `transform` is a * generic function that may transform emitted element, skip it or emit it multiple times. */ -@ExperimentalCoroutinesApi -public inline fun combineTransform( +public fun combineTransform( flow: Flow, flow2: Flow, flow3: Flow, flow4: Flow, flow5: Flow, - @BuilderInference crossinline transform: suspend FlowCollector.(T1, T2, T3, T4, T5) -> Unit -): Flow = combineTransform(flow, flow2, flow3, flow4, flow5) { args: Array<*> -> + @BuilderInference transform: suspend FlowCollector.(T1, T2, T3, T4, T5) -> Unit +): Flow = combineTransformUnsafe(flow, flow2, flow3, flow4, flow5) { args: Array<*> -> transform( args[0] as T1, args[1] as T2, @@ -243,7 +231,6 @@ public inline fun combineTransform( * Returns a [Flow] whose values are generated with [transform] function by combining * the most recently emitted values by each flow. */ -@ExperimentalCoroutinesApi public inline fun combine( vararg flows: Flow, crossinline transform: suspend (Array) -> R @@ -257,7 +244,6 @@ public inline fun combine( * The receiver of the [transform] is [FlowCollector] and thus `transform` is a * generic function that may transform emitted element, skip it or emit it multiple times. */ -@ExperimentalCoroutinesApi public inline fun combineTransform( vararg flows: Flow, @BuilderInference crossinline transform: suspend FlowCollector.(Array) -> Unit @@ -265,11 +251,35 @@ public inline fun combineTransform( combineInternal(flows, { arrayOfNulls(flows.size) }, { transform(it) }) } +/* + * Same as combine, but does not copy array each time, deconstructing existing + * array each time. Used in overloads that accept FunctionN instead of Function> + */ +private inline fun combineUnsafe( + vararg flows: Flow, + crossinline transform: suspend (Array) -> R +): Flow = flow { + combineInternal(flows, nullArrayFactory(), { emit(transform(it)) }) +} + +/* + * Same as combineTransform, but does not copy array each time, deconstructing existing + * array each time. Used in overloads that accept FunctionN instead of Function> + */ +private inline fun combineTransformUnsafe( + vararg flows: Flow, + @BuilderInference crossinline transform: suspend FlowCollector.(Array) -> Unit +): Flow = safeFlow { + combineInternal(flows, nullArrayFactory(), { transform(it) }) +} + +// Saves bunch of anonymous classes +private fun nullArrayFactory(): () -> Array? = { null } + /** * Returns a [Flow] whose values are generated with [transform] function by combining * the most recently emitted values by each flow. */ -@ExperimentalCoroutinesApi public inline fun combine( flows: Iterable>, crossinline transform: suspend (Array) -> R @@ -289,7 +299,6 @@ public inline fun combine( * The receiver of the [transform] is [FlowCollector] and thus `transform` is a * generic function that may transform emitted element, skip it or emit it multiple times. */ -@ExperimentalCoroutinesApi public inline fun combineTransform( flows: Iterable>, @BuilderInference crossinline transform: suspend FlowCollector.(Array) -> Unit @@ -306,12 +315,17 @@ public inline fun combineTransform( * * It can be demonstrated with the following example: * ``` - * val flow = flowOf(1, 2, 3).delayEach(10) - * val flow2 = flowOf("a", "b", "c", "d").delayEach(15) + * val flow = flowOf(1, 2, 3).onEach { delay(10) } + * val flow2 = flowOf("a", "b", "c", "d").onEach { delay(15) } * flow.zip(flow2) { i, s -> i.toString() + s }.collect { * println(it) // Will print "1a 2b 3c" * } * ``` + * + * ### Buffering + * + * The upstream flow is collected sequentially in the same coroutine without any buffering, while the + * [other] flow is collected concurrently as if `buffer(0)` is used. See documentation in the [buffer] operator + * for explanation. You can use additional calls to the [buffer] operator as needed for more concurrency. */ -@ExperimentalCoroutinesApi public fun Flow.zip(other: Flow, transform: suspend (T1, T2) -> R): Flow = zipImpl(this, other, transform) diff --git a/kotlinx-coroutines-core/common/src/flow/terminal/Collect.kt b/kotlinx-coroutines-core/common/src/flow/terminal/Collect.kt index 52d060f255..e8f2a9a3b6 100644 --- a/kotlinx-coroutines-core/common/src/flow/terminal/Collect.kt +++ b/kotlinx-coroutines-core/common/src/flow/terminal/Collect.kt @@ -27,7 +27,7 @@ import kotlin.jvm.* * .collect() // trigger collection of the flow * ``` */ -public suspend fun Flow<*>.collect() = collect(NopCollector) +public suspend fun Flow<*>.collect(): Unit = collect(NopCollector) /** * Terminal flow operator that [launches][launch] the [collection][collect] of the given flow in the [scope]. @@ -46,7 +46,6 @@ public suspend fun Flow<*>.collect() = collect(NopCollector) * * Note that resulting value of [launchIn] is not used the provided scope takes care of cancellation. */ -@ExperimentalCoroutinesApi // tentatively stable in 1.3.0 public fun Flow.launchIn(scope: CoroutineScope): Job = scope.launch { collect() // tail-call } @@ -80,7 +79,6 @@ public suspend inline fun Flow.collect(crossinline action: suspend (value * * See also [collect] and [withIndex]. */ -@ExperimentalCoroutinesApi public suspend inline fun Flow.collectIndexed(crossinline action: suspend (index: Int, value: T) -> Unit): Unit = collect(object : FlowCollector { private var index = 0 @@ -108,7 +106,6 @@ public suspend inline fun Flow.collectIndexed(crossinline action: suspend * * prints "Collecting 1, Collecting 2, 2 collected" */ -@ExperimentalCoroutinesApi public suspend fun Flow.collectLatest(action: suspend (value: T) -> Unit) { /* * Implementation note: @@ -131,5 +128,4 @@ public suspend fun Flow.collectLatest(action: suspend (value: T) -> Unit) * It is a shorthand for `flow.collect { value -> emit(value) }`. */ @BuilderInference -@ExperimentalCoroutinesApi -public suspend inline fun FlowCollector.emitAll(flow: Flow) = flow.collect(this) +public suspend inline fun FlowCollector.emitAll(flow: Flow): Unit = flow.collect(this) diff --git a/kotlinx-coroutines-core/common/src/flow/terminal/Count.kt b/kotlinx-coroutines-core/common/src/flow/terminal/Count.kt index 63cf52434b..d50c027268 100644 --- a/kotlinx-coroutines-core/common/src/flow/terminal/Count.kt +++ b/kotlinx-coroutines-core/common/src/flow/terminal/Count.kt @@ -13,7 +13,6 @@ import kotlin.jvm.* /** * Returns the number of elements in this flow. */ -@ExperimentalCoroutinesApi public suspend fun Flow.count(): Int { var i = 0 collect { @@ -26,7 +25,6 @@ public suspend fun Flow.count(): Int { /** * Returns the number of elements matching the given predicate. */ -@ExperimentalCoroutinesApi public suspend fun Flow.count(predicate: suspend (T) -> Boolean): Int { var i = 0 collect { value -> diff --git a/kotlinx-coroutines-core/common/src/flow/terminal/Reduce.kt b/kotlinx-coroutines-core/common/src/flow/terminal/Reduce.kt index 674f8322f2..83f5498e4d 100644 --- a/kotlinx-coroutines-core/common/src/flow/terminal/Reduce.kt +++ b/kotlinx-coroutines-core/common/src/flow/terminal/Reduce.kt @@ -8,16 +8,14 @@ package kotlinx.coroutines.flow -import kotlinx.coroutines.* import kotlinx.coroutines.flow.internal.* -import kotlinx.coroutines.flow.internal.unsafeFlow as flow +import kotlinx.coroutines.internal.Symbol import kotlin.jvm.* /** * Accumulates value starting with the first element and applying [operation] to current accumulator value and each element. * Throws [NoSuchElementException] if flow was empty. */ -@ExperimentalCoroutinesApi public suspend fun Flow.reduce(operation: suspend (accumulator: S, value: T) -> S): S { var accumulator: Any? = NULL @@ -38,7 +36,6 @@ public suspend fun Flow.reduce(operation: suspend (accumulator: S, /** * Accumulates value starting with [initial] value and applying [operation] current accumulator value and each element */ -@ExperimentalCoroutinesApi public suspend inline fun Flow.fold( initial: R, crossinline operation: suspend (acc: R, value: T) -> R @@ -51,33 +48,39 @@ public suspend inline fun Flow.fold( } /** - * The terminal operator, that awaits for one and only one value to be published. + * The terminal operator that awaits for one and only one value to be emitted. * Throws [NoSuchElementException] for empty flow and [IllegalStateException] for flow * that contains more than one element. */ public suspend fun Flow.single(): T { var result: Any? = NULL collect { value -> - if (result !== NULL) error("Expected only one element") + require(result === NULL) { "Flow has more than one element" } result = value } - if (result === NULL) throw NoSuchElementException("Expected at least one element") - @Suppress("UNCHECKED_CAST") + if (result === NULL) throw NoSuchElementException("Flow is empty") return result as T } /** - * The terminal operator, that awaits for one and only one value to be published. - * Throws [IllegalStateException] for flow that contains more than one element. + * The terminal operator that awaits for one and only one value to be emitted. + * Returns the single value or `null`, if the flow was empty or emitted more than one value. */ -public suspend fun Flow.singleOrNull(): T? { - var result: T? = null - collect { value -> - if (result != null) error("Expected only one element") - result = value +public suspend fun Flow.singleOrNull(): T? { + var result: Any? = NULL + collectWhile { + // No values yet, update result + if (result === NULL) { + result = it + true + } else { + // Second value, reset result and bail out + result = NULL + false + } } - return result + return if (result === NULL) null else result as T } /** @@ -86,15 +89,10 @@ public suspend fun Flow.singleOrNull(): T? { */ public suspend fun Flow.first(): T { var result: Any? = NULL - try { - collect { value -> - result = value - throw AbortFlowException(NopCollector) - } - } catch (e: AbortFlowException) { - // Do nothing + collectWhile { + result = it + false } - if (result === NULL) throw NoSuchElementException("Expected at least one element") return result as T } @@ -105,17 +103,14 @@ public suspend fun Flow.first(): T { */ public suspend fun Flow.first(predicate: suspend (T) -> Boolean): T { var result: Any? = NULL - try { - collect { value -> - if (predicate(value)) { - result = value - throw AbortFlowException(NopCollector) - } + collectWhile { + if (predicate(it)) { + result = it + false + } else { + true } - } catch (e: AbortFlowException) { - // Do nothing } - if (result === NULL) throw NoSuchElementException("Expected at least one element matching the predicate $predicate") return result as T } @@ -124,34 +119,28 @@ public suspend fun Flow.first(predicate: suspend (T) -> Boolean): T { * The terminal operator that returns the first element emitted by the flow and then cancels flow's collection. * Returns `null` if the flow was empty. */ -public suspend fun Flow.firstOrNull(): T? { +public suspend fun Flow.firstOrNull(): T? { var result: T? = null - try { - collect { value -> - result = value - throw AbortFlowException(NopCollector) - } - } catch (e: AbortFlowException) { - // Do nothing + collectWhile { + result = it + false } return result } /** - * The terminal operator that returns the first element emitted by the flow matching the given [predicate] and then cancels flow's collection. + * The terminal operator that returns the first element emitted by the flow matching the given [predicate] and then cancels flow's collection. * Returns `null` if the flow did not contain an element matching the [predicate]. */ -public suspend fun Flow.firstOrNull(predicate: suspend (T) -> Boolean): T? { +public suspend fun Flow.firstOrNull(predicate: suspend (T) -> Boolean): T? { var result: T? = null - try { - collect { value -> - if (predicate(value)) { - result = value - throw AbortFlowException(NopCollector) - } + collectWhile { + if (predicate(it)) { + result = it + false + } else { + true } - } catch (e: AbortFlowException) { - // Do nothing } return result } diff --git a/kotlinx-coroutines-core/common/src/internal/Atomic.kt b/kotlinx-coroutines-core/common/src/internal/Atomic.kt index 56fd35b731..a27d5491d1 100644 --- a/kotlinx-coroutines-core/common/src/internal/Atomic.kt +++ b/kotlinx-coroutines-core/common/src/internal/Atomic.kt @@ -1,6 +1,7 @@ /* * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ +@file:Suppress("NO_EXPLICIT_VISIBILITY_IN_API_MODE") package kotlinx.coroutines.internal @@ -38,7 +39,8 @@ public abstract class OpDescriptor { } @SharedImmutable -private val NO_DECISION: Any = Symbol("NO_DECISION") +@JvmField +internal val NO_DECISION: Any = Symbol("NO_DECISION") /** * Descriptor for multi-word atomic operation. @@ -51,9 +53,13 @@ private val NO_DECISION: Any = Symbol("NO_DECISION") * * @suppress **This is unstable API and it is subject to change.** */ +@InternalCoroutinesApi public abstract class AtomicOp : OpDescriptor() { private val _consensus = atomic(NO_DECISION) + // Returns NO_DECISION when there is not decision yet + val consensus: Any? get() = _consensus.value + val isDecided: Boolean get() = _consensus.value !== NO_DECISION /** diff --git a/kotlinx-coroutines-core/common/src/internal/ConcurrentLinkedList.kt b/kotlinx-coroutines-core/common/src/internal/ConcurrentLinkedList.kt new file mode 100644 index 0000000000..128a1998ef --- /dev/null +++ b/kotlinx-coroutines-core/common/src/internal/ConcurrentLinkedList.kt @@ -0,0 +1,240 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.internal + +import kotlinx.atomicfu.* +import kotlinx.coroutines.* +import kotlin.native.concurrent.SharedImmutable + +/** + * Returns the first segment `s` with `s.id >= id` or `CLOSED` + * if all the segments in this linked list have lower `id`, and the list is closed for further segment additions. + */ +private inline fun > S.findSegmentInternal( + id: Long, + createNewSegment: (id: Long, prev: S?) -> S +): SegmentOrClosed { + /* + Go through `next` references and add new segments if needed, similarly to the `push` in the Michael-Scott + queue algorithm. The only difference is that "CAS failure" means that the required segment has already been + added, so the algorithm just uses it. This way, only one segment with each id can be added. + */ + var cur: S = this + while (cur.id < id || cur.removed) { + val next = cur.nextOrIfClosed { return SegmentOrClosed(CLOSED) } + if (next != null) { // there is a next node -- move there + cur = next + continue + } + val newTail = createNewSegment(cur.id + 1, cur) + if (cur.trySetNext(newTail)) { // successfully added new node -- move there + if (cur.removed) cur.remove() + cur = newTail + } + } + return SegmentOrClosed(cur) +} + +/** + * Returns `false` if the segment `to` is logically removed, `true` on a successful update. + */ +@Suppress("NOTHING_TO_INLINE") // Must be inline because it is an AtomicRef extension +private inline fun > AtomicRef.moveForward(to: S): Boolean = loop { cur -> + if (cur.id >= to.id) return true + if (!to.tryIncPointers()) return false + if (compareAndSet(cur, to)) { // the segment is moved + if (cur.decPointers()) cur.remove() + return true + } + if (to.decPointers()) to.remove() // undo tryIncPointers +} + +/** + * Tries to find a segment with the specified [id] following by next references from the + * [startFrom] segment and creating new ones if needed. The typical use-case is reading this `AtomicRef` values, + * doing some synchronization, and invoking this function to find the required segment and update the pointer. + * At the same time, [Segment.cleanPrev] should also be invoked if the previous segments are no longer needed + * (e.g., queues should use it in dequeue operations). + * + * Since segments can be removed from the list, or it can be closed for further segment additions. + * Returns the segment `s` with `s.id >= id` or `CLOSED` if all the segments in this linked list have lower `id`, + * and the list is closed. + */ +internal inline fun > AtomicRef.findSegmentAndMoveForward( + id: Long, + startFrom: S, + createNewSegment: (id: Long, prev: S?) -> S +): SegmentOrClosed { + while (true) { + val s = startFrom.findSegmentInternal(id, createNewSegment) + if (s.isClosed || moveForward(s.segment)) return s + } +} + +/** + * Closes this linked list of nodes by forbidding adding new ones, + * returns the last node in the list. + */ +internal fun > N.close(): N { + var cur: N = this + while (true) { + val next = cur.nextOrIfClosed { return cur } + if (next === null) { + if (cur.markAsClosed()) return cur + } else { + cur = next + } + } +} + +internal abstract class ConcurrentLinkedListNode>(prev: N?) { + // Pointer to the next node, updates similarly to the Michael-Scott queue algorithm. + private val _next = atomic(null) + // Pointer to the previous node, updates in [remove] function. + private val _prev = atomic(prev) + + private val nextOrClosed get() = _next.value + + /** + * Returns the next segment or `null` of the one does not exist, + * and invokes [onClosedAction] if this segment is marked as closed. + */ + @Suppress("UNCHECKED_CAST") + inline fun nextOrIfClosed(onClosedAction: () -> Nothing): N? = nextOrClosed.let { + if (it === CLOSED) { + onClosedAction() + } else { + it as N? + } + } + + val next: N? get() = nextOrIfClosed { return null } + + /** + * Tries to set the next segment if it is not specified and this segment is not marked as closed. + */ + fun trySetNext(value: N): Boolean = _next.compareAndSet(null, value) + + /** + * Checks whether this node is the physical tail of the current linked list. + */ + val isTail: Boolean get() = next == null + + val prev: N? get() = _prev.value + + /** + * Cleans the pointer to the previous node. + */ + fun cleanPrev() { _prev.lazySet(null) } + + /** + * Tries to mark the linked list as closed by forbidding adding new nodes after this one. + */ + fun markAsClosed() = _next.compareAndSet(null, CLOSED) + + /** + * This property indicates whether the current node is logically removed. + * The expected use-case is removing the node logically (so that [removed] becomes true), + * and invoking [remove] after that. Note that this implementation relies on the contract + * that the physical tail cannot be logically removed. Please, do not break this contract; + * otherwise, memory leaks and unexpected behavior can occur. + */ + abstract val removed: Boolean + + /** + * Removes this node physically from this linked list. The node should be + * logically removed (so [removed] returns `true`) at the point of invocation. + */ + fun remove() { + assert { removed } // The node should be logically removed at first. + assert { !isTail } // The physical tail cannot be removed. + while (true) { + // Read `next` and `prev` pointers ignoring logically removed nodes. + val prev = leftmostAliveNode + val next = rightmostAliveNode + // Link `next` and `prev`. + next._prev.value = prev + if (prev !== null) prev._next.value = next + // Checks that prev and next are still alive. + if (next.removed) continue + if (prev !== null && prev.removed) continue + // This node is removed. + return + } + } + + private val leftmostAliveNode: N? get() { + var cur = prev + while (cur !== null && cur.removed) + cur = cur._prev.value + return cur + } + + private val rightmostAliveNode: N get() { + assert { !isTail } // Should not be invoked on the tail node + var cur = next!! + while (cur.removed) + cur = cur.next!! + return cur + } +} + +/** + * Each segment in the list has a unique id and is created by the provided to [findSegmentAndMoveForward] method. + * Essentially, this is a node in the Michael-Scott queue algorithm, + * but with maintaining [prev] pointer for efficient [remove] implementation. + */ +internal abstract class Segment>(val id: Long, prev: S?, pointers: Int): ConcurrentLinkedListNode(prev) { + /** + * This property should return the maximal number of slots in this segment, + * it is used to define whether the segment is logically removed. + */ + abstract val maxSlots: Int + + /** + * Numbers of cleaned slots (the lowest bits) and AtomicRef pointers to this segment (the highest bits) + */ + private val cleanedAndPointers = atomic(pointers shl POINTERS_SHIFT) + + /** + * The segment is considered as removed if all the slots are cleaned. + * There are no pointers to this segment from outside, and + * it is not a physical tail in the linked list of segments. + */ + override val removed get() = cleanedAndPointers.value == maxSlots && !isTail + + // increments the number of pointers if this segment is not logically removed. + internal fun tryIncPointers() = cleanedAndPointers.addConditionally(1 shl POINTERS_SHIFT) { it != maxSlots || isTail } + + // returns `true` if this segment is logically removed after the decrement. + internal fun decPointers() = cleanedAndPointers.addAndGet(-(1 shl POINTERS_SHIFT)) == maxSlots && !isTail + + /** + * Invoked on each slot clean-up; should not be invoked twice for the same slot. + */ + fun onSlotCleaned() { + if (cleanedAndPointers.incrementAndGet() == maxSlots && !isTail) remove() + } +} + +private inline fun AtomicInt.addConditionally(delta: Int, condition: (cur: Int) -> Boolean): Boolean { + while (true) { + val cur = this.value + if (!condition(cur)) return false + if (this.compareAndSet(cur, cur + delta)) return true + } +} + +@Suppress("EXPERIMENTAL_FEATURE_WARNING") // We are using inline class only internally, so it is Ok +internal inline class SegmentOrClosed>(private val value: Any?) { + val isClosed: Boolean get() = value === CLOSED + @Suppress("UNCHECKED_CAST") + val segment: S get() = if (value === CLOSED) error("Does not contain segment") else value as S +} + +private const val POINTERS_SHIFT = 16 + +@SharedImmutable +private val CLOSED = Symbol("CLOSED") \ No newline at end of file diff --git a/kotlinx-coroutines-core/common/src/internal/DispatchedContinuation.kt b/kotlinx-coroutines-core/common/src/internal/DispatchedContinuation.kt index 50758146af..b7b2954f6a 100644 --- a/kotlinx-coroutines-core/common/src/internal/DispatchedContinuation.kt +++ b/kotlinx-coroutines-core/common/src/internal/DispatchedContinuation.kt @@ -2,10 +2,10 @@ * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ -package kotlinx.coroutines +package kotlinx.coroutines.internal import kotlinx.atomicfu.* -import kotlinx.coroutines.internal.* +import kotlinx.coroutines.* import kotlin.coroutines.* import kotlin.jvm.* import kotlin.native.concurrent.* @@ -19,7 +19,7 @@ internal val REUSABLE_CLAIMED = Symbol("REUSABLE_CLAIMED") internal class DispatchedContinuation( @JvmField val dispatcher: CoroutineDispatcher, @JvmField val continuation: Continuation -) : DispatchedTask(MODE_ATOMIC_DEFAULT), CoroutineStackFrame, Continuation by continuation { +) : DispatchedTask(MODE_UNINITIALIZED), CoroutineStackFrame, Continuation by continuation { @JvmField @Suppress("PropertyName") internal var _state: Any? = UNDEFINED @@ -37,19 +37,19 @@ internal class DispatchedContinuation( * 3) [REUSABLE_CLAIMED]. CC is currently being reused and its owner executes `suspend` block: * ``` * // state == null | CC - * suspendAtomicCancellableCoroutineReusable { cont -> + * suspendCancellableCoroutineReusable { cont -> * // state == REUSABLE_CLAIMED * block(cont) * } * // state == CC * ``` - * 4) [Throwable] continuation was cancelled with this cause while being in [suspendAtomicCancellableCoroutineReusable], + * 4) [Throwable] continuation was cancelled with this cause while being in [suspendCancellableCoroutineReusable], * [CancellableContinuationImpl.getResult] will check for cancellation later. * * [REUSABLE_CLAIMED] state is required to prevent the lost resume in the channel. * AbstractChannel.receive method relies on the fact that the following pattern * ``` - * suspendAtomicCancellableCoroutineReusable { cont -> + * suspendCancellableCoroutineReusable { cont -> * val result = pollFastPath() * if (result != null) cont.resume(result) * } @@ -67,12 +67,12 @@ internal class DispatchedContinuation( /* * Reusability control: * `null` -> no reusability at all, false - * If current state is not CCI, then we are within `suspendAtomicCancellableCoroutineReusable`, true + * If current state is not CCI, then we are within `suspendCancellableCoroutineReusable`, true * Else, if result is CCI === requester. * Identity check my fail for the following pattern: * ``` * loop: - * suspendAtomicCancellableCoroutineReusable { } // Reusable, outer coroutine stores the child handle + * suspendCancellableCoroutineReusable { } // Reusable, outer coroutine stores the child handle * suspendCancellableCoroutine { } // **Not reusable**, handle should be disposed after {}, otherwise * it will leak because it won't be freed by `releaseInterceptedContinuation` * ``` @@ -83,7 +83,7 @@ internal class DispatchedContinuation( } /** - * Claims the continuation for [suspendAtomicCancellableCoroutineReusable] block, + * Claims the continuation for [suspendCancellableCoroutineReusable] block, * so all cancellations will be postponed. */ @Suppress("UNCHECKED_CAST") @@ -119,7 +119,7 @@ internal class DispatchedContinuation( * If continuation was cancelled, it becomes non-reusable. * * ``` - * suspendAtomicCancellableCoroutineReusable { // <- claimed + * suspendCancellableCoroutineReusable { // <- claimed * // Any asynchronous cancellation is "postponed" while this block * // is being executed * } // postponed cancellation is checked here in `getResult` @@ -180,10 +180,10 @@ internal class DispatchedContinuation( val state = result.toState() if (dispatcher.isDispatchNeeded(context)) { _state = state - resumeMode = MODE_ATOMIC_DEFAULT + resumeMode = MODE_ATOMIC dispatcher.dispatch(context, this) } else { - executeUnconfined(state, MODE_ATOMIC_DEFAULT) { + executeUnconfined(state, MODE_ATOMIC) { withCoroutineContext(this.context, countOrElement) { continuation.resumeWith(result) } @@ -194,29 +194,42 @@ internal class DispatchedContinuation( // We inline it to save an entry on the stack in cases where it shows (unconfined dispatcher) // It is used only in Continuation.resumeCancellableWith @Suppress("NOTHING_TO_INLINE") - inline fun resumeCancellableWith(result: Result) { - val state = result.toState() + inline fun resumeCancellableWith( + result: Result, + noinline onCancellation: ((cause: Throwable) -> Unit)? + ) { + val state = result.toState(onCancellation) if (dispatcher.isDispatchNeeded(context)) { _state = state resumeMode = MODE_CANCELLABLE dispatcher.dispatch(context, this) } else { executeUnconfined(state, MODE_CANCELLABLE) { - if (!resumeCancelled()) { + if (!resumeCancelled(state)) { resumeUndispatchedWith(result) } } } } + // takeState had already cleared the state so we cancel takenState here + override fun cancelCompletedResult(takenState: Any?, cause: Throwable) { + // It is Ok to call onCancellation here without try/catch around it, since this function only faces + // a "bound" cancellation handler that performs the safe call to the user-specified code. + if (takenState is CompletedWithCancellation) { + takenState.onCancellation(cause) + } + } + @Suppress("NOTHING_TO_INLINE") - inline fun resumeCancelled(): Boolean { + inline fun resumeCancelled(state: Any?): Boolean { val job = context[Job] if (job != null && !job.isActive) { - resumeWithException(job.getCancellationException()) + val cause = job.getCancellationException() + cancelCompletedResult(state, cause) + resumeWithException(cause) return true } - return false } @@ -245,8 +258,11 @@ internal class DispatchedContinuation( * @suppress **This an internal API and should not be used from general code.** */ @InternalCoroutinesApi -public fun Continuation.resumeCancellableWith(result: Result) = when (this) { - is DispatchedContinuation -> resumeCancellableWith(result) +public fun Continuation.resumeCancellableWith( + result: Result, + onCancellation: ((cause: Throwable) -> Unit)? = null +): Unit = when (this) { + is DispatchedContinuation -> resumeCancellableWith(result, onCancellation) else -> resumeWith(result) } @@ -265,6 +281,7 @@ private inline fun DispatchedContinuation<*>.executeUnconfined( contState: Any?, mode: Int, doYield: Boolean = false, block: () -> Unit ): Boolean { + assert { mode != MODE_UNINITIALIZED } // invalid execution mode val eventLoop = ThreadLocalEventLoop.eventLoop // If we are yielding and unconfined queue is empty, we can bail out as part of fast path if (doYield && eventLoop.isUnconfinedQueueEmpty) return false diff --git a/kotlinx-coroutines-core/common/src/internal/DispatchedTask.kt b/kotlinx-coroutines-core/common/src/internal/DispatchedTask.kt index 9588d22b17..1f4942a358 100644 --- a/kotlinx-coroutines-core/common/src/internal/DispatchedTask.kt +++ b/kotlinx-coroutines-core/common/src/internal/DispatchedTask.kt @@ -8,12 +8,44 @@ import kotlinx.coroutines.internal.* import kotlin.coroutines.* import kotlin.jvm.* -@PublishedApi internal const val MODE_ATOMIC_DEFAULT = 0 // schedule non-cancellable dispatch for suspendCoroutine -@PublishedApi internal const val MODE_CANCELLABLE = 1 // schedule cancellable dispatch for suspendCancellableCoroutine -@PublishedApi internal const val MODE_UNDISPATCHED = 2 // when the thread is right, but need to mark it with current coroutine +/** + * Non-cancellable dispatch mode. + * + * **DO NOT CHANGE THE CONSTANT VALUE**. It might be inlined into legacy user code that was calling + * inline `suspendAtomicCancellableCoroutine` function and did not support reuse. + */ +internal const val MODE_ATOMIC = 0 + +/** + * Cancellable dispatch mode. It is used by user-facing [suspendCancellableCoroutine]. + * Note, that implementation of cancellability checks mode via [Int.isCancellableMode] extension. + * + * **DO NOT CHANGE THE CONSTANT VALUE**. It is being into the user code from [suspendCancellableCoroutine]. + */ +@PublishedApi +internal const val MODE_CANCELLABLE = 1 + +/** + * Cancellable dispatch mode for [suspendCancellableCoroutineReusable]. + * Note, that implementation of cancellability checks mode via [Int.isCancellableMode] extension; + * implementation of reuse checks mode via [Int.isReusableMode] extension. + */ +internal const val MODE_CANCELLABLE_REUSABLE = 2 + +/** + * Undispatched mode for [CancellableContinuation.resumeUndispatched]. + * It is used when the thread is right, but it needs to be marked with the current coroutine. + */ +internal const val MODE_UNDISPATCHED = 4 -internal val Int.isCancellableMode get() = this == MODE_CANCELLABLE -internal val Int.isDispatchedMode get() = this == MODE_ATOMIC_DEFAULT || this == MODE_CANCELLABLE +/** + * Initial mode for [DispatchedContinuation] implementation, should never be used for dispatch, because it is always + * overwritten when continuation is resumed with the actual resume mode. + */ +internal const val MODE_UNINITIALIZED = -1 + +internal val Int.isCancellableMode get() = this == MODE_CANCELLABLE || this == MODE_CANCELLABLE_REUSABLE +internal val Int.isReusableMode get() = this == MODE_CANCELLABLE_REUSABLE internal abstract class DispatchedTask( @JvmField public var resumeMode: Int @@ -22,16 +54,32 @@ internal abstract class DispatchedTask( internal abstract fun takeState(): Any? - internal open fun cancelResult(state: Any?, cause: Throwable) {} + /** + * Called when this task was cancelled while it was being dispatched. + */ + internal open fun cancelCompletedResult(takenState: Any?, cause: Throwable) {} + /** + * There are two implementations of `DispatchedTask`: + * * [DispatchedContinuation] keeps only simple values as successfully results. + * * [CancellableContinuationImpl] keeps additional data with values and overrides this method to unwrap it. + */ @Suppress("UNCHECKED_CAST") internal open fun getSuccessfulResult(state: Any?): T = state as T - internal fun getExceptionalResult(state: Any?): Throwable? = + /** + * There are two implementations of `DispatchedTask`: + * * [DispatchedContinuation] is just an intermediate storage that stores the exception that has its stack-trace + * properly recovered and is ready to pass to the [delegate] continuation directly. + * * [CancellableContinuationImpl] stores raw cause of the failure in its state; when it needs to be dispatched + * its stack-trace has to be recovered, so it overrides this method for that purpose. + */ + internal open fun getExceptionalResult(state: Any?): Throwable? = (state as? CompletedExceptionally)?.cause public final override fun run() { + assert { resumeMode != MODE_UNINITIALIZED } // should have been set before dispatching val taskContext = this.taskContext var fatalException: Throwable? = null try { @@ -41,19 +89,22 @@ internal abstract class DispatchedTask( val state = takeState() // NOTE: Must take state in any case, even if cancelled withCoroutineContext(context, delegate.countOrElement) { val exception = getExceptionalResult(state) - val job = if (resumeMode.isCancellableMode) context[Job] else null /* * Check whether continuation was originally resumed with an exception. * If so, it dominates cancellation, otherwise the original exception * will be silently lost. */ - if (exception == null && job != null && !job.isActive) { + val job = if (exception == null && resumeMode.isCancellableMode) context[Job] else null + if (job != null && !job.isActive) { val cause = job.getCancellationException() - cancelResult(state, cause) + cancelCompletedResult(state, cause) continuation.resumeWithStackTrace(cause) } else { - if (exception != null) continuation.resumeWithException(exception) - else continuation.resume(getSuccessfulResult(state)) + if (exception != null) { + continuation.resumeWithException(exception) + } else { + continuation.resume(getSuccessfulResult(state)) + } } } } catch (e: Throwable) { @@ -97,8 +148,10 @@ internal abstract class DispatchedTask( } internal fun DispatchedTask.dispatch(mode: Int) { + assert { mode != MODE_UNINITIALIZED } // invalid mode value for this method val delegate = this.delegate - if (mode.isDispatchedMode && delegate is DispatchedContinuation<*> && mode.isCancellableMode == resumeMode.isCancellableMode) { + val undispatched = mode == MODE_UNDISPATCHED + if (!undispatched && delegate is DispatchedContinuation<*> && mode.isCancellableMode == resumeMode.isCancellableMode) { // dispatch directly using this instance's Runnable implementation val dispatcher = delegate.dispatcher val context = delegate.context @@ -108,21 +161,21 @@ internal fun DispatchedTask.dispatch(mode: Int) { resumeUnconfined() } } else { - resume(delegate, mode) + // delegate is coming from 3rd-party interceptor implementation (and does not support cancellation) + // or undispatched mode was requested + resume(delegate, undispatched) } } @Suppress("UNCHECKED_CAST") -internal fun DispatchedTask.resume(delegate: Continuation, useMode: Int) { - // slow-path - use delegate +internal fun DispatchedTask.resume(delegate: Continuation, undispatched: Boolean) { + // This resume is never cancellable. The result is always delivered to delegate continuation. val state = takeState() - val exception = getExceptionalResult(state)?.let { recoverStackTrace(it, delegate) } - val result = if (exception != null) Result.failure(exception) else Result.success(state as T) - when (useMode) { - MODE_ATOMIC_DEFAULT -> delegate.resumeWith(result) - MODE_CANCELLABLE -> delegate.resumeCancellableWith(result) - MODE_UNDISPATCHED -> (delegate as DispatchedContinuation).resumeUndispatchedWith(result) - else -> error("Invalid mode $useMode") + val exception = getExceptionalResult(state) + val result = if (exception != null) Result.failure(exception) else Result.success(getSuccessfulResult(state)) + when { + undispatched -> (delegate as DispatchedContinuation).resumeUndispatchedWith(result) + else -> delegate.resumeWith(result) } } @@ -134,7 +187,7 @@ private fun DispatchedTask<*>.resumeUnconfined() { } else { // Was not active -- run event loop until all unconfined tasks are executed runUnconfinedEventLoop(eventLoop) { - resume(delegate, MODE_UNDISPATCHED) + resume(delegate, undispatched = true) } } } diff --git a/kotlinx-coroutines-core/common/src/internal/LocalAtomics.common.kt b/kotlinx-coroutines-core/common/src/internal/LocalAtomics.common.kt new file mode 100644 index 0000000000..bcfb932de3 --- /dev/null +++ b/kotlinx-coroutines-core/common/src/internal/LocalAtomics.common.kt @@ -0,0 +1,21 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.internal + +/* + * These are atomics that are used as local variables + * where atomicfu doesn't support its tranformations. + * + * Have `Local` prefix to avoid AFU clashes during star-imports + */ +internal expect class LocalAtomicInt(value: Int) { + fun get(): Int + fun set(value: Int) + fun decrementAndGet(): Int +} + +internal inline var LocalAtomicInt.value + get() = get() + set(value) = set(value) diff --git a/kotlinx-coroutines-core/common/src/internal/LockFreeLinkedList.common.kt b/kotlinx-coroutines-core/common/src/internal/LockFreeLinkedList.common.kt index 216ce7b56b..8508e39239 100644 --- a/kotlinx-coroutines-core/common/src/internal/LockFreeLinkedList.common.kt +++ b/kotlinx-coroutines-core/common/src/internal/LockFreeLinkedList.common.kt @@ -1,6 +1,7 @@ /* * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ +@file:Suppress("NO_EXPLICIT_VISIBILITY_IN_API_MODE") package kotlinx.coroutines.internal @@ -72,6 +73,7 @@ public expect abstract class AbstractAtomicDesc : AtomicDesc { protected open fun retry(affected: LockFreeLinkedListNode, next: Any): Boolean public abstract fun finishPrepare(prepareOp: PrepareOp) // non-null on failure public open fun onPrepare(prepareOp: PrepareOp): Any? // non-null on failure + public open fun onRemoved(affected: LockFreeLinkedListNode) // non-null on failure protected abstract fun finishOnSuccess(affected: LockFreeLinkedListNode, next: LockFreeLinkedListNode) } diff --git a/kotlinx-coroutines-core/common/src/internal/MainDispatcherFactory.kt b/kotlinx-coroutines-core/common/src/internal/MainDispatcherFactory.kt index 93142df2cd..c3587af6e1 100644 --- a/kotlinx-coroutines-core/common/src/internal/MainDispatcherFactory.kt +++ b/kotlinx-coroutines-core/common/src/internal/MainDispatcherFactory.kt @@ -9,16 +9,16 @@ import kotlinx.coroutines.* /** @suppress */ @InternalCoroutinesApi // Emulating DI for Kotlin object's public interface MainDispatcherFactory { - val loadPriority: Int // higher priority wins + public val loadPriority: Int // higher priority wins /** * Creates the main dispatcher. [allFactories] parameter contains all factories found by service loader. * This method is not guaranteed to be idempotent. */ - fun createDispatcher(allFactories: List): MainCoroutineDispatcher + public fun createDispatcher(allFactories: List): MainCoroutineDispatcher /** * Hint used along with error message when the factory failed to create a dispatcher. */ - fun hintOnError(): String? = null + public fun hintOnError(): String? = null } diff --git a/kotlinx-coroutines-core/common/src/internal/OnUndeliveredElement.kt b/kotlinx-coroutines-core/common/src/internal/OnUndeliveredElement.kt new file mode 100644 index 0000000000..1744359e93 --- /dev/null +++ b/kotlinx-coroutines-core/common/src/internal/OnUndeliveredElement.kt @@ -0,0 +1,43 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.internal + +import kotlinx.coroutines.* +import kotlin.coroutines.* + +internal typealias OnUndeliveredElement = (E) -> Unit + +internal fun OnUndeliveredElement.callUndeliveredElementCatchingException( + element: E, + undeliveredElementException: UndeliveredElementException? = null +): UndeliveredElementException? { + try { + invoke(element) + } catch (ex: Throwable) { + // undeliveredElementException.cause !== ex is an optimization in case the same exception is thrown + // over and over again by on OnUndeliveredElement + if (undeliveredElementException != null && undeliveredElementException.cause !== ex) { + undeliveredElementException.addSuppressedThrowable(ex) + } else { + return UndeliveredElementException("Exception in undelivered element handler for $element", ex) + } + } + return undeliveredElementException +} + +internal fun OnUndeliveredElement.callUndeliveredElement(element: E, context: CoroutineContext) { + callUndeliveredElementCatchingException(element, null)?.let { ex -> + handleCoroutineException(context, ex) + } +} + +internal fun OnUndeliveredElement.bindCancellationFun(element: E, context: CoroutineContext): (Throwable) -> Unit = + { _: Throwable -> callUndeliveredElement(element, context) } + +/** + * Internal exception that is thrown when [OnUndeliveredElement] handler in + * a [kotlinx.coroutines.channels.Channel] throws an exception. + */ +internal class UndeliveredElementException(message: String, cause: Throwable) : RuntimeException(message, cause) diff --git a/kotlinx-coroutines-core/common/src/internal/SegmentQueue.kt b/kotlinx-coroutines-core/common/src/internal/SegmentQueue.kt deleted file mode 100644 index 0091d13671..0000000000 --- a/kotlinx-coroutines-core/common/src/internal/SegmentQueue.kt +++ /dev/null @@ -1,179 +0,0 @@ -/* - * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package kotlinx.coroutines.internal - -import kotlinx.atomicfu.* -import kotlinx.coroutines.* - -/** - * Essentially, this segment queue is an infinite array of segments, which is represented as - * a Michael-Scott queue of them. All segments are instances of [Segment] class and - * follow in natural order (see [Segment.id]) in the queue. - */ -internal abstract class SegmentQueue>() { - private val _head: AtomicRef - /** - * Returns the first segment in the queue. - */ - protected val head: S get() = _head.value - - private val _tail: AtomicRef - /** - * Returns the last segment in the queue. - */ - protected val tail: S get() = _tail.value - - init { - val initialSegment = newSegment(0) - _head = atomic(initialSegment) - _tail = atomic(initialSegment) - } - - /** - * The implementation should create an instance of segment [S] with the specified id - * and initial reference to the previous one. - */ - abstract fun newSegment(id: Long, prev: S? = null): S - - /** - * Finds a segment with the specified [id] following by next references from the - * [startFrom] segment. The typical use-case is reading [tail] or [head], doing some - * synchronization, and invoking [getSegment] or [getSegmentAndMoveHead] correspondingly - * to find the required segment. - */ - protected fun getSegment(startFrom: S, id: Long): S? { - // Go through `next` references and add new segments if needed, - // similarly to the `push` in the Michael-Scott queue algorithm. - // The only difference is that `CAS failure` means that the - // required segment has already been added, so the algorithm just - // uses it. This way, only one segment with each id can be in the queue. - var cur: S = startFrom - while (cur.id < id) { - var curNext = cur.next - if (curNext == null) { - // Add a new segment. - val newTail = newSegment(cur.id + 1, cur) - curNext = if (cur.casNext(null, newTail)) { - if (cur.removed) { - cur.remove() - } - moveTailForward(newTail) - newTail - } else { - cur.next!! - } - } - cur = curNext - } - if (cur.id != id) return null - return cur - } - - /** - * Invokes [getSegment] and replaces [head] with the result if its [id] is greater. - */ - protected fun getSegmentAndMoveHead(startFrom: S, id: Long): S? { - @Suppress("LeakingThis") - if (startFrom.id == id) return startFrom - val s = getSegment(startFrom, id) ?: return null - moveHeadForward(s) - return s - } - - /** - * Updates [head] to the specified segment - * if its `id` is greater. - */ - private fun moveHeadForward(new: S) { - _head.loop { curHead -> - if (curHead.id > new.id) return - if (_head.compareAndSet(curHead, new)) { - new.prev.value = null - return - } - } - } - - /** - * Updates [tail] to the specified segment - * if its `id` is greater. - */ - private fun moveTailForward(new: S) { - _tail.loop { curTail -> - if (curTail.id > new.id) return - if (_tail.compareAndSet(curTail, new)) return - } - } -} - -/** - * Each segment in [SegmentQueue] has a unique id and is created by [SegmentQueue.newSegment]. - * Essentially, this is a node in the Michael-Scott queue algorithm, but with - * maintaining [prev] pointer for efficient [remove] implementation. - */ -internal abstract class Segment>(val id: Long, prev: S?) { - // Pointer to the next segment, updates similarly to the Michael-Scott queue algorithm. - private val _next = atomic(null) - val next: S? get() = _next.value - fun casNext(expected: S?, value: S?): Boolean = _next.compareAndSet(expected, value) - // Pointer to the previous segment, updates in [remove] function. - val prev = atomic(null) - - /** - * Returns `true` if this segment is logically removed from the queue. - * The [remove] function should be called right after it becomes logically removed. - */ - abstract val removed: Boolean - - init { - this.prev.value = prev - } - - /** - * Removes this segment physically from the segment queue. The segment should be - * logically removed (so [removed] returns `true`) at the point of invocation. - */ - fun remove() { - assert { removed } // The segment should be logically removed at first - // Read `next` and `prev` pointers. - var next = this._next.value ?: return // tail cannot be removed - var prev = prev.value ?: return // head cannot be removed - // Link `next` and `prev`. - prev.moveNextToRight(next) - while (prev.removed) { - prev = prev.prev.value ?: break - prev.moveNextToRight(next) - } - next.movePrevToLeft(prev) - while (next.removed) { - next = next.next ?: break - next.movePrevToLeft(prev) - } - } - - /** - * Updates [next] pointer to the specified segment if - * the [id] of the specified segment is greater. - */ - private fun moveNextToRight(next: S) { - while (true) { - val curNext = this._next.value as S - if (next.id <= curNext.id) return - if (this._next.compareAndSet(curNext, next)) return - } - } - - /** - * Updates [prev] pointer to the specified segment if - * the [id] of the specified segment is lower. - */ - private fun movePrevToLeft(prev: S) { - while (true) { - val curPrev = this.prev.value ?: return - if (curPrev.id <= prev.id) return - if (this.prev.compareAndSet(curPrev, prev)) return - } - } -} diff --git a/kotlinx-coroutines-core/common/src/internal/ThreadSafeHeap.kt b/kotlinx-coroutines-core/common/src/internal/ThreadSafeHeap.kt index 12d6a38a81..df55c28f13 100644 --- a/kotlinx-coroutines-core/common/src/internal/ThreadSafeHeap.kt +++ b/kotlinx-coroutines-core/common/src/internal/ThreadSafeHeap.kt @@ -32,7 +32,7 @@ public open class ThreadSafeHeap : SynchronizedObject() where T: ThreadSafeHe public val isEmpty: Boolean get() = size == 0 - public fun clear() = synchronized(this) { + public fun clear(): Unit = synchronized(this) { a?.fill(null) _size.value = 0 } @@ -57,7 +57,7 @@ public open class ThreadSafeHeap : SynchronizedObject() where T: ThreadSafeHe } } - public fun addLast(node: T) = synchronized(this) { addImpl(node) } + public fun addLast(node: T): Unit = synchronized(this) { addImpl(node) } // @Synchronized // NOTE! NOTE! NOTE! inline fun cannot be @Synchronized // Condition also receives current first node in the heap diff --git a/kotlinx-coroutines-core/common/src/intrinsics/Cancellable.kt b/kotlinx-coroutines-core/common/src/intrinsics/Cancellable.kt index 0951349ebd..f814b152b2 100644 --- a/kotlinx-coroutines-core/common/src/intrinsics/Cancellable.kt +++ b/kotlinx-coroutines-core/common/src/intrinsics/Cancellable.kt @@ -5,6 +5,7 @@ package kotlinx.coroutines.intrinsics import kotlinx.coroutines.* +import kotlinx.coroutines.internal.* import kotlin.coroutines.* import kotlin.coroutines.intrinsics.* @@ -13,7 +14,7 @@ import kotlin.coroutines.intrinsics.* * while waiting to be dispatched. */ @InternalCoroutinesApi -public fun (suspend () -> T).startCoroutineCancellable(completion: Continuation) = runSafely(completion) { +public fun (suspend () -> T).startCoroutineCancellable(completion: Continuation): Unit = runSafely(completion) { createCoroutineUnintercepted(completion).intercepted().resumeCancellableWith(Result.success(Unit)) } @@ -21,9 +22,12 @@ public fun (suspend () -> T).startCoroutineCancellable(completion: Continuat * Use this function to start coroutine in a cancellable way, so that it can be cancelled * while waiting to be dispatched. */ -internal fun (suspend (R) -> T).startCoroutineCancellable(receiver: R, completion: Continuation) = +internal fun (suspend (R) -> T).startCoroutineCancellable( + receiver: R, completion: Continuation, + onCancellation: ((cause: Throwable) -> Unit)? = null +) = runSafely(completion) { - createCoroutineUnintercepted(receiver, completion).intercepted().resumeCancellableWith(Result.success(Unit)) + createCoroutineUnintercepted(receiver, completion).intercepted().resumeCancellableWith(Result.success(Unit), onCancellation) } /** diff --git a/kotlinx-coroutines-core/common/src/selects/Select.kt b/kotlinx-coroutines-core/common/src/selects/Select.kt index 1df5c62bf5..99c54f8417 100644 --- a/kotlinx-coroutines-core/common/src/selects/Select.kt +++ b/kotlinx-coroutines-core/common/src/selects/Select.kt @@ -1,6 +1,7 @@ /* * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ +@file:OptIn(ExperimentalContracts::class) package kotlinx.coroutines.selects @@ -10,6 +11,7 @@ import kotlinx.coroutines.channels.* import kotlinx.coroutines.internal.* import kotlinx.coroutines.intrinsics.* import kotlinx.coroutines.sync.* +import kotlin.contracts.* import kotlin.coroutines.* import kotlin.coroutines.intrinsics.* import kotlin.jvm.* @@ -39,7 +41,7 @@ public interface SelectBuilder { * Registers clause in this [select] expression with additional nullable parameter of type [P] * with the `null` value for this parameter that selects value of type [Q]. */ - public operator fun SelectClause2.invoke(block: suspend (Q) -> R) = invoke(null, block) + public operator fun SelectClause2.invoke(block: suspend (Q) -> R): Unit = invoke(null, block) /** * Clause that selects the given [block] after a specified timeout passes. @@ -61,7 +63,7 @@ public interface SelectBuilder { */ @ExperimentalCoroutinesApi @ExperimentalTime -public fun SelectBuilder.onTimeout(timeout: Duration, block: suspend () -> R) = +public fun SelectBuilder.onTimeout(timeout: Duration, block: suspend () -> R): Unit = onTimeout(timeout.toDelayMillis(), block) /** @@ -187,20 +189,17 @@ public interface SelectInstance { * * This suspending function is cancellable. If the [Job] of the current coroutine is cancelled or completed while this * function is suspended, this function immediately resumes with [CancellationException]. - * - * Atomicity of cancellation depends on the clause: [onSend][SendChannel.onSend], [onReceive][ReceiveChannel.onReceive], - * [onReceiveOrNull][ReceiveChannel.onReceiveOrNull], and [onLock][Mutex.onLock] clauses are - * *atomically cancellable*. When select throws [CancellationException] it means that those clauses had not performed - * their respective operations. - * As a side-effect of atomic cancellation, a thread-bound coroutine (to some UI thread, for example) may - * continue to execute even after it was cancelled from the same thread in the case when this select operation - * was already resumed on atomically cancellable clause and the continuation was posted for execution to the thread's queue. + * There is a **prompt cancellation guarantee**. If the job was cancelled while this function was + * suspended, it will not resume successfully. See [suspendCancellableCoroutine] documentation for low-level details. * * Note that this function does not check for cancellation when it is not suspended. * Use [yield] or [CoroutineScope.isActive] to periodically check for cancellation in tight loops if needed. */ -public suspend inline fun select(crossinline builder: SelectBuilder.() -> Unit): R = - suspendCoroutineUninterceptedOrReturn { uCont -> +public suspend inline fun select(crossinline builder: SelectBuilder.() -> Unit): R { + contract { + callsInPlace(builder, InvocationKind.EXACTLY_ONCE) + } + return suspendCoroutineUninterceptedOrReturn { uCont -> val scope = SelectBuilderImpl(uCont) try { builder(scope) @@ -209,8 +208,11 @@ public suspend inline fun select(crossinline builder: SelectBuilder.() -> } scope.getResult() } +} +@SharedImmutable +internal val NOT_SELECTED: Any = Symbol("NOT_SELECTED") @SharedImmutable internal val ALREADY_SELECTED: Any = Symbol("ALREADY_SELECTED") @SharedImmutable @@ -240,8 +242,8 @@ internal class SelectBuilderImpl( override fun getStackTraceElement(): StackTraceElement? = null - // selection state is "this" (list of nodes) initially and is replaced by idempotent marker (or null) when selected - private val _state = atomic(this) + // selection state is NOT_SELECTED initially and is replaced by idempotent marker (or null) when selected + private val _state = atomic(NOT_SELECTED) // this is basically our own SafeContinuation private val _result = atomic(UNDECIDED) @@ -360,7 +362,7 @@ internal class SelectBuilderImpl( override val isSelected: Boolean get() = _state.loop { state -> when { - state === this -> return false + state === NOT_SELECTED -> return false state is OpDescriptor -> state.perform(this) // help else -> return true // already selected } @@ -483,14 +485,14 @@ internal class SelectBuilderImpl( _state.loop { state -> // lock-free loop on state when { // Found initial state (not selected yet) -- try to make it selected - state === this -> { + state === NOT_SELECTED -> { if (otherOp == null) { // regular trySelect -- just mark as select - if (!_state.compareAndSet(this, null)) return@loop + if (!_state.compareAndSet(NOT_SELECTED, null)) return@loop } else { // Rendezvous with another select instance -- install PairSelectOp val pairSelectOp = PairSelectOp(otherOp) - if (!_state.compareAndSet(this, pairSelectOp)) return@loop + if (!_state.compareAndSet(NOT_SELECTED, pairSelectOp)) return@loop val decision = pairSelectOp.perform(this) if (decision !== null) return decision } @@ -546,7 +548,7 @@ internal class SelectBuilderImpl( // we must finish preparation of another operation before attempting to reach decision to select otherOp.finishPrepare() val decision = otherOp.atomicOp.decide(null) // try decide for success of operation - val update: Any = if (decision == null) otherOp.desc else impl + val update: Any = if (decision == null) otherOp.desc else NOT_SELECTED impl._state.compareAndSet(this, update) return decision } @@ -558,10 +560,7 @@ internal class SelectBuilderImpl( override fun performAtomicTrySelect(desc: AtomicDesc): Any? = AtomicSelectOp(this, desc).perform(null) - override fun toString(): String { - val state = _state.value - return "SelectInstance(state=${if (state === this) "this" else state.toString()}, result=${_result.value})" - } + override fun toString(): String = "SelectInstance(state=${_state.value}, result=${_result.value})" private class AtomicSelectOp( @JvmField val impl: SelectBuilderImpl<*>, @@ -600,8 +599,8 @@ internal class SelectBuilderImpl( when { state === this -> return null // already in progress state is OpDescriptor -> state.perform(impl) // help - state === impl -> { - if (impl._state.compareAndSet(impl, this)) + state === NOT_SELECTED -> { + if (impl._state.compareAndSet(NOT_SELECTED, this)) return null // success } else -> return ALREADY_SELECTED @@ -611,12 +610,12 @@ internal class SelectBuilderImpl( // reverts the change done by prepareSelectedOp private fun undoPrepare() { - impl._state.compareAndSet(this, impl) + impl._state.compareAndSet(this, NOT_SELECTED) } private fun completeSelect(failure: Any?) { val selectSuccess = failure == null - val update = if (selectSuccess) null else impl + val update = if (selectSuccess) null else NOT_SELECTED if (impl._state.compareAndSet(this, update)) { if (selectSuccess) impl.doAfterSelect() @@ -650,7 +649,7 @@ internal class SelectBuilderImpl( if (trySelect()) block.startCoroutineCancellable(completion) // shall be cancellable while waits for dispatch } - disposeOnSelect(context.delay.invokeOnTimeout(timeMillis, action)) + disposeOnSelect(context.delay.invokeOnTimeout(timeMillis, action, context)) } private class DisposeNode( diff --git a/kotlinx-coroutines-core/common/src/sync/Mutex.kt b/kotlinx-coroutines-core/common/src/sync/Mutex.kt index 1b11bc96cc..73aaab5fbf 100644 --- a/kotlinx-coroutines-core/common/src/sync/Mutex.kt +++ b/kotlinx-coroutines-core/common/src/sync/Mutex.kt @@ -9,6 +9,7 @@ import kotlinx.coroutines.* import kotlinx.coroutines.internal.* import kotlinx.coroutines.intrinsics.* import kotlinx.coroutines.selects.* +import kotlin.contracts.* import kotlin.coroutines.* import kotlin.jvm.* import kotlin.native.concurrent.* @@ -44,12 +45,10 @@ public interface Mutex { * * This suspending function is cancellable. If the [Job] of the current coroutine is cancelled or completed while this * function is suspended, this function immediately resumes with [CancellationException]. - * - * *Cancellation of suspended lock invocation is atomic* -- when this function - * throws [CancellationException] it means that the mutex was not locked. - * As a side-effect of atomic cancellation, a thread-bound coroutine (to some UI thread, for example) may - * continue to execute even after it was cancelled from the same thread in the case when this lock operation - * was already resumed and the continuation was posted for execution to the thread's queue. + * There is a **prompt cancellation guarantee**. If the job was cancelled while this function was + * suspended, it will not resume successfully. See [suspendCancellableCoroutine] documentation for low-level details. + * This function releases the lock if it was already acquired by this function before the [CancellationException] + * was thrown. * * Note that this function does not check for cancellation when it is not suspended. * Use [yield] or [CoroutineScope.isActive] to periodically check for cancellation in tight loops if needed. @@ -106,7 +105,12 @@ public fun Mutex(locked: Boolean = false): Mutex = * * @return the return value of the action. */ +@OptIn(ExperimentalContracts::class) public suspend inline fun Mutex.withLock(owner: Any? = null, action: () -> T): T { + contract { + callsInPlace(action, InvocationKind.EXACTLY_ONCE) + } + lock(owner) try { return action() @@ -118,8 +122,6 @@ public suspend inline fun Mutex.withLock(owner: Any? = null, action: () -> T @SharedImmutable private val LOCK_FAIL = Symbol("LOCK_FAIL") @SharedImmutable -private val ENQUEUE_FAIL = Symbol("ENQUEUE_FAIL") -@SharedImmutable private val UNLOCK_FAIL = Symbol("UNLOCK_FAIL") @SharedImmutable private val SELECT_SUCCESS = Symbol("SELECT_SUCCESS") @@ -188,7 +190,7 @@ internal class MutexImpl(locked: Boolean) : Mutex, SelectClause2 { return lockSuspend(owner) } - private suspend fun lockSuspend(owner: Any?) = suspendAtomicCancellableCoroutineReusable sc@ { cont -> + private suspend fun lockSuspend(owner: Any?) = suspendCancellableCoroutineReusable sc@ { cont -> val waiter = LockCont(owner, cont) _state.loop { state -> when (state) { @@ -248,7 +250,7 @@ internal class MutexImpl(locked: Boolean) : Mutex, SelectClause2 { } is LockedQueue -> { check(state.owner !== owner) { "Already locked by $owner" } - val node = LockSelect(owner, this, select, block) + val node = LockSelect(owner, select, block) if (state.addLastIf(node) { _state.value === state }) { // successfully enqueued select.disposeOnSelect(node) @@ -346,7 +348,7 @@ internal class MutexImpl(locked: Boolean) : Mutex, SelectClause2 { override fun toString(): String = "LockedQueue[$owner]" } - private abstract class LockWaiter( + private abstract inner class LockWaiter( @JvmField val owner: Any? ) : LockFreeLinkedListNode(), DisposableHandle { final override fun dispose() { remove() } @@ -354,52 +356,44 @@ internal class MutexImpl(locked: Boolean) : Mutex, SelectClause2 { abstract fun completeResumeLockWaiter(token: Any) } - private class LockCont( + private inner class LockCont( owner: Any?, @JvmField val cont: CancellableContinuation ) : LockWaiter(owner) { - override fun tryResumeLockWaiter() = cont.tryResume(Unit) + override fun tryResumeLockWaiter() = cont.tryResume(Unit, idempotent = null) { + // if this continuation gets cancelled during dispatch to the caller, then release the lock + unlock(owner) + } override fun completeResumeLockWaiter(token: Any) = cont.completeResume(token) - override fun toString(): String = "LockCont[$owner, $cont]" + override fun toString(): String = "LockCont[$owner, $cont] for ${this@MutexImpl}" } - private class LockSelect( + private inner class LockSelect( owner: Any?, - @JvmField val mutex: Mutex, @JvmField val select: SelectInstance, @JvmField val block: suspend (Mutex) -> R ) : LockWaiter(owner) { override fun tryResumeLockWaiter(): Any? = if (select.trySelect()) SELECT_SUCCESS else null override fun completeResumeLockWaiter(token: Any) { assert { token === SELECT_SUCCESS } - block.startCoroutine(receiver = mutex, completion = select.completion) + block.startCoroutineCancellable(receiver = this@MutexImpl, completion = select.completion) { + // if this continuation gets cancelled during dispatch to the caller, then release the lock + unlock(owner) + } } - override fun toString(): String = "LockSelect[$owner, $mutex, $select]" + override fun toString(): String = "LockSelect[$owner, $select] for ${this@MutexImpl}" } // atomic unlock operation that checks that waiters queue is empty private class UnlockOp( @JvmField val queue: LockedQueue - ) : OpDescriptor() { - override val atomicOp: AtomicOp<*>? get() = null - - override fun perform(affected: Any?): Any? { - /* - Note: queue cannot change while this UnlockOp is in progress, so all concurrent attempts to - make a decision will reach it consistently. It does not matter what is a proposed - decision when this UnlockOp is no longer active, because in this case the following CAS - will fail anyway. - */ - val success = queue.isEmpty - val update: Any = if (success) EMPTY_UNLOCKED else queue - (affected as MutexImpl)._state.compareAndSet(this@UnlockOp, update) - /* - `perform` invocation from the original `unlock` invocation may be coming too late, when - some other thread had already helped to complete it (either successfully or not). - That operation was unsuccessful if `state` was restored to this `queue` reference and - that is what is being checked below. - */ - return if (affected._state.value === queue) UNLOCK_FAIL else null + ) : AtomicOp() { + override fun prepare(affected: MutexImpl): Any? = + if (queue.isEmpty) null else UNLOCK_FAIL + + override fun complete(affected: MutexImpl, failure: Any?) { + val update: Any = if (failure == null) EMPTY_UNLOCKED else queue + affected._state.compareAndSet(this, update) } } } diff --git a/kotlinx-coroutines-core/common/src/sync/Semaphore.kt b/kotlinx-coroutines-core/common/src/sync/Semaphore.kt index aa7ed63d3d..84b7f4f8a2 100644 --- a/kotlinx-coroutines-core/common/src/sync/Semaphore.kt +++ b/kotlinx-coroutines-core/common/src/sync/Semaphore.kt @@ -7,10 +7,10 @@ package kotlinx.coroutines.sync import kotlinx.atomicfu.* import kotlinx.coroutines.* import kotlinx.coroutines.internal.* +import kotlin.contracts.* import kotlin.coroutines.* -import kotlin.jvm.* import kotlin.math.* -import kotlin.native.concurrent.* +import kotlin.native.concurrent.SharedImmutable /** * A counting semaphore for coroutines that logically maintains a number of available permits. @@ -33,9 +33,10 @@ public interface Semaphore { * * This suspending function is cancellable. If the [Job] of the current coroutine is cancelled or completed while this * function is suspended, this function immediately resumes with [CancellationException]. - * - * *Cancellation of suspended semaphore acquisition is atomic* -- when this function - * throws [CancellationException] it means that the semaphore was not acquired. + * There is a **prompt cancellation guarantee**. If the job was cancelled while this function was + * suspended, it will not resume successfully. See [suspendCancellableCoroutine] documentation for low-level details. + * This function releases the semaphore if it was already acquired by this function before the [CancellationException] + * was thrown. * * Note, that this function does not check for cancellation when it does not suspend. * Use [CoroutineScope.isActive] or [CoroutineScope.ensureActive] to periodically @@ -75,7 +76,12 @@ public fun Semaphore(permits: Int, acquiredPermits: Int = 0): Semaphore = Semaph * * @return the return value of the [action]. */ +@OptIn(ExperimentalContracts::class) public suspend inline fun Semaphore.withPermit(action: () -> T): T { + contract { + callsInPlace(action, InvocationKind.EXACTLY_ONCE) + } + acquire() try { return action() @@ -84,16 +90,55 @@ public suspend inline fun Semaphore.withPermit(action: () -> T): T { } } -private class SemaphoreImpl( - private val permits: Int, acquiredPermits: Int -) : Semaphore, SegmentQueue() { +private class SemaphoreImpl(private val permits: Int, acquiredPermits: Int) : Semaphore { + /* + The queue of waiting acquirers is essentially an infinite array based on the list of segments + (see `SemaphoreSegment`); each segment contains a fixed number of slots. To determine a slot for each enqueue + and dequeue operation, we increment the corresponding counter at the beginning of the operation + and use the value before the increment as a slot number. This way, each enqueue-dequeue pair + works with an individual cell. We use the corresponding segment pointers to find the required ones. + + Here is a state machine for cells. Note that only one `acquire` and at most one `release` operation + can deal with each cell, and that `release` uses `getAndSet(PERMIT)` to perform transitions for performance reasons + so that the state `PERMIT` represents different logical states. + + +------+ `acquire` suspends +------+ `release` tries +--------+ // if `cont.tryResume(..)` succeeds, then + | NULL | -------------------> | cont | -------------------> | PERMIT | (cont RETRIEVED) // the corresponding `acquire` operation gets + +------+ +------+ to resume `cont` +--------+ // a permit and the `release` one completes. + | | + | | `acquire` request is cancelled and the continuation is + | `release` comes | replaced with a special `CANCEL` token to avoid memory leaks + | to the slot before V + | `acquire` and puts +-----------+ `release` has +--------+ + | a permit into the | CANCELLED | -----------------> | PERMIT | (RElEASE FAILED) + | slot, waiting for +-----------+ failed +--------+ + | `acquire` after + | that. + | + | `acquire` gets +-------+ + | +-----------------> | TAKEN | (ELIMINATION HAPPENED) + V | the permit +-------+ + +--------+ | + | PERMIT | -< + +--------+ | + | `release` has waited a bounded time, +--------+ + +---------------------------------------> | BROKEN | (BOTH RELEASE AND ACQUIRE FAILED) + but `acquire` has not come +--------+ + */ + + private val head: AtomicRef + private val deqIdx = atomic(0L) + private val tail: AtomicRef + private val enqIdx = atomic(0L) + init { require(permits > 0) { "Semaphore should have at least 1 permit, but had $permits" } require(acquiredPermits in 0..permits) { "The number of acquired permits should be in 0..$permits" } + val s = SemaphoreSegment(0, null, 2) + head = atomic(s) + tail = atomic(s) } - override fun newSegment(id: Long, prev: SemaphoreSegment?) = SemaphoreSegment(id, prev) - /** * This counter indicates a number of available permits if it is non-negative, * or the size with minus sign otherwise. Note, that 32-bit counter is enough here @@ -104,13 +149,7 @@ private class SemaphoreImpl( private val _availablePermits = atomic(permits - acquiredPermits) override val availablePermits: Int get() = max(_availablePermits.value, 0) - // The queue of waiting acquirers is essentially an infinite array based on `SegmentQueue`; - // each segment contains a fixed number of slots. To determine a slot for each enqueue - // and dequeue operation, we increment the corresponding counter at the beginning of the operation - // and use the value before the increment as a slot number. This way, each enqueue-dequeue pair - // works with an individual cell. - private val enqIdx = atomic(0L) - private val deqIdx = atomic(0L) + private val onCancellationRelease = { _: Throwable -> release() } override fun tryAcquire(): Boolean { _availablePermits.loop { p -> @@ -122,72 +161,118 @@ private class SemaphoreImpl( override suspend fun acquire() { val p = _availablePermits.getAndDecrement() if (p > 0) return // permit acquired - addToQueueAndSuspend() + // While it looks better when the following function is inlined, + // it is important to make `suspend` function invocations in a way + // so that the tail-call optimization can be applied. + acquireSlowPath() } - override fun release() { - val p = incPermits() - if (p >= 0) return // no waiters - resumeNextFromQueue() + private suspend fun acquireSlowPath() = suspendCancellableCoroutineReusable sc@ { cont -> + while (true) { + if (addAcquireToQueue(cont)) return@sc + val p = _availablePermits.getAndDecrement() + if (p > 0) { // permit acquired + cont.resume(Unit) + return@sc + } + } } - fun incPermits() = _availablePermits.getAndUpdate { cur -> - check(cur < permits) { "The number of released permits cannot be greater than $permits" } - cur + 1 + override fun release() { + while (true) { + val p = _availablePermits.getAndUpdate { cur -> + check(cur < permits) { "The number of released permits cannot be greater than $permits" } + cur + 1 + } + if (p >= 0) return + if (tryResumeNextFromQueue()) return + } } - private suspend fun addToQueueAndSuspend() = suspendAtomicCancellableCoroutineReusable sc@ { cont -> - val last = this.tail + /** + * Returns `false` if the received permit cannot be used and the calling operation should restart. + */ + private fun addAcquireToQueue(cont: CancellableContinuation): Boolean { + val curTail = this.tail.value val enqIdx = enqIdx.getAndIncrement() - val segment = getSegment(last, enqIdx / SEGMENT_SIZE) + val segment = this.tail.findSegmentAndMoveForward(id = enqIdx / SEGMENT_SIZE, startFrom = curTail, + createNewSegment = ::createSegment).segment // cannot be closed val i = (enqIdx % SEGMENT_SIZE).toInt() - if (segment === null || segment.get(i) === RESUMED || !segment.cas(i, null, cont)) { - // already resumed + // the regular (fast) path -- if the cell is empty, try to install continuation + if (segment.cas(i, null, cont)) { // installed continuation successfully + cont.invokeOnCancellation(CancelSemaphoreAcquisitionHandler(segment, i).asHandler) + return true + } + // On CAS failure -- the cell must be either PERMIT or BROKEN + // If the cell already has PERMIT from tryResumeNextFromQueue, try to grab it + if (segment.cas(i, PERMIT, TAKEN)) { // took permit thus eliminating acquire/release pair + // The following resume must always succeed, since continuation was not published yet and we don't have + // to pass onCancellationRelease handle, since the coroutine did not suspend yet and cannot be cancelled cont.resume(Unit) - return@sc + return true } - cont.invokeOnCancellation(CancelSemaphoreAcquisitionHandler(this, segment, i).asHandler) + assert { segment.get(i) === BROKEN } // it must be broken in this case, no other way around it + return false // broken cell, need to retry on a different cell } @Suppress("UNCHECKED_CAST") - internal fun resumeNextFromQueue() { - try_again@while (true) { - val first = this.head - val deqIdx = deqIdx.getAndIncrement() - val segment = getSegmentAndMoveHead(first, deqIdx / SEGMENT_SIZE) ?: continue@try_again - val i = (deqIdx % SEGMENT_SIZE).toInt() - val cont = segment.getAndSet(i, RESUMED) - if (cont === null) return // just resumed - if (cont === CANCELLED) continue@try_again - (cont as CancellableContinuation).resume(Unit) - return + private fun tryResumeNextFromQueue(): Boolean { + val curHead = this.head.value + val deqIdx = deqIdx.getAndIncrement() + val id = deqIdx / SEGMENT_SIZE + val segment = this.head.findSegmentAndMoveForward(id, startFrom = curHead, + createNewSegment = ::createSegment).segment // cannot be closed + segment.cleanPrev() + if (segment.id > id) return false + val i = (deqIdx % SEGMENT_SIZE).toInt() + val cellState = segment.getAndSet(i, PERMIT) // set PERMIT and retrieve the prev cell state + when { + cellState === null -> { + // Acquire has not touched this cell yet, wait until it comes for a bounded time + // The cell state can only transition from PERMIT to TAKEN by addAcquireToQueue + repeat(MAX_SPIN_CYCLES) { + if (segment.get(i) === TAKEN) return true + } + // Try to break the slot in order not to wait + return !segment.cas(i, PERMIT, BROKEN) + } + cellState === CANCELLED -> return false // the acquire was already cancelled + else -> return (cellState as CancellableContinuation).tryResumeAcquire() } } + + private fun CancellableContinuation.tryResumeAcquire(): Boolean { + val token = tryResume(Unit, null, onCancellationRelease) ?: return false + completeResume(token) + return true + } } private class CancelSemaphoreAcquisitionHandler( - private val semaphore: SemaphoreImpl, private val segment: SemaphoreSegment, private val index: Int ) : CancelHandler() { override fun invoke(cause: Throwable?) { - val p = semaphore.incPermits() - if (p >= 0) return - if (segment.cancel(index)) return - semaphore.resumeNextFromQueue() + segment.cancel(index) } - override fun toString() = "CancelSemaphoreAcquisitionHandler[$semaphore, $segment, $index]" + override fun toString() = "CancelSemaphoreAcquisitionHandler[$segment, $index]" } -private class SemaphoreSegment(id: Long, prev: SemaphoreSegment?): Segment(id, prev) { +private fun createSegment(id: Long, prev: SemaphoreSegment?) = SemaphoreSegment(id, prev, 0) + +private class SemaphoreSegment(id: Long, prev: SemaphoreSegment?, pointers: Int) : Segment(id, prev, pointers) { val acquirers = atomicArrayOfNulls(SEGMENT_SIZE) - private val cancelledSlots = atomic(0) - override val removed get() = cancelledSlots.value == SEGMENT_SIZE + override val maxSlots: Int get() = SEGMENT_SIZE @Suppress("NOTHING_TO_INLINE") inline fun get(index: Int): Any? = acquirers[index].value + @Suppress("NOTHING_TO_INLINE") + inline fun set(index: Int, value: Any?) { + acquirers[index].value = value + } + @Suppress("NOTHING_TO_INLINE") inline fun cas(index: Int, expected: Any?, value: Any?): Boolean = acquirers[index].compareAndSet(expected, value) @@ -196,20 +281,23 @@ private class SemaphoreSegment(id: Long, prev: SemaphoreSegment?): Segment = async { + expect(2) + awaitCancellation() + } + yield() + expect(3) + require(deferred.isActive) + deferred.cancel() + finish(4) + deferred.await() + } + } +} diff --git a/kotlinx-coroutines-core/common/test/BuilderContractsTest.kt b/kotlinx-coroutines-core/common/test/BuilderContractsTest.kt new file mode 100644 index 0000000000..b20dd6b1d2 --- /dev/null +++ b/kotlinx-coroutines-core/common/test/BuilderContractsTest.kt @@ -0,0 +1,52 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines + +import kotlinx.coroutines.selects.* +import kotlin.test.* + +class BuilderContractsTest : TestBase() { + + @Test + fun testContracts() = runTest { + // Coroutine scope + val cs: Int + coroutineScope { + cs = 42 + } + consume(cs) + + // Supervisor scope + val svs: Int + supervisorScope { + svs = 21 + } + consume(svs) + + // with context scope + val wctx: Int + withContext(Dispatchers.Unconfined) { + wctx = 239 + } + consume(wctx) + + val wt: Int + withTimeout(Long.MAX_VALUE) { + wt = 123 + } + consume(wt) + + val s: Int + select { + s = 42 + Job().apply { complete() }.onJoin {} + } + consume(s) + } + + private fun consume(a: Int) { + a.hashCode() // BE codegen verification + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/common/test/CancellableContinuationHandlersTest.kt b/kotlinx-coroutines-core/common/test/CancellableContinuationHandlersTest.kt index 00f719e632..3c11182e00 100644 --- a/kotlinx-coroutines-core/common/test/CancellableContinuationHandlersTest.kt +++ b/kotlinx-coroutines-core/common/test/CancellableContinuationHandlersTest.kt @@ -23,10 +23,23 @@ class CancellableContinuationHandlersTest : TestBase() { fun testDoubleSubscriptionAfterCompletion() = runTest { suspendCancellableCoroutine { c -> c.resume(Unit) - // Nothing happened - c.invokeOnCancellation { expectUnreached() } - // Cannot validate after completion + // First invokeOnCancellation is Ok c.invokeOnCancellation { expectUnreached() } + // Second invokeOnCancellation is not allowed + assertFailsWith { c.invokeOnCancellation { expectUnreached() } } + } + } + + @Test + fun testDoubleSubscriptionAfterCompletionWithException() = runTest { + assertFailsWith { + suspendCancellableCoroutine { c -> + c.resumeWithException(TestException()) + // First invokeOnCancellation is Ok + c.invokeOnCancellation { expectUnreached() } + // Second invokeOnCancellation is not allowed + assertFailsWith { c.invokeOnCancellation { expectUnreached() } } + } } } @@ -46,6 +59,59 @@ class CancellableContinuationHandlersTest : TestBase() { } } + @Test + fun testSecondSubscriptionAfterCancellation() = runTest { + try { + suspendCancellableCoroutine { c -> + // Set IOC first + c.invokeOnCancellation { + assertNull(it) + expect(2) + } + expect(1) + // then cancel (it gets called) + c.cancel() + // then try to install another one + assertFailsWith { c.invokeOnCancellation { expectUnreached() } } + } + } catch (e: CancellationException) { + finish(3) + } + } + + @Test + fun testSecondSubscriptionAfterResumeCancelAndDispatch() = runTest { + var cont: CancellableContinuation? = null + val job = launch(start = CoroutineStart.UNDISPATCHED) { + // will be cancelled during dispatch + assertFailsWith { + suspendCancellableCoroutine { c -> + cont = c + // Set IOC first -- not called (completed) + c.invokeOnCancellation { + assertTrue(it is CancellationException) + expect(4) + } + expect(1) + } + } + expect(5) + } + expect(2) + // then resume it + cont!!.resume(Unit) // schedule cancelled continuation for dispatch + // then cancel the job during dispatch + job.cancel() + expect(3) + yield() // finish dispatching (will call IOC handler here!) + expect(6) + // then try to install another one after we've done dispatching it + assertFailsWith { + cont!!.invokeOnCancellation { expectUnreached() } + } + finish(7) + } + @Test fun testDoubleSubscriptionAfterCancellationWithCause() = runTest { try { diff --git a/kotlinx-coroutines-core/common/test/CancellableContinuationTest.kt b/kotlinx-coroutines-core/common/test/CancellableContinuationTest.kt index 38fc9ff281..f9f610ceb5 100644 --- a/kotlinx-coroutines-core/common/test/CancellableContinuationTest.kt +++ b/kotlinx-coroutines-core/common/test/CancellableContinuationTest.kt @@ -116,4 +116,26 @@ class CancellableContinuationTest : TestBase() { continuation!!.resume(Unit) // Should not fail finish(4) } + + @Test + fun testCompleteJobWhileSuspended() = runTest { + expect(1) + val completableJob = Job() + val coroutineBlock = suspend { + assertFailsWith { + suspendCancellableCoroutine { cont -> + expect(2) + assertSame(completableJob, cont.context[Job]) + completableJob.complete() + } + expectUnreached() + } + expect(3) + } + coroutineBlock.startCoroutine(Continuation(completableJob) { + assertEquals(Unit, it.getOrNull()) + expect(4) + }) + finish(5) + } } \ No newline at end of file diff --git a/kotlinx-coroutines-core/common/test/CancellableResumeTest.kt b/kotlinx-coroutines-core/common/test/CancellableResumeTest.kt index b2cde6b978..fbfa082555 100644 --- a/kotlinx-coroutines-core/common/test/CancellableResumeTest.kt +++ b/kotlinx-coroutines-core/common/test/CancellableResumeTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ @file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED") // KT-21913 @@ -30,7 +30,7 @@ class CancellableResumeTest : TestBase() { expected = { it is TestException } ) { expect(1) - val ok = suspendCancellableCoroutine { cont -> + suspendCancellableCoroutine { cont -> expect(2) cont.invokeOnCancellation { expect(3) } cont.cancel(TestException("FAIL")) @@ -44,6 +44,79 @@ class CancellableResumeTest : TestBase() { expectUnreached() } + @Test + fun testResumeImmediateAfterCancelWithHandlerFailure() = runTest( + expected = { it is TestException }, + unhandled = listOf( + { it is CompletionHandlerException && it.cause is TestException2 }, + { it is CompletionHandlerException && it.cause is TestException3 } + ) + ) { + expect(1) + suspendCancellableCoroutine { cont -> + expect(2) + cont.invokeOnCancellation { + expect(3) + throw TestException2("FAIL") // invokeOnCancellation handler fails with exception + } + cont.cancel(TestException("FAIL")) + expect(4) + cont.resume("OK") { cause -> + expect(5) + assertTrue(cause is TestException) + throw TestException3("FAIL") // onCancellation block fails with exception + } + finish(6) + } + expectUnreached() + } + + @Test + fun testResumeImmediateAfterIndirectCancel() = runTest( + expected = { it is CancellationException } + ) { + expect(1) + val ctx = coroutineContext + suspendCancellableCoroutine { cont -> + expect(2) + cont.invokeOnCancellation { expect(3) } + ctx.cancel() + expect(4) + cont.resume("OK") { cause -> + expect(5) + } + finish(6) + } + expectUnreached() + } + + @Test + fun testResumeImmediateAfterIndirectCancelWithHandlerFailure() = runTest( + expected = { it is CancellationException }, + unhandled = listOf( + { it is CompletionHandlerException && it.cause is TestException2 }, + { it is CompletionHandlerException && it.cause is TestException3 } + ) + ) { + expect(1) + val ctx = coroutineContext + suspendCancellableCoroutine { cont -> + expect(2) + cont.invokeOnCancellation { + expect(3) + throw TestException2("FAIL") // invokeOnCancellation handler fails with exception + } + ctx.cancel() + expect(4) + cont.resume("OK") { cause -> + expect(5) + throw TestException3("FAIL") // onCancellation block fails with exception + } + finish(6) + } + expectUnreached() + } + @Test fun testResumeLaterNormally() = runTest { expect(1) @@ -91,7 +164,12 @@ class CancellableResumeTest : TestBase() { } @Test - fun testResumeCancelWhileDispatched() = runTest { + fun testResumeLaterAfterCancelWithHandlerFailure() = runTest( + unhandled = listOf( + { it is CompletionHandlerException && it.cause is TestException2 }, + { it is CompletionHandlerException && it.cause is TestException3 } + ) + ) { expect(1) lateinit var cc: CancellableContinuation val job = launch(start = CoroutineStart.UNDISPATCHED) { @@ -99,24 +177,117 @@ class CancellableResumeTest : TestBase() { try { suspendCancellableCoroutine { cont -> expect(3) - // resumed first, then cancelled, so no invokeOnCancellation call - cont.invokeOnCancellation { expectUnreached() } + cont.invokeOnCancellation { + expect(5) + throw TestException2("FAIL") // invokeOnCancellation handler fails with exception + } cc = cont } expectUnreached() } catch (e: CancellationException) { - expect(8) + finish(9) } } expect(4) + job.cancel(TestCancellationException()) + expect(6) cc.resume("OK") { cause -> expect(7) assertTrue(cause is TestCancellationException) + throw TestException3("FAIL") // onCancellation block fails with exception + } + expect(8) + } + + @Test + fun testResumeCancelWhileDispatched() = runTest { + expect(1) + lateinit var cc: CancellableContinuation + val job = launch(start = CoroutineStart.UNDISPATCHED) { + expect(2) + try { + suspendCancellableCoroutine { cont -> + expect(3) + // resumed first, dispatched, then cancelled, but still got invokeOnCancellation call + cont.invokeOnCancellation { cause -> + // Note: invokeOnCancellation is called before cc.resume(value) { ... } handler + expect(7) + assertTrue(cause is TestCancellationException) + } + cc = cont + } + expectUnreached() + } catch (e: CancellationException) { + expect(9) + } + } + expect(4) + cc.resume("OK") { cause -> + // Note: this handler is called after invokeOnCancellation handler + expect(8) + assertTrue(cause is TestCancellationException) + } + expect(5) + job.cancel(TestCancellationException()) // cancel while execution is dispatched + expect(6) + yield() // to coroutine -- throws cancellation exception + finish(10) + } + + @Test + fun testResumeCancelWhileDispatchedWithHandlerFailure() = runTest( + unhandled = listOf( + { it is CompletionHandlerException && it.cause is TestException2 }, + { it is CompletionHandlerException && it.cause is TestException3 } + ) + ) { + expect(1) + lateinit var cc: CancellableContinuation + val job = launch(start = CoroutineStart.UNDISPATCHED) { + expect(2) + try { + suspendCancellableCoroutine { cont -> + expect(3) + // resumed first, dispatched, then cancelled, but still got invokeOnCancellation call + cont.invokeOnCancellation { cause -> + // Note: invokeOnCancellation is called before cc.resume(value) { ... } handler + expect(7) + assertTrue(cause is TestCancellationException) + throw TestException2("FAIL") // invokeOnCancellation handler fails with exception + } + cc = cont + } + expectUnreached() + } catch (e: CancellationException) { + expect(9) + } + } + expect(4) + cc.resume("OK") { cause -> + // Note: this handler is called after invokeOnCancellation handler + expect(8) + assertTrue(cause is TestCancellationException) + throw TestException3("FAIL") // onCancellation block fails with exception } expect(5) job.cancel(TestCancellationException()) // cancel while execution is dispatched expect(6) yield() // to coroutine -- throws cancellation exception - finish(9) + finish(10) + } + + @Test + fun testResumeUnconfined() = runTest { + val outerScope = this + withContext(Dispatchers.Unconfined) { + val result = suspendCancellableCoroutine { + outerScope.launch { + it.resume("OK") { + expectUnreached() + } + } + } + assertEquals("OK", result) + } } -} \ No newline at end of file +} diff --git a/kotlinx-coroutines-core/common/test/DispatchedContinuationTest.kt b/kotlinx-coroutines-core/common/test/DispatchedContinuationTest.kt new file mode 100644 index 0000000000..b69eb22e17 --- /dev/null +++ b/kotlinx-coroutines-core/common/test/DispatchedContinuationTest.kt @@ -0,0 +1,78 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines + +import kotlin.coroutines.* +import kotlin.test.* + +/** + * When using [suspendCoroutine] from the standard library the continuation must be dispatched atomically, + * without checking for cancellation at any point in time. + */ +class DispatchedContinuationTest : TestBase() { + private lateinit var cont: Continuation + + @Test + fun testCancelThenResume() = runTest { + expect(1) + launch(start = CoroutineStart.UNDISPATCHED) { + expect(2) + coroutineContext[Job]!!.cancel() + // a regular suspendCoroutine will still suspend despite the fact that coroutine was cancelled + val value = suspendCoroutine { + expect(3) + cont = it + } + expect(6) + assertEquals("OK", value) + } + expect(4) + cont.resume("OK") + expect(5) + yield() // to the launched job + finish(7) + } + + @Test + fun testCancelThenResumeUnconfined() = runTest { + expect(1) + launch(Dispatchers.Unconfined) { + expect(2) + coroutineContext[Job]!!.cancel() + // a regular suspendCoroutine will still suspend despite the fact that coroutine was cancelled + val value = suspendCoroutine { + expect(3) + cont = it + } + expect(5) + assertEquals("OK", value) + } + expect(4) + cont.resume("OK") // immediately resumes -- because unconfined + finish(6) + } + + @Test + fun testResumeThenCancel() = runTest { + expect(1) + val job = launch(start = CoroutineStart.UNDISPATCHED) { + expect(2) + val value = suspendCoroutine { + expect(3) + cont = it + } + expect(7) + assertEquals("OK", value) + } + expect(4) + cont.resume("OK") + expect(5) + // now cancel the job, which the coroutine is waiting to be dispatched + job.cancel() + expect(6) + yield() // to the launched job + finish(8) + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/common/test/EmptyContext.kt b/kotlinx-coroutines-core/common/test/EmptyContext.kt new file mode 100644 index 0000000000..ad78429d2b --- /dev/null +++ b/kotlinx-coroutines-core/common/test/EmptyContext.kt @@ -0,0 +1,16 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines + +import kotlinx.coroutines.intrinsics.* +import kotlin.coroutines.* + +suspend fun withEmptyContext(block: suspend () -> T): T { + val baseline = Result.failure(IllegalStateException("Block was suspended")) + var result: Result = baseline + block.startCoroutineUnintercepted(Continuation(EmptyCoroutineContext) { result = it }) + while (result == baseline) yield() + return result.getOrThrow() +} diff --git a/kotlinx-coroutines-core/common/test/EnsureActiveTest.kt b/kotlinx-coroutines-core/common/test/JobExtensionsTest.kt similarity index 75% rename from kotlinx-coroutines-core/common/test/EnsureActiveTest.kt rename to kotlinx-coroutines-core/common/test/JobExtensionsTest.kt index 716be629e8..b335926b1f 100644 --- a/kotlinx-coroutines-core/common/test/EnsureActiveTest.kt +++ b/kotlinx-coroutines-core/common/test/JobExtensionsTest.kt @@ -4,9 +4,10 @@ package kotlinx.coroutines +import kotlin.coroutines.* import kotlin.test.* -class EnsureActiveTest : TestBase() { +class JobExtensionsTest : TestBase() { private val job = Job() private val scope = CoroutineScope(job + CoroutineExceptionHandler { _, _ -> }) @@ -68,10 +69,27 @@ class EnsureActiveTest : TestBase() { finish(4) } + @Test + fun testEnsureActiveWithEmptyContext() = runTest { + withEmptyContext { + ensureActive() // should not do anything + } + } + private inline fun checkException(block: () -> Unit) { val result = runCatching(block) val exception = result.exceptionOrNull() ?: fail() assertTrue(exception is JobCancellationException) assertTrue(exception.cause is TestException) } + + @Test + fun testJobExtension() = runTest { + assertSame(coroutineContext[Job]!!, coroutineContext.job) + assertSame(NonCancellable, NonCancellable.job) + assertSame(job, job.job) + assertFailsWith { EmptyCoroutineContext.job } + assertFailsWith { Dispatchers.Default.job } + assertFailsWith { (Dispatchers.Default + CoroutineName("")).job } + } } diff --git a/kotlinx-coroutines-core/common/test/channels/BasicOperationsTest.kt b/kotlinx-coroutines-core/common/test/channels/BasicOperationsTest.kt index a6ddd81185..91d941b32c 100644 --- a/kotlinx-coroutines-core/common/test/channels/BasicOperationsTest.kt +++ b/kotlinx-coroutines-core/common/test/channels/BasicOperationsTest.kt @@ -42,7 +42,7 @@ class BasicOperationsTest : TestBase() { @Test fun testInvokeOnClose() = TestChannelKind.values().forEach { kind -> reset() - val channel = kind.create() + val channel = kind.create() channel.invokeOnClose { if (it is AssertionError) { expect(3) @@ -59,7 +59,7 @@ class BasicOperationsTest : TestBase() { fun testInvokeOnClosed() = TestChannelKind.values().forEach { kind -> reset() expect(1) - val channel = kind.create() + val channel = kind.create() channel.close() channel.invokeOnClose { expect(2) } assertFailsWith { channel.invokeOnClose { expect(3) } } @@ -69,7 +69,7 @@ class BasicOperationsTest : TestBase() { @Test fun testMultipleInvokeOnClose() = TestChannelKind.values().forEach { kind -> reset() - val channel = kind.create() + val channel = kind.create() channel.invokeOnClose { expect(3) } expect(1) assertFailsWith { channel.invokeOnClose { expect(4) } } @@ -81,7 +81,7 @@ class BasicOperationsTest : TestBase() { @Test fun testIterator() = runTest { TestChannelKind.values().forEach { kind -> - val channel = kind.create() + val channel = kind.create() val iterator = channel.iterator() assertFailsWith { iterator.next() } channel.close() @@ -91,7 +91,7 @@ class BasicOperationsTest : TestBase() { } private suspend fun testReceiveOrNull(kind: TestChannelKind) = coroutineScope { - val channel = kind.create() + val channel = kind.create() val d = async(NonCancellable) { channel.receive() } @@ -108,7 +108,7 @@ class BasicOperationsTest : TestBase() { } private suspend fun testReceiveOrNullException(kind: TestChannelKind) = coroutineScope { - val channel = kind.create() + val channel = kind.create() val d = async(NonCancellable) { channel.receive() } @@ -132,7 +132,7 @@ class BasicOperationsTest : TestBase() { @Suppress("ReplaceAssertBooleanWithAssertEquality") private suspend fun testReceiveOrClosed(kind: TestChannelKind) = coroutineScope { reset() - val channel = kind.create() + val channel = kind.create() launch { expect(2) channel.send(1) @@ -159,7 +159,7 @@ class BasicOperationsTest : TestBase() { } private suspend fun testOffer(kind: TestChannelKind) = coroutineScope { - val channel = kind.create() + val channel = kind.create() val d = async { channel.send(42) } yield() channel.close() @@ -184,7 +184,7 @@ class BasicOperationsTest : TestBase() { private suspend fun testSendAfterClose(kind: TestChannelKind) { assertFailsWith { coroutineScope { - val channel = kind.create() + val channel = kind.create() channel.close() launch { @@ -195,7 +195,7 @@ class BasicOperationsTest : TestBase() { } private suspend fun testSendReceive(kind: TestChannelKind, iterations: Int) = coroutineScope { - val channel = kind.create() + val channel = kind.create() launch { repeat(iterations) { channel.send(it) } channel.close() diff --git a/kotlinx-coroutines-core/common/test/channels/BroadcastTest.kt b/kotlinx-coroutines-core/common/test/channels/BroadcastTest.kt index bb3142e54c..ab1a85d697 100644 --- a/kotlinx-coroutines-core/common/test/channels/BroadcastTest.kt +++ b/kotlinx-coroutines-core/common/test/channels/BroadcastTest.kt @@ -63,7 +63,7 @@ class BroadcastTest : TestBase() { val a = produce { expect(3) send("MSG") - expect(5) + expectUnreached() // is not executed, because send is cancelled } expect(2) yield() // to produce @@ -72,7 +72,7 @@ class BroadcastTest : TestBase() { expect(4) yield() // to abort produce assertTrue(a.isClosedForReceive) // the source channel was consumed - finish(6) + finish(5) } @Test diff --git a/kotlinx-coroutines-core/common/test/channels/ChannelBufferOverflowTest.kt b/kotlinx-coroutines-core/common/test/channels/ChannelBufferOverflowTest.kt new file mode 100644 index 0000000000..41f60479f2 --- /dev/null +++ b/kotlinx-coroutines-core/common/test/channels/ChannelBufferOverflowTest.kt @@ -0,0 +1,40 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.channels + +import kotlinx.coroutines.* +import kotlin.test.* + +class ChannelBufferOverflowTest : TestBase() { + @Test + fun testDropLatest() = runTest { + val c = Channel(2, BufferOverflow.DROP_LATEST) + assertTrue(c.offer(1)) + assertTrue(c.offer(2)) + assertTrue(c.offer(3)) // overflows, dropped + c.send(4) // overflows dropped + assertEquals(1, c.receive()) + assertTrue(c.offer(5)) + assertTrue(c.offer(6)) // overflows, dropped + assertEquals(2, c.receive()) + assertEquals(5, c.receive()) + assertEquals(null, c.poll()) + } + + @Test + fun testDropOldest() = runTest { + val c = Channel(2, BufferOverflow.DROP_OLDEST) + assertTrue(c.offer(1)) + assertTrue(c.offer(2)) + assertTrue(c.offer(3)) // overflows, keeps 2, 3 + c.send(4) // overflows, keeps 3, 4 + assertEquals(3, c.receive()) + assertTrue(c.offer(5)) + assertTrue(c.offer(6)) // overflows, keeps 5, 6 + assertEquals(5, c.receive()) + assertEquals(6, c.receive()) + assertEquals(null, c.poll()) + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/common/test/channels/ChannelFactoryTest.kt b/kotlinx-coroutines-core/common/test/channels/ChannelFactoryTest.kt index 72ba315450..413c91f5a7 100644 --- a/kotlinx-coroutines-core/common/test/channels/ChannelFactoryTest.kt +++ b/kotlinx-coroutines-core/common/test/channels/ChannelFactoryTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ package kotlinx.coroutines.channels @@ -9,7 +9,6 @@ import kotlin.test.* class ChannelFactoryTest : TestBase() { - @Test fun testRendezvousChannel() { assertTrue(Channel() is RendezvousChannel) @@ -19,21 +18,31 @@ class ChannelFactoryTest : TestBase() { @Test fun testLinkedListChannel() { assertTrue(Channel(Channel.UNLIMITED) is LinkedListChannel) + assertTrue(Channel(Channel.UNLIMITED, BufferOverflow.DROP_OLDEST) is LinkedListChannel) + assertTrue(Channel(Channel.UNLIMITED, BufferOverflow.DROP_LATEST) is LinkedListChannel) } @Test fun testConflatedChannel() { assertTrue(Channel(Channel.CONFLATED) is ConflatedChannel) + assertTrue(Channel(1, BufferOverflow.DROP_OLDEST) is ConflatedChannel) } @Test fun testArrayChannel() { assertTrue(Channel(1) is ArrayChannel) + assertTrue(Channel(1, BufferOverflow.DROP_LATEST) is ArrayChannel) assertTrue(Channel(10) is ArrayChannel) } @Test - fun testInvalidCapacityNotSupported() = runTest({ it is IllegalArgumentException }) { - Channel(-3) + fun testInvalidCapacityNotSupported() { + assertFailsWith { Channel(-3) } + } + + @Test + fun testUnsupportedBufferOverflow() { + assertFailsWith { Channel(Channel.CONFLATED, BufferOverflow.DROP_OLDEST) } + assertFailsWith { Channel(Channel.CONFLATED, BufferOverflow.DROP_LATEST) } } } diff --git a/kotlinx-coroutines-core/common/test/channels/ChannelUndeliveredElementFailureTest.kt b/kotlinx-coroutines-core/common/test/channels/ChannelUndeliveredElementFailureTest.kt new file mode 100644 index 0000000000..d2ef3d2691 --- /dev/null +++ b/kotlinx-coroutines-core/common/test/channels/ChannelUndeliveredElementFailureTest.kt @@ -0,0 +1,143 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.channels + +import kotlinx.coroutines.* +import kotlinx.coroutines.internal.* +import kotlinx.coroutines.selects.* +import kotlin.test.* + +/** + * Tests for failures inside `onUndeliveredElement` handler in [Channel]. + */ +class ChannelUndeliveredElementFailureTest : TestBase() { + private val item = "LOST" + private val onCancelFail: (String) -> Unit = { throw TestException(it) } + private val shouldBeUnhandled: List<(Throwable) -> Boolean> = listOf({ it.isElementCancelException() }) + + private fun Throwable.isElementCancelException() = + this is UndeliveredElementException && cause is TestException && cause!!.message == item + + @Test + fun testSendCancelledFail() = runTest(unhandled = shouldBeUnhandled) { + val channel = Channel(onUndeliveredElement = onCancelFail) + val job = launch(start = CoroutineStart.UNDISPATCHED) { + channel.send(item) + expectUnreached() + } + job.cancel() + } + + @Test + fun testSendSelectCancelledFail() = runTest(unhandled = shouldBeUnhandled) { + val channel = Channel(onUndeliveredElement = onCancelFail) + val job = launch(start = CoroutineStart.UNDISPATCHED) { + select { + channel.onSend(item) { + expectUnreached() + } + } + } + job.cancel() + } + + @Test + fun testReceiveCancelledFail() = runTest(unhandled = shouldBeUnhandled) { + val channel = Channel(onUndeliveredElement = onCancelFail) + val job = launch(start = CoroutineStart.UNDISPATCHED) { + channel.receive() + expectUnreached() // will be cancelled before it dispatches + } + channel.send(item) + job.cancel() + } + + @Test + fun testReceiveSelectCancelledFail() = runTest(unhandled = shouldBeUnhandled) { + val channel = Channel(onUndeliveredElement = onCancelFail) + val job = launch(start = CoroutineStart.UNDISPATCHED) { + select { + channel.onReceive { + expectUnreached() + } + } + expectUnreached() // will be cancelled before it dispatches + } + channel.send(item) + job.cancel() + } + + @Test + fun testReceiveOrNullCancelledFail() = runTest(unhandled = shouldBeUnhandled) { + val channel = Channel(onUndeliveredElement = onCancelFail) + val job = launch(start = CoroutineStart.UNDISPATCHED) { + channel.receiveOrNull() + expectUnreached() // will be cancelled before it dispatches + } + channel.send(item) + job.cancel() + } + + @Test + fun testReceiveOrNullSelectCancelledFail() = runTest(unhandled = shouldBeUnhandled) { + val channel = Channel(onUndeliveredElement = onCancelFail) + val job = launch(start = CoroutineStart.UNDISPATCHED) { + select { + channel.onReceiveOrNull { + expectUnreached() + } + } + expectUnreached() // will be cancelled before it dispatches + } + channel.send(item) + job.cancel() + } + + @Test + fun testReceiveOrClosedCancelledFail() = runTest(unhandled = shouldBeUnhandled) { + val channel = Channel(onUndeliveredElement = onCancelFail) + val job = launch(start = CoroutineStart.UNDISPATCHED) { + channel.receiveOrClosed() + expectUnreached() // will be cancelled before it dispatches + } + channel.send(item) + job.cancel() + } + + @Test + fun testReceiveOrClosedSelectCancelledFail() = runTest(unhandled = shouldBeUnhandled) { + val channel = Channel(onUndeliveredElement = onCancelFail) + val job = launch(start = CoroutineStart.UNDISPATCHED) { + select { + channel.onReceiveOrClosed { + expectUnreached() + } + } + expectUnreached() // will be cancelled before it dispatches + } + channel.send(item) + job.cancel() + } + + @Test + fun testHasNextCancelledFail() = runTest(unhandled = shouldBeUnhandled) { + val channel = Channel(onUndeliveredElement = onCancelFail) + val job = launch(start = CoroutineStart.UNDISPATCHED) { + channel.iterator().hasNext() + expectUnreached() // will be cancelled before it dispatches + } + channel.send(item) + job.cancel() + } + + @Test + fun testChannelCancelledFail() = runTest(expected = { it.isElementCancelException()}) { + val channel = Channel(1, onUndeliveredElement = onCancelFail) + channel.send(item) + channel.cancel() + expectUnreached() + } + +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/common/test/channels/ChannelUndeliveredElementTest.kt b/kotlinx-coroutines-core/common/test/channels/ChannelUndeliveredElementTest.kt new file mode 100644 index 0000000000..0391e00033 --- /dev/null +++ b/kotlinx-coroutines-core/common/test/channels/ChannelUndeliveredElementTest.kt @@ -0,0 +1,104 @@ +package kotlinx.coroutines.channels + +import kotlinx.atomicfu.* +import kotlinx.coroutines.* +import kotlin.test.* + +class ChannelUndeliveredElementTest : TestBase() { + @Test + fun testSendSuccessfully() = runAllKindsTest { kind -> + val channel = kind.create { it.cancel() } + val res = Resource("OK") + launch { + channel.send(res) + } + val ok = channel.receive() + assertEquals("OK", ok.value) + assertFalse(res.isCancelled) // was not cancelled + channel.close() + assertFalse(res.isCancelled) // still was not cancelled + } + + @Test + fun testRendezvousSendCancelled() = runTest { + val channel = Channel { it.cancel() } + val res = Resource("OK") + val sender = launch(start = CoroutineStart.UNDISPATCHED) { + assertFailsWith { + channel.send(res) // suspends & get cancelled + } + } + sender.cancelAndJoin() + assertTrue(res.isCancelled) + } + + @Test + fun testBufferedSendCancelled() = runTest { + val channel = Channel(1) { it.cancel() } + val resA = Resource("A") + val resB = Resource("B") + val sender = launch(start = CoroutineStart.UNDISPATCHED) { + channel.send(resA) // goes to buffer + assertFailsWith { + channel.send(resB) // suspends & get cancelled + } + } + sender.cancelAndJoin() + assertFalse(resA.isCancelled) // it is in buffer, not cancelled + assertTrue(resB.isCancelled) // send was cancelled + channel.cancel() // now cancel the channel + assertTrue(resA.isCancelled) // now cancelled in buffer + } + + @Test + fun testConflatedResourceCancelled() = runTest { + val channel = Channel(Channel.CONFLATED) { it.cancel() } + val resA = Resource("A") + val resB = Resource("B") + channel.send(resA) + assertFalse(resA.isCancelled) + assertFalse(resB.isCancelled) + channel.send(resB) + assertTrue(resA.isCancelled) // it was conflated (lost) and thus cancelled + assertFalse(resB.isCancelled) + channel.close() + assertFalse(resB.isCancelled) // not cancelled yet, can be still read by receiver + channel.cancel() + assertTrue(resB.isCancelled) // now it is cancelled + } + + @Test + fun testSendToClosedChannel() = runAllKindsTest { kind -> + val channel = kind.create { it.cancel() } + channel.close() // immediately close channel + val res = Resource("OK") + assertFailsWith { + channel.send(res) // send fails to closed channel, resource was not delivered + } + assertTrue(res.isCancelled) + } + + private fun runAllKindsTest(test: suspend CoroutineScope.(TestChannelKind) -> Unit) { + for (kind in TestChannelKind.values()) { + if (kind.viaBroadcast) continue // does not support onUndeliveredElement + try { + runTest { + test(kind) + } + } catch(e: Throwable) { + error("$kind: $e", e) + } + } + } + + private class Resource(val value: String) { + private val _cancelled = atomic(false) + + val isCancelled: Boolean + get() = _cancelled.value + + fun cancel() { + check(!_cancelled.getAndSet(true)) { "Already cancelled" } + } + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/common/test/channels/ChannelsTest.kt b/kotlinx-coroutines-core/common/test/channels/ChannelsTest.kt index ba786d53cc..fb704c5b86 100644 --- a/kotlinx-coroutines-core/common/test/channels/ChannelsTest.kt +++ b/kotlinx-coroutines-core/common/test/channels/ChannelsTest.kt @@ -50,123 +50,6 @@ class ChannelsTest: TestBase() { finish(7) } - @Test - fun testAssociate() = runTest { - assertEquals(testList.associate { it * 2 to it * 3 }, - testList.asReceiveChannel().associate { it * 2 to it * 3 }.toMap()) - } - - @Test - fun testAssociateBy() = runTest { - assertEquals(testList.associateBy { it % 2 }, testList.asReceiveChannel().associateBy { it % 2 }) - } - - @Test - fun testAssociateBy2() = runTest { - assertEquals(testList.associateBy({ it * 2}, { it * 3 }), - testList.asReceiveChannel().associateBy({ it * 2}, { it * 3 }).toMap()) - } - - @Test - fun testDistinct() = runTest { - assertEquals(testList.map { it % 2 }.distinct(), testList.asReceiveChannel().map { it % 2 }.distinct().toList()) - } - - @Test - fun testDistinctBy() = runTest { - assertEquals(testList.distinctBy { it % 2 }.toList(), testList.asReceiveChannel().distinctBy { it % 2 }.toList()) - } - - @Test - fun testToCollection() = runTest { - val target = mutableListOf() - testList.asReceiveChannel().toCollection(target) - assertEquals(testList, target) - } - - @Test - fun testDrop() = runTest { - for (i in 0..testList.size) { - assertEquals(testList.drop(i), testList.asReceiveChannel().drop(i).toList(), "Drop $i") - } - } - - @Test - fun testElementAtOrElse() = runTest { - assertEquals(testList.elementAtOrElse(2) { 42 }, testList.asReceiveChannel().elementAtOrElse(2) { 42 }) - assertEquals(testList.elementAtOrElse(9) { 42 }, testList.asReceiveChannel().elementAtOrElse(9) { 42 }) - } - - @Test - fun testFirst() = runTest { - assertEquals(testList.first(), testList.asReceiveChannel().first()) - for (i in testList) { - assertEquals(testList.first { it == i }, testList.asReceiveChannel().first { it == i }) - } - try { - testList.asReceiveChannel().first { it == 9 } - fail() - } catch (nse: NoSuchElementException) { - } - } - - @Test - fun testFirstOrNull() = runTest { - assertEquals(testList.firstOrNull(), testList.asReceiveChannel().firstOrNull()) - assertEquals(testList.firstOrNull { it == 2 }, testList.asReceiveChannel().firstOrNull { it == 2 }) - assertEquals(testList.firstOrNull { it == 9 }, testList.asReceiveChannel().firstOrNull { it == 9 }) - } - - @Test - fun testFlatMap() = runTest { - assertEquals(testList.flatMap { (0..it).toList() }, testList.asReceiveChannel().flatMap { (0..it).asReceiveChannel() }.toList()) - - } - - @Test - fun testFold() = runTest { - assertEquals(testList.fold(mutableListOf(42)) { acc, e -> acc.apply { add(e) } }, - testList.asReceiveChannel().fold(mutableListOf(42)) { acc, e -> acc.apply { add(e) } }.toList()) - } - - @Test - fun testFoldIndexed() = runTest { - assertEquals(testList.foldIndexed(mutableListOf(42)) { index, acc, e -> acc.apply { add(index + e) } }, - testList.asReceiveChannel().foldIndexed(mutableListOf(42)) { index, acc, e -> acc.apply { add(index + e) } }.toList()) - } - - @Test - fun testGroupBy() = runTest { - assertEquals(testList.groupBy { it % 2 }, testList.asReceiveChannel().groupBy { it % 2 }) - } - - @Test - fun testGroupBy2() = runTest { - assertEquals(testList.groupBy({ -it }, { it + 100 }), testList.asReceiveChannel().groupBy({ -it }, { it + 100 }).toMap()) - - } - - @Test - fun testMap() = runTest { - assertEquals(testList.map { it + 10 }, testList.asReceiveChannel().map { it + 10 }.toList()) - - } - - @Test - fun testMapToCollection() = runTest { - val c = mutableListOf() - testList.asReceiveChannel().mapTo(c) { it + 10 } - assertEquals(testList.map { it + 10 }, c) - } - - @Test - fun testMapToSendChannel() = runTest { - val c = produce { - testList.asReceiveChannel().mapTo(channel) { it + 10 } - } - assertEquals(testList.map { it + 10 }, c.toList()) - } - @Test fun testEmptyList() = runTest { assertTrue(emptyList().asReceiveChannel().toList().isEmpty()) @@ -178,413 +61,6 @@ class ChannelsTest: TestBase() { } - @Test - fun testEmptySet() = runTest { - assertTrue(emptyList().asReceiveChannel().toSet().isEmpty()) - - } - - @Test - fun testToSet() = runTest { - assertEquals(testList.toSet(), testList.asReceiveChannel().toSet()) - } - - @Test - fun testToMutableSet() = runTest { - assertEquals(testList.toMutableSet(), testList.asReceiveChannel().toMutableSet()) - } - - @Test - fun testEmptySequence() = runTest { - val channel = Channel() - channel.close() - - assertEquals(emptyList().asReceiveChannel().count(), 0) - } - - @Test - fun testEmptyMap() = runTest { - val channel = Channel>() - channel.close() - - assertTrue(channel.toMap().isEmpty()) - } - - @Test - fun testToMap() = runTest { - val values = testList.map { it to it.toString() } - assertEquals(values.toMap(), values.asReceiveChannel().toMap()) - } - - @Test - fun testReduce() = runTest { - assertEquals(testList.reduce { acc, e -> acc * e }, - testList.asReceiveChannel().reduce { acc, e -> acc * e }) - } - - @Test - fun testReduceIndexed() = runTest { - assertEquals(testList.reduceIndexed { index, acc, e -> index + acc * e }, - testList.asReceiveChannel().reduceIndexed { index, acc, e -> index + acc * e }) - } - - @Test - fun testTake() = runTest { - for (i in 0..testList.size) { - assertEquals(testList.take(i), testList.asReceiveChannel().take(i).toList()) - } - } - - @Test - fun testPartition() = runTest { - assertEquals(testList.partition { it % 2 == 0 }, testList.asReceiveChannel().partition { it % 2 == 0 }) - } - - @Test - fun testZip() = runTest { - val other = listOf("a", "b") - assertEquals(testList.zip(other), testList.asReceiveChannel().zip(other.asReceiveChannel()).toList()) - } - - @Test - fun testElementAt() = runTest { - testList.indices.forEach { i -> - assertEquals(testList[i], testList.asReceiveChannel().elementAt(i)) - } - } - - @Test - fun testElementAtOrNull() = runTest { - testList.indices.forEach { i -> - assertEquals(testList[i], testList.asReceiveChannel().elementAtOrNull(i)) - } - assertNull(testList.asReceiveChannel().elementAtOrNull(-1)) - assertNull(testList.asReceiveChannel().elementAtOrNull(testList.size)) - } - - @Test - fun testFind() = runTest { - repeat(3) { mod -> - assertEquals(testList.find { it % 2 == mod }, - testList.asReceiveChannel().find { it % 2 == mod }) - } - } - - @Test - fun testFindLast() = runTest { - repeat(3) { mod -> - assertEquals(testList.findLast { it % 2 == mod }, testList.asReceiveChannel().findLast { it % 2 == mod }) - } - } - - @Test - fun testIndexOf() = runTest { - repeat(testList.size + 1) { i -> - assertEquals(testList.indexOf(i), testList.asReceiveChannel().indexOf(i)) - } - } - - @Test - fun testLastIndexOf() = runTest { - repeat(testList.size + 1) { i -> - assertEquals(testList.lastIndexOf(i), testList.asReceiveChannel().lastIndexOf(i)) - } - } - - @Test - fun testIndexOfFirst() = runTest { - repeat(3) { mod -> - assertEquals(testList.indexOfFirst { it % 2 == mod }, - testList.asReceiveChannel().indexOfFirst { it % 2 == mod }) - } - } - - @Test - fun testIndexOfLast() = runTest { - repeat(3) { mod -> - assertEquals(testList.indexOfLast { it % 2 != mod }, - testList.asReceiveChannel().indexOfLast { it % 2 != mod }) - } - } - - @Test - fun testLastOrNull() = runTest { - assertEquals(testList.lastOrNull(), testList.asReceiveChannel().lastOrNull()) - assertNull(emptyList().asReceiveChannel().lastOrNull()) - } - - @Test - fun testSingleOrNull() = runTest { - assertEquals(1, listOf(1).asReceiveChannel().singleOrNull()) - assertNull(listOf(1, 2).asReceiveChannel().singleOrNull()) - assertNull(emptyList().asReceiveChannel().singleOrNull()) - repeat(testList.size + 1) { i -> - assertEquals(testList.singleOrNull { it == i }, - testList.asReceiveChannel().singleOrNull { it == i }) - } - repeat(3) { mod -> - assertEquals(testList.singleOrNull { it % 2 == mod }, - testList.asReceiveChannel().singleOrNull { it % 2 == mod }) - } - } - - @Test - fun testDropWhile() = runTest { - repeat(3) { mod -> - assertEquals(testList.dropWhile { it % 2 == mod }, - testList.asReceiveChannel().dropWhile { it % 2 == mod }.toList()) - } - } - - @Test - fun testFilter() = runTest { - repeat(3) { mod -> - assertEquals(testList.filter { it % 2 == mod }, - testList.asReceiveChannel().filter { it % 2 == mod }.toList()) - } - } - - @Test - fun testFilterToCollection() = runTest { - repeat(3) { mod -> - val c = mutableListOf() - testList.asReceiveChannel().filterTo(c) { it % 2 == mod } - assertEquals(testList.filter { it % 2 == mod }, c) - } - } - - @Test - fun testFilterToSendChannel() = runTest { - repeat(3) { mod -> - val c = produce { - testList.asReceiveChannel().filterTo(channel) { it % 2 == mod } - } - assertEquals(testList.filter { it % 2 == mod }, c.toList()) - } - } - - @Test - fun testFilterNot() = runTest { - repeat(3) { mod -> - assertEquals(testList.filterNot { it % 2 == mod }, - testList.asReceiveChannel().filterNot { it % 2 == mod }.toList()) - } - } - - @Test - fun testFilterNotToCollection() = runTest { - repeat(3) { mod -> - val c = mutableListOf() - testList.asReceiveChannel().filterNotTo(c) { it % 2 == mod } - assertEquals(testList.filterNot { it % 2 == mod }, c) - } - } - - @Test - fun testFilterNotToSendChannel() = runTest { - repeat(3) { mod -> - val c = produce { - testList.asReceiveChannel().filterNotTo(channel) { it % 2 == mod } - } - assertEquals(testList.filterNot { it % 2 == mod }, c.toList()) - } - } - - @Test - fun testFilterNotNull() = runTest { - repeat(3) { mod -> - assertEquals( - testList.mapNotNull { it.takeIf { it % 2 == mod } }, - testList.asReceiveChannel().map { it.takeIf { it % 2 == mod } }.filterNotNull().toList()) - } - } - - @Test - fun testFilterNotNullToCollection() = runTest { - repeat(3) { mod -> - val c = mutableListOf() - testList.asReceiveChannel().map { it.takeIf { it % 2 == mod } }.filterNotNullTo(c) - assertEquals(testList.mapNotNull { it.takeIf { it % 2 == mod } }, c) - } - } - - @Test - fun testFilterNotNullToSendChannel() = runTest { - repeat(3) { mod -> - val c = produce { - testList.asReceiveChannel().map { it.takeIf { it % 2 == mod } }.filterNotNullTo(channel) - } - assertEquals(testList.mapNotNull { it.takeIf { it % 2 == mod } }, c.toList()) - } - } - - @Test - fun testFilterIndexed() = runTest { - repeat(3) { mod -> - assertEquals(testList.filterIndexed { index, _ -> index % 2 == mod }, - testList.asReceiveChannel().filterIndexed { index, _ -> index % 2 == mod }.toList()) - } - } - - @Test - fun testFilterIndexedToCollection() = runTest { - repeat(3) { mod -> - val c = mutableListOf() - testList.asReceiveChannel().filterIndexedTo(c) { index, _ -> index % 2 == mod } - assertEquals(testList.filterIndexed { index, _ -> index % 2 == mod }, c) - } - } - - @Test - fun testFilterIndexedToChannel() = runTest { - repeat(3) { mod -> - val c = produce { - testList.asReceiveChannel().filterIndexedTo(channel) { index, _ -> index % 2 == mod } - } - assertEquals(testList.filterIndexed { index, _ -> index % 2 == mod }, c.toList()) - } - } - - @Test - fun testTakeWhile() = runTest { - repeat(3) { mod -> - assertEquals(testList.takeWhile { it % 2 != mod }, - testList.asReceiveChannel().takeWhile { it % 2 != mod }.toList()) - } - } - - @Test - fun testToChannel() = runTest { - val c = produce { - testList.asReceiveChannel().toChannel(channel) - } - assertEquals(testList, c.toList()) - } - - @Test - fun testMapIndexed() = runTest { - assertEquals(testList.mapIndexed { index, i -> index + i }, - testList.asReceiveChannel().mapIndexed { index, i -> index + i }.toList()) - } - - @Test - fun testMapIndexedToCollection() = runTest { - val c = mutableListOf() - testList.asReceiveChannel().mapIndexedTo(c) { index, i -> index + i } - assertEquals(testList.mapIndexed { index, i -> index + i }, c) - } - - @Test - fun testMapIndexedToSendChannel() = runTest { - val c = produce { - testList.asReceiveChannel().mapIndexedTo(channel) { index, i -> index + i } - } - assertEquals(testList.mapIndexed { index, i -> index + i }, c.toList()) - } - - @Test - fun testMapNotNull() = runTest { - repeat(3) { mod -> - assertEquals(testList.mapNotNull { i -> i.takeIf { i % 2 == mod } }, - testList.asReceiveChannel().mapNotNull { i -> i.takeIf { i % 2 == mod } }.toList()) - } - } - - @Test - fun testMapNotNullToCollection() = runTest { - repeat(3) { mod -> - val c = mutableListOf() - testList.asReceiveChannel().mapNotNullTo(c) { i -> i.takeIf { i % 2 == mod } } - assertEquals(testList.mapNotNull { i -> i.takeIf { i % 2 == mod } }, c) - } - } - - @Test - fun testMapNotNullToSendChannel() = runTest { - repeat(3) { mod -> - val c = produce { - testList.asReceiveChannel().mapNotNullTo(channel) { i -> i.takeIf { i % 2 == mod } } - } - assertEquals(testList.mapNotNull { i -> i.takeIf { i % 2 == mod } }, c.toList()) - } - } - - @Test - fun testMapIndexedNotNull() = runTest { - repeat(3) { mod -> - assertEquals(testList.mapIndexedNotNull { index, i -> index.takeIf { i % 2 == mod } }, - testList.asReceiveChannel().mapIndexedNotNull { index, i -> index.takeIf { i % 2 == mod } }.toList()) - } - } - - @Test - fun testMapIndexedNotNullToCollection() = runTest { - repeat(3) { mod -> - val c = mutableListOf() - testList.asReceiveChannel().mapIndexedNotNullTo(c) { index, i -> index.takeIf { i % 2 == mod } } - assertEquals(testList.mapIndexedNotNull { index, i -> index.takeIf { i % 2 == mod } }, c) - } - } - - @Test - fun testMapIndexedNotNullToSendChannel() = runTest { - repeat(3) { mod -> - val c = produce { - testList.asReceiveChannel().mapIndexedNotNullTo(channel) { index, i -> index.takeIf { i % 2 == mod } } - } - assertEquals(testList.mapIndexedNotNull { index, i -> index.takeIf { i % 2 == mod } }, c.toList()) - } - } - - @Test - fun testWithIndex() = runTest { - assertEquals(testList.withIndex().toList(), testList.asReceiveChannel().withIndex().toList()) - } - - @Test - fun testMaxBy() = runTest { - assertEquals(testList.maxBy { 10 - abs(it - 2) }, - testList.asReceiveChannel().maxBy { 10 - abs(it - 2) }) - } - - @Test - fun testMaxWith() = runTest { - val cmp = compareBy { 10 - abs(it - 2) } - assertEquals(testList.maxWith(cmp), - testList.asReceiveChannel().maxWith(cmp)) - } - - @Test - fun testMinBy() = runTest { - assertEquals(testList.minBy { abs(it - 2) }, - testList.asReceiveChannel().minBy { abs(it - 2) }) - } - - @Test - fun testMinWith() = runTest { - val cmp = compareBy { abs(it - 2) } - assertEquals(testList.minWith(cmp), - testList.asReceiveChannel().minWith(cmp)) - } - - @Test - fun testSumBy() = runTest { - assertEquals(testList.sumBy { it * 3 }, - testList.asReceiveChannel().sumBy { it * 3 }) - } - - @Test - fun testSumByDouble() = runTest { - val expected = testList.sumByDouble { it * 3.0 } - val actual = testList.asReceiveChannel().sumByDouble { it * 3.0 } - assertEquals(expected, actual) - } - - @Test - fun testRequireNoNulls() = runTest { - assertEquals(testList.requireNoNulls(), testList.asReceiveChannel().requireNoNulls().toList()) - } - private fun Iterable.asReceiveChannel(context: CoroutineContext = Dispatchers.Unconfined): ReceiveChannel = GlobalScope.produce(context) { for (element in this@asReceiveChannel) diff --git a/kotlinx-coroutines-core/common/test/channels/ConflatedChannelArrayModelTest.kt b/kotlinx-coroutines-core/common/test/channels/ConflatedChannelArrayModelTest.kt new file mode 100644 index 0000000000..e80309be89 --- /dev/null +++ b/kotlinx-coroutines-core/common/test/channels/ConflatedChannelArrayModelTest.kt @@ -0,0 +1,11 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.channels + +// Test that ArrayChannel(1, DROP_OLDEST) works just like ConflatedChannel() +class ConflatedChannelArrayModelTest : ConflatedChannelTest() { + override fun createConflatedChannel(): Channel = + ArrayChannel(1, BufferOverflow.DROP_OLDEST, null) +} diff --git a/kotlinx-coroutines-core/common/test/channels/ConflatedChannelTest.kt b/kotlinx-coroutines-core/common/test/channels/ConflatedChannelTest.kt index 4deb3858f0..18f2843868 100644 --- a/kotlinx-coroutines-core/common/test/channels/ConflatedChannelTest.kt +++ b/kotlinx-coroutines-core/common/test/channels/ConflatedChannelTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ package kotlinx.coroutines.channels @@ -7,10 +7,13 @@ package kotlinx.coroutines.channels import kotlinx.coroutines.* import kotlin.test.* -class ConflatedChannelTest : TestBase() { +open class ConflatedChannelTest : TestBase() { + protected open fun createConflatedChannel() = + Channel(Channel.CONFLATED) + @Test fun testBasicConflationOfferPoll() { - val q = Channel(Channel.CONFLATED) + val q = createConflatedChannel() assertNull(q.poll()) assertTrue(q.offer(1)) assertTrue(q.offer(2)) @@ -21,7 +24,7 @@ class ConflatedChannelTest : TestBase() { @Test fun testConflatedSend() = runTest { - val q = ConflatedChannel() + val q = createConflatedChannel() q.send(1) q.send(2) // shall conflated previously sent assertEquals(2, q.receiveOrNull()) @@ -29,7 +32,7 @@ class ConflatedChannelTest : TestBase() { @Test fun testConflatedClose() = runTest { - val q = Channel(Channel.CONFLATED) + val q = createConflatedChannel() q.send(1) q.close() // shall become closed but do not conflate last sent item yet assertTrue(q.isClosedForSend) @@ -43,7 +46,7 @@ class ConflatedChannelTest : TestBase() { @Test fun testConflationSendReceive() = runTest { - val q = Channel(Channel.CONFLATED) + val q = createConflatedChannel() expect(1) launch { // receiver coroutine expect(4) @@ -71,7 +74,7 @@ class ConflatedChannelTest : TestBase() { @Test fun testConsumeAll() = runTest { - val q = Channel(Channel.CONFLATED) + val q = createConflatedChannel() expect(1) for (i in 1..10) { q.send(i) // stores as last @@ -85,7 +88,7 @@ class ConflatedChannelTest : TestBase() { @Test fun testCancelWithCause() = runTest({ it is TestCancellationException }) { - val channel = Channel(Channel.CONFLATED) + val channel = createConflatedChannel() channel.cancel(TestCancellationException()) channel.receiveOrNull() } diff --git a/kotlinx-coroutines-core/common/test/channels/ProduceTest.kt b/kotlinx-coroutines-core/common/test/channels/ProduceTest.kt index 885f1d6c8f..6ddde001e2 100644 --- a/kotlinx-coroutines-core/common/test/channels/ProduceTest.kt +++ b/kotlinx-coroutines-core/common/test/channels/ProduceTest.kt @@ -163,7 +163,7 @@ class ProduceTest : TestBase() { private suspend fun cancelOnCompletion(coroutineContext: CoroutineContext) = CoroutineScope(coroutineContext).apply { val source = Channel() expect(1) - val produced = produce(coroutineContext, onCompletion = source.consumes()) { + val produced = produce(coroutineContext, onCompletion = { source.cancelConsumed(it) }) { expect(2) source.receive() } diff --git a/kotlinx-coroutines-core/common/test/channels/TestChannelKind.kt b/kotlinx-coroutines-core/common/test/channels/TestChannelKind.kt index 69d8fd03e3..993be78e17 100644 --- a/kotlinx-coroutines-core/common/test/channels/TestChannelKind.kt +++ b/kotlinx-coroutines-core/common/test/channels/TestChannelKind.kt @@ -7,9 +7,10 @@ package kotlinx.coroutines.channels import kotlinx.coroutines.* import kotlinx.coroutines.selects.* -enum class TestChannelKind(val capacity: Int, - private val description: String, - private val viaBroadcast: Boolean = false +enum class TestChannelKind( + val capacity: Int, + private val description: String, + val viaBroadcast: Boolean = false ) { RENDEZVOUS(0, "RendezvousChannel"), ARRAY_1(1, "ArrayChannel(1)"), @@ -22,8 +23,11 @@ enum class TestChannelKind(val capacity: Int, CONFLATED_BROADCAST(Channel.CONFLATED, "ConflatedBroadcastChannel", viaBroadcast = true) ; - fun create(): Channel = if (viaBroadcast) ChannelViaBroadcast(BroadcastChannel(capacity)) - else Channel(capacity) + fun create(onUndeliveredElement: ((T) -> Unit)? = null): Channel = when { + viaBroadcast && onUndeliveredElement != null -> error("Broadcast channels to do not support onUndeliveredElement") + viaBroadcast -> ChannelViaBroadcast(BroadcastChannel(capacity)) + else -> Channel(capacity, onUndeliveredElement = onUndeliveredElement) + } val isConflated get() = capacity == Channel.CONFLATED override fun toString(): String = description diff --git a/kotlinx-coroutines-core/common/test/flow/FlowInvariantsTest.kt b/kotlinx-coroutines-core/common/test/flow/FlowInvariantsTest.kt index ce93f1fdb2..b5f1bf7bb8 100644 --- a/kotlinx-coroutines-core/common/test/flow/FlowInvariantsTest.kt +++ b/kotlinx-coroutines-core/common/test/flow/FlowInvariantsTest.kt @@ -6,7 +6,6 @@ package kotlinx.coroutines.flow import kotlinx.coroutines.* import kotlinx.coroutines.channels.* -import kotlinx.coroutines.intrinsics.* import kotlin.coroutines.* import kotlin.reflect.* import kotlin.test.* @@ -193,7 +192,7 @@ class FlowInvariantsTest : TestBase() { } @Test - fun testEmptyCoroutineContext() = runTest { + fun testEmptyCoroutineContextMap() = runTest { emptyContextTest { map { expect(it) @@ -213,7 +212,18 @@ class FlowInvariantsTest : TestBase() { } @Test - fun testEmptyCoroutineContextViolation() = runTest { + fun testEmptyCoroutineContextTransformWhile() = runTest { + emptyContextTest { + transformWhile { + expect(it) + emit(it + 1) + true + } + } + } + + @Test + fun testEmptyCoroutineContextViolationTransform() = runTest { try { emptyContextTest { transform { @@ -230,6 +240,25 @@ class FlowInvariantsTest : TestBase() { } } + @Test + fun testEmptyCoroutineContextViolationTransformWhile() = runTest { + try { + emptyContextTest { + transformWhile { + expect(it) + withContext(Dispatchers.Unconfined) { + emit(it + 1) + } + true + } + } + expectUnreached() + } catch (e: IllegalStateException) { + assertTrue(e.message!!.contains("Flow invariant is violated")) + finish(2) + } + } + private suspend fun emptyContextTest(block: Flow.() -> Flow) { suspend fun collector(): Int { var result: Int = -1 @@ -243,16 +272,8 @@ class FlowInvariantsTest : TestBase() { return result } - val result = runSuspendFun { collector() } + val result = withEmptyContext { collector() } assertEquals(2, result) finish(3) } - - private suspend fun runSuspendFun(block: suspend () -> Int): Int { - val baseline = Result.failure(IllegalStateException("Block was suspended")) - var result: Result = baseline - block.startCoroutineUnintercepted(Continuation(EmptyCoroutineContext) { result = it }) - while (result == baseline) yield() - return result.getOrThrow() - } } diff --git a/kotlinx-coroutines-core/common/test/flow/VirtualTime.kt b/kotlinx-coroutines-core/common/test/flow/VirtualTime.kt index 9b257d933e..ddb1d88ae2 100644 --- a/kotlinx-coroutines-core/common/test/flow/VirtualTime.kt +++ b/kotlinx-coroutines-core/common/test/flow/VirtualTime.kt @@ -1,5 +1,5 @@ /* - * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ package kotlinx.coroutines @@ -7,11 +7,12 @@ package kotlinx.coroutines import kotlin.coroutines.* import kotlin.jvm.* -private class VirtualTimeDispatcher(enclosingScope: CoroutineScope) : CoroutineDispatcher(), Delay { - +internal class VirtualTimeDispatcher(enclosingScope: CoroutineScope) : CoroutineDispatcher(), Delay { private val originalDispatcher = enclosingScope.coroutineContext[ContinuationInterceptor] as CoroutineDispatcher private val heap = ArrayList() // TODO use MPP heap/ordered set implementation (commonize ThreadSafeHeap) - private var currentTime = 0L + + var currentTime = 0L + private set init { /* @@ -50,17 +51,20 @@ private class VirtualTimeDispatcher(enclosingScope: CoroutineScope) : CoroutineD @ExperimentalCoroutinesApi override fun isDispatchNeeded(context: CoroutineContext): Boolean = originalDispatcher.isDispatchNeeded(context) - override fun invokeOnTimeout(timeMillis: Long, block: Runnable): DisposableHandle { - val task = TimedTask(block, currentTime + timeMillis) + override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle { + val task = TimedTask(block, deadline(timeMillis)) heap += task return task } override fun scheduleResumeAfterDelay(timeMillis: Long, continuation: CancellableContinuation) { - val task = TimedTask(Runnable { with(continuation) { resumeUndispatched(Unit) } }, currentTime + timeMillis) + val task = TimedTask(Runnable { with(continuation) { resumeUndispatched(Unit) } }, deadline(timeMillis)) heap += task continuation.invokeOnCancellation { task.dispose() } } + + private fun deadline(timeMillis: Long) = + if (timeMillis == Long.MAX_VALUE) Long.MAX_VALUE else currentTime + timeMillis } /** diff --git a/kotlinx-coroutines-core/common/test/flow/operators/BufferConflationTest.kt b/kotlinx-coroutines-core/common/test/flow/operators/BufferConflationTest.kt new file mode 100644 index 0000000000..7b66977226 --- /dev/null +++ b/kotlinx-coroutines-core/common/test/flow/operators/BufferConflationTest.kt @@ -0,0 +1,146 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.flow + +import kotlinx.coroutines.* +import kotlinx.coroutines.channels.* +import kotlin.test.* + +/** + * A _behavioral_ test for conflation options that can be configured by the [buffer] operator to test that it is + * implemented properly and that adjacent [buffer] calls are fused properly. +*/ +class BufferConflationTest : TestBase() { + private val n = 100 // number of elements to emit for test + + private fun checkConflate( + capacity: Int, + onBufferOverflow: BufferOverflow = BufferOverflow.DROP_OLDEST, + op: suspend Flow.() -> Flow + ) = runTest { + expect(1) + // emit all and conflate, then collect first & last + val expectedList = when (onBufferOverflow) { + BufferOverflow.DROP_OLDEST -> listOf(0) + (n - capacity until n).toList() // first item & capacity last ones + BufferOverflow.DROP_LATEST -> (0..capacity).toList() // first & capacity following ones + else -> error("cannot happen") + } + flow { + repeat(n) { i -> + expect(i + 2) + emit(i) + } + } + .op() + .collect { i -> + val j = expectedList.indexOf(i) + expect(n + 2 + j) + } + finish(n + 2 + expectedList.size) + } + + @Test + fun testConflate() = + checkConflate(1) { + conflate() + } + + @Test + fun testBufferConflated() = + checkConflate(1) { + buffer(Channel.CONFLATED) + } + + @Test + fun testBufferDropOldest() = + checkConflate(1) { + buffer(onBufferOverflow = BufferOverflow.DROP_OLDEST) + } + + @Test + fun testBuffer0DropOldest() = + checkConflate(1) { + buffer(0, onBufferOverflow = BufferOverflow.DROP_OLDEST) + } + + @Test + fun testBuffer1DropOldest() = + checkConflate(1) { + buffer(1, onBufferOverflow = BufferOverflow.DROP_OLDEST) + } + + @Test + fun testBuffer10DropOldest() = + checkConflate(10) { + buffer(10, onBufferOverflow = BufferOverflow.DROP_OLDEST) + } + + @Test + fun testConflateOverridesBuffer() = + checkConflate(1) { + buffer(42).conflate() + } + + @Test // conflate().conflate() should work like a single conflate + fun testDoubleConflate() = + checkConflate(1) { + conflate().conflate() + } + + @Test + fun testConflateBuffer10Combine() = + checkConflate(10) { + conflate().buffer(10) + } + + @Test + fun testBufferDropLatest() = + checkConflate(1, BufferOverflow.DROP_LATEST) { + buffer(onBufferOverflow = BufferOverflow.DROP_LATEST) + } + + @Test + fun testBuffer0DropLatest() = + checkConflate(1, BufferOverflow.DROP_LATEST) { + buffer(0, onBufferOverflow = BufferOverflow.DROP_LATEST) + } + + @Test + fun testBuffer1DropLatest() = + checkConflate(1, BufferOverflow.DROP_LATEST) { + buffer(1, onBufferOverflow = BufferOverflow.DROP_LATEST) + } + + @Test // overrides previous buffer + fun testBufferDropLatestOverrideBuffer() = + checkConflate(1, BufferOverflow.DROP_LATEST) { + buffer(42).buffer(onBufferOverflow = BufferOverflow.DROP_LATEST) + } + + @Test // overrides previous conflate + fun testBufferDropLatestOverrideConflate() = + checkConflate(1, BufferOverflow.DROP_LATEST) { + conflate().buffer(onBufferOverflow = BufferOverflow.DROP_LATEST) + } + + @Test + fun testBufferDropLatestBuffer7Combine() = + checkConflate(7, BufferOverflow.DROP_LATEST) { + buffer(onBufferOverflow = BufferOverflow.DROP_LATEST).buffer(7) + } + + @Test + fun testConflateOverrideBufferDropLatest() = + checkConflate(1) { + buffer(onBufferOverflow = BufferOverflow.DROP_LATEST).conflate() + } + + @Test + fun testBuffer3DropOldestOverrideBuffer8DropLatest() = + checkConflate(3, BufferOverflow.DROP_OLDEST) { + buffer(8, onBufferOverflow = BufferOverflow.DROP_LATEST) + .buffer(3, BufferOverflow.DROP_OLDEST) + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/common/test/flow/operators/BufferTest.kt b/kotlinx-coroutines-core/common/test/flow/operators/BufferTest.kt index b68e115637..6352aacf41 100644 --- a/kotlinx-coroutines-core/common/test/flow/operators/BufferTest.kt +++ b/kotlinx-coroutines-core/common/test/flow/operators/BufferTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ package kotlinx.coroutines.flow @@ -9,13 +9,21 @@ import kotlinx.coroutines.channels.* import kotlin.math.* import kotlin.test.* +/** + * A _behavioral_ test for buffering that is introduced by the [buffer] operator to test that it is + * implemented properly and that adjacent [buffer] calls are fused properly. + */ class BufferTest : TestBase() { - private val n = 50 // number of elements to emit for test + private val n = 200 // number of elements to emit for test private val defaultBufferSize = 64 // expected default buffer size (per docs) // Use capacity == -1 to check case of "no buffer" private fun checkBuffer(capacity: Int, op: suspend Flow.() -> Flow) = runTest { expect(1) + /* + Channels perform full rendezvous. Sender does not suspend when there is a suspended receiver and vice-versa. + Thus, perceived batch size is +2 from capacity. + */ val batchSize = capacity + 2 flow { repeat(n) { i -> @@ -163,27 +171,6 @@ class BufferTest : TestBase() { .flowOn(wrapperDispatcher()).buffer(5) } - @Test - fun testConflate() = runTest { - expect(1) - // emit all and conflate / then collect first & last - flow { - repeat(n) { i -> - expect(i + 2) - emit(i) - } - } - .buffer(Channel.CONFLATED) - .collect { i -> - when (i) { - 0 -> expect(n + 2) // first value - n - 1 -> expect(n + 3) // last value - else -> error("Unexpected $i") - } - } - finish(n + 4) - } - @Test fun testCancellation() = runTest { val result = flow { diff --git a/kotlinx-coroutines-core/common/test/flow/operators/CancellableTest.kt b/kotlinx-coroutines-core/common/test/flow/operators/CancellableTest.kt new file mode 100644 index 0000000000..b1b14c4c40 --- /dev/null +++ b/kotlinx-coroutines-core/common/test/flow/operators/CancellableTest.kt @@ -0,0 +1,38 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.flow.operators + +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* +import kotlin.test.* + +class CancellableTest : TestBase() { + + @Test + fun testCancellable() = runTest { + var sum = 0 + val flow = (0..1000).asFlow() + .onEach { + if (it != 0) currentCoroutineContext().cancel() + sum += it + } + + flow.launchIn(this).join() + assertEquals(500500, sum) + + sum = 0 + flow.cancellable().launchIn(this).join() + assertEquals(1, sum) + } + + @Test + fun testFastPath() { + val flow = listOf(1).asFlow() + assertNotSame(flow, flow.cancellable()) + + val cancellableFlow = flow { emit(42) } + assertSame(cancellableFlow, cancellableFlow.cancellable()) + } +} diff --git a/kotlinx-coroutines-core/common/test/flow/operators/CatchTest.kt b/kotlinx-coroutines-core/common/test/flow/operators/CatchTest.kt index 802ba1ef2f..eedfac2ea3 100644 --- a/kotlinx-coroutines-core/common/test/flow/operators/CatchTest.kt +++ b/kotlinx-coroutines-core/common/test/flow/operators/CatchTest.kt @@ -134,15 +134,14 @@ class CatchTest : TestBase() { // flowOn with a different dispatcher introduces asynchrony so that all exceptions in the // upstream flows are handled before they go downstream .onEach { value -> - expect(8) - assertEquals("OK", value) + expectUnreached() // already cancelled } .catch { e -> - expect(9) + expect(8) assertTrue(e is TestException) assertSame(d0, kotlin.coroutines.coroutineContext[ContinuationInterceptor] as CoroutineContext) } .collect() - finish(10) + finish(9) } } diff --git a/kotlinx-coroutines-core/common/test/flow/operators/CombineParametersTestBase.kt b/kotlinx-coroutines-core/common/test/flow/operators/CombineParametersTestBase.kt index b51197e395..8c65ea4fe7 100644 --- a/kotlinx-coroutines-core/common/test/flow/operators/CombineParametersTestBase.kt +++ b/kotlinx-coroutines-core/common/test/flow/operators/CombineParametersTestBase.kt @@ -161,4 +161,33 @@ class CombineParametersTest : TestBase() { }.singleOrNull() assertNull(value) } + + @Test + fun testFairnessInVariousConfigurations() = runTest { + // Test various configurations + for (flowsCount in 2..5) { + for (flowSize in 1..5) { + val flows = List(flowsCount) { (1..flowSize).asFlow() } + val combined = combine(flows) { it.joinToString(separator = "") }.toList() + val expected = List(flowSize) { (it + 1).toString().repeat(flowsCount) } + assertEquals(expected, combined, "Count: $flowsCount, size: $flowSize") + } + } + } + + @Test + fun testEpochOverflow() = runTest { + val flow = (0..1023).asFlow() + val result = flow.combine(flow) { a, b -> a + b }.toList() + assertEquals(List(1024) { it * 2 } , result) + } + + @Test + fun testArrayType() = runTest { + val arr = flowOf(1) + combine(listOf(arr, arr)) { + println(it[0]) + it[0] + }.toList().also { println(it) } + } } diff --git a/kotlinx-coroutines-core/common/test/flow/operators/CombineTest.kt b/kotlinx-coroutines-core/common/test/flow/operators/CombineTest.kt index a619355b68..5e2926d082 100644 --- a/kotlinx-coroutines-core/common/test/flow/operators/CombineTest.kt +++ b/kotlinx-coroutines-core/common/test/flow/operators/CombineTest.kt @@ -1,10 +1,11 @@ /* - * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ - -package kotlinx.coroutines.flow +@file:Suppress("UNCHECKED_CAST") +package kotlinx.coroutines.flow.operators import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* import kotlin.test.* import kotlinx.coroutines.flow.combine as combineOriginal import kotlinx.coroutines.flow.combineTransform as combineTransformOriginal @@ -21,28 +22,28 @@ abstract class CombineTestBase : TestBase() { val flow = flowOf("a", "b", "c") val flow2 = flowOf(1, 2, 3) val list = flow.combineLatest(flow2) { i, j -> i + j }.toList() - assertEquals(listOf("a1", "b1", "b2", "c2", "c3"), list) + assertEquals(listOf("a1", "b2", "c3"), list) } @Test fun testNulls() = runTest { val flow = flowOf("a", null, null) val flow2 = flowOf(1, 2, 3) - val list = flow.combineLatest(flow2, { i, j -> i + j }).toList() - assertEquals(listOf("a1", "null1", "null2", "null2", "null3"), list) + val list = flow.combineLatest(flow2) { i, j -> i + j }.toList() + assertEquals(listOf("a1", "null2", "null3"), list) } @Test fun testNullsOther() = runTest { val flow = flowOf("a", "b", "c") val flow2 = flowOf(null, 2, null) - val list = flow.combineLatest(flow2, { i, j -> i + j }).toList() - assertEquals(listOf("anull", "bnull", "b2", "c2", "cnull"), list) + val list = flow.combineLatest(flow2) { i, j -> i + j }.toList() + assertEquals(listOf("anull", "b2", "cnull"), list) } @Test fun testEmptyFlow() = runTest { - val flow = emptyFlow().combineLatest(emptyFlow(), { i, j -> i + j }) + val flow = emptyFlow().combineLatest(emptyFlow()) { i, j -> i + j } assertNull(flow.singleOrNull()) } @@ -208,12 +209,12 @@ abstract class CombineTestBase : TestBase() { } val f2 = flow { emit(1) - hang { expect(3) } + expectUnreached() } - val flow = f1.combineLatest(f2, { _, _ -> 1 }).onEach { expect(2) } + val flow = f1.combineLatest(f2) { _, _ -> 1 }.onEach { expect(2) } assertFailsWith(flow) - finish(4) + finish(3) } @Test @@ -229,7 +230,7 @@ abstract class CombineTestBase : TestBase() { hang { expect(6) } } - val flow = f1.combineLatest(f2, { _, _ -> 1 }).onEach { + val flow = f1.combineLatest(f2) { _, _ -> 1 }.onEach { expect(1) yield() expect(4) @@ -238,6 +239,22 @@ abstract class CombineTestBase : TestBase() { assertFailsWith(flow) finish(7) } + + @Test + fun testCancelledCombine() = runTest( + expected = { it is CancellationException } + ) { + coroutineScope { + val flow = flow { + emit(Unit) // emit + } + cancel() // cancel the scope + flow.combineLatest(flow) { _, _ -> }.collect { + // should not be reached, because cancelled before it runs + expectUnreached() + } + } + } } class CombineTest : CombineTestBase() { @@ -249,15 +266,26 @@ class CombineTransformTest : CombineTestBase() { emit(transform(a, b)) } } +// Array null-out is an additional test for our array elimination optimization class CombineVarargAdapterTest : CombineTestBase() { override fun Flow.combineLatest(other: Flow, transform: suspend (T1, T2) -> R): Flow = - combineOriginal(this, other) { args: Array -> transform(args[0] as T1, args[1] as T2) } + combineOriginal(this, other) { args: Array -> + transform(args[0] as T1, args[1] as T2).also { + args[0] = null + args[1] = null + } + } } class CombineIterableTest : CombineTestBase() { override fun Flow.combineLatest(other: Flow, transform: suspend (T1, T2) -> R): Flow = - combineOriginal(listOf(this, other)) { args -> transform(args[0] as T1, args[1] as T2) } + combineOriginal(listOf(this, other)) { args -> + transform(args[0] as T1, args[1] as T2).also { + args[0] = null + args[1] = null + } + } } class CombineTransformAdapterTest : CombineTestBase() { @@ -267,11 +295,20 @@ class CombineTransformAdapterTest : CombineTestBase() { class CombineTransformVarargAdapterTest : CombineTestBase() { override fun Flow.combineLatest(other: Flow, transform: suspend (T1, T2) -> R): Flow = - combineTransformOriginal(this, other) { args: Array -> emit(transform(args[0] as T1, args[1] as T2)) } + combineTransformOriginal(this, other) { args: Array -> + emit(transform(args[0] as T1, args[1] as T2)) // Mess up with array + args[0] = null + args[1] = null + } } class CombineTransformIterableTest : CombineTestBase() { override fun Flow.combineLatest(other: Flow, transform: suspend (T1, T2) -> R): Flow = - combineTransformOriginal(listOf(this, other)) { args -> emit(transform(args[0] as T1, args[1] as T2)) } + combineTransformOriginal(listOf(this, other)) { args -> + emit(transform(args[0] as T1, args[1] as T2)) + // Mess up with array + args[0] = null + args[1] = null + } } diff --git a/kotlinx-coroutines-core/common/test/flow/operators/DebounceTest.kt b/kotlinx-coroutines-core/common/test/flow/operators/DebounceTest.kt index 4065671e3d..ce75e598e9 100644 --- a/kotlinx-coroutines-core/common/test/flow/operators/DebounceTest.kt +++ b/kotlinx-coroutines-core/common/test/flow/operators/DebounceTest.kt @@ -11,7 +11,7 @@ import kotlin.time.* class DebounceTest : TestBase() { @Test - public fun testBasic() = withVirtualTime { + fun testBasic() = withVirtualTime { expect(1) val flow = flow { expect(3) @@ -159,7 +159,7 @@ class DebounceTest : TestBase() { expect(2) throw TestException() }.flowOn(NamedDispatchers("source")).debounce(Long.MAX_VALUE).map { - expectUnreached() + expectUnreached() } assertFailsWith(flow) finish(3) @@ -175,7 +175,6 @@ class DebounceTest : TestBase() { expect(2) yield() throw TestException() - it } assertFailsWith(flow) @@ -193,7 +192,6 @@ class DebounceTest : TestBase() { expect(2) yield() throw TestException() - it } assertFailsWith(flow) @@ -202,7 +200,7 @@ class DebounceTest : TestBase() { @ExperimentalTime @Test - public fun testDurationBasic() = withVirtualTime { + fun testDurationBasic() = withVirtualTime { expect(1) val flow = flow { expect(3) @@ -223,4 +221,102 @@ class DebounceTest : TestBase() { assertEquals(listOf("A", "D", "E"), result) finish(5) } + + @ExperimentalTime + @Test + fun testDebounceSelectorBasic() = withVirtualTime { + expect(1) + val flow = flow { + expect(3) + emit(1) + delay(90) + emit(2) + delay(90) + emit(3) + delay(1010) + emit(4) + delay(1010) + emit(5) + expect(4) + } + + expect(2) + val result = flow.debounce { + if (it == 1) { + 0 + } else { + 1000 + } + }.toList() + + assertEquals(listOf(1, 3, 4, 5), result) + finish(5) + } + + @Test + fun testZeroDebounceTime() = withVirtualTime { + expect(1) + val flow = flow { + expect(3) + emit("A") + emit("B") + emit("C") + expect(4) + } + + expect(2) + val result = flow.debounce(0).toList() + + assertEquals(listOf("A", "B", "C"), result) + finish(5) + } + + @ExperimentalTime + @Test + fun testZeroDebounceTimeSelector() = withVirtualTime { + expect(1) + val flow = flow { + expect(3) + emit("A") + emit("B") + expect(4) + } + + expect(2) + val result = flow.debounce { 0 }.toList() + + assertEquals(listOf("A", "B"), result) + finish(5) + } + + @ExperimentalTime + @Test + fun testDebounceDurationSelectorBasic() = withVirtualTime { + expect(1) + val flow = flow { + expect(3) + emit("A") + delay(1500.milliseconds) + emit("B") + delay(500.milliseconds) + emit("C") + delay(250.milliseconds) + emit("D") + delay(2000.milliseconds) + emit("E") + expect(4) + } + + expect(2) + val result = flow.debounce { + if (it == "C") { + 0.milliseconds + } else { + 1000.milliseconds + } + }.toList() + + assertEquals(listOf("A", "C", "D", "E"), result) + finish(5) + } } diff --git a/kotlinx-coroutines-core/common/test/flow/operators/DistinctUntilChangedTest.kt b/kotlinx-coroutines-core/common/test/flow/operators/DistinctUntilChangedTest.kt index fc03d367c6..68e7f66b9d 100644 --- a/kotlinx-coroutines-core/common/test/flow/operators/DistinctUntilChangedTest.kt +++ b/kotlinx-coroutines-core/common/test/flow/operators/DistinctUntilChangedTest.kt @@ -94,4 +94,33 @@ class DistinctUntilChangedTest : TestBase() { val flow = flowOf(null, 1, null, null).distinctUntilChanged() assertEquals(listOf(null, 1, null), flow.toList()) } + + @Test + fun testRepeatedDistinctFusionDefault() = testRepeatedDistinctFusion { + distinctUntilChanged() + } + + // A separate variable is needed for K/N that does not optimize non-captured lambdas (yet) + private val areEquivalentTestFun: (old: Int, new: Int) -> Boolean = { old, new -> old == new } + + @Test + fun testRepeatedDistinctFusionAreEquivalent() = testRepeatedDistinctFusion { + distinctUntilChanged(areEquivalentTestFun) + } + + // A separate variable is needed for K/N that does not optimize non-captured lambdas (yet) + private val keySelectorTestFun: (Int) -> Int = { it % 2 } + + @Test + fun testRepeatedDistinctFusionByKey() = testRepeatedDistinctFusion { + distinctUntilChangedBy(keySelectorTestFun) + } + + private fun testRepeatedDistinctFusion(op: Flow.() -> Flow) = runTest { + val flow = (1..10).asFlow() + val d1 = flow.op() + assertNotSame(flow, d1) + val d2 = d1.op() + assertSame(d1, d2) + } } diff --git a/kotlinx-coroutines-core/common/test/flow/operators/FlatMapMergeTest.kt b/kotlinx-coroutines-core/common/test/flow/operators/FlatMapMergeTest.kt index 684923c861..7470289ece 100644 --- a/kotlinx-coroutines-core/common/test/flow/operators/FlatMapMergeTest.kt +++ b/kotlinx-coroutines-core/common/test/flow/operators/FlatMapMergeTest.kt @@ -58,6 +58,7 @@ class FlatMapMergeTest : FlatMapMergeBaseTest() { } launch { expect(2) + yield() job.cancel() } } diff --git a/kotlinx-coroutines-core/common/test/flow/operators/FlowOnTest.kt b/kotlinx-coroutines-core/common/test/flow/operators/FlowOnTest.kt index f8350ff584..68653281cc 100644 --- a/kotlinx-coroutines-core/common/test/flow/operators/FlowOnTest.kt +++ b/kotlinx-coroutines-core/common/test/flow/operators/FlowOnTest.kt @@ -341,4 +341,20 @@ class FlowOnTest : TestBase() { assertEquals(expected, value) } } + + @Test + fun testCancelledFlowOn() = runTest { + assertFailsWith { + coroutineScope { + val scope = this + flow { + emit(Unit) // emit to buffer + scope.cancel() // now cancel outer scope + }.flowOn(wrapperDispatcher()).collect { + // should not be reached, because cancelled before it runs + expectUnreached() + } + } + } + } } diff --git a/kotlinx-coroutines-core/common/test/flow/operators/OnCompletionTest.kt b/kotlinx-coroutines-core/common/test/flow/operators/OnCompletionTest.kt index c079500ef7..f55e8beeb2 100644 --- a/kotlinx-coroutines-core/common/test/flow/operators/OnCompletionTest.kt +++ b/kotlinx-coroutines-core/common/test/flow/operators/OnCompletionTest.kt @@ -50,7 +50,7 @@ class OnCompletionTest : TestBase() { }.onEach { expect(2) }.onCompletion { - assertNull(it) + assertTrue(it is TestException) // flow fails because of this exception expect(4) }.onEach { expect(3) @@ -65,7 +65,7 @@ class OnCompletionTest : TestBase() { @Test fun testMultipleOnCompletions() = runTest { flowOf(1).onCompletion { - assertNull(it) + assertTrue(it is TestException) expect(2) }.onEach { expect(1) @@ -145,12 +145,12 @@ class OnCompletionTest : TestBase() { .onCompletion { e -> expect(8) assertTrue(e is TestException) - emit(TestData.Done(e)) + emit(TestData.Done(e)) // will fail }.collect { collected += it } } - val expected = (1..5).map { TestData.Value(it) } + TestData.Done(TestException("OK")) + val expected: List = (1..5).map { TestData.Value(it) } assertEquals(expected, collected) finish(9) } @@ -171,7 +171,7 @@ class OnCompletionTest : TestBase() { } .onCompletion { e -> expect(8) - assertNull(e) + assertTrue(e is CancellationException) try { emit(TestData.Done(e)) expectUnreached() @@ -197,7 +197,7 @@ class OnCompletionTest : TestBase() { emit(TestData.Value(2)) expectUnreached() }.onCompletion { - assertNull(it) + assertSame(cause, it) // flow failed because of the exception in downstream expect(3) try { emit(TestData.Done(it)) @@ -216,7 +216,7 @@ class OnCompletionTest : TestBase() { @Test fun testFirst() = runTest { val value = flowOf(239).onCompletion { - assertNull(it) + assertNotNull(it) // the flow did not complete normally expect(1) try { emit(42) @@ -231,7 +231,7 @@ class OnCompletionTest : TestBase() { @Test fun testSingle() = runTest { - assertFailsWith { + assertFailsWith { flowOf(239).onCompletion { assertNull(it) expect(1) @@ -240,7 +240,7 @@ class OnCompletionTest : TestBase() { expectUnreached() } catch (e: Throwable) { // Second emit -- failure - assertTrue { e is IllegalStateException } + assertTrue { e is IllegalArgumentException } throw e } }.single() @@ -259,4 +259,35 @@ class OnCompletionTest : TestBase() { assertEquals(42, value) finish(2) } + + @Test + fun testTransparencyViolation() = runTest { + val flow = emptyFlow().onCompletion { + expect(2) + coroutineScope { + launch { + try { + emit(1) + } catch (e: IllegalStateException) { + expect(3) + } + } + } + } + expect(1) + assertNull(flow.singleOrNull()) + finish(4) + } + + @Test + fun testTakeOnCompletion() = runTest { + // even though it uses "take" from the outside it completes normally + val flow = (1..10).asFlow().take(5) + val result = flow.onCompletion { cause -> + assertNull(cause) + emit(-1) + }.toList() + val expected = (1..5).toList() + (-1) + assertEquals(expected, result) + } } diff --git a/kotlinx-coroutines-core/common/test/flow/operators/OnEmptyTest.kt b/kotlinx-coroutines-core/common/test/flow/operators/OnEmptyTest.kt new file mode 100644 index 0000000000..3da166645b --- /dev/null +++ b/kotlinx-coroutines-core/common/test/flow/operators/OnEmptyTest.kt @@ -0,0 +1,81 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.flow.operators + +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* +import kotlin.test.* + +class OnEmptyTest : TestBase() { + + @Test + fun testOnEmptyInvoked() = runTest { + val flow = emptyFlow().onEmpty { emit(1) } + assertEquals(1, flow.single()) + } + + @Test + fun testOnEmptyNotInvoked() = runTest { + val flow = flowOf(1).onEmpty { emit(2) } + assertEquals(1, flow.single()) + } + + @Test + fun testOnEmptyNotInvokedOnError() = runTest { + val flow = flow { + throw TestException() + }.onEmpty { expectUnreached() } + assertFailsWith(flow) + } + + @Test + fun testOnEmptyNotInvokedOnCancellation() = runTest { + val flow = flow { + expect(2) + hang { expect(4) } + }.onEmpty { expectUnreached() } + + expect(1) + val job = flow.onEach { expectUnreached() }.launchIn(this) + yield() + expect(3) + job.cancelAndJoin() + finish(5) + } + + @Test + fun testOnEmptyCancellation() = runTest { + val flow = emptyFlow().onEmpty { + expect(2) + hang { expect(4) } + emit(1) + } + expect(1) + val job = flow.onEach { expectUnreached() }.launchIn(this) + yield() + expect(3) + job.cancelAndJoin() + finish(5) + } + + @Test + fun testTransparencyViolation() = runTest { + val flow = emptyFlow().onEmpty { + expect(2) + coroutineScope { + launch { + try { + emit(1) + } catch (e: IllegalStateException) { + expect(3) + } + } + } + } + expect(1) + assertNull(flow.singleOrNull()) + finish(4) + } +} diff --git a/kotlinx-coroutines-core/common/test/flow/operators/OnStartTest.kt b/kotlinx-coroutines-core/common/test/flow/operators/OnStartTest.kt index a0981ab042..0443e56a2c 100644 --- a/kotlinx-coroutines-core/common/test/flow/operators/OnStartTest.kt +++ b/kotlinx-coroutines-core/common/test/flow/operators/OnStartTest.kt @@ -14,4 +14,23 @@ class OnStartTest : TestBase() { .onStart { emit("Begin") } assertEquals(listOf("Begin", "a", "b", "c"), flow.toList()) } -} \ No newline at end of file + + @Test + fun testTransparencyViolation() = runTest { + val flow = emptyFlow().onStart { + expect(2) + coroutineScope { + launch { + try { + emit(1) + } catch (e: IllegalStateException) { + expect(3) + } + } + } + } + expect(1) + assertNull(flow.singleOrNull()) + finish(4) + } +} diff --git a/kotlinx-coroutines-core/common/test/flow/operators/ScanTest.kt b/kotlinx-coroutines-core/common/test/flow/operators/ScanTest.kt index 0108ea174e..20e07873bb 100644 --- a/kotlinx-coroutines-core/common/test/flow/operators/ScanTest.kt +++ b/kotlinx-coroutines-core/common/test/flow/operators/ScanTest.kt @@ -12,7 +12,7 @@ class ScanTest : TestBase() { @Test fun testScan() = runTest { val flow = flowOf(1, 2, 3, 4, 5) - val result = flow.scanReduce { acc, v -> acc + v }.toList() + val result = flow.runningReduce { acc, v -> acc + v }.toList() assertEquals(listOf(1, 3, 6, 10, 15), result) } @@ -26,13 +26,13 @@ class ScanTest : TestBase() { @Test fun testNulls() = runTest { val flow = flowOf(null, 2, null, null, null, 5) - val result = flow.scanReduce { acc, v -> if (v == null) acc else (if (acc == null) v else acc + v) }.toList() + val result = flow.runningReduce { acc, v -> if (v == null) acc else (if (acc == null) v else acc + v) }.toList() assertEquals(listOf(null, 2, 2, 2, 2, 7), result) } @Test fun testEmptyFlow() = runTest { - val result = emptyFlow().scanReduce { _, _ -> 1 }.toList() + val result = emptyFlow().runningReduce { _, _ -> 1 }.toList() assertTrue(result.isEmpty()) } @@ -49,7 +49,7 @@ class ScanTest : TestBase() { emit(1) emit(2) } - }.scanReduce { _, value -> + }.runningReduce { _, value -> expect(value) // 2 latch.receive() throw TestException() @@ -59,7 +59,7 @@ class ScanTest : TestBase() { finish(4) } - public operator fun Collection.plus(element: T): List { + private operator fun Collection.plus(element: T): List { val result = ArrayList(size + 1) result.addAll(this) result.add(element) diff --git a/kotlinx-coroutines-core/common/test/flow/operators/TransformWhileTest.kt b/kotlinx-coroutines-core/common/test/flow/operators/TransformWhileTest.kt new file mode 100644 index 0000000000..df660103c3 --- /dev/null +++ b/kotlinx-coroutines-core/common/test/flow/operators/TransformWhileTest.kt @@ -0,0 +1,70 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.flow + +import kotlinx.coroutines.* +import kotlin.test.* + +class TransformWhileTest : TestBase() { + @Test + fun testSimple() = runTest { + val flow = (0..10).asFlow() + val expected = listOf("A", "B", "C", "D") + val actual = flow.transformWhile { value -> + when(value) { + 0 -> { emit("A"); true } + 1 -> true + 2 -> { emit("B"); emit("C"); true } + 3 -> { emit("D"); false } + else -> { expectUnreached(); false } + } + }.toList() + assertEquals(expected, actual) + } + + @Test + fun testCancelUpstream() = runTest { + var cancelled = false + val flow = flow { + coroutineScope { + launch(start = CoroutineStart.ATOMIC) { + hang { cancelled = true } + } + emit(1) + emit(2) + emit(3) + } + } + val transformed = flow.transformWhile { + emit(it) + it < 2 + } + assertEquals(listOf(1, 2), transformed.toList()) + assertTrue(cancelled) + } + + @Test + fun testExample() = runTest { + val source = listOf( + DownloadProgress(0), + DownloadProgress(50), + DownloadProgress(100), + DownloadProgress(147) + ) + val expected = source.subList(0, 3) + val actual = source.asFlow().completeWhenDone().toList() + assertEquals(expected, actual) + } + + private fun Flow.completeWhenDone(): Flow = + transformWhile { progress -> + emit(progress) // always emit progress + !progress.isDone() // continue while download is not done + } + + private data class DownloadProgress(val percent: Int) { + fun isDone() = percent >= 100 + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/common/test/flow/operators/ZipTest.kt b/kotlinx-coroutines-core/common/test/flow/operators/ZipTest.kt index b28320c391..02dbfc40d9 100644 --- a/kotlinx-coroutines-core/common/test/flow/operators/ZipTest.kt +++ b/kotlinx-coroutines-core/common/test/flow/operators/ZipTest.kt @@ -23,7 +23,7 @@ class ZipTest : TestBase() { fun testUnevenZip() = runTest { val f1 = flowOf("a", "b", "c", "d", "e") val f2 = flowOf(1, 2, 3) - assertEquals(listOf("a1", "b2", "c3"), f1.zip(f2, { i, j -> i + j }).toList()) + assertEquals(listOf("a1", "b2", "c3"), f1.zip(f2) { i, j -> i + j }.toList()) assertEquals(listOf("a1", "b2", "c3"), f2.zip(f1) { i, j -> j + i }.toList()) } @@ -67,47 +67,52 @@ class ZipTest : TestBase() { val f1 = flow { emit("1") emit("2") - hang { - expect(1) - } } - val f2 = flowOf("a", "b") + val f2 = flow { + emit("a") + emit("b") + expectUnreached() + } assertEquals(listOf("1a", "2b"), f1.zip(f2) { s1, s2 -> s1 + s2 }.toList()) - finish(2) + finish(1) } @Test - fun testCancelWhenFlowIsDoneReversed() = runTest { + fun testCancelWhenFlowIsDone2() = runTest { val f1 = flow { emit("1") emit("2") - hang { + try { + emit("3") + expectUnreached() + } finally { expect(1) } } val f2 = flowOf("a", "b") - assertEquals(listOf("a1", "b2"), f2.zip(f1) { s1, s2 -> s1 + s2 }.toList()) + assertEquals(listOf("1a", "2b"), f1.zip(f2) { s1, s2 -> s1 + s2 }.toList()) finish(2) } @Test - fun testCancelWhenFlowIsDone2() = runTest { + fun testCancelWhenFlowIsDoneReversed() = runTest { val f1 = flow { emit("1") emit("2") - try { - emit("3") - expectUnreached() - } finally { + hang { expect(1) } + } + val f2 = flow { + emit("a") + emit("b") + yield() } - val f2 = flowOf("a", "b") - assertEquals(listOf("1a", "2b"), f1.zip(f2) { s1, s2 -> s1 + s2 }.toList()) + assertEquals(listOf("a1", "b2"), f2.zip(f1) { s1, s2 -> s1 + s2 }.toList()) finish(2) } @@ -116,19 +121,19 @@ class ZipTest : TestBase() { val f1 = flow { emit("a") assertEquals("first", NamedDispatchers.name()) - expect(1) + expect(3) }.flowOn(NamedDispatchers("first")).onEach { assertEquals("with", NamedDispatchers.name()) - expect(2) + expect(4) }.flowOn(NamedDispatchers("with")) val f2 = flow { emit(1) assertEquals("second", NamedDispatchers.name()) - expect(3) + expect(1) }.flowOn(NamedDispatchers("second")).onEach { assertEquals("nested", NamedDispatchers.name()) - expect(4) + expect(2) }.flowOn(NamedDispatchers("nested")) val value = withContext(NamedDispatchers("main")) { @@ -148,14 +153,14 @@ class ZipTest : TestBase() { val f1 = flow { emit("a") hang { - expect(2) + expect(3) } }.flowOn(NamedDispatchers("first")) val f2 = flow { emit(1) hang { - expect(3) + expect(2) } }.flowOn(NamedDispatchers("second")) @@ -195,19 +200,18 @@ class ZipTest : TestBase() { val f1 = flow { expect(1) emit(1) - yield() - expect(4) + expect(5) throw CancellationException("") } val f2 = flow { expect(2) emit(1) - expect(5) + expect(3) hang { expect(6) } } - val flow = f1.zip(f2, { _, _ -> 1 }).onEach { expect(3) } + val flow = f1.zip(f2) { _, _ -> 1 }.onEach { expect(4) } assertFailsWith(flow) finish(7) } @@ -217,24 +221,37 @@ class ZipTest : TestBase() { val f1 = flow { expect(1) emit(1) - yield() - expect(4) - hang { expect(6) } + expectUnreached() // Will throw CE } val f2 = flow { expect(2) emit(1) - expect(5) - hang { expect(7) } + expect(3) + hang { expect(5) } } val flow = f1.zip(f2, { _, _ -> 1 }).onEach { - expect(3) + expect(4) yield() throw CancellationException("") } assertFailsWith(flow) - finish(8) + finish(6) + } + + @Test + fun testCancellationOfCollector() = runTest { + val f1 = flow { + emit("1") + awaitCancellation() + } + + val f2 = flow { + emit("2") + yield() + } + + f1.zip(f2) { a, b -> a + b }.collect { } } } diff --git a/kotlinx-coroutines-core/common/test/flow/sharing/ShareInBufferTest.kt b/kotlinx-coroutines-core/common/test/flow/sharing/ShareInBufferTest.kt new file mode 100644 index 0000000000..9c6aed211a --- /dev/null +++ b/kotlinx-coroutines-core/common/test/flow/sharing/ShareInBufferTest.kt @@ -0,0 +1,98 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.flow + +import kotlinx.coroutines.* +import kotlin.math.* +import kotlin.test.* + +/** + * Similar to [BufferTest], but tests [shareIn] buffering and its fusion with [buffer] operators. + */ +class ShareInBufferTest : TestBase() { + private val n = 200 // number of elements to emit for test + private val defaultBufferSize = 64 // expected default buffer size (per docs) + + // Use capacity == -1 to check case of "no buffer" + private fun checkBuffer(capacity: Int, op: suspend Flow.(CoroutineScope) -> Flow) = runTest { + expect(1) + /* + Shared flows do not perform full rendezvous. On buffer overflow emitter always suspends until all + subscribers get the value and then resumes. Thus, perceived batch size is +1 from buffer capacity. + */ + val batchSize = capacity + 1 + val upstream = flow { + repeat(n) { i -> + val batchNo = i / batchSize + val batchIdx = i % batchSize + expect(batchNo * batchSize * 2 + batchIdx + 2) + emit(i) + } + emit(-1) // done + } + coroutineScope { + upstream + .op(this) + .takeWhile { i -> i >= 0 } // until done + .collect { i -> + val batchNo = i / batchSize + val batchIdx = i % batchSize + // last batch might have smaller size + val k = min((batchNo + 1) * batchSize, n) - batchNo * batchSize + expect(batchNo * batchSize * 2 + k + batchIdx + 2) + } + coroutineContext.cancelChildren() // cancels sharing + } + finish(2 * n + 2) + } + + @Test + fun testReplay0DefaultBuffer() = + checkBuffer(defaultBufferSize) { + shareIn(it, SharingStarted.Eagerly) + } + + @Test + fun testReplay1DefaultBuffer() = + checkBuffer(defaultBufferSize) { + shareIn(it, SharingStarted.Eagerly, 1) + } + + @Test // buffer is padded to default size as needed + fun testReplay10DefaultBuffer() = + checkBuffer(maxOf(10, defaultBufferSize)) { + shareIn(it, SharingStarted.Eagerly, 10) + } + + @Test // buffer is padded to default size as needed + fun testReplay100DefaultBuffer() = + checkBuffer( maxOf(100, defaultBufferSize)) { + shareIn(it, SharingStarted.Eagerly, 100) + } + + @Test + fun testDefaultBufferKeepsDefault() = + checkBuffer(defaultBufferSize) { + buffer().shareIn(it, SharingStarted.Eagerly) + } + + @Test + fun testOverrideDefaultBuffer0() = + checkBuffer(0) { + buffer(0).shareIn(it, SharingStarted.Eagerly) + } + + @Test + fun testOverrideDefaultBuffer10() = + checkBuffer(10) { + buffer(10).shareIn(it, SharingStarted.Eagerly) + } + + @Test // buffer and replay sizes add up + fun testBufferReplaySum() = + checkBuffer(41) { + buffer(10).buffer(20).shareIn(it, SharingStarted.Eagerly, 11) + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/common/test/flow/sharing/ShareInConflationTest.kt b/kotlinx-coroutines-core/common/test/flow/sharing/ShareInConflationTest.kt new file mode 100644 index 0000000000..0528e97e7d --- /dev/null +++ b/kotlinx-coroutines-core/common/test/flow/sharing/ShareInConflationTest.kt @@ -0,0 +1,162 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.flow + +import kotlinx.coroutines.* +import kotlinx.coroutines.channels.* +import kotlin.test.* + +/** + * Similar to [ShareInBufferTest] and [BufferConflationTest], + * but tests [shareIn] and its fusion with [conflate] operator. + */ +class ShareInConflationTest : TestBase() { + private val n = 100 + + private fun checkConflation( + bufferCapacity: Int, + onBufferOverflow: BufferOverflow = BufferOverflow.DROP_OLDEST, + op: suspend Flow.(CoroutineScope) -> Flow + ) = runTest { + expect(1) + // emit all and conflate, then should collect bufferCapacity latest ones + val done = Job() + flow { + repeat(n) { i -> + expect(i + 2) + emit(i) + } + done.join() // wait until done collection + emit(-1) // signal flow completion + } + .op(this) + .takeWhile { i -> i >= 0 } + .collect { i -> + val first = if (onBufferOverflow == BufferOverflow.DROP_LATEST) 0 else n - bufferCapacity + val last = first + bufferCapacity - 1 + if (i in first..last) { + expect(n + i - first + 2) + if (i == last) done.complete() // received the last one + } else { + error("Unexpected $i") + } + } + finish(n + bufferCapacity + 2) + } + + @Test + fun testConflateReplay1() = + checkConflation(1) { + conflate().shareIn(it, SharingStarted.Eagerly, 1) + } + + @Test // still looks like conflating the last value for the first subscriber (will not replay to others though) + fun testConflateReplay0() = + checkConflation(1) { + conflate().shareIn(it, SharingStarted.Eagerly, 0) + } + + @Test + fun testConflateReplay5() = + checkConflation(5) { + conflate().shareIn(it, SharingStarted.Eagerly, 5) + } + + @Test + fun testBufferDropOldestReplay1() = + checkConflation(1) { + buffer(onBufferOverflow = BufferOverflow.DROP_OLDEST).shareIn(it, SharingStarted.Eagerly, 1) + } + + @Test + fun testBufferDropOldestReplay0() = + checkConflation(1) { + buffer(onBufferOverflow = BufferOverflow.DROP_OLDEST).shareIn(it, SharingStarted.Eagerly, 0) + } + + @Test + fun testBufferDropOldestReplay10() = + checkConflation(10) { + buffer(onBufferOverflow = BufferOverflow.DROP_OLDEST).shareIn(it, SharingStarted.Eagerly, 10) + } + + @Test + fun testBuffer20DropOldestReplay0() = + checkConflation(20) { + buffer(20, onBufferOverflow = BufferOverflow.DROP_OLDEST).shareIn(it, SharingStarted.Eagerly, 0) + } + + @Test + fun testBuffer7DropOldestReplay11() = + checkConflation(18) { + buffer(7, onBufferOverflow = BufferOverflow.DROP_OLDEST).shareIn(it, SharingStarted.Eagerly, 11) + } + + @Test // a preceding buffer() gets overridden by conflate() + fun testBufferConflateOverride() = + checkConflation(1) { + buffer(23).conflate().shareIn(it, SharingStarted.Eagerly, 1) + } + + @Test // a preceding buffer() gets overridden by buffer(onBufferOverflow = BufferOverflow.DROP_OLDEST) + fun testBufferDropOldestOverride() = + checkConflation(1) { + buffer(23).buffer(onBufferOverflow = BufferOverflow.DROP_OLDEST).shareIn(it, SharingStarted.Eagerly, 1) + } + + @Test + fun testBufferDropLatestReplay0() = + checkConflation(1, BufferOverflow.DROP_LATEST) { + buffer(onBufferOverflow = BufferOverflow.DROP_LATEST).shareIn(it, SharingStarted.Eagerly, 0) + } + + @Test + fun testBufferDropLatestReplay1() = + checkConflation(1, BufferOverflow.DROP_LATEST) { + buffer(onBufferOverflow = BufferOverflow.DROP_LATEST).shareIn(it, SharingStarted.Eagerly, 1) + } + + @Test + fun testBufferDropLatestReplay10() = + checkConflation(10, BufferOverflow.DROP_LATEST) { + buffer(onBufferOverflow = BufferOverflow.DROP_LATEST).shareIn(it, SharingStarted.Eagerly, 10) + } + + @Test + fun testBuffer0DropLatestReplay0() = + checkConflation(1, BufferOverflow.DROP_LATEST) { + buffer(0, onBufferOverflow = BufferOverflow.DROP_LATEST).shareIn(it, SharingStarted.Eagerly, 0) + } + + @Test + fun testBuffer0DropLatestReplay1() = + checkConflation(1, BufferOverflow.DROP_LATEST) { + buffer(0, onBufferOverflow = BufferOverflow.DROP_LATEST).shareIn(it, SharingStarted.Eagerly, 1) + } + + @Test + fun testBuffer0DropLatestReplay10() = + checkConflation(10, BufferOverflow.DROP_LATEST) { + buffer(0, onBufferOverflow = BufferOverflow.DROP_LATEST).shareIn(it, SharingStarted.Eagerly, 10) + } + + @Test + fun testBuffer5DropLatestReplay0() = + checkConflation(5, BufferOverflow.DROP_LATEST) { + buffer(5, onBufferOverflow = BufferOverflow.DROP_LATEST).shareIn(it, SharingStarted.Eagerly, 0) + } + + @Test + fun testBuffer5DropLatestReplay10() = + checkConflation(15, BufferOverflow.DROP_LATEST) { + buffer(5, onBufferOverflow = BufferOverflow.DROP_LATEST).shareIn(it, SharingStarted.Eagerly, 10) + } + + @Test // a preceding buffer() gets overridden by buffer(onBufferOverflow = BufferOverflow.DROP_LATEST) + fun testBufferDropLatestOverride() = + checkConflation(1, BufferOverflow.DROP_LATEST) { + buffer(23).buffer(onBufferOverflow = BufferOverflow.DROP_LATEST).shareIn(it, SharingStarted.Eagerly, 0) + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/common/test/flow/sharing/ShareInFusionTest.kt b/kotlinx-coroutines-core/common/test/flow/sharing/ShareInFusionTest.kt new file mode 100644 index 0000000000..371d01472e --- /dev/null +++ b/kotlinx-coroutines-core/common/test/flow/sharing/ShareInFusionTest.kt @@ -0,0 +1,56 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.flow + +import kotlinx.coroutines.* +import kotlinx.coroutines.channels.* +import kotlin.test.* + +class ShareInFusionTest : TestBase() { + /** + * Test perfect fusion for operators **after** [shareIn]. + */ + @Test + fun testOperatorFusion() = runTest { + val sh = emptyFlow().shareIn(this, SharingStarted.Eagerly) + assertTrue(sh !is MutableSharedFlow<*>) // cannot be cast to mutable shared flow!!! + assertSame(sh, (sh as Flow<*>).cancellable()) + assertSame(sh, (sh as Flow<*>).flowOn(Dispatchers.Default)) + assertSame(sh, sh.buffer(Channel.RENDEZVOUS)) + coroutineContext.cancelChildren() + } + + @Test + fun testFlowOnContextFusion() = runTest { + val flow = flow { + assertEquals("FlowCtx", currentCoroutineContext()[CoroutineName]?.name) + emit("OK") + }.flowOn(CoroutineName("FlowCtx")) + assertEquals("OK", flow.shareIn(this, SharingStarted.Eagerly, 1).first()) + coroutineContext.cancelChildren() + } + + /** + * Tests that `channelFlow { ... }.buffer(x)` works according to the [channelFlow] docs, and subsequent + * application of [shareIn] does not leak upstream. + */ + @Test + fun testChannelFlowBufferShareIn() = runTest { + expect(1) + val flow = channelFlow { + // send a batch of 10 elements using [offer] + for (i in 1..10) { + assertTrue(offer(i)) // offer must succeed, because buffer + } + send(0) // done + }.buffer(10) // request a buffer of 10 + // ^^^^^^^^^ buffer stays here + val shared = flow.shareIn(this, SharingStarted.Eagerly) + shared + .takeWhile { it > 0 } + .collect { i -> expect(i + 1) } + finish(12) + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/common/test/flow/sharing/ShareInTest.kt b/kotlinx-coroutines-core/common/test/flow/sharing/ShareInTest.kt new file mode 100644 index 0000000000..9020f5f311 --- /dev/null +++ b/kotlinx-coroutines-core/common/test/flow/sharing/ShareInTest.kt @@ -0,0 +1,215 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.flow + +import kotlinx.coroutines.* +import kotlinx.coroutines.channels.* +import kotlin.test.* + +class ShareInTest : TestBase() { + @Test + fun testReplay0Eager() = runTest { + expect(1) + val flow = flowOf("OK") + val shared = flow.shareIn(this, SharingStarted.Eagerly) + yield() // actually start sharing + // all subscribers miss "OK" + val jobs = List(10) { + shared.onEach { expectUnreached() }.launchIn(this) + } + yield() // ensure nothing is collected + jobs.forEach { it.cancel() } + finish(2) + } + + @Test + fun testReplay0Lazy() = testReplayZeroOrOne(0) + + @Test + fun testReplay1Lazy() = testReplayZeroOrOne(1) + + private fun testReplayZeroOrOne(replay: Int) = runTest { + expect(1) + val doneBarrier = Job() + val flow = flow { + expect(2) + emit("OK") + doneBarrier.join() + emit("DONE") + } + val sharingJob = Job() + val shared = flow.shareIn(this + sharingJob, started = SharingStarted.Lazily, replay = replay) + yield() // should not start sharing + // first subscriber gets "OK", other subscribers miss "OK" + val n = 10 + val replayOfs = replay * (n - 1) + val subscriberJobs = List(n) { index -> + val subscribedBarrier = Job() + val job = shared + .onSubscription { + subscribedBarrier.complete() + } + .onEach { value -> + when (value) { + "OK" -> { + expect(3 + index) + if (replay == 0) { // only the first subscriber collects "OK" without replay + assertEquals(0, index) + } + } + "DONE" -> { + expect(4 + index + replayOfs) + } + else -> expectUnreached() + } + } + .takeWhile { it != "DONE" } + .launchIn(this) + subscribedBarrier.join() // wait until the launched job subscribed before launching the next one + job + } + doneBarrier.complete() + subscriberJobs.joinAll() + expect(4 + n + replayOfs) + sharingJob.cancel() + finish(5 + n + replayOfs) + } + + @Test + fun testUpstreamCompleted() = + testUpstreamCompletedOrFailed(failed = false) + + @Test + fun testUpstreamFailed() = + testUpstreamCompletedOrFailed(failed = true) + + private fun testUpstreamCompletedOrFailed(failed: Boolean) = runTest { + val emitted = Job() + val terminate = Job() + val sharingJob = CompletableDeferred() + val upstream = flow { + emit("OK") + emitted.complete() + terminate.join() + if (failed) throw TestException() + } + val shared = upstream.shareIn(this + sharingJob, SharingStarted.Eagerly, 1) + assertEquals(emptyList(), shared.replayCache) + emitted.join() // should start sharing, emit & cache + assertEquals(listOf("OK"), shared.replayCache) + terminate.complete() + sharingJob.complete(Unit) + sharingJob.join() // should complete sharing + assertEquals(listOf("OK"), shared.replayCache) // cache is still there + if (failed) { + assertTrue(sharingJob.getCompletionExceptionOrNull() is TestException) + } else { + assertNull(sharingJob.getCompletionExceptionOrNull()) + } + } + + @Test + fun testWhileSubscribedBasic() = + testWhileSubscribed(1, SharingStarted.WhileSubscribed()) + + @Test + fun testWhileSubscribedCustomAtLeast1() = + testWhileSubscribed(1, SharingStarted.WhileSubscribedAtLeast(1)) + + @Test + fun testWhileSubscribedCustomAtLeast2() = + testWhileSubscribed(2, SharingStarted.WhileSubscribedAtLeast(2)) + + @OptIn(ExperimentalStdlibApi::class) + private fun testWhileSubscribed(threshold: Int, started: SharingStarted) = runTest { + expect(1) + val flowState = FlowState() + val n = 3 // max number of subscribers + val log = Channel(2 * n) + + suspend fun checkStartTransition(subscribers: Int) { + when (subscribers) { + in 0 until threshold -> assertFalse(flowState.started) + threshold -> { + flowState.awaitStart() // must eventually start the flow + for (i in 1..threshold) { + assertEquals("sub$i: OK", log.receive()) // threshold subs must receive the values + } + } + in threshold + 1..n -> assertTrue(flowState.started) + } + } + + suspend fun checkStopTransition(subscribers: Int) { + when (subscribers) { + in threshold + 1..n -> assertTrue(flowState.started) + threshold - 1 -> flowState.awaitStop() // upstream flow must be eventually stopped + in 0..threshold - 2 -> assertFalse(flowState.started) // should have stopped already + } + } + + val flow = flow { + flowState.track { + emit("OK") + delay(Long.MAX_VALUE) // await forever, will get cancelled + } + } + + val shared = flow.shareIn(this, started) + repeat(5) { // repeat scenario a few times + yield() + assertFalse(flowState.started) // flow is not running even if we yield + // start 3 subscribers + val subs = ArrayList() + for (i in 1..n) { + subs += shared + .onEach { value -> // only the first threshold subscribers get the value + when (i) { + in 1..threshold -> log.offer("sub$i: $value") + else -> expectUnreached() + } + } + .onCompletion { log.offer("sub$i: completion") } + .launchIn(this) + checkStartTransition(i) + } + // now cancel all subscribers + for (i in 1..n) { + subs.removeFirst().cancel() // cancel subscriber + assertEquals("sub$i: completion", log.receive()) // subscriber shall shutdown + checkStopTransition(n - i) + } + } + coroutineContext.cancelChildren() // cancel sharing job + finish(2) + } + + @Suppress("TestFunctionName") + private fun SharingStarted.Companion.WhileSubscribedAtLeast(threshold: Int): SharingStarted = + object : SharingStarted { + override fun command(subscriptionCount: StateFlow): Flow = + subscriptionCount + .map { if (it >= threshold) SharingCommand.START else SharingCommand.STOP } + } + + private class FlowState { + private val timeLimit = 10000L + private val _started = MutableStateFlow(false) + val started: Boolean get() = _started.value + fun start() = check(_started.compareAndSet(expect = false, update = true)) + fun stop() = check(_started.compareAndSet(expect = true, update = false)) + suspend fun awaitStart() = withTimeout(timeLimit) { _started.first { it } } + suspend fun awaitStop() = withTimeout(timeLimit) { _started.first { !it } } + } + + private suspend fun FlowState.track(block: suspend () -> Unit) { + start() + try { + block() + } finally { + stop() + } + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/common/test/flow/sharing/SharedFlowScenarioTest.kt b/kotlinx-coroutines-core/common/test/flow/sharing/SharedFlowScenarioTest.kt new file mode 100644 index 0000000000..c3eb2dac04 --- /dev/null +++ b/kotlinx-coroutines-core/common/test/flow/sharing/SharedFlowScenarioTest.kt @@ -0,0 +1,365 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.flow + +import kotlinx.coroutines.* +import kotlinx.coroutines.channels.* +import kotlin.coroutines.* +import kotlin.test.* + +/** + * This test suit for [SharedFlow] has a dense framework that allows to test complex + * suspend/resume scenarios while keeping the code readable. Each test here is for + * one specific [SharedFlow] configuration, testing all the various corner cases in its + * behavior. + */ +class SharedFlowScenarioTest : TestBase() { + @Test + fun testReplay1Extra2() = + testSharedFlow(MutableSharedFlow(1, 2)) { + // total buffer size == 3 + expectReplayOf() + emitRightNow(1); expectReplayOf(1) + emitRightNow(2); expectReplayOf(2) + emitRightNow(3); expectReplayOf(3) + emitRightNow(4); expectReplayOf(4) // no prob - no subscribers + val a = subscribe("a"); collect(a, 4) + emitRightNow(5); expectReplayOf(5) + emitRightNow(6); expectReplayOf(6) + emitRightNow(7); expectReplayOf(7) + // suspend/collect sequentially + val e8 = emitSuspends(8); collect(a, 5); emitResumes(e8); expectReplayOf(8) + val e9 = emitSuspends(9); collect(a, 6); emitResumes(e9); expectReplayOf(9) + // buffer full, but parallel emitters can still suspend (queue up) + val e10 = emitSuspends(10) + val e11 = emitSuspends(11) + val e12 = emitSuspends(12) + collect(a, 7); emitResumes(e10); expectReplayOf(10) // buffer 8, 9 | 10 + collect(a, 8); emitResumes(e11); expectReplayOf(11) // buffer 9, 10 | 11 + sharedFlow.resetReplayCache(); expectReplayOf() // 9, 10 11 | no replay + collect(a, 9); emitResumes(e12); expectReplayOf(12) + collect(a, 10, 11, 12); expectReplayOf(12) // buffer empty | 12 + emitRightNow(13); expectReplayOf(13) + emitRightNow(14); expectReplayOf(14) + emitRightNow(15); expectReplayOf(15) // buffer 13, 14 | 15 + val e16 = emitSuspends(16) + val e17 = emitSuspends(17) + val e18 = emitSuspends(18) + cancel(e17); expectReplayOf(15) // cancel in the middle of three emits; buffer 13, 14 | 15 + collect(a, 13); emitResumes(e16); expectReplayOf(16) // buffer 14, 15, | 16 + collect(a, 14); emitResumes(e18); expectReplayOf(18) // buffer 15, 16 | 18 + val e19 = emitSuspends(19) + val e20 = emitSuspends(20) + val e21 = emitSuspends(21) + cancel(e21); expectReplayOf(18) // cancel last emit; buffer 15, 16, 18 + collect(a, 15); emitResumes(e19); expectReplayOf(19) // buffer 16, 18 | 19 + collect(a, 16); emitResumes(e20); expectReplayOf(20) // buffer 18, 19 | 20 + collect(a, 18, 19, 20); expectReplayOf(20) // buffer empty | 20 + emitRightNow(22); expectReplayOf(22) + emitRightNow(23); expectReplayOf(23) + emitRightNow(24); expectReplayOf(24) // buffer 22, 23 | 24 + val e25 = emitSuspends(25) + val e26 = emitSuspends(26) + val e27 = emitSuspends(27) + cancel(e25); expectReplayOf(24) // cancel first emit, buffer 22, 23 | 24 + sharedFlow.resetReplayCache(); expectReplayOf() // buffer 22, 23, 24 | no replay + val b = subscribe("b") // new subscriber + collect(a, 22); emitResumes(e26); expectReplayOf(26) // buffer 23, 24 | 26 + collect(b, 26) + collect(a, 23); emitResumes(e27); expectReplayOf(27) // buffer 24, 26 | 27 + collect(a, 24, 26, 27) // buffer empty | 27 + emitRightNow(28); expectReplayOf(28) + emitRightNow(29); expectReplayOf(29) // buffer 27, 28 | 29 + collect(a, 28, 29) // but b is slow + val e30 = emitSuspends(30) + val e31 = emitSuspends(31) + val e32 = emitSuspends(32) + val e33 = emitSuspends(33) + val e34 = emitSuspends(34) + val e35 = emitSuspends(35) + val e36 = emitSuspends(36) + val e37 = emitSuspends(37) + val e38 = emitSuspends(38) + val e39 = emitSuspends(39) + cancel(e31) // cancel emitter in queue + cancel(b) // cancel slow subscriber -> 3 emitters resume + emitResumes(e30); emitResumes(e32); emitResumes(e33); expectReplayOf(33) // buffer 30, 32 | 33 + val c = subscribe("c"); collect(c, 33) // replays + cancel(e34) + collect(a, 30); emitResumes(e35); expectReplayOf(35) // buffer 32, 33 | 35 + cancel(e37) + cancel(a); emitResumes(e36); emitResumes(e38); expectReplayOf(38) // buffer 35, 36 | 38 + collect(c, 35); emitResumes(e39); expectReplayOf(39) // buffer 36, 38 | 39 + collect(c, 36, 38, 39); expectReplayOf(39) + cancel(c); expectReplayOf(39) // replay stays + } + + @Test + fun testReplay1() = + testSharedFlow(MutableSharedFlow(1)) { + emitRightNow(0); expectReplayOf(0) + emitRightNow(1); expectReplayOf(1) + emitRightNow(2); expectReplayOf(2) + sharedFlow.resetReplayCache(); expectReplayOf() + sharedFlow.resetReplayCache(); expectReplayOf() + emitRightNow(3); expectReplayOf(3) + emitRightNow(4); expectReplayOf(4) + val a = subscribe("a"); collect(a, 4) + emitRightNow(5); expectReplayOf(5); collect(a, 5) + emitRightNow(6) + sharedFlow.resetReplayCache(); expectReplayOf() + sharedFlow.resetReplayCache(); expectReplayOf() + val e7 = emitSuspends(7) + val e8 = emitSuspends(8) + val e9 = emitSuspends(9) + collect(a, 6); emitResumes(e7); expectReplayOf(7) + sharedFlow.resetReplayCache(); expectReplayOf() + sharedFlow.resetReplayCache(); expectReplayOf() // buffer 7 | -- no replay, but still buffered + val b = subscribe("b") + collect(a, 7); emitResumes(e8); expectReplayOf(8) + collect(b, 8) // buffer | 8 -- a is slow + val e10 = emitSuspends(10) + val e11 = emitSuspends(11) + val e12 = emitSuspends(12) + cancel(e9) + collect(a, 8); emitResumes(e10); expectReplayOf(10) + collect(a, 10) // now b's slow + cancel(e11) + collect(b, 10); emitResumes(e12); expectReplayOf(12) + collect(a, 12) + collect(b, 12) + sharedFlow.resetReplayCache(); expectReplayOf() + sharedFlow.resetReplayCache(); expectReplayOf() // nothing is buffered -- both collectors up to date + emitRightNow(13); expectReplayOf(13) + collect(b, 13) // a is slow + val e14 = emitSuspends(14) + val e15 = emitSuspends(15) + val e16 = emitSuspends(16) + cancel(e14) + cancel(a); emitResumes(e15); expectReplayOf(15) // cancelling slow subscriber + collect(b, 15); emitResumes(e16); expectReplayOf(16) + collect(b, 16) + } + + @Test + fun testReplay2Extra2DropOldest() = + testSharedFlow(MutableSharedFlow(2, 2, BufferOverflow.DROP_OLDEST)) { + emitRightNow(0); expectReplayOf(0) + emitRightNow(1); expectReplayOf(0, 1) + emitRightNow(2); expectReplayOf(1, 2) + emitRightNow(3); expectReplayOf(2, 3) + emitRightNow(4); expectReplayOf(3, 4) + val a = subscribe("a") + collect(a, 3) + emitRightNow(5); expectReplayOf(4, 5) + emitRightNow(6); expectReplayOf(5, 6) + emitRightNow(7); expectReplayOf(6, 7) // buffer 4, 5 | 6, 7 + emitRightNow(8); expectReplayOf(7, 8) // buffer 5, 6 | 7, 8 + emitRightNow(9); expectReplayOf(8, 9) // buffer 6, 7 | 8, 9 + collect(a, 6, 7) + val b = subscribe("b") + collect(b, 8, 9) // buffer | 8, 9 + emitRightNow(10); expectReplayOf(9, 10) // buffer 8 | 9, 10 + collect(a, 8, 9, 10) // buffer | 9, 10, note "b" had not collected 10 yet + emitRightNow(11); expectReplayOf(10, 11) // buffer | 10, 11 + emitRightNow(12); expectReplayOf(11, 12) // buffer 10 | 11, 12 + emitRightNow(13); expectReplayOf(12, 13) // buffer 10, 11 | 12, 13 + emitRightNow(14); expectReplayOf(13, 14) // buffer 11, 12 | 13, 14, "b" missed 10 + collect(b, 11, 12, 13, 14) + sharedFlow.resetReplayCache(); expectReplayOf() // buffer 11, 12, 13, 14 | + sharedFlow.resetReplayCache(); expectReplayOf() + collect(a, 11, 12, 13, 14) + emitRightNow(15); expectReplayOf(15) + collect(a, 15) + collect(b, 15) + } + + @Test // https://github.com/Kotlin/kotlinx.coroutines/issues/2320 + fun testResumeFastSubscriberOnResumedEmitter() = + testSharedFlow(MutableSharedFlow(1)) { + // create two subscribers and start collecting + val s1 = subscribe("s1"); resumeCollecting(s1) + val s2 = subscribe("s2"); resumeCollecting(s2) + // now emit 0, make sure it is collected + emitRightNow(0); expectReplayOf(0) + awaitCollected(s1, 0) + awaitCollected(s2, 0) + // now emit 1, and only first subscriber continues and collects it + emitRightNow(1); expectReplayOf(1) + collect(s1, 1) + // now emit 2, it suspend (s2 is blocking it) + val e2 = emitSuspends(2) + resumeCollecting(s1) // resume, but does not collect (e2 is still queued) + collect(s2, 1) // resume + collect next --> resumes emitter, thus resumes s1 + awaitCollected(s1, 2) // <-- S1 collects value from the newly resumed emitter here !!! + emitResumes(e2); expectReplayOf(2) + // now emit 3, it suspends (s2 blocks it) + val e3 = emitSuspends(3) + collect(s2, 2) + emitResumes(e3); expectReplayOf(3) + } + + private fun testSharedFlow( + sharedFlow: MutableSharedFlow, + scenario: suspend ScenarioDsl.() -> Unit + ) = runTest { + var dsl: ScenarioDsl? = null + try { + coroutineScope { + dsl = ScenarioDsl(sharedFlow, coroutineContext) + dsl!!.scenario() + dsl!!.stop() + } + } catch (e: Throwable) { + dsl?.printLog() + throw e + } + } + + private data class TestJob(val job: Job, val name: String) { + override fun toString(): String = name + } + + private open class Action + private data class EmitResumes(val job: TestJob) : Action() + private data class Collected(val job: TestJob, val value: Any?) : Action() + private data class ResumeCollecting(val job: TestJob) : Action() + private data class Cancelled(val job: TestJob) : Action() + + @OptIn(ExperimentalStdlibApi::class) + private class ScenarioDsl( + val sharedFlow: MutableSharedFlow, + coroutineContext: CoroutineContext + ) { + private val log = ArrayList() + private val timeout = 10000L + private val scope = CoroutineScope(coroutineContext + Job()) + private val actions = HashSet() + private val actionWaiters = ArrayDeque>() + private var expectedReplay = emptyList() + + private fun checkReplay() { + assertEquals(expectedReplay, sharedFlow.replayCache) + } + + private fun wakeupWaiters() { + repeat(actionWaiters.size) { + actionWaiters.removeFirst().resume(Unit) + } + } + + private fun addAction(action: Action) { + actions.add(action) + wakeupWaiters() + } + + private suspend fun awaitAction(action: Action) { + withTimeoutOrNull(timeout) { + while (!actions.remove(action)) { + suspendCancellableCoroutine { actionWaiters.add(it) } + } + } ?: error("Timed out waiting for action: $action") + wakeupWaiters() + } + + private fun launchEmit(a: T): TestJob { + val name = "emit($a)" + val job = scope.launch(start = CoroutineStart.UNDISPATCHED) { + val job = TestJob(coroutineContext[Job]!!, name) + try { + log(name) + sharedFlow.emit(a) + log("$name resumes") + addAction(EmitResumes(job)) + } catch(e: CancellationException) { + log("$name cancelled") + addAction(Cancelled(job)) + } + } + return TestJob(job, name) + } + + fun expectReplayOf(vararg a: T) { + expectedReplay = a.toList() + checkReplay() + } + + fun emitRightNow(a: T) { + val job = launchEmit(a) + assertTrue(actions.remove(EmitResumes(job))) + } + + fun emitSuspends(a: T): TestJob { + val job = launchEmit(a) + assertFalse(EmitResumes(job) in actions) + checkReplay() + return job + } + + suspend fun emitResumes(job: TestJob) { + awaitAction(EmitResumes(job)) + } + + suspend fun cancel(job: TestJob) { + log("cancel(${job.name})") + job.job.cancel() + awaitAction(Cancelled(job)) + } + + fun subscribe(id: String): TestJob { + val name = "collect($id)" + val job = scope.launch(start = CoroutineStart.UNDISPATCHED) { + val job = TestJob(coroutineContext[Job]!!, name) + try { + awaitAction(ResumeCollecting(job)) + log("$name start") + sharedFlow.collect { value -> + log("$name -> $value") + addAction(Collected(job, value)) + awaitAction(ResumeCollecting(job)) + log("$name -> $value resumes") + } + error("$name completed") + } catch(e: CancellationException) { + log("$name cancelled") + addAction(Cancelled(job)) + } + } + return TestJob(job, name) + } + + // collect ~== resumeCollecting + awaitCollected (for each value) + suspend fun collect(job: TestJob, vararg a: T) { + for (value in a) { + checkReplay() // should not have changed + resumeCollecting(job) + awaitCollected(job, value) + } + } + + suspend fun resumeCollecting(job: TestJob) { + addAction(ResumeCollecting(job)) + } + + suspend fun awaitCollected(job: TestJob, value: T) { + awaitAction(Collected(job, value)) + } + + fun stop() { + log("--- stop") + scope.cancel() + } + + private fun log(text: String) { + log.add(text) + } + + fun printLog() { + println("--- The most recent log entries ---") + log.takeLast(30).forEach(::println) + println("--- That's it ---") + } + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/common/test/flow/sharing/SharedFlowTest.kt b/kotlinx-coroutines-core/common/test/flow/sharing/SharedFlowTest.kt new file mode 100644 index 0000000000..32d88f3c99 --- /dev/null +++ b/kotlinx-coroutines-core/common/test/flow/sharing/SharedFlowTest.kt @@ -0,0 +1,798 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.flow + +import kotlinx.coroutines.* +import kotlinx.coroutines.channels.* +import kotlin.random.* +import kotlin.test.* + +/** + * This test suite contains some basic tests for [SharedFlow]. There are some scenarios here written + * using [expect] and they are not very readable. See [SharedFlowScenarioTest] for a better + * behavioral test-suit. + */ +class SharedFlowTest : TestBase() { + @Test + fun testRendezvousSharedFlowBasic() = runTest { + expect(1) + val sh = MutableSharedFlow() + assertTrue(sh.replayCache.isEmpty()) + assertEquals(0, sh.subscriptionCount.value) + sh.emit(1) // no suspend + assertTrue(sh.replayCache.isEmpty()) + assertEquals(0, sh.subscriptionCount.value) + expect(2) + // one collector + val job1 = launch(start = CoroutineStart.UNDISPATCHED) { + expect(3) + sh.collect { + when(it) { + 4 -> expect(5) + 6 -> expect(7) + 10 -> expect(11) + 13 -> expect(14) + else -> expectUnreached() + } + } + expectUnreached() // does not complete normally + } + expect(4) + assertEquals(1, sh.subscriptionCount.value) + sh.emit(4) + assertTrue(sh.replayCache.isEmpty()) + expect(6) + sh.emit(6) + expect(8) + // one more collector + val job2 = launch(start = CoroutineStart.UNDISPATCHED) { + expect(9) + sh.collect { + when(it) { + 10 -> expect(12) + 13 -> expect(15) + 17 -> expect(18) + null -> expect(20) + 21 -> expect(22) + else -> expectUnreached() + } + } + expectUnreached() // does not complete normally + } + expect(10) + assertEquals(2, sh.subscriptionCount.value) + sh.emit(10) // to both collectors now! + assertTrue(sh.replayCache.isEmpty()) + expect(13) + sh.emit(13) + expect(16) + job1.cancel() // cancel the first collector + yield() + assertEquals(1, sh.subscriptionCount.value) + expect(17) + sh.emit(17) // only to second collector + expect(19) + sh.emit(null) // emit null to the second collector + expect(21) + sh.emit(21) // non-null again + expect(23) + job2.cancel() // cancel the second collector + yield() + assertEquals(0, sh.subscriptionCount.value) + expect(24) + sh.emit(24) // does not go anywhere + assertEquals(0, sh.subscriptionCount.value) + assertTrue(sh.replayCache.isEmpty()) + finish(25) + } + + @Test + fun testRendezvousSharedFlowReset() = runTest { + expect(1) + val sh = MutableSharedFlow() + val barrier = Channel(1) + val job = launch(start = CoroutineStart.UNDISPATCHED) { + expect(2) + sh.collect { + when (it) { + 3 -> { + expect(4) + barrier.receive() // hold on before collecting next one + } + 6 -> expect(10) + else -> expectUnreached() + } + } + expectUnreached() // does not complete normally + } + expect(3) + sh.emit(3) // rendezvous + expect(5) + assertFalse(sh.tryEmit(5)) // collector is not ready now + launch(start = CoroutineStart.UNDISPATCHED) { + expect(6) + sh.emit(6) // suspends + expect(12) + } + expect(7) + yield() // no wakeup -> all suspended + expect(8) + // now reset cache -> nothing happens, there is no cache + sh.resetReplayCache() + yield() + expect(9) + // now resume collector + barrier.send(Unit) + yield() // to collector + expect(11) + yield() // to emitter + expect(13) + assertFalse(sh.tryEmit(13)) // rendezvous does not work this way + job.cancel() + finish(14) + } + + @Test + fun testReplay1SharedFlowBasic() = runTest { + expect(1) + val sh = MutableSharedFlow(1) + assertTrue(sh.replayCache.isEmpty()) + assertEquals(0, sh.subscriptionCount.value) + sh.emit(1) // no suspend + assertEquals(listOf(1), sh.replayCache) + assertEquals(0, sh.subscriptionCount.value) + expect(2) + sh.emit(2) // no suspend + assertEquals(listOf(2), sh.replayCache) + expect(3) + // one collector + val job1 = launch(start = CoroutineStart.UNDISPATCHED) { + expect(4) + sh.collect { + when(it) { + 2 -> expect(5) // got it immediately from replay cache + 6 -> expect(8) + null -> expect(14) + 17 -> expect(18) + else -> expectUnreached() + } + } + expectUnreached() // does not complete normally + } + expect(6) + assertEquals(1, sh.subscriptionCount.value) + sh.emit(6) // does not suspend, but buffers + assertEquals(listOf(6), sh.replayCache) + expect(7) + yield() + expect(9) + // one more collector + val job2 = launch(start = CoroutineStart.UNDISPATCHED) { + expect(10) + sh.collect { + when(it) { + 6 -> expect(11) // from replay cache + null -> expect(15) + else -> expectUnreached() + } + } + expectUnreached() // does not complete normally + } + expect(12) + assertEquals(2, sh.subscriptionCount.value) + sh.emit(null) + expect(13) + assertEquals(listOf(null), sh.replayCache) + yield() + assertEquals(listOf(null), sh.replayCache) + expect(16) + job2.cancel() + yield() + assertEquals(1, sh.subscriptionCount.value) + expect(17) + sh.emit(17) + assertEquals(listOf(17), sh.replayCache) + yield() + expect(19) + job1.cancel() + yield() + assertEquals(0, sh.subscriptionCount.value) + assertEquals(listOf(17), sh.replayCache) + finish(20) + } + + @Test + fun testReplay1() = runTest { + expect(1) + val sh = MutableSharedFlow(1) + assertEquals(listOf(), sh.replayCache) + val barrier = Channel(1) + val job = launch(start = CoroutineStart.UNDISPATCHED) { + expect(2) + sh.collect { + when (it) { + 3 -> { + expect(4) + barrier.receive() // collector waits + } + 5 -> expect(10) + 6 -> expect(11) + else -> expectUnreached() + } + } + expectUnreached() // does not complete normally + } + expect(3) + assertTrue(sh.tryEmit(3)) // buffered + assertEquals(listOf(3), sh.replayCache) + yield() // to collector + expect(5) + assertTrue(sh.tryEmit(5)) // buffered + assertEquals(listOf(5), sh.replayCache) + launch(start = CoroutineStart.UNDISPATCHED) { + expect(6) + sh.emit(6) // buffer full, suspended + expect(13) + } + expect(7) + assertEquals(listOf(5), sh.replayCache) + sh.resetReplayCache() // clear cache + assertEquals(listOf(), sh.replayCache) + expect(8) + yield() // emitter still suspended + expect(9) + assertEquals(listOf(), sh.replayCache) + assertFalse(sh.tryEmit(10)) // still no buffer space + assertEquals(listOf(), sh.replayCache) + barrier.send(Unit) // resume collector + yield() // to collector + expect(12) + yield() // to emitter, that should have resumed + expect(14) + job.cancel() + assertEquals(listOf(6), sh.replayCache) + finish(15) + } + + @Test + fun testReplay2Extra1() = runTest { + expect(1) + val sh = MutableSharedFlow( + replay = 2, + extraBufferCapacity = 1 + ) + assertEquals(listOf(), sh.replayCache) + assertTrue(sh.tryEmit(0)) + assertEquals(listOf(0), sh.replayCache) + val job = launch(start = CoroutineStart.UNDISPATCHED) { + expect(2) + var cnt = 0 + sh.collect { + when (it) { + 0 -> when (cnt++) { + 0 -> expect(3) + 1 -> expect(14) + else -> expectUnreached() + } + 1 -> expect(6) + 2 -> expect(7) + 3 -> expect(8) + 4 -> expect(12) + 5 -> expect(13) + 16 -> expect(17) + else -> expectUnreached() + } + } + expectUnreached() // does not complete normally + } + expect(4) + assertTrue(sh.tryEmit(1)) // buffered + assertEquals(listOf(0, 1), sh.replayCache) + assertTrue(sh.tryEmit(2)) // buffered + assertEquals(listOf(1, 2), sh.replayCache) + assertTrue(sh.tryEmit(3)) // buffered (buffer size is 3) + assertEquals(listOf(2, 3), sh.replayCache) + expect(5) + yield() // to collector + expect(9) + assertEquals(listOf(2, 3), sh.replayCache) + assertTrue(sh.tryEmit(4)) // can buffer now + assertEquals(listOf(3, 4), sh.replayCache) + assertTrue(sh.tryEmit(5)) // can buffer now + assertEquals(listOf(4, 5), sh.replayCache) + assertTrue(sh.tryEmit(0)) // can buffer one more, let it be zero again + assertEquals(listOf(5, 0), sh.replayCache) + expect(10) + assertFalse(sh.tryEmit(10)) // cannot buffer anymore! + sh.resetReplayCache() // replay cache + assertEquals(listOf(), sh.replayCache) // empty + assertFalse(sh.tryEmit(0)) // still cannot buffer anymore (reset does not help) + assertEquals(listOf(), sh.replayCache) // empty + expect(11) + yield() // resume collector, will get next values + expect(15) + sh.resetReplayCache() // reset again, nothing happens + assertEquals(listOf(), sh.replayCache) // empty + yield() // collector gets nothing -- no change + expect(16) + assertTrue(sh.tryEmit(16)) + assertEquals(listOf(16), sh.replayCache) + yield() // gets it + expect(18) + job.cancel() + finish(19) + } + + @Test + fun testBufferNoReplayCancelWhileBuffering() = runTest { + val n = 123 + val sh = MutableSharedFlow(replay = 0, extraBufferCapacity = n) + repeat(3) { + val m = n / 2 // collect half, then suspend + val barrier = Channel(1) + val collectorJob = sh + .onSubscription { + barrier.send(1) + } + .onEach { value -> + if (value == m) { + barrier.send(2) + delay(Long.MAX_VALUE) + } + } + .launchIn(this) + assertEquals(1, barrier.receive()) // make sure it subscribes + launch(start = CoroutineStart.UNDISPATCHED) { + for (i in 0 until n + m) sh.emit(i) // these emits should go Ok + barrier.send(3) + sh.emit(n + 4) // this emit will suspend on buffer overflow + barrier.send(4) + } + assertEquals(2, barrier.receive()) // wait until m collected + assertEquals(3, barrier.receive()) // wait until all are emitted + collectorJob.cancel() // cancelling collector job must clear buffer and resume emitter + assertEquals(4, barrier.receive()) // verify that emitter resumes + } + } + + @Test + fun testRepeatedResetWithReplay() = runTest { + val n = 10 + val sh = MutableSharedFlow(n) + var i = 0 + repeat(3) { + // collector is slow + val collector = sh.onEach { delay(Long.MAX_VALUE) }.launchIn(this) + val emitter = launch { + repeat(3 * n) { sh.emit(i); i++ } + } + repeat(3) { yield() } // enough to run it to suspension + assertEquals((i - n until i).toList(), sh.replayCache) + sh.resetReplayCache() + assertEquals(emptyList(), sh.replayCache) + repeat(3) { yield() } // enough to run it to suspension + assertEquals(emptyList(), sh.replayCache) // still blocked + collector.cancel() + emitter.cancel() + repeat(3) { yield() } // enough to run it to suspension + } + } + + @Test + fun testSynchronousSharedFlowEmitterCancel() = runTest { + expect(1) + val sh = MutableSharedFlow() + val barrier1 = Job() + val barrier2 = Job() + val barrier3 = Job() + val collector1 = sh.onEach { + when (it) { + 1 -> expect(3) + 2 -> { + expect(6) + barrier2.complete() + } + 3 -> { + expect(9) + barrier3.complete() + } + else -> expectUnreached() + } + }.launchIn(this) + val collector2 = sh.onEach { + when (it) { + 1 -> { + expect(4) + barrier1.complete() + delay(Long.MAX_VALUE) + } + else -> expectUnreached() + } + }.launchIn(this) + repeat(2) { yield() } // launch both subscribers + val emitter = launch(start = CoroutineStart.UNDISPATCHED) { + expect(2) + sh.emit(1) + barrier1.join() + expect(5) + sh.emit(2) // suspends because of slow collector2 + expectUnreached() // will be cancelled + } + barrier2.join() // wait + expect(7) + // Now cancel the emitter! + emitter.cancel() + yield() + // Cancel slow collector + collector2.cancel() + yield() + // emit to fast collector1 + expect(8) + sh.emit(3) + barrier3.join() + expect(10) + // cancel it, too + collector1.cancel() + finish(11) + } + + @Test + fun testDifferentBufferedFlowCapacities() { + for (replay in 0..10) { + for (extraBufferCapacity in 0..5) { + if (replay == 0 && extraBufferCapacity == 0) continue // test only buffered shared flows + try { + val sh = MutableSharedFlow(replay, extraBufferCapacity) + // repeat the whole test a few times to make sure it works correctly when slots are reused + repeat(3) { + testBufferedFlow(sh, replay) + } + } catch (e: Throwable) { + error("Failed for replay=$replay, extraBufferCapacity=$extraBufferCapacity", e) + } + } + } + } + + private fun testBufferedFlow(sh: MutableSharedFlow, replay: Int) = runTest { + reset() + expect(1) + val n = 100 // initially emitted to fill buffer + for (i in 1..n) assertTrue(sh.tryEmit(i)) + // initial expected replayCache + val rcStart = n - replay + 1 + val rcRange = rcStart..n + val rcSize = n - rcStart + 1 + assertEquals(rcRange.toList(), sh.replayCache) + // create collectors + val m = 10 // collectors created + var ofs = 0 + val k = 42 // emissions to collectors + val ecRange = n + 1..n + k + val jobs = List(m) { jobIndex -> + launch(start = CoroutineStart.UNDISPATCHED) { + sh.collect { i -> + when (i) { + in rcRange -> expect(2 + i - rcStart + jobIndex * rcSize) + in ecRange -> expect(2 + ofs + jobIndex) + else -> expectUnreached() + } + } + expectUnreached() // does not complete normally + } + } + ofs = rcSize * m + 2 + expect(ofs) + // emit to all k times + for (p in ecRange) { + sh.emit(p) + expect(1 + ofs) // buffered, no suspend + yield() + ofs += 2 + m + expect(ofs) + } + assertEquals(ecRange.toList().takeLast(replay), sh.replayCache) + // cancel all collectors + jobs.forEach { it.cancel() } + yield() + // replay cache is still there + assertEquals(ecRange.toList().takeLast(replay), sh.replayCache) + finish(1 + ofs) + } + + @Test + fun testDropLatest() = testDropLatestOrOldest(BufferOverflow.DROP_LATEST) + + @Test + fun testDropOldest() = testDropLatestOrOldest(BufferOverflow.DROP_OLDEST) + + private fun testDropLatestOrOldest(bufferOverflow: BufferOverflow) = runTest { + reset() + expect(1) + val sh = MutableSharedFlow(1, onBufferOverflow = bufferOverflow) + sh.emit(1) + sh.emit(2) + // always keeps last w/o collectors + assertEquals(listOf(2), sh.replayCache) + assertEquals(0, sh.subscriptionCount.value) + // one collector + val valueAfterOverflow = when (bufferOverflow) { + BufferOverflow.DROP_OLDEST -> 5 + BufferOverflow.DROP_LATEST -> 4 + else -> error("not supported in this test: $bufferOverflow") + } + val job = launch(start = CoroutineStart.UNDISPATCHED) { + expect(2) + sh.collect { + when(it) { + 2 -> { // replayed + expect(3) + yield() // and suspends, busy waiting + } + valueAfterOverflow -> expect(7) + 8 -> expect(9) + else -> expectUnreached() + } + } + expectUnreached() // does not complete normally + } + expect(4) + assertEquals(1, sh.subscriptionCount.value) + assertEquals(listOf(2), sh.replayCache) + sh.emit(4) // buffering, collector is busy + assertEquals(listOf(4), sh.replayCache) + expect(5) + sh.emit(5) // Buffer overflow here, will not suspend + assertEquals(listOf(valueAfterOverflow), sh.replayCache) + expect(6) + yield() // to the job + expect(8) + sh.emit(8) // not busy now + assertEquals(listOf(8), sh.replayCache) // buffered + yield() // to process + expect(10) + job.cancel() // cancel the job + yield() + assertEquals(0, sh.subscriptionCount.value) + finish(11) + } + + @Test + public fun testOnSubscription() = runTest { + expect(1) + val sh = MutableSharedFlow() + fun share(s: String) { launch(start = CoroutineStart.UNDISPATCHED) { sh.emit(s) } } + sh + .onSubscription { + emit("collector->A") + share("share->A") + } + .onSubscription { + emit("collector->B") + share("share->B") + } + .onStart { + emit("collector->C") + share("share->C") // get's lost, no subscribers yet + } + .onStart { + emit("collector->D") + share("share->D") // get's lost, no subscribers yet + } + .onEach { + when (it) { + "collector->D" -> expect(2) + "collector->C" -> expect(3) + "collector->A" -> expect(4) + "collector->B" -> expect(5) + "share->A" -> expect(6) + "share->B" -> { + expect(7) + currentCoroutineContext().cancel() + } + else -> expectUnreached() + } + } + .launchIn(this) + .join() + finish(8) + } + + @Test + fun onSubscriptionThrows() = runTest { + expect(1) + val sh = MutableSharedFlow(1) + sh.tryEmit("OK") // buffer a string + assertEquals(listOf("OK"), sh.replayCache) + sh + .onSubscription { + expect(2) + throw TestException() + } + .catch { e -> + assertTrue(e is TestException) + expect(3) + } + .collect { + // onSubscription throw before replay is emitted, so no value is collected if it throws + expectUnreached() + } + assertEquals(0, sh.subscriptionCount.value) + finish(4) + } + + @Test + fun testBigReplayManySubscribers() = testManySubscribers(true) + + @Test + fun testBigBufferManySubscribers() = testManySubscribers(false) + + private fun testManySubscribers(replay: Boolean) = runTest { + val n = 100 + val rnd = Random(replay.hashCode()) + val sh = MutableSharedFlow( + replay = if (replay) n else 0, + extraBufferCapacity = if (replay) 0 else n + ) + val subs = ArrayList() + for (i in 1..n) { + sh.emit(i) + val subBarrier = Channel() + val subJob = SubJob() + subs += subJob + // will receive all starting from replay or from new emissions only + subJob.lastReceived = if (replay) 0 else i + subJob.job = sh + .onSubscription { + subBarrier.send(Unit) // signal subscribed + } + .onEach { value -> + assertEquals(subJob.lastReceived + 1, value) + subJob.lastReceived = value + } + .launchIn(this) + subBarrier.receive() // wait until subscribed + // must have also receive all from the replay buffer directly after being subscribed + assertEquals(subJob.lastReceived, i) + // 50% of time cancel one subscriber + if (i % 2 == 0) { + val victim = subs.removeAt(rnd.nextInt(subs.size)) + yield() // make sure victim processed all emissions + assertEquals(victim.lastReceived, i) + victim.job.cancel() + } + } + yield() // make sure the last emission is processed + for (subJob in subs) { + assertEquals(subJob.lastReceived, n) + subJob.job.cancel() + } + } + + private class SubJob { + lateinit var job: Job + var lastReceived = 0 + } + + @Test + fun testStateFlowModel() = runTest { + val stateFlow = MutableStateFlow(null) + val expect = modelLog(stateFlow) + val sharedFlow = MutableSharedFlow( + replay = 1, + onBufferOverflow = BufferOverflow.DROP_OLDEST + ) + sharedFlow.tryEmit(null) // initial value + val actual = modelLog(sharedFlow) { distinctUntilChanged() } + for (i in 0 until minOf(expect.size, actual.size)) { + if (actual[i] != expect[i]) { + for (j in maxOf(0, i - 10)..i) println("Actual log item #$j: ${actual[j]}") + assertEquals(expect[i], actual[i], "Log item #$i") + } + } + assertEquals(expect.size, actual.size) + } + + private suspend fun modelLog( + sh: MutableSharedFlow, + op: Flow.() -> Flow = { this } + ): List = coroutineScope { + val rnd = Random(1) + val result = ArrayList() + val job = launch { + sh.op().collect { value -> + result.add("Collect: $value") + repeat(rnd.nextInt(0..2)) { + result.add("Collect: yield") + yield() + } + } + } + repeat(1000) { + val value = if (rnd.nextBoolean()) null else rnd.nextData() + if (rnd.nextInt(20) == 0) { + result.add("resetReplayCache & emit: $value") + if (sh !is StateFlow<*>) sh.resetReplayCache() + assertTrue(sh.tryEmit(value)) + } else { + result.add("Emit: $value") + sh.emit(value) + } + repeat(rnd.nextInt(0..2)) { + result.add("Emit: yield") + yield() + } + } + result.add("main: cancel") + job.cancel() + result.add("main: yield") + yield() + result.add("main: join") + job.join() + result + } + + data class Data(val x: Int) + private val dataCache = (1..5).associateWith { Data(it) } + + // Note that we test proper null support here, too + private fun Random.nextData(): Data? { + val x = nextInt(0..5) + if (x == 0) return null + // randomly reuse ref or create a new instance + return if(nextBoolean()) dataCache[x] else Data(x) + } + + @Test + fun testOperatorFusion() { + val sh = MutableSharedFlow() + assertSame(sh, (sh as Flow<*>).cancellable()) + assertSame(sh, (sh as Flow<*>).flowOn(Dispatchers.Default)) + assertSame(sh, sh.buffer(Channel.RENDEZVOUS)) + } + + @Test + fun testIllegalArgumentException() { + assertFailsWith { MutableSharedFlow(-1) } + assertFailsWith { MutableSharedFlow(0, extraBufferCapacity = -1) } + assertFailsWith { MutableSharedFlow(0, onBufferOverflow = BufferOverflow.DROP_LATEST) } + assertFailsWith { MutableSharedFlow(0, onBufferOverflow = BufferOverflow.DROP_OLDEST) } + } + + @Test + public fun testReplayCancellability() = testCancellability(fromReplay = true) + + @Test + public fun testEmitCancellability() = testCancellability(fromReplay = false) + + private fun testCancellability(fromReplay: Boolean) = runTest { + expect(1) + val sh = MutableSharedFlow(5) + fun emitTestData() { + for (i in 1..5) assertTrue(sh.tryEmit(i)) + } + if (fromReplay) emitTestData() // fill in replay first + var subscribed = true + val job = sh + .onSubscription { subscribed = true } + .onEach { i -> + when (i) { + 1 -> expect(2) + 2 -> expect(3) + 3 -> { + expect(4) + currentCoroutineContext().cancel() + } + else -> expectUnreached() // shall check for cancellation + } + } + .launchIn(this) + yield() + assertTrue(subscribed) // yielding in enough + if (!fromReplay) emitTestData() // emit after subscription + job.join() + finish(5) + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/common/test/flow/sharing/SharingStartedTest.kt b/kotlinx-coroutines-core/common/test/flow/sharing/SharingStartedTest.kt new file mode 100644 index 0000000000..496fb7f8ff --- /dev/null +++ b/kotlinx-coroutines-core/common/test/flow/sharing/SharingStartedTest.kt @@ -0,0 +1,183 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.flow + +import kotlinx.coroutines.* +import kotlin.coroutines.* +import kotlin.test.* + +/** + * Functional tests for [SharingStarted] using [withVirtualTime] and a DSL to describe + * testing scenarios and expected behavior for different implementations. + */ +class SharingStartedTest : TestBase() { + @Test + fun testEagerly() = + testSharingStarted(SharingStarted.Eagerly, SharingCommand.START) { + subscriptions(1) + rampUpAndDown() + subscriptions(0) + delay(100) + } + + @Test + fun testLazily() = + testSharingStarted(SharingStarted.Lazily) { + subscriptions(1, SharingCommand.START) + rampUpAndDown() + subscriptions(0) + } + + @Test + fun testWhileSubscribed() = + testSharingStarted(SharingStarted.WhileSubscribed()) { + subscriptions(1, SharingCommand.START) + rampUpAndDown() + subscriptions(0, SharingCommand.STOP) + delay(100) + } + + @Test + fun testWhileSubscribedExpireImmediately() = + testSharingStarted(SharingStarted.WhileSubscribed(replayExpirationMillis = 0)) { + subscriptions(1, SharingCommand.START) + rampUpAndDown() + subscriptions(0, SharingCommand.STOP_AND_RESET_REPLAY_CACHE) + delay(100) + } + + @Test + fun testWhileSubscribedWithTimeout() = + testSharingStarted(SharingStarted.WhileSubscribed(stopTimeoutMillis = 100)) { + subscriptions(1, SharingCommand.START) + rampUpAndDown() + subscriptions(0) + delay(50) // don't give it time to stop + subscriptions(1) // resubscribe again + rampUpAndDown() + subscriptions(0) + afterTime(100, SharingCommand.STOP) + delay(100) + } + + @Test + fun testWhileSubscribedExpiration() = + testSharingStarted(SharingStarted.WhileSubscribed(replayExpirationMillis = 200)) { + subscriptions(1, SharingCommand.START) + rampUpAndDown() + subscriptions(0, SharingCommand.STOP) + delay(150) // don't give it time to reset cache + subscriptions(1, SharingCommand.START) + rampUpAndDown() + subscriptions(0, SharingCommand.STOP) + afterTime(200, SharingCommand.STOP_AND_RESET_REPLAY_CACHE) + } + + @Test + fun testWhileSubscribedStopAndExpiration() = + testSharingStarted(SharingStarted.WhileSubscribed(stopTimeoutMillis = 400, replayExpirationMillis = 300)) { + subscriptions(1, SharingCommand.START) + rampUpAndDown() + subscriptions(0) + delay(350) // don't give it time to stop + subscriptions(1) + rampUpAndDown() + subscriptions(0) + afterTime(400, SharingCommand.STOP) + delay(250) // don't give it time to reset cache + subscriptions(1, SharingCommand.START) + rampUpAndDown() + subscriptions(0) + afterTime(400, SharingCommand.STOP) + afterTime(300, SharingCommand.STOP_AND_RESET_REPLAY_CACHE) + delay(100) + } + + private suspend fun SharingStartedDsl.rampUpAndDown() { + for (i in 2..10) { + delay(100) + subscriptions(i) + } + delay(1000) + for (i in 9 downTo 1) { + subscriptions(i) + delay(100) + } + } + + private fun testSharingStarted( + started: SharingStarted, + initialCommand: SharingCommand? = null, + scenario: suspend SharingStartedDsl.() -> Unit + ) = withVirtualTime { + expect(1) + val dsl = SharingStartedDsl(started, initialCommand, coroutineContext) + dsl.launch() + // repeat every scenario 3 times + repeat(3) { + dsl.scenario() + delay(1000) + } + dsl.stop() + finish(2) + } + + private class SharingStartedDsl( + val started: SharingStarted, + initialCommand: SharingCommand?, + coroutineContext: CoroutineContext + ) { + val subscriptionCount = MutableStateFlow(0) + var previousCommand: SharingCommand? = null + var expectedCommand: SharingCommand? = initialCommand + var expectedTime = 0L + + val dispatcher = coroutineContext[ContinuationInterceptor] as VirtualTimeDispatcher + val scope = CoroutineScope(coroutineContext + Job()) + + suspend fun launch() { + started + .command(subscriptionCount.asStateFlow()) + .onEach { checkCommand(it) } + .launchIn(scope) + letItRun() + } + + fun checkCommand(command: SharingCommand) { + assertTrue(command != previousCommand) + previousCommand = command + assertEquals(expectedCommand, command) + assertEquals(expectedTime, dispatcher.currentTime) + } + + suspend fun subscriptions(count: Int, command: SharingCommand? = null) { + expectedTime = dispatcher.currentTime + subscriptionCount.value = count + if (command != null) { + afterTime(0, command) + } else { + letItRun() + } + } + + suspend fun afterTime(time: Long = 0, command: SharingCommand) { + expectedCommand = command + val remaining = (time - 1).coerceAtLeast(0) // previous letItRun delayed 1ms + expectedTime += remaining + delay(remaining) + letItRun() + } + + private suspend fun letItRun() { + delay(1) + assertEquals(expectedCommand, previousCommand) // make sure expected command was emitted + expectedTime++ // make one more time tick we've delayed + } + + fun stop() { + scope.cancel() + } + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/common/test/flow/sharing/SharingStartedWhileSubscribedTest.kt b/kotlinx-coroutines-core/common/test/flow/sharing/SharingStartedWhileSubscribedTest.kt new file mode 100644 index 0000000000..bcf626e3e3 --- /dev/null +++ b/kotlinx-coroutines-core/common/test/flow/sharing/SharingStartedWhileSubscribedTest.kt @@ -0,0 +1,42 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.flow + +import kotlinx.coroutines.* +import kotlin.test.* +import kotlin.time.* + +class SharingStartedWhileSubscribedTest : TestBase() { + @Test // make sure equals works properly, or otherwise other tests don't make sense + fun testEqualsAndHashcode() { + val params = listOf(0L, 1L, 10L, 100L, 213L, Long.MAX_VALUE) + // HashMap will simultaneously test equals, hashcode and their consistency + val map = HashMap>() + for (i in params) { + for (j in params) { + map[SharingStarted.WhileSubscribed(i, j)] = i to j + } + } + for (i in params) { + for (j in params) { + assertEquals(i to j, map[SharingStarted.WhileSubscribed(i, j)]) + } + } + } + + @OptIn(ExperimentalTime::class) + @Test + fun testDurationParams() { + assertEquals(SharingStarted.WhileSubscribed(0), SharingStarted.WhileSubscribed(Duration.ZERO)) + assertEquals(SharingStarted.WhileSubscribed(10), SharingStarted.WhileSubscribed(10.milliseconds)) + assertEquals(SharingStarted.WhileSubscribed(1000), SharingStarted.WhileSubscribed(1.seconds)) + assertEquals(SharingStarted.WhileSubscribed(Long.MAX_VALUE), SharingStarted.WhileSubscribed(Duration.INFINITE)) + assertEquals(SharingStarted.WhileSubscribed(replayExpirationMillis = 0), SharingStarted.WhileSubscribed(replayExpiration = Duration.ZERO)) + assertEquals(SharingStarted.WhileSubscribed(replayExpirationMillis = 3), SharingStarted.WhileSubscribed(replayExpiration = 3.milliseconds)) + assertEquals(SharingStarted.WhileSubscribed(replayExpirationMillis = 7000), SharingStarted.WhileSubscribed(replayExpiration = 7.seconds)) + assertEquals(SharingStarted.WhileSubscribed(replayExpirationMillis = Long.MAX_VALUE), SharingStarted.WhileSubscribed(replayExpiration = Duration.INFINITE)) + } +} + diff --git a/kotlinx-coroutines-core/common/test/flow/sharing/StateFlowTest.kt b/kotlinx-coroutines-core/common/test/flow/sharing/StateFlowTest.kt new file mode 100644 index 0000000000..0a2c0458c4 --- /dev/null +++ b/kotlinx-coroutines-core/common/test/flow/sharing/StateFlowTest.kt @@ -0,0 +1,196 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.flow + +import kotlinx.coroutines.* +import kotlinx.coroutines.channels.* +import kotlin.test.* + +class StateFlowTest : TestBase() { + @Test + fun testNormalAndNull() = runTest { + expect(1) + val state = MutableStateFlow(0) + val job = launch(start = CoroutineStart.UNDISPATCHED) { + expect(2) + assertFailsWith { + state.collect { value -> + when (value) { + 0 -> expect(3) + 1 -> expect(5) + null -> expect(8) + 2 -> expect(10) + else -> expectUnreached() + } + } + } + expect(12) + } + expect(4) // collector is waiting + state.value = 1 // fire in the hole! + assertEquals(1, state.value) + yield() + expect(6) + state.value = 1 // same value, nothing happens + yield() + expect(7) + state.value = null // null value + assertNull(state.value) + yield() + expect(9) + state.value = 2 // another value + assertEquals(2, state.value) + yield() + expect(11) + job.cancel() + yield() + finish(13) + } + + @Test + fun testEqualsConflation() = runTest { + expect(1) + val state = MutableStateFlow(Data(0)) + val job = launch(start = CoroutineStart.UNDISPATCHED) { + expect(2) + assertFailsWith { + state.collect { value -> + when (value.i) { + 0 -> expect(3) // initial value + 2 -> expect(5) + 4 -> expect(7) + else -> error("Unexpected $value") + } + } + } + expect(9) + } + state.value = Data(1) // conflated + state.value = Data(0) // equals to last emitted + yield() // no repeat zero + state.value = Data(3) // conflated + state.value = Data(2) // delivered + expect(4) + yield() + state.value = Data(2) // equals to last one, dropped + yield() + state.value = Data(5) // conflated + state.value = Data(4) // delivered + expect(6) + yield() + expect(8) + job.cancel() + yield() + finish(10) + } + + data class Data(val i: Int) + + @Test + fun testDataModel() = runTest { + val s = CounterModel() + launch { + val sum = s.counter.take(11).sum() + assertEquals(55, sum) + } + repeat(10) { + yield() + s.inc() + } + } + + class CounterModel { + // private data flow + private val _counter = MutableStateFlow(0) + + // publicly exposed as a flow + val counter: StateFlow get() = _counter + + fun inc() { + _counter.value++ + } + } + + @Test + public fun testOnSubscriptionWithException() = runTest { + expect(1) + val state = MutableStateFlow("A") + state + .onSubscription { + emit("collector->A") + state.value = "A" + } + .onSubscription { + emit("collector->B") + state.value = "B" + throw TestException() + } + .onStart { + emit("collector->C") + state.value = "C" + } + .onStart { + emit("collector->D") + state.value = "D" + } + .onEach { + when (it) { + "collector->D" -> expect(2) + "collector->C" -> expect(3) + "collector->A" -> expect(4) + "collector->B" -> expect(5) + else -> expectUnreached() + } + } + .catch { e -> + assertTrue(e is TestException) + expect(6) + } + .launchIn(this) + .join() + assertEquals(0, state.subscriptionCount.value) + finish(7) + } + + @Test + fun testOperatorFusion() { + val state = MutableStateFlow(String) + assertSame(state, (state as Flow<*>).cancellable()) + assertSame(state, (state as Flow<*>).distinctUntilChanged()) + assertSame(state, (state as Flow<*>).flowOn(Dispatchers.Default)) + assertSame(state, (state as Flow<*>).conflate()) + assertSame(state, state.buffer(Channel.CONFLATED)) + assertSame(state, state.buffer(Channel.RENDEZVOUS)) + } + + @Test + fun testResetUnsupported() { + val state = MutableStateFlow(42) + assertFailsWith { state.resetReplayCache() } + assertEquals(42, state.value) + assertEquals(listOf(42), state.replayCache) + } + + @Test + fun testReferenceUpdatesAndCAS() { + val d0 = Data(0) + val d0_1 = Data(0) + val d1 = Data(1) + val d1_1 = Data(1) + val d1_2 = Data(1) + val state = MutableStateFlow(d0) + assertSame(d0, state.value) + state.value = d0_1 // equal, nothing changes + assertSame(d0, state.value) + state.value = d1 // updates + assertSame(d1, state.value) + assertFalse(state.compareAndSet(d0, d0)) // wrong value + assertSame(d1, state.value) + assertTrue(state.compareAndSet(d1_1, d1_2)) // "updates", but ref stays + assertSame(d1, state.value) + assertTrue(state.compareAndSet(d1_1, d0)) // updates, reference changes + assertSame(d0, state.value) + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/common/test/flow/sharing/StateInTest.kt b/kotlinx-coroutines-core/common/test/flow/sharing/StateInTest.kt new file mode 100644 index 0000000000..d0e76c461e --- /dev/null +++ b/kotlinx-coroutines-core/common/test/flow/sharing/StateInTest.kt @@ -0,0 +1,89 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.flow + +import kotlinx.coroutines.* +import kotlinx.coroutines.channels.* +import kotlin.test.* + +/** + * It is mostly covered by [ShareInTest], this just add state-specific checks. + */ +class StateInTest : TestBase() { + @Test + fun testOperatorFusion() = runTest { + val state = flowOf("OK").stateIn(this) + assertTrue(state !is MutableStateFlow<*>) // cannot be cast to mutable state flow + assertSame(state, (state as Flow<*>).cancellable()) + assertSame(state, (state as Flow<*>).distinctUntilChanged()) + assertSame(state, (state as Flow<*>).flowOn(Dispatchers.Default)) + assertSame(state, (state as Flow<*>).conflate()) + assertSame(state, state.buffer(Channel.CONFLATED)) + assertSame(state, state.buffer(Channel.RENDEZVOUS)) + assertSame(state, state.buffer(onBufferOverflow = BufferOverflow.DROP_OLDEST)) + assertSame(state, state.buffer(0, onBufferOverflow = BufferOverflow.DROP_OLDEST)) + assertSame(state, state.buffer(1, onBufferOverflow = BufferOverflow.DROP_OLDEST)) + coroutineContext.cancelChildren() + } + + @Test + fun testUpstreamCompletedNoInitialValue() = + testUpstreamCompletedOrFailedReset(failed = false, withInitialValue = false) + + @Test + fun testUpstreamFailedNoInitialValue() = + testUpstreamCompletedOrFailedReset(failed = true, withInitialValue = false) + + @Test + fun testUpstreamCompletedWithInitialValue() = + testUpstreamCompletedOrFailedReset(failed = false, withInitialValue = true) + + @Test + fun testUpstreamFailedWithInitialValue() = + testUpstreamCompletedOrFailedReset(failed = true, withInitialValue = true) + + private fun testUpstreamCompletedOrFailedReset(failed: Boolean, withInitialValue: Boolean) = runTest { + val emitted = Job() + val terminate = Job() + val sharingJob = CompletableDeferred() + val upstream = flow { + emit("OK") + emitted.complete() + terminate.join() + if (failed) throw TestException() + } + val scope = this + sharingJob + val shared: StateFlow + if (withInitialValue) { + shared = upstream.stateIn(scope, SharingStarted.Eagerly, null) + assertEquals(null, shared.value) + } else { + shared = upstream.stateIn(scope) + assertEquals("OK", shared.value) // waited until upstream emitted + } + emitted.join() // should start sharing, emit & cache + assertEquals("OK", shared.value) + terminate.complete() + sharingJob.complete(Unit) + sharingJob.join() // should complete sharing + assertEquals("OK", shared.value) // value is still there + if (failed) { + assertTrue(sharingJob.getCompletionExceptionOrNull() is TestException) + } else { + assertNull(sharingJob.getCompletionExceptionOrNull()) + } + } + + @Test + fun testUpstreamFailedImmediatelyWithInitialValue() = runTest { + val ceh = CoroutineExceptionHandler { _, _ -> expect(2) } + val flow = flow { + expect(1) + throw TestException() + } + assertFailsWith { flow.stateIn(CoroutineScope(currentCoroutineContext() + Job() + ceh)) } + finish(3) + } +} diff --git a/kotlinx-coroutines-core/common/test/flow/terminal/FirstTest.kt b/kotlinx-coroutines-core/common/test/flow/terminal/FirstTest.kt index f737a1d0de..fa7fc9cb6c 100644 --- a/kotlinx-coroutines-core/common/test/flow/terminal/FirstTest.kt +++ b/kotlinx-coroutines-core/common/test/flow/terminal/FirstTest.kt @@ -6,6 +6,7 @@ package kotlinx.coroutines.flow import kotlinx.coroutines.* import kotlinx.coroutines.channels.* +import kotlinx.coroutines.flow.internal.* import kotlin.test.* class FirstTest : TestBase() { @@ -127,6 +128,12 @@ class FirstTest : TestBase() { assertNull(emptyFlow().firstOrNull { true }) } + @Test + fun testFirstOrNullWithNullElement() = runTest { + assertNull(flowOf(null).firstOrNull()) + assertNull(flowOf(null).firstOrNull { true }) + } + @Test fun testFirstOrNullWhenErrorCancelsUpstream() = runTest { val latch = Channel() @@ -160,4 +167,13 @@ class FirstTest : TestBase() { assertSame(instance, flow.first { true }) assertSame(instance, flow.firstOrNull { true }) } + + @Test + fun testAbortFlowException() = runTest { + val flow = flow { + throw AbortFlowException(NopCollector) // Emulate cancellation + } + + assertFailsWith { flow.first() } + } } diff --git a/kotlinx-coroutines-core/common/test/flow/terminal/SingleTest.kt b/kotlinx-coroutines-core/common/test/flow/terminal/SingleTest.kt index 4e89b93bd7..2c1277b1e1 100644 --- a/kotlinx-coroutines-core/common/test/flow/terminal/SingleTest.kt +++ b/kotlinx-coroutines-core/common/test/flow/terminal/SingleTest.kt @@ -7,7 +7,7 @@ package kotlinx.coroutines.flow import kotlinx.coroutines.* import kotlin.test.* -class SingleTest : TestBase() { +class SingleTest : TestBase() { @Test fun testSingle() = runTest { @@ -25,8 +25,8 @@ class SingleTest : TestBase() { emit(239L) emit(240L) } - assertFailsWith { flow.single() } - assertFailsWith { flow.singleOrNull() } + assertFailsWith { flow.single() } + assertNull(flow.singleOrNull()) } @Test @@ -61,6 +61,10 @@ class SingleTest : TestBase() { assertEquals(1, flowOf(1).single()) assertNull(flowOf(null).single()) assertFailsWith { flowOf().single() } + + assertEquals(1, flowOf(1).singleOrNull()) + assertNull(flowOf(null).singleOrNull()) + assertNull(flowOf().singleOrNull()) } @Test @@ -69,5 +73,22 @@ class SingleTest : TestBase() { val flow = flowOf(instance) assertSame(instance, flow.single()) assertSame(instance, flow.singleOrNull()) + + val flow2 = flow { + emit(BadClass()) + emit(BadClass()) + } + assertFailsWith { flow2.single() } + } + + @Test + fun testSingleNoWait() = runTest { + val flow = flow { + emit(1) + emit(2) + awaitCancellation() + } + + assertNull(flow.singleOrNull()) } } diff --git a/kotlinx-coroutines-core/common/test/selects/SelectLoopTest.kt b/kotlinx-coroutines-core/common/test/selects/SelectLoopTest.kt index 5af68f6be5..e31ccfc16d 100644 --- a/kotlinx-coroutines-core/common/test/selects/SelectLoopTest.kt +++ b/kotlinx-coroutines-core/common/test/selects/SelectLoopTest.kt @@ -24,19 +24,20 @@ class SelectLoopTest : TestBase() { expect(3) throw TestException() } - var isDone = false - while (!isDone) { - select { - channel.onReceiveOrNull { - expect(4) - assertEquals(Unit, it) - } - job.onJoin { - expect(5) - isDone = true + try { + while (true) { + select { + channel.onReceiveOrNull { + expectUnreached() + } + job.onJoin { + expectUnreached() + } } } + } catch (e: CancellationException) { + // select will get cancelled because of the failure of job + finish(4) } - finish(6) } } \ No newline at end of file diff --git a/kotlinx-coroutines-core/js/src/Dispatchers.kt b/kotlinx-coroutines-core/js/src/Dispatchers.kt index 995801ea0d..06b938d41a 100644 --- a/kotlinx-coroutines-core/js/src/Dispatchers.kt +++ b/kotlinx-coroutines-core/js/src/Dispatchers.kt @@ -7,24 +7,19 @@ package kotlinx.coroutines import kotlin.coroutines.* public actual object Dispatchers { - public actual val Default: CoroutineDispatcher = createDefaultDispatcher() - - public actual val Main: MainCoroutineDispatcher = JsMainDispatcher(Default) - + public actual val Main: MainCoroutineDispatcher = JsMainDispatcher(Default, false) public actual val Unconfined: CoroutineDispatcher = kotlinx.coroutines.Unconfined } -private class JsMainDispatcher(val delegate: CoroutineDispatcher) : MainCoroutineDispatcher() { - - override val immediate: MainCoroutineDispatcher - get() = throw UnsupportedOperationException("Immediate dispatching is not supported on JS") - +private class JsMainDispatcher( + val delegate: CoroutineDispatcher, + private val invokeImmediately: Boolean +) : MainCoroutineDispatcher() { + override val immediate: MainCoroutineDispatcher = + if (invokeImmediately) this else JsMainDispatcher(delegate, true) + override fun isDispatchNeeded(context: CoroutineContext): Boolean = !invokeImmediately override fun dispatch(context: CoroutineContext, block: Runnable) = delegate.dispatch(context, block) - - override fun isDispatchNeeded(context: CoroutineContext): Boolean = delegate.isDispatchNeeded(context) - override fun dispatchYield(context: CoroutineContext, block: Runnable) = delegate.dispatchYield(context, block) - - override fun toString(): String = delegate.toString() + override fun toString(): String = toStringInternalImpl() ?: delegate.toString() } diff --git a/kotlinx-coroutines-core/js/src/Exceptions.kt b/kotlinx-coroutines-core/js/src/Exceptions.kt index 39b3344ac1..c82199a429 100644 --- a/kotlinx-coroutines-core/js/src/Exceptions.kt +++ b/kotlinx-coroutines-core/js/src/Exceptions.kt @@ -14,7 +14,7 @@ public actual open class CancellationException( message: String?, cause: Throwable? ) : IllegalStateException(message, cause) { - actual constructor(message: String?) : this(message, null) + public actual constructor(message: String?) : this(message, null) } /** diff --git a/kotlinx-coroutines-core/js/src/JSDispatcher.kt b/kotlinx-coroutines-core/js/src/JSDispatcher.kt index a0dfcba2b7..e1b3dcd7a9 100644 --- a/kotlinx-coroutines-core/js/src/JSDispatcher.kt +++ b/kotlinx-coroutines-core/js/src/JSDispatcher.kt @@ -35,7 +35,7 @@ internal sealed class SetTimeoutBasedDispatcher: CoroutineDispatcher(), Delay { messageQueue.enqueue(block) } - override fun invokeOnTimeout(timeMillis: Long, block: Runnable): DisposableHandle { + override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle { val handle = setTimeout({ block.run() }, delayToInt(timeMillis)) return ClearTimeout(handle) } @@ -81,7 +81,7 @@ internal class WindowDispatcher(private val window: Window) : CoroutineDispatche window.setTimeout({ with(continuation) { resumeUndispatched(Unit) } }, delayToInt(timeMillis)) } - override fun invokeOnTimeout(timeMillis: Long, block: Runnable): DisposableHandle { + override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle { val handle = window.setTimeout({ block.run() }, delayToInt(timeMillis)) return object : DisposableHandle { override fun dispose() { diff --git a/kotlinx-coroutines-core/js/src/Promise.kt b/kotlinx-coroutines-core/js/src/Promise.kt index 6c3de76426..ab2003236a 100644 --- a/kotlinx-coroutines-core/js/src/Promise.kt +++ b/kotlinx-coroutines-core/js/src/Promise.kt @@ -62,6 +62,8 @@ public fun Promise.asDeferred(): Deferred { * This suspending function is cancellable. * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function * stops waiting for the promise and immediately resumes with [CancellationException]. + * There is a **prompt cancellation guarantee**. If the job was cancelled while this function was + * suspended, it will not resume successfully. See [suspendCancellableCoroutine] documentation for low-level details. */ public suspend fun Promise.await(): T = suspendCancellableCoroutine { cont: CancellableContinuation -> this@await.then( diff --git a/kotlinx-coroutines-core/js/src/flow/internal/SafeCollector.kt b/kotlinx-coroutines-core/js/src/flow/internal/SafeCollector.kt index 78f1bdb2a9..ace633ccc6 100644 --- a/kotlinx-coroutines-core/js/src/flow/internal/SafeCollector.kt +++ b/kotlinx-coroutines-core/js/src/flow/internal/SafeCollector.kt @@ -4,6 +4,7 @@ package kotlinx.coroutines.flow.internal +import kotlinx.coroutines.* import kotlinx.coroutines.flow.* import kotlin.coroutines.* @@ -17,7 +18,8 @@ internal actual class SafeCollector actual constructor( private var lastEmissionContext: CoroutineContext? = null override suspend fun emit(value: T) { - val currentContext = coroutineContext + val currentContext = currentCoroutineContext() + currentContext.ensureActive() if (lastEmissionContext !== currentContext) { checkContext(currentContext) lastEmissionContext = currentContext diff --git a/kotlinx-coroutines-core/js/src/internal/LinkedList.kt b/kotlinx-coroutines-core/js/src/internal/LinkedList.kt index 7daeef2d94..b69850576e 100644 --- a/kotlinx-coroutines-core/js/src/internal/LinkedList.kt +++ b/kotlinx-coroutines-core/js/src/internal/LinkedList.kt @@ -2,12 +2,11 @@ * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ -@file:Suppress("unused") +@file:Suppress("unused", "NO_EXPLICIT_RETURN_TYPE_IN_API_MODE", "NO_EXPLICIT_VISIBILITY_IN_API_MODE") package kotlinx.coroutines.internal private typealias Node = LinkedListNode - /** @suppress **This is unstable API and it is subject to change.** */ @Suppress("NO_ACTUAL_CLASS_MEMBER_FOR_EXPECTED_CLASS") // :TODO: Remove when fixed: https://youtrack.jetbrains.com/issue/KT-23703 public actual typealias LockFreeLinkedListNode = LinkedListNode @@ -125,6 +124,8 @@ public actual abstract class AbstractAtomicDesc : AtomicDesc() { return null } + actual open fun onRemoved(affected: Node) {} + actual final override fun prepare(op: AtomicOp<*>): Any? { val affected = affectedNode val failure = failure(affected) diff --git a/kotlinx-coroutines-core/js/src/internal/LocalAtomics.kt b/kotlinx-coroutines-core/js/src/internal/LocalAtomics.kt new file mode 100644 index 0000000000..fffd76c452 --- /dev/null +++ b/kotlinx-coroutines-core/js/src/internal/LocalAtomics.kt @@ -0,0 +1,15 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.internal + +internal actual class LocalAtomicInt actual constructor(private var value: Int) { + actual fun set(value: Int) { + this.value = value + } + + actual fun get(): Int = value + + actual fun decrementAndGet(): Int = --value +} diff --git a/kotlinx-coroutines-core/js/test/ImmediateDispatcherTest.kt b/kotlinx-coroutines-core/js/test/ImmediateDispatcherTest.kt new file mode 100644 index 0000000000..7ca6a242b2 --- /dev/null +++ b/kotlinx-coroutines-core/js/test/ImmediateDispatcherTest.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines + +import kotlin.test.* + +class ImmediateDispatcherTest : TestBase() { + + @Test + fun testImmediate() = runTest { + expect(1) + val job = launch { expect(3) } + withContext(Dispatchers.Main.immediate) { + expect(2) + } + job.join() + finish(4) + } + + @Test + fun testMain() = runTest { + expect(1) + val job = launch { expect(2) } + withContext(Dispatchers.Main) { + expect(3) + } + job.join() + finish(4) + } +} diff --git a/kotlinx-coroutines-core/jvm/resources/DebugProbesKt.bin b/kotlinx-coroutines-core/jvm/resources/DebugProbesKt.bin new file mode 100644 index 0000000000..76ee41159d Binary files /dev/null and b/kotlinx-coroutines-core/jvm/resources/DebugProbesKt.bin differ diff --git a/kotlinx-coroutines-core/jvm/resources/META-INF/proguard/coroutines.pro b/kotlinx-coroutines-core/jvm/resources/META-INF/proguard/coroutines.pro index 2f9a2daee5..60c8d61243 100644 --- a/kotlinx-coroutines-core/jvm/resources/META-INF/proguard/coroutines.pro +++ b/kotlinx-coroutines-core/jvm/resources/META-INF/proguard/coroutines.pro @@ -1,8 +1,6 @@ # ServiceLoader support -keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {} -keepnames class kotlinx.coroutines.CoroutineExceptionHandler {} --keepnames class kotlinx.coroutines.android.AndroidExceptionPreHandler {} --keepnames class kotlinx.coroutines.android.AndroidDispatcherFactory {} # Most of volatile fields are updated with AFU and should not be mangled -keepclassmembernames class kotlinx.** { @@ -12,4 +10,11 @@ # Same story for the standard library's SafeContinuation that also uses AtomicReferenceFieldUpdater -keepclassmembernames class kotlin.coroutines.SafeContinuation { volatile ; -} \ No newline at end of file +} + +# These classes are only required by kotlinx.coroutines.debug.AgentPremain, which is only loaded when +# kotlinx-coroutines-core is used as a Java agent, so these are not needed in contexts where ProGuard is used. +-dontwarn java.lang.instrument.ClassFileTransformer +-dontwarn sun.misc.SignalHandler +-dontwarn java.lang.instrument.Instrumentation +-dontwarn sun.misc.Signal diff --git a/kotlinx-coroutines-core/jvm/src/Builders.kt b/kotlinx-coroutines-core/jvm/src/Builders.kt index b8a250fef6..e4504ccdd4 100644 --- a/kotlinx-coroutines-core/jvm/src/Builders.kt +++ b/kotlinx-coroutines-core/jvm/src/Builders.kt @@ -4,10 +4,12 @@ @file:JvmMultifileClass @file:JvmName("BuildersKt") +@file:OptIn(ExperimentalContracts::class) package kotlinx.coroutines import java.util.concurrent.locks.* +import kotlin.contracts.* import kotlin.coroutines.* /** @@ -34,6 +36,9 @@ import kotlin.coroutines.* */ @Throws(InterruptedException::class) public fun runBlocking(context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> T): T { + contract { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + } val currentThread = Thread.currentThread() val contextInterceptor = context[ContinuationInterceptor] val eventLoop: EventLoop? diff --git a/kotlinx-coroutines-core/jvm/src/CommonPool.kt b/kotlinx-coroutines-core/jvm/src/CommonPool.kt index 60f30cfe14..2203313120 100644 --- a/kotlinx-coroutines-core/jvm/src/CommonPool.kt +++ b/kotlinx-coroutines-core/jvm/src/CommonPool.kt @@ -103,6 +103,8 @@ internal object CommonPool : ExecutorCoroutineDispatcher() { (pool ?: getOrCreatePoolSync()).execute(wrapTask(block)) } catch (e: RejectedExecutionException) { unTrackTask() + // CommonPool only rejects execution when it is being closed and this behavior is reserved + // for testing purposes, so we don't have to worry about cancelling the affected Job here. DefaultExecutor.enqueue(block) } } diff --git a/kotlinx-coroutines-core/jvm/src/Debug.kt b/kotlinx-coroutines-core/jvm/src/Debug.kt index aac06ade79..8108d235c1 100644 --- a/kotlinx-coroutines-core/jvm/src/Debug.kt +++ b/kotlinx-coroutines-core/jvm/src/Debug.kt @@ -33,7 +33,7 @@ import kotlin.internal.InlineOnly * Debugging facilities are implemented by [newCoroutineContext][CoroutineScope.newCoroutineContext] function that * is used in all coroutine builders to create context of a new coroutine. */ -public const val DEBUG_PROPERTY_NAME = "kotlinx.coroutines.debug" +public const val DEBUG_PROPERTY_NAME: String = "kotlinx.coroutines.debug" /** * Name of the boolean property that controls stacktrace recovery (enabled by default) on JVM. @@ -51,17 +51,17 @@ internal const val STACKTRACE_RECOVERY_PROPERTY_NAME = "kotlinx.coroutines.stack /** * Automatic debug configuration value for [DEBUG_PROPERTY_NAME]. */ -public const val DEBUG_PROPERTY_VALUE_AUTO = "auto" +public const val DEBUG_PROPERTY_VALUE_AUTO: String = "auto" /** * Debug turned on value for [DEBUG_PROPERTY_NAME]. */ -public const val DEBUG_PROPERTY_VALUE_ON = "on" +public const val DEBUG_PROPERTY_VALUE_ON: String = "on" /** * Debug turned on value for [DEBUG_PROPERTY_NAME]. */ -public const val DEBUG_PROPERTY_VALUE_OFF = "off" +public const val DEBUG_PROPERTY_VALUE_OFF: String = "off" // @JvmField: Don't use JvmField here to enable R8 optimizations via "assumenosideeffects" internal val ASSERTIONS_ENABLED = CoroutineId::class.java.desiredAssertionStatus() diff --git a/kotlinx-coroutines-core/jvm/src/DebugStrings.kt b/kotlinx-coroutines-core/jvm/src/DebugStrings.kt index 184fb655e3..2ccfebc6d3 100644 --- a/kotlinx-coroutines-core/jvm/src/DebugStrings.kt +++ b/kotlinx-coroutines-core/jvm/src/DebugStrings.kt @@ -4,6 +4,7 @@ package kotlinx.coroutines +import kotlinx.coroutines.internal.* import kotlin.coroutines.* // internal debugging tools for string representation diff --git a/kotlinx-coroutines-core/jvm/src/DefaultExecutor.kt b/kotlinx-coroutines-core/jvm/src/DefaultExecutor.kt index 4e107a7b1d..787cbf9c44 100644 --- a/kotlinx-coroutines-core/jvm/src/DefaultExecutor.kt +++ b/kotlinx-coroutines-core/jvm/src/DefaultExecutor.kt @@ -5,6 +5,7 @@ package kotlinx.coroutines import java.util.concurrent.* +import kotlin.coroutines.* internal actual val DefaultDelay: Delay = DefaultExecutor @@ -54,7 +55,7 @@ internal actual object DefaultExecutor : EventLoopImplBase(), Runnable { * Livelock is possible only if `runBlocking` is called on internal default executed (which is used by default [delay]), * but it's not exposed as public API. */ - override fun invokeOnTimeout(timeMillis: Long, block: Runnable): DisposableHandle = + override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle = scheduleInvokeOnTimeout(timeMillis, block) override fun run() { @@ -68,15 +69,13 @@ internal actual object DefaultExecutor : EventLoopImplBase(), Runnable { var parkNanos = processNextEvent() if (parkNanos == Long.MAX_VALUE) { // nothing to do, initialize shutdown timeout - if (shutdownNanos == Long.MAX_VALUE) { - val now = nanoTime() - if (shutdownNanos == Long.MAX_VALUE) shutdownNanos = now + KEEP_ALIVE_NANOS - val tillShutdown = shutdownNanos - now - if (tillShutdown <= 0) return // shut thread down - parkNanos = parkNanos.coerceAtMost(tillShutdown) - } else - parkNanos = parkNanos.coerceAtMost(KEEP_ALIVE_NANOS) // limit wait time anyway - } + val now = nanoTime() + if (shutdownNanos == Long.MAX_VALUE) shutdownNanos = now + KEEP_ALIVE_NANOS + val tillShutdown = shutdownNanos - now + if (tillShutdown <= 0) return // shut thread down + parkNanos = parkNanos.coerceAtMost(tillShutdown) + } else + shutdownNanos = Long.MAX_VALUE if (parkNanos > 0) { // check if shutdown was requested and bail out in this case if (isShutdownRequested) return @@ -142,4 +141,7 @@ internal actual object DefaultExecutor : EventLoopImplBase(), Runnable { resetAll() // clear queues (this as Object).notifyAll() } + + internal val isThreadPresent + get() = _thread != null } diff --git a/kotlinx-coroutines-core/jvm/src/Dispatchers.kt b/kotlinx-coroutines-core/jvm/src/Dispatchers.kt index b9df7ab2e6..8033fb38e5 100644 --- a/kotlinx-coroutines-core/jvm/src/Dispatchers.kt +++ b/kotlinx-coroutines-core/jvm/src/Dispatchers.kt @@ -14,7 +14,7 @@ import kotlin.coroutines.* /** * Name of the property that defines the maximal number of threads that are used by [Dispatchers.IO] coroutines dispatcher. */ -public const val IO_PARALLELISM_PROPERTY_NAME = "kotlinx.coroutines.io.parallelism" +public const val IO_PARALLELISM_PROPERTY_NAME: String = "kotlinx.coroutines.io.parallelism" /** * Groups various implementations of [CoroutineDispatcher]. @@ -97,7 +97,7 @@ public actual object Dispatchers { * The [CoroutineDispatcher] that is designed for offloading blocking IO tasks to a shared pool of threads. * * Additional threads in this pool are created and are shutdown on demand. - * The number of threads used by this dispatcher is limited by the value of + * The number of threads used by tasks in this dispatcher is limited by the value of * "`kotlinx.coroutines.io.parallelism`" ([IO_PARALLELISM_PROPERTY_NAME]) system property. * It defaults to the limit of 64 threads or the number of cores (whichever is larger). * @@ -106,9 +106,13 @@ public actual object Dispatchers { * If you need a higher number of parallel threads, * you should use a custom dispatcher backed by your own thread pool. * + * ### Implementation note + * * This dispatcher shares threads with a [Default][Dispatchers.Default] dispatcher, so using * `withContext(Dispatchers.IO) { ... }` does not lead to an actual switching to another thread — * typically execution continues in the same thread. + * As a result of thread sharing, more than 64 (default parallelism) threads can be created (but not used) + * during operations over IO dispatcher. */ @JvmStatic public val IO: CoroutineDispatcher = DefaultScheduler.IO diff --git a/kotlinx-coroutines-core/jvm/src/Executors.kt b/kotlinx-coroutines-core/jvm/src/Executors.kt index a4d6b46c43..8ffc22d8bb 100644 --- a/kotlinx-coroutines-core/jvm/src/Executors.kt +++ b/kotlinx-coroutines-core/jvm/src/Executors.kt @@ -14,7 +14,7 @@ import kotlin.coroutines.* * Instances of [ExecutorCoroutineDispatcher] should be closed by the owner of the dispatcher. * * This class is generally used as a bridge between coroutine-based API and - * asynchronous API which requires instance of the [Executor]. + * asynchronous API that requires an instance of the [Executor]. */ public abstract class ExecutorCoroutineDispatcher: CoroutineDispatcher(), Closeable { /** @suppress */ @@ -38,6 +38,12 @@ public abstract class ExecutorCoroutineDispatcher: CoroutineDispatcher(), Closea /** * Converts an instance of [ExecutorService] to an implementation of [ExecutorCoroutineDispatcher]. + * + * If the underlying executor throws [RejectedExecutionException] on + * attempt to submit a continuation task (it happens when [closing][ExecutorCoroutineDispatcher.close] the + * resulting dispatcher, on underlying executor [shutdown][ExecutorService.shutdown], or when it uses limited queues), + * then the [Job] of the affected task is [cancelled][Job.cancel] and the task is submitted to the + * [Dispatchers.IO], so that the affected coroutine can cleanup its resources and promptly complete. */ @JvmName("from") // this is for a nice Java API, see issue #255 public fun ExecutorService.asCoroutineDispatcher(): ExecutorCoroutineDispatcher = @@ -45,6 +51,12 @@ public fun ExecutorService.asCoroutineDispatcher(): ExecutorCoroutineDispatcher /** * Converts an instance of [Executor] to an implementation of [CoroutineDispatcher]. + * + * If the underlying executor throws [RejectedExecutionException] on + * attempt to submit a continuation task (it happens when [closing][ExecutorCoroutineDispatcher.close] the + * resulting dispatcher, on underlying executor [shutdown][ExecutorService.shutdown], or when it uses limited queues), + * then the [Job] of the affected task is [cancelled][Job.cancel] and the task is submitted to the + * [Dispatchers.IO], so that the affected coroutine can cleanup its resources and promptly complete. */ @JvmName("from") // this is for a nice Java API, see issue #255 public fun Executor.asCoroutineDispatcher(): CoroutineDispatcher = @@ -82,7 +94,8 @@ internal abstract class ExecutorCoroutineDispatcherBase : ExecutorCoroutineDispa executor.execute(wrapTask(block)) } catch (e: RejectedExecutionException) { unTrackTask() - DefaultExecutor.enqueue(block) + cancelJobOnRejection(context, e) + Dispatchers.IO.dispatch(context, block) } } @@ -93,7 +106,7 @@ internal abstract class ExecutorCoroutineDispatcherBase : ExecutorCoroutineDispa */ override fun scheduleResumeAfterDelay(timeMillis: Long, continuation: CancellableContinuation) { val future = if (removesFutureOnCancellation) { - scheduleBlock(ResumeUndispatchedRunnable(this, continuation), timeMillis, TimeUnit.MILLISECONDS) + scheduleBlock(ResumeUndispatchedRunnable(this, continuation), continuation.context, timeMillis) } else { null } @@ -106,24 +119,31 @@ internal abstract class ExecutorCoroutineDispatcherBase : ExecutorCoroutineDispa DefaultExecutor.scheduleResumeAfterDelay(timeMillis, continuation) } - override fun invokeOnTimeout(timeMillis: Long, block: Runnable): DisposableHandle { + override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle { val future = if (removesFutureOnCancellation) { - scheduleBlock(block, timeMillis, TimeUnit.MILLISECONDS) + scheduleBlock(block, context, timeMillis) } else { null } - - return if (future != null ) DisposableFutureHandle(future) else DefaultExecutor.invokeOnTimeout(timeMillis, block) + return when { + future != null -> DisposableFutureHandle(future) + else -> DefaultExecutor.invokeOnTimeout(timeMillis, block, context) + } } - private fun scheduleBlock(block: Runnable, time: Long, unit: TimeUnit): ScheduledFuture<*>? { + private fun scheduleBlock(block: Runnable, context: CoroutineContext, timeMillis: Long): ScheduledFuture<*>? { return try { - (executor as? ScheduledExecutorService)?.schedule(block, time, unit) + (executor as? ScheduledExecutorService)?.schedule(block, timeMillis, TimeUnit.MILLISECONDS) } catch (e: RejectedExecutionException) { + cancelJobOnRejection(context, e) null } } + private fun cancelJobOnRejection(context: CoroutineContext, exception: RejectedExecutionException) { + context.cancel(CancellationException("The task was rejected", exception)) + } + override fun close() { (executor as? ExecutorService)?.shutdown() } diff --git a/kotlinx-coroutines-core/jvm/src/Future.kt b/kotlinx-coroutines-core/jvm/src/Future.kt index 54825c309d..bd16f49af0 100644 --- a/kotlinx-coroutines-core/jvm/src/Future.kt +++ b/kotlinx-coroutines-core/jvm/src/Future.kt @@ -29,7 +29,7 @@ public fun Job.cancelFutureOnCompletion(future: Future<*>): DisposableHandle = * invokeOnCancellation { future.cancel(false) } * ``` */ -public fun CancellableContinuation<*>.cancelFutureOnCancellation(future: Future<*>) = +public fun CancellableContinuation<*>.cancelFutureOnCancellation(future: Future<*>): Unit = invokeOnCancellation(handler = CancelFutureOnCancel(future)) private class CancelFutureOnCompletion( diff --git a/kotlinx-coroutines-core/jvm/src/Interruptible.kt b/kotlinx-coroutines-core/jvm/src/Interruptible.kt new file mode 100644 index 0000000000..070aa62497 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/src/Interruptible.kt @@ -0,0 +1,156 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines + +import kotlinx.atomicfu.* +import kotlin.coroutines.* + +/** + * Calls the specified [block] with a given coroutine context in a interruptible manner. + * The blocking code block will be interrupted and this function will throw [CancellationException] + * if the coroutine is cancelled. + * + * Example: + * + * ``` + * withTimeout(500L) { // Cancels coroutine on timeout + * runInterruptible { // Throws CancellationException if interrupted + * doSomethingBlocking() // Interrupted on coroutines cancellation + * } + * } + * ``` + * + * There is an optional [context] parameter to this function working just like [withContext]. + * It enables single-call conversion of interruptible Java methods into suspending functions. + * With one call here we are moving the call to [Dispatchers.IO] and supporting interruption: + * + * ``` + * suspend fun BlockingQueue.awaitTake(): T = + * runInterruptible(Dispatchers.IO) { queue.take() } + * ``` + */ +public suspend fun runInterruptible( + context: CoroutineContext = EmptyCoroutineContext, + block: () -> T +): T = withContext(context) { + runInterruptibleInExpectedContext(coroutineContext, block) +} + +private fun runInterruptibleInExpectedContext(coroutineContext: CoroutineContext, block: () -> T): T { + try { + val threadState = ThreadState(coroutineContext.job) + threadState.setup() + try { + return block() + } finally { + threadState.clearInterrupt() + } + } catch (e: InterruptedException) { + throw CancellationException("Blocking call was interrupted due to parent cancellation").initCause(e) + } +} + +private const val WORKING = 0 +private const val FINISHED = 1 +private const val INTERRUPTING = 2 +private const val INTERRUPTED = 3 + +private class ThreadState(private val job: Job) : CompletionHandler { + /* + === States === + + WORKING: running normally + FINISH: complete normally + INTERRUPTING: canceled, going to interrupt this thread + INTERRUPTED: this thread is interrupted + + === Possible Transitions === + + +----------------+ register job +-------------------------+ + | WORKING | cancellation listener | WORKING | + | (thread, null) | -------------------------> | (thread, cancel handle) | + +----------------+ +-------------------------+ + | | | + | cancel cancel | | complete + | | | + V | | + +---------------+ | | + | INTERRUPTING | <--------------------------------------+ | + +---------------+ | + | | + | interrupt | + | | + V V + +---------------+ +-------------------------+ + | INTERRUPTED | | FINISHED | + +---------------+ +-------------------------+ + */ + private val _state = atomic(WORKING) + private val targetThread = Thread.currentThread() + + // Registered cancellation handler + private var cancelHandle: DisposableHandle? = null + + fun setup() { + cancelHandle = job.invokeOnCompletion(onCancelling = true, invokeImmediately = true, handler = this) + // Either we successfully stored it or it was immediately cancelled + _state.loop { state -> + when (state) { + // Happy-path, move forward + WORKING -> if (_state.compareAndSet(state, WORKING)) return + // Immediately cancelled, just continue + INTERRUPTING, INTERRUPTED -> return + else -> invalidState(state) + } + } + } + + fun clearInterrupt() { + /* + * Do not allow to untriggered interrupt to leak + */ + _state.loop { state -> + when (state) { + WORKING -> if (_state.compareAndSet(state, FINISHED)) { + cancelHandle?.dispose() + return + } + INTERRUPTING -> { + /* + * Spin, cancellation mechanism is interrupting our thread right now + * and we have to wait it and then clear interrupt status + */ + } + INTERRUPTED -> { + // Clear it and bail out + Thread.interrupted() + return + } + else -> invalidState(state) + } + } + } + + // Cancellation handler + override fun invoke(cause: Throwable?) { + _state.loop { state -> + when (state) { + // Working -> try to transite state and interrupt the thread + WORKING -> { + if (_state.compareAndSet(state, INTERRUPTING)) { + targetThread.interrupt() + _state.value = INTERRUPTED + return + } + } + // Finished -- runInterruptible is already complete, INTERRUPTING - ignore + FINISHED, INTERRUPTING, INTERRUPTED -> return + else -> invalidState(state) + } + } + } + + private fun invalidState(state: Int): Nothing = error("Illegal state $state") +} diff --git a/kotlinx-coroutines-core/jvm/src/ThreadPoolDispatcher.kt b/kotlinx-coroutines-core/jvm/src/ThreadPoolDispatcher.kt index 7291f0c4fc..aa18cd38d6 100644 --- a/kotlinx-coroutines-core/jvm/src/ThreadPoolDispatcher.kt +++ b/kotlinx-coroutines-core/jvm/src/ThreadPoolDispatcher.kt @@ -14,6 +14,11 @@ import kotlin.coroutines.* * **NOTE: The resulting [ExecutorCoroutineDispatcher] owns native resources (its thread). * Resources are reclaimed by [ExecutorCoroutineDispatcher.close].** * + * If the resulting dispatcher is [closed][ExecutorCoroutineDispatcher.close] and + * attempt to submit a continuation task is made, + * then the [Job] of the affected task is [cancelled][Job.cancel] and the task is submitted to the + * [Dispatchers.IO], so that the affected coroutine can cleanup its resources and promptly complete. + * * **NOTE: This API will be replaced in the future**. A different API to create thread-limited thread pools * that is based on a shared thread-pool and does not require the resulting dispatcher to be explicitly closed * will be provided, thus avoiding potential thread leaks and also significantly improving performance, due @@ -27,7 +32,7 @@ import kotlin.coroutines.* * @param name the base name of the created thread. */ @ObsoleteCoroutinesApi -fun newSingleThreadContext(name: String): ExecutorCoroutineDispatcher = +public fun newSingleThreadContext(name: String): ExecutorCoroutineDispatcher = newFixedThreadPoolContext(1, name) /** @@ -35,6 +40,11 @@ fun newSingleThreadContext(name: String): ExecutorCoroutineDispatcher = * **NOTE: The resulting [ExecutorCoroutineDispatcher] owns native resources (its threads). * Resources are reclaimed by [ExecutorCoroutineDispatcher.close].** * + * If the resulting dispatcher is [closed][ExecutorCoroutineDispatcher.close] and + * attempt to submit a continuation task is made, + * then the [Job] of the affected task is [cancelled][Job.cancel] and the task is submitted to the + * [Dispatchers.IO], so that the affected coroutine can cleanup its resources and promptly complete. + * * **NOTE: This API will be replaced in the future**. A different API to create thread-limited thread pools * that is based on a shared thread-pool and does not require the resulting dispatcher to be explicitly closed * will be provided, thus avoiding potential thread leaks and also significantly improving performance, due @@ -49,7 +59,7 @@ fun newSingleThreadContext(name: String): ExecutorCoroutineDispatcher = * @param name the base name of the created threads. */ @ObsoleteCoroutinesApi -fun newFixedThreadPoolContext(nThreads: Int, name: String): ExecutorCoroutineDispatcher { +public fun newFixedThreadPoolContext(nThreads: Int, name: String): ExecutorCoroutineDispatcher { require(nThreads >= 1) { "Expected at least one thread, but $nThreads specified" } return ThreadPoolDispatcher(nThreads, name) } diff --git a/kotlinx-coroutines-core/jvm/src/channels/Actor.kt b/kotlinx-coroutines-core/jvm/src/channels/Actor.kt index ea48630b12..a905426585 100644 --- a/kotlinx-coroutines-core/jvm/src/channels/Actor.kt +++ b/kotlinx-coroutines-core/jvm/src/channels/Actor.kt @@ -25,7 +25,7 @@ public interface ActorScope : CoroutineScope, ReceiveChannel { * All the [ReceiveChannel] functions on this interface delegate to * the channel instance returned by this function. */ - val channel: Channel + public val channel: Channel } /** diff --git a/kotlinx-coroutines-core/jvm/src/channels/TickerChannels.kt b/kotlinx-coroutines-core/jvm/src/channels/TickerChannels.kt index 0f9321a58d..1e6797accc 100644 --- a/kotlinx-coroutines-core/jvm/src/channels/TickerChannels.kt +++ b/kotlinx-coroutines-core/jvm/src/channels/TickerChannels.kt @@ -13,7 +13,7 @@ import kotlin.coroutines.* * **Note: Ticker channels are not currently integrated with structured concurrency and their api will change in the future.** */ @ObsoleteCoroutinesApi -enum class TickerMode { +public enum class TickerMode { /** * Adjust delay to maintain fixed period if consumer cannot keep up or is otherwise slow. * **This is a default mode.** diff --git a/kotlinx-coroutines-core/jvm/src/debug/AgentPremain.kt b/kotlinx-coroutines-core/jvm/src/debug/AgentPremain.kt new file mode 100644 index 0000000000..5a1a1ed1b7 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/src/debug/AgentPremain.kt @@ -0,0 +1,75 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.debug + +import kotlinx.coroutines.debug.internal.DebugProbesImpl +import sun.misc.* +import java.lang.instrument.* +import java.lang.instrument.ClassFileTransformer +import java.security.* +import android.annotation.* + +/* + * This class is loaded if and only if kotlinx-coroutines-core was used as -javaagent argument, + * but Android complains anyway (java.lang.instrument.*), so we suppress all lint checks here + */ +@Suppress("unused") +@SuppressLint("all") +internal object AgentPremain { + + public var isInstalledStatically = false + + private val enableCreationStackTraces = runCatching { + System.getProperty("kotlinx.coroutines.debug.enable.creation.stack.trace")?.toBoolean() + }.getOrNull() ?: DebugProbesImpl.enableCreationStackTraces + + @JvmStatic + public fun premain(args: String?, instrumentation: Instrumentation) { + isInstalledStatically = true + instrumentation.addTransformer(DebugProbesTransformer) + DebugProbesImpl.enableCreationStackTraces = enableCreationStackTraces + DebugProbesImpl.install() + installSignalHandler() + } + + internal object DebugProbesTransformer : ClassFileTransformer { + override fun transform( + loader: ClassLoader, + className: String, + classBeingRedefined: Class<*>?, + protectionDomain: ProtectionDomain, + classfileBuffer: ByteArray? + ): ByteArray? { + if (className != "kotlin/coroutines/jvm/internal/DebugProbesKt") { + return null + } + /* + * DebugProbesKt.bin contains `kotlin.coroutines.jvm.internal.DebugProbesKt` class + * with method bodies that delegate all calls directly to their counterparts in + * kotlinx.coroutines.debug.DebugProbesImpl. This is done to avoid classfile patching + * on the fly (-> get rid of ASM dependency). + * You can verify its content either by using javap on it or looking at out integration test module. + */ + isInstalledStatically = true + return loader.getResourceAsStream("DebugProbesKt.bin").readBytes() + } + } + + private fun installSignalHandler() { + try { + Signal.handle(Signal("TRAP")) { // kill -5 + if (DebugProbesImpl.isInstalled) { + // Case with 'isInstalled' changed between this check-and-act is not considered + // a real debug probes use-case, thus is not guarded against. + DebugProbesImpl.dumpCoroutines(System.out) + } else { + println("Cannot perform coroutines dump, debug probes are disabled") + } + } + } catch (t: Throwable) { + // Do nothing, signal cannot be installed, e.g. because we are on Windows + } + } +} diff --git a/kotlinx-coroutines-core/jvm/src/debug/internal/ConcurrentWeakMap.kt b/kotlinx-coroutines-core/jvm/src/debug/internal/ConcurrentWeakMap.kt new file mode 100644 index 0000000000..79f024cc93 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/src/debug/internal/ConcurrentWeakMap.kt @@ -0,0 +1,284 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.debug.internal + +import kotlinx.atomicfu.* +import kotlinx.coroutines.internal.* +import java.lang.ref.* + +// This is very limited implementation, not suitable as a generic map replacement. +// It has lock-free get and put with synchronized rehash for simplicity (and better CPU usage on contention) +@OptIn(ExperimentalStdlibApi::class) +@Suppress("UNCHECKED_CAST") +internal class ConcurrentWeakMap( + /** + * Weak reference queue is needed when a small key is mapped to a large value and we need to promptly release a + * reference to the value when the key was already disposed. + */ + weakRefQueue: Boolean = false +) : AbstractMutableMap() { + private val _size = atomic(0) + private val core = atomic(Core(MIN_CAPACITY)) + private val weakRefQueue: ReferenceQueue? = if (weakRefQueue) ReferenceQueue() else null + + override val size: Int + get() = _size.value + + private fun decrementSize() { _size.decrementAndGet() } + + override fun get(key: K): V? = core.value.getImpl(key) + + override fun put(key: K, value: V): V? { + var oldValue = core.value.putImpl(key, value) + if (oldValue === REHASH) oldValue = putSynchronized(key, value) + if (oldValue == null) _size.incrementAndGet() + return oldValue as V? + } + + override fun remove(key: K): V? { + var oldValue = core.value.putImpl(key, null) + if (oldValue === REHASH) oldValue = putSynchronized(key, null) + if (oldValue != null) _size.decrementAndGet() + return oldValue as V? + } + + @Synchronized + private fun putSynchronized(key: K, value: V?): V? { + // Note: concurrent put leaves chance that we fail to put even after rehash, we retry until successful + var curCore = core.value + while (true) { + val oldValue = curCore.putImpl(key, value) + if (oldValue !== REHASH) return oldValue as V? + curCore = curCore.rehash() + core.value = curCore + } + } + + override val keys: MutableSet + get() = KeyValueSet { k, _ -> k } + + override val entries: MutableSet> + get() = KeyValueSet { k, v -> Entry(k, v) } + + // We don't care much about clear's efficiency + override fun clear() { + for (k in keys) remove(k) + } + + fun runWeakRefQueueCleaningLoopUntilInterrupted() { + check(weakRefQueue != null) { "Must be created with weakRefQueue = true" } + try { + while (true) { + cleanWeakRef(weakRefQueue.remove() as HashedWeakRef<*>) + } + } catch(e: InterruptedException) { + Thread.currentThread().interrupt() + } + } + + private fun cleanWeakRef(w: HashedWeakRef<*>) { + core.value.cleanWeakRef(w) + } + + @Suppress("UNCHECKED_CAST") + private inner class Core(private val allocated: Int) { + private val shift = allocated.countLeadingZeroBits() + 1 + private val threshold = 2 * allocated / 3 // max fill factor at 66% to ensure speedy lookups + private val load = atomic(0) // counts how many slots are occupied in this core + private val keys = atomicArrayOfNulls?>(allocated) + private val values = atomicArrayOfNulls(allocated) + + private fun index(hash: Int) = (hash * MAGIC) ushr shift + + // get is always lock-free, unwraps the value that was marked by concurrent rehash + fun getImpl(key: K): V? { + var index = index(key.hashCode()) + while (true) { + val w = keys[index].value ?: return null // not found + val k = w.get() + if (key == k) { + val value = values[index].value + return (if (value is Marked) value.ref else value) as V? + } + if (k == null) removeCleanedAt(index) // weak ref was here, but collected + if (index == 0) index = allocated + index-- + } + } + + private fun removeCleanedAt(index: Int) { + while (true) { + val oldValue = values[index].value ?: return // return when already removed + if (oldValue is Marked) return // cannot remove marked (rehash is working on it, will not copy) + if (values[index].compareAndSet(oldValue, null)) { // removed + decrementSize() + return + } + } + } + + // returns REHASH when rehash is needed (the value was not put) + fun putImpl(key: K, value: V?, weakKey0: HashedWeakRef? = null): Any? { + var index = index(key.hashCode()) + var loadIncremented = false + var weakKey: HashedWeakRef? = weakKey0 + while (true) { + val w = keys[index].value + if (w == null) { // slot empty => not found => try reserving slot + if (value == null) return null // removing missing value, nothing to do here + if (!loadIncremented) { + // We must increment load before we even try to occupy a slot to avoid overfill during concurrent put + load.update { n -> + if (n >= threshold) return REHASH // the load is already too big -- rehash + n + 1 // otherwise increment + } + loadIncremented = true + } + if (weakKey == null) weakKey = HashedWeakRef(key, weakRefQueue) + if (keys[index].compareAndSet(null, weakKey)) break // slot reserved !!! + continue // retry at this slot on CAS failure (somebody already reserved this slot) + } + val k = w.get() + if (key == k) { // found already reserved slot at index + if (loadIncremented) load.decrementAndGet() // undo increment, because found a slot + break + } + if (k == null) removeCleanedAt(index) // weak ref was here, but collected + if (index == 0) index = allocated + index-- + } + // update value + var oldValue: Any? + while (true) { + oldValue = values[index].value + if (oldValue is Marked) return REHASH // rehash started, cannot work here + if (values[index].compareAndSet(oldValue, value)) break + } + return oldValue as V? + } + + // only one thread can rehash, but may have concurrent puts/gets + fun rehash(): Core { + // use size to approximate new required capacity to have at least 25-50% fill factor, + // may fail due to concurrent modification, will retry + retry@while (true) { + val newCapacity = size.coerceAtLeast(MIN_CAPACITY / 4).takeHighestOneBit() * 4 + val newCore = Core(newCapacity) + for (index in 0 until allocated) { + // load the key + val w = keys[index].value + val k = w?.get() + if (w != null && k == null) removeCleanedAt(index) // weak ref was here, but collected + // mark value so that it cannot be changed while we rehash to new core + var value: Any? + while (true) { + value = values[index].value + if (value is Marked) { // already marked -- good + value = value.ref + break + } + // try mark + if (values[index].compareAndSet(value, value.mark())) break + } + if (k != null && value != null) { + val oldValue = newCore.putImpl(k, value as V, w) + if (oldValue === REHASH) continue@retry // retry if we underestimated capacity + assert(oldValue == null) + } + } + return newCore // rehashed everything successfully + } + } + + fun cleanWeakRef(weakRef: HashedWeakRef<*>) { + var index = index(weakRef.hash) + while (true) { + val w = keys[index].value ?: return // return when slots are over + if (w === weakRef) { // found + removeCleanedAt(index) + return + } + if (index == 0) index = allocated + index-- + } + } + + fun keyValueIterator(factory: (K, V) -> E): MutableIterator = KeyValueIterator(factory) + + private inner class KeyValueIterator(private val factory: (K, V) -> E) : MutableIterator { + private var index = -1 + private lateinit var key: K + private lateinit var value: V + + init { findNext() } + + private fun findNext() { + while (++index < allocated) { + key = keys[index].value?.get() ?: continue + var value = values[index].value + if (value is Marked) value = value.ref + if (value != null) { + this.value = value as V + return + } + } + } + + override fun hasNext(): Boolean = index < allocated + + override fun next(): E { + if (index >= allocated) throw NoSuchElementException() + return factory(key, value).also { findNext() } + } + + override fun remove() = noImpl() + } + } + + private class Entry(override val key: K, override val value: V) : MutableMap.MutableEntry { + override fun setValue(newValue: V): V = noImpl() + } + + private inner class KeyValueSet( + private val factory: (K, V) -> E + ) : AbstractMutableSet() { + override val size: Int get() = this@ConcurrentWeakMap.size + override fun add(element: E): Boolean = noImpl() + override fun iterator(): MutableIterator = core.value.keyValueIterator(factory) + } +} + +private const val MAGIC = 2654435769L.toInt() // golden ratio +private const val MIN_CAPACITY = 16 +private val REHASH = Symbol("REHASH") +private val MARKED_NULL = Marked(null) +private val MARKED_TRUE = Marked(true) // When using map as set "true" used as value, optimize its mark allocation + +/** + * Weak reference that stores the original hash code so that we can use reference queue to promptly clean them up + * from the hashtable even in the absence of ongoing modifications. + */ +internal class HashedWeakRef( + ref: T, queue: ReferenceQueue? +) : WeakReference(ref, queue) { + @JvmField + val hash = ref.hashCode() +} + +/** + * Marked values cannot be modified. The marking is performed when rehash has started to ensure that concurrent + * modifications (that are lock-free) cannot perform any changes and are forced to synchronize with ongoing rehash. + */ +private class Marked(@JvmField val ref: Any?) + +private fun Any?.mark(): Marked = when(this) { + null -> MARKED_NULL + true -> MARKED_TRUE + else -> Marked(this) +} + +private fun noImpl(): Nothing { + throw UnsupportedOperationException("not implemented") +} diff --git a/kotlinx-coroutines-core/jvm/src/debug/internal/DebugCoroutineInfo.kt b/kotlinx-coroutines-core/jvm/src/debug/internal/DebugCoroutineInfo.kt new file mode 100644 index 0000000000..9d9fa3fbb2 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/src/debug/internal/DebugCoroutineInfo.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.debug.internal + +import kotlin.coroutines.* +import kotlin.coroutines.jvm.internal.* + +/** + * This class represents the data required by IDEA debugger. + * IDEA debugger either directly reads data from the corresponding JVM fields of this class or calls the getters, + * so we keep both for maximal flexibility for now. + * **DO NOT MAKE BINARY-INCOMPATIBLE CHANGES TO THIS CLASS**. + */ +@Suppress("unused") +@PublishedApi +internal class DebugCoroutineInfo( + source: DebugCoroutineInfoImpl, + public val context: CoroutineContext // field is used as of 1.4-M3 +) { + public val creationStackBottom: CoroutineStackFrame? = source.creationStackBottom // field is used as of 1.4-M3 + public val sequenceNumber: Long = source.sequenceNumber // field is used as of 1.4-M3 + public val creationStackTrace = source.creationStackTrace // getter is used as of 1.4-M3 + public val state: String = source.state // getter is used as of 1.4-M3 + public val lastObservedThread: Thread? = source.lastObservedThread // field is used as of 1.4-M3 + public val lastObservedFrame: CoroutineStackFrame? = source.lastObservedFrame // field is used as of 1.4-M3 + @get:JvmName("lastObservedStackTrace") // method with this name is used as of 1.4-M3 + public val lastObservedStackTrace: List = source.lastObservedStackTrace() +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/jvm/src/debug/internal/DebugCoroutineInfoImpl.kt b/kotlinx-coroutines-core/jvm/src/debug/internal/DebugCoroutineInfoImpl.kt new file mode 100644 index 0000000000..cf007bb978 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/src/debug/internal/DebugCoroutineInfoImpl.kt @@ -0,0 +1,100 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.debug.internal + +import java.lang.ref.* +import kotlin.coroutines.* +import kotlin.coroutines.jvm.internal.* + +internal const val CREATED = "CREATED" +internal const val RUNNING = "RUNNING" +internal const val SUSPENDED = "SUSPENDED" + +/** + * Internal implementation class where debugger tracks details it knows about each coroutine. + */ +internal class DebugCoroutineInfoImpl( + context: CoroutineContext?, + /** + * A reference to a stack-trace that is converted to a [StackTraceFrame] which implements [CoroutineStackFrame]. + * The actual reference to the coroutine is not stored here, so we keep a strong reference. + */ + public val creationStackBottom: StackTraceFrame?, + @JvmField internal val sequenceNumber: Long +) { + /** + * We cannot keep a strong reference to the context, because with the [Job] in the context it will indirectly + * keep a reference to the last frame of an abandoned coroutine which the debugger should not be preventing + * garbage-collection of. The reference to context will not disappear as long as the coroutine itself is not lost. + */ + private val _context = WeakReference(context) + public val context: CoroutineContext? // can be null when the coroutine was already garbage-collected + get() = _context.get() + + public val creationStackTrace: List get() = creationStackTrace() + + /** + * Last observed state of the coroutine. + * Can be CREATED, RUNNING, SUSPENDED. + */ + public val state: String get() = _state + private var _state: String = CREATED + + @JvmField + internal var lastObservedThread: Thread? = null + + /** + * We cannot keep a strong reference to the last observed frame of the coroutine, because this will + * prevent garbage-collection of a coroutine that was lost. + */ + private var _lastObservedFrame: WeakReference? = null + internal var lastObservedFrame: CoroutineStackFrame? + get() = _lastObservedFrame?.get() + set(value) { _lastObservedFrame = value?.let { WeakReference(it) } } + + /** + * Last observed stacktrace of the coroutine captured on its suspension or resumption point. + * It means that for [running][State.RUNNING] coroutines resulting stacktrace is inaccurate and + * reflects stacktrace of the resumption point, not the actual current stacktrace. + */ + public fun lastObservedStackTrace(): List { + var frame: CoroutineStackFrame? = lastObservedFrame ?: return emptyList() + val result = ArrayList() + while (frame != null) { + frame.getStackTraceElement()?.let { result.add(it) } + frame = frame.callerFrame + } + return result + } + + private fun creationStackTrace(): List { + val bottom = creationStackBottom ?: return emptyList() + // Skip "Coroutine creation stacktrace" frame + return sequence { yieldFrames(bottom.callerFrame) }.toList() + } + + private tailrec suspend fun SequenceScope.yieldFrames(frame: CoroutineStackFrame?) { + if (frame == null) return + frame.getStackTraceElement()?.let { yield(it) } + val caller = frame.callerFrame + if (caller != null) { + yieldFrames(caller) + } + } + + internal fun updateState(state: String, frame: Continuation<*>) { + // Propagate only duplicating transitions to running for KT-29997 + if (_state == state && state == SUSPENDED && lastObservedFrame != null) return + _state = state + lastObservedFrame = frame as? CoroutineStackFrame + lastObservedThread = if (state == RUNNING) { + Thread.currentThread() + } else { + null + } + } + + override fun toString(): String = "DebugCoroutineInfo(state=$state,context=$context)" +} diff --git a/kotlinx-coroutines-debug/src/internal/DebugProbesImpl.kt b/kotlinx-coroutines-core/jvm/src/debug/internal/DebugProbesImpl.kt similarity index 68% rename from kotlinx-coroutines-debug/src/internal/DebugProbesImpl.kt rename to kotlinx-coroutines-core/jvm/src/debug/internal/DebugProbesImpl.kt index 090d3e5d89..83bc02c6d6 100644 --- a/kotlinx-coroutines-debug/src/internal/DebugProbesImpl.kt +++ b/kotlinx-coroutines-core/jvm/src/debug/internal/DebugProbesImpl.kt @@ -7,32 +7,38 @@ package kotlinx.coroutines.debug.internal import kotlinx.atomicfu.* import kotlinx.coroutines.* import kotlinx.coroutines.debug.* -import net.bytebuddy.* -import net.bytebuddy.agent.* -import net.bytebuddy.dynamic.loading.* +import kotlinx.coroutines.internal.* +import kotlinx.coroutines.internal.ScopeCoroutine import java.io.* +import java.lang.StackTraceElement import java.text.* -import java.util.* -import java.util.concurrent.* import java.util.concurrent.locks.* import kotlin.collections.ArrayList import kotlin.concurrent.* import kotlin.coroutines.* -import kotlin.coroutines.jvm.internal.* +import kotlin.coroutines.jvm.internal.CoroutineStackFrame +import kotlin.synchronized import kotlinx.coroutines.internal.artificialFrame as createArtificialFrame // IDEA bug workaround -/** - * Mirror of [DebugProbes] with actual implementation. - * [DebugProbes] are implemented with pimpl to simplify user-facing class and make it look simple and - * documented. - */ internal object DebugProbesImpl { private const val ARTIFICIAL_FRAME_MESSAGE = "Coroutine creation stacktrace" private val dateFormat = SimpleDateFormat("yyyy/MM/dd HH:mm:ss") - private val capturedCoroutines = Collections.newSetFromMap(ConcurrentHashMap, Boolean>()) + + private var weakRefCleanerThread: Thread? = null + + // Values are boolean, so this map does not need to use a weak reference queue + private val capturedCoroutinesMap = ConcurrentWeakMap, Boolean>() + private val capturedCoroutines: Set> get() = capturedCoroutinesMap.keys + @Volatile private var installations = 0 + + /** + * This internal method is used by IDEA debugger under the JVM name of + * "isInstalled$kotlinx_coroutines_debug". + */ internal val isInstalled: Boolean get() = installations > 0 + // To sort coroutines by creation order, used as unique id private val sequenceNumber = atomic(0L) /* @@ -44,6 +50,22 @@ internal object DebugProbesImpl { */ private val coroutineStateLock = ReentrantReadWriteLock() + public var sanitizeStackTraces: Boolean = true + public var enableCreationStackTraces: Boolean = true + + /* + * Substitute for service loader, DI between core and debug modules. + * If the agent was installed via command line -javaagent parameter, do not use byte-byddy to avoud + */ + private val dynamicAttach = getDynamicAttach() + + @Suppress("UNCHECKED_CAST") + private fun getDynamicAttach(): Function1? = runCatching { + val clz = Class.forName("kotlinx.coroutines.debug.internal.ByteBuddyDynamicAttach") + val ctor = clz.constructors[0] + ctor.newInstance() as Function1 + }.getOrNull() + /* * This is an optimization in the face of KT-29997: * Consider suspending call stack a()->b()->c() and c() completes its execution and every call is @@ -51,56 +73,57 @@ internal object DebugProbesImpl { * * Then at least three RUNNING -> RUNNING transitions will occur consecutively and complexity of each is O(depth). * To avoid that quadratic complexity, we are caching lookup result for such chains in this map and update it incrementally. + * + * [DebugCoroutineInfoImpl] keeps a lot of auxiliary information about a coroutine, so we use a weak reference queue + * to promptly release the corresponding memory when the reference to the coroutine itself was already collected. */ - private val callerInfoCache = ConcurrentHashMap() + private val callerInfoCache = ConcurrentWeakMap(weakRefQueue = true) public fun install(): Unit = coroutineStateLock.write { if (++installations > 1) return - - ByteBuddyAgent.install() - val cl = Class.forName("kotlin.coroutines.jvm.internal.DebugProbesKt") - val cl2 = Class.forName("kotlinx.coroutines.debug.DebugProbesKt") - - ByteBuddy() - .redefine(cl2) - .name(cl.name) - .make() - .load(cl.classLoader, ClassReloadingStrategy.fromInstalledAgent()) + startWeakRefCleanerThread() + if (AgentPremain.isInstalledStatically) return + dynamicAttach?.invoke(true) // attach } public fun uninstall(): Unit = coroutineStateLock.write { check(isInstalled) { "Agent was not installed" } if (--installations != 0) return - - capturedCoroutines.clear() + stopWeakRefCleanerThread() + capturedCoroutinesMap.clear() callerInfoCache.clear() - val cl = Class.forName("kotlin.coroutines.jvm.internal.DebugProbesKt") - val cl2 = Class.forName("kotlinx.coroutines.debug.internal.NoOpProbesKt") - - ByteBuddy() - .redefine(cl2) - .name(cl.name) - .make() - .load(cl.classLoader, ClassReloadingStrategy.fromInstalledAgent()) + if (AgentPremain.isInstalledStatically) return + dynamicAttach?.invoke(false) // detach + } + + private fun startWeakRefCleanerThread() { + weakRefCleanerThread = thread(isDaemon = true, name = "Coroutines Debugger Cleaner") { + callerInfoCache.runWeakRefQueueCleaningLoopUntilInterrupted() + } + } + + private fun stopWeakRefCleanerThread() { + weakRefCleanerThread?.interrupt() + weakRefCleanerThread = null } public fun hierarchyToString(job: Job): String = coroutineStateLock.write { check(isInstalled) { "Debug probes are not installed" } val jobToStack = capturedCoroutines .filter { it.delegate.context[Job] != null } - .associateBy({ it.delegate.context[Job]!! }, { it.info }) + .associateBy({ it.delegate.context.job }, { it.info }) return buildString { job.build(jobToStack, this, "") } } - private fun Job.build(map: Map, builder: StringBuilder, indent: String) { + private fun Job.build(map: Map, builder: StringBuilder, indent: String) { val info = map[this] val newIndent: String if (info == null) { // Append coroutine without stacktrace // Do not print scoped coroutines and do not increase indentation level @Suppress("INVISIBLE_REFERENCE") - if (this !is kotlinx.coroutines.internal.ScopeCoroutine<*>) { + if (this !is ScopeCoroutine<*>) { builder.append("$indent$debugString\n") newIndent = indent + "\t" } else { @@ -122,13 +145,36 @@ internal object DebugProbesImpl { @Suppress("DEPRECATION_ERROR") // JobSupport private val Job.debugString: String get() = if (this is JobSupport) toDebugString() else toString() - public fun dumpCoroutinesInfo(): List = coroutineStateLock.write { - check(isInstalled) { "Debug probes are not installed" } - return capturedCoroutines.asSequence() - .map { it.info.copy() } // Copy as CoroutineInfo can be mutated concurrently by DebugProbes - .sortedBy { it.sequenceNumber } - .toList() - } + /** + * Private method that dumps coroutines so that different public-facing method can use + * to produce different result types. + */ + private inline fun dumpCoroutinesInfoImpl(create: (CoroutineOwner<*>, CoroutineContext) -> R): List = + coroutineStateLock.write { + check(isInstalled) { "Debug probes are not installed" } + capturedCoroutines + // Stable ordering of coroutines by their sequence number + .sortedBy { it.info.sequenceNumber } + // Leave in the dump only the coroutines that were not collected while we were dumping them + .mapNotNull { owner -> + // Fuse map and filter into one operation to save an inline + if (owner.isFinished()) null + else owner.info.context?.let { context -> create(owner, context) } + } + } + + /* + * Internal (JVM-public) method used by IDEA debugger as of 1.4-M3. + */ + public fun dumpCoroutinesInfo(): List = + dumpCoroutinesInfoImpl { owner, context -> DebugCoroutineInfo(owner.info, context) } + + /* + * Internal (JVM-public) method to be used by IDEA debugger in the future (not used as of 1.4-M3). + * It is equivalent to [dumpCoroutinesInfo], but returns serializable (and thus less typed) objects. + */ + public fun dumpDebuggerInfo(): List = + dumpCoroutinesInfoImpl { owner, context -> DebuggerInfo(owner.info, context) } public fun dumpCoroutines(out: PrintStream): Unit = synchronized(out) { /* @@ -141,21 +187,36 @@ internal object DebugProbesImpl { dumpCoroutinesSynchronized(out) } + /* + * Filters out coroutines that do not call probeCoroutineCompleted, + * are completed, but not yet garbage collected. + * + * Typically, we intercept completion of the coroutine so it invokes "probeCoroutineCompleted", + * but it's not the case for lazy coroutines that get cancelled before start. + */ + private fun CoroutineOwner<*>.isFinished(): Boolean { + // Guarded by lock + val job = info.context?.get(Job) ?: return false + if (!job.isCompleted) return false + capturedCoroutinesMap.remove(this) // Clean it up by the way + return true + } + private fun dumpCoroutinesSynchronized(out: PrintStream): Unit = coroutineStateLock.write { check(isInstalled) { "Debug probes are not installed" } out.print("Coroutines dump ${dateFormat.format(System.currentTimeMillis())}") capturedCoroutines .asSequence() + .filter { !it.isFinished() } .sortedBy { it.info.sequenceNumber } .forEach { owner -> val info = owner.info val observedStackTrace = info.lastObservedStackTrace() - val enhancedStackTrace = enhanceStackTraceWithThreadDump(info, observedStackTrace) - val state = if (info.state == State.RUNNING && enhancedStackTrace === observedStackTrace) + val enhancedStackTrace = enhanceStackTraceWithThreadDumpImpl(info.state, info.lastObservedThread, observedStackTrace) + val state = if (info.state == RUNNING && enhancedStackTrace === observedStackTrace) "${info.state} (Last suspension stacktrace, not an actual stacktrace)" else - info.state.toString() - + info.state out.print("\n\nCoroutine ${owner.delegate}, state: $state") if (observedStackTrace.isEmpty()) { out.print("\n\tat ${createArtificialFrame(ARTIFICIAL_FRAME_MESSAGE)}") @@ -172,18 +233,29 @@ internal object DebugProbesImpl { } } + /* + * Internal (JVM-public) method used by IDEA debugger as of 1.4-M3. + * It is similar to [enhanceStackTraceWithThreadDumpImpl], but uses debugger-facing [DebugCoroutineInfo] type. + */ + @Suppress("unused") + public fun enhanceStackTraceWithThreadDump( + info: DebugCoroutineInfo, + coroutineTrace: List + ): List = + enhanceStackTraceWithThreadDumpImpl(info.state, info.lastObservedThread, coroutineTrace) + /** - * Tries to enhance [coroutineTrace] (obtained by call to [CoroutineInfo.lastObservedStackTrace]) with - * thread dump of [CoroutineInfo.lastObservedThread]. + * Tries to enhance [coroutineTrace] (obtained by call to [DebugCoroutineInfoImpl.lastObservedStackTrace]) with + * thread dump of [DebugCoroutineInfoImpl.lastObservedThread]. * * Returns [coroutineTrace] if enhancement was unsuccessful or the enhancement result. */ - private fun enhanceStackTraceWithThreadDump( - info: CoroutineInfo, + private fun enhanceStackTraceWithThreadDumpImpl( + state: String, + thread: Thread?, coroutineTrace: List ): List { - val thread = info.lastObservedThread - if (info.state != State.RUNNING || thread == null) return coroutineTrace + if (state != RUNNING || thread == null) return coroutineTrace // Avoid security manager issues val actualTrace = runCatching { thread.stackTrace }.getOrNull() ?: return coroutineTrace @@ -213,7 +285,8 @@ internal object DebugProbesImpl { val (continuationStartFrame, frameSkipped) = findContinuationStartIndex( indexOfResumeWith, actualTrace, - coroutineTrace) + coroutineTrace + ) if (continuationStartFrame == -1) return coroutineTrace @@ -266,13 +339,14 @@ internal object DebugProbesImpl { } } - internal fun probeCoroutineResumed(frame: Continuation<*>) = updateState(frame, State.RUNNING) + internal fun probeCoroutineResumed(frame: Continuation<*>) = updateState(frame, RUNNING) - internal fun probeCoroutineSuspended(frame: Continuation<*>) = updateState(frame, State.SUSPENDED) + internal fun probeCoroutineSuspended(frame: Continuation<*>) = updateState(frame, SUSPENDED) - private fun updateState(frame: Continuation<*>, state: State) { + private fun updateState(frame: Continuation<*>, state: String) { + if (!isInstalled) return // KT-29997 is here only since 1.3.30 - if (state == State.RUNNING && KotlinVersion.CURRENT.isAtLeast(1, 3, 30)) { + if (state == RUNNING && KotlinVersion.CURRENT.isAtLeast(1, 3, 30)) { val stackFrame = frame as? CoroutineStackFrame ?: return updateRunningState(stackFrame, state) return @@ -284,10 +358,10 @@ internal object DebugProbesImpl { } // See comment to callerInfoCache - private fun updateRunningState(frame: CoroutineStackFrame, state: State): Unit = coroutineStateLock.read { + private fun updateRunningState(frame: CoroutineStackFrame, state: String): Unit = coroutineStateLock.read { if (!isInstalled) return // Lookup coroutine info in cache or by traversing stack frame - val info: CoroutineInfo + val info: DebugCoroutineInfoImpl val cached = callerInfoCache.remove(frame) if (cached != null) { info = cached @@ -309,7 +383,7 @@ internal object DebugProbesImpl { return if (caller.getStackTraceElement() != null) caller else caller.realCaller() } - private fun updateState(owner: CoroutineOwner<*>, frame: Continuation<*>, state: State) = coroutineStateLock.read { + private fun updateState(owner: CoroutineOwner<*>, frame: Continuation<*>, state: String) = coroutineStateLock.read { if (!isInstalled) return owner.info.updateState(state, frame) } @@ -329,39 +403,36 @@ internal object DebugProbesImpl { val owner = completion.owner() if (owner != null) return completion /* - * Here we replace completion with a sequence of CoroutineStackFrame objects + * Here we replace completion with a sequence of StackTraceFrame objects * which represents creation stacktrace, thus making stacktrace recovery mechanism * even more verbose (it will attach coroutine creation stacktrace to all exceptions), * and then using CoroutineOwner completion as unique identifier of coroutineSuspended/resumed calls. */ - - val frame = if (DebugProbes.enableCreationStackTraces) { - val stacktrace = sanitizeStackTrace(Exception()) - stacktrace.foldRight(null) { frame, acc -> - object : CoroutineStackFrame { - override val callerFrame: CoroutineStackFrame? = acc - override fun getStackTraceElement(): StackTraceElement = frame - } - } + val frame = if (enableCreationStackTraces) { + sanitizeStackTrace(Exception()).toStackTraceFrame() } else { null } - return createOwner(completion, frame) } - private fun createOwner(completion: Continuation, frame: CoroutineStackFrame?): Continuation { + private fun List.toStackTraceFrame(): StackTraceFrame? = + foldRight(null) { frame, acc -> + StackTraceFrame(acc, frame) + } + + private fun createOwner(completion: Continuation, frame: StackTraceFrame?): Continuation { if (!isInstalled) return completion - val info = CoroutineInfo(completion.context, frame, sequenceNumber.incrementAndGet()) + val info = DebugCoroutineInfoImpl(completion.context, frame, sequenceNumber.incrementAndGet()) val owner = CoroutineOwner(completion, info, frame) - capturedCoroutines += owner - if (!isInstalled) capturedCoroutines.clear() + capturedCoroutinesMap[owner] = true + if (!isInstalled) capturedCoroutinesMap.clear() return owner } // Not guarded by the lock at all, does not really affect consistency private fun probeCoroutineCompleted(owner: CoroutineOwner<*>) { - capturedCoroutines.remove(owner) + capturedCoroutinesMap.remove(owner) /* * This removal is a guard against improperly implemented CoroutineStackFrame * and bugs in the compiler. @@ -376,7 +447,7 @@ internal object DebugProbesImpl { */ private class CoroutineOwner( @JvmField val delegate: Continuation, - @JvmField val info: CoroutineInfo, + @JvmField val info: DebugCoroutineInfoImpl, private val frame: CoroutineStackFrame? ) : Continuation by delegate, CoroutineStackFrame { @@ -398,7 +469,7 @@ internal object DebugProbesImpl { val size = stackTrace.size val probeIndex = stackTrace.indexOfLast { it.className == "kotlin.coroutines.jvm.internal.DebugProbesKt" } - if (!DebugProbes.sanitizeStackTraces) { + if (!sanitizeStackTraces) { return List(size - probeIndex) { if (it == 0) createArtificialFrame(ARTIFICIAL_FRAME_MESSAGE) else stackTrace[it + probeIndex] } diff --git a/kotlinx-coroutines-core/jvm/src/debug/internal/DebuggerInfo.kt b/kotlinx-coroutines-core/jvm/src/debug/internal/DebuggerInfo.kt new file mode 100644 index 0000000000..3e9533b09e --- /dev/null +++ b/kotlinx-coroutines-core/jvm/src/debug/internal/DebuggerInfo.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +@file:Suppress("UNUSED") + +package kotlinx.coroutines.debug.internal + +import java.io.Serializable +import kotlin.coroutines.* +import kotlinx.coroutines.* + +/* + * This class represents all the data required by IDEA debugger. + * It is serializable in order to speedup JDWP interactions. + * **DO NOT MAKE BINARY-INCOMPATIBLE CHANGES TO THIS CLASS**. + */ +@PublishedApi +internal class DebuggerInfo(source: DebugCoroutineInfoImpl, context: CoroutineContext) : Serializable { + public val coroutineId: Long? = context[CoroutineId]?.id + public val dispatcher: String? = context[ContinuationInterceptor]?.toString() + public val name: String? = context[CoroutineName]?.name + public val state: String = source.state + public val lastObservedThreadState: String? = source.lastObservedThread?.state?.toString() + public val lastObservedThreadName = source.lastObservedThread?.name + public val lastObservedStackTrace: List = source.lastObservedStackTrace() + public val sequenceNumber: Long = source.sequenceNumber +} diff --git a/kotlinx-coroutines-core/jvm/src/debug/internal/StackTraceFrame.kt b/kotlinx-coroutines-core/jvm/src/debug/internal/StackTraceFrame.kt new file mode 100644 index 0000000000..37c60eeb56 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/src/debug/internal/StackTraceFrame.kt @@ -0,0 +1,17 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.debug.internal + +import kotlin.coroutines.jvm.internal.* + +/** + * A stack-trace represented as [CoroutineStackFrame]. + */ +internal class StackTraceFrame( + override val callerFrame: CoroutineStackFrame?, + private val stackTraceElement: StackTraceElement +) : CoroutineStackFrame { + override fun getStackTraceElement(): StackTraceElement = stackTraceElement +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/jvm/src/flow/internal/SafeCollector.kt b/kotlinx-coroutines-core/jvm/src/flow/internal/SafeCollector.kt index 4647a14245..ab42b6345f 100644 --- a/kotlinx-coroutines-core/jvm/src/flow/internal/SafeCollector.kt +++ b/kotlinx-coroutines-core/jvm/src/flow/internal/SafeCollector.kt @@ -4,6 +4,7 @@ package kotlinx.coroutines.flow.internal +import kotlinx.coroutines.* import kotlinx.coroutines.flow.* import kotlin.coroutines.* import kotlin.coroutines.intrinsics.* @@ -20,7 +21,11 @@ private val emitFun = internal actual class SafeCollector actual constructor( @JvmField internal actual val collector: FlowCollector, @JvmField internal actual val collectContext: CoroutineContext -) : FlowCollector, ContinuationImpl(NoOpContinuation, EmptyCoroutineContext) { +) : FlowCollector, ContinuationImpl(NoOpContinuation, EmptyCoroutineContext), CoroutineStackFrame { + + override val callerFrame: CoroutineStackFrame? get() = completion as? CoroutineStackFrame + + override fun getStackTraceElement(): StackTraceElement? = null @JvmField // Note, it is non-capturing lambda, so no extra allocation during init of SafeCollector internal actual val collectContextSize = collectContext.fold(0) { count, _ -> count + 1 } @@ -62,6 +67,7 @@ internal actual class SafeCollector actual constructor( private fun emit(uCont: Continuation, value: T): Any? { val currentContext = uCont.context + currentContext.ensureActive() // This check is triggered once per flow on happy path. val previousContext = lastEmissionContext if (previousContext !== currentContext) { diff --git a/kotlinx-coroutines-core/jvm/src/internal/Concurrent.kt b/kotlinx-coroutines-core/jvm/src/internal/Concurrent.kt index 171748ff68..75b668a335 100644 --- a/kotlinx-coroutines-core/jvm/src/internal/Concurrent.kt +++ b/kotlinx-coroutines-core/jvm/src/internal/Concurrent.kt @@ -29,10 +29,10 @@ private val REMOVE_FUTURE_ON_CANCEL: Method? = try { @Suppress("NAME_SHADOWING") internal fun removeFutureOnCancel(executor: Executor): Boolean { try { - val executor = executor as? ScheduledExecutorService ?: return false + val executor = executor as? ScheduledThreadPoolExecutor ?: return false (REMOVE_FUTURE_ON_CANCEL ?: return false).invoke(executor, true) return true } catch (e: Throwable) { - return true + return false // failed to setRemoveOnCancelPolicy, assume it does not removes future on cancel } } diff --git a/kotlinx-coroutines-core/jvm/src/internal/LocalAtomics.kt b/kotlinx-coroutines-core/jvm/src/internal/LocalAtomics.kt new file mode 100644 index 0000000000..f508749ed0 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/src/internal/LocalAtomics.kt @@ -0,0 +1,8 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.internal + +@Suppress("ACTUAL_WITHOUT_EXPECT") +internal actual typealias LocalAtomicInt = java.util.concurrent.atomic.AtomicInteger diff --git a/kotlinx-coroutines-core/jvm/src/internal/LockFreeLinkedList.kt b/kotlinx-coroutines-core/jvm/src/internal/LockFreeLinkedList.kt index f718df04b5..97f9978139 100644 --- a/kotlinx-coroutines-core/jvm/src/internal/LockFreeLinkedList.kt +++ b/kotlinx-coroutines-core/jvm/src/internal/LockFreeLinkedList.kt @@ -1,6 +1,7 @@ /* * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ +@file:Suppress("NO_EXPLICIT_VISIBILITY_IN_API_MODE") package kotlinx.coroutines.internal @@ -415,25 +416,31 @@ public actual open class LockFreeLinkedListNode { val next = this.next val removed = next.removed() if (affected._next.compareAndSet(this, removed)) { + // The element was actually removed + desc.onRemoved(affected) // Complete removal operation here. It bails out if next node is also removed and it becomes // responsibility of the next's removes to call correctPrev which would help fix all the links. next.correctPrev(null) } return REMOVE_PREPARED } - val isDecided = if (decision != null) { + // We need to ensure progress even if it operation result consensus was already decided + val consensus = if (decision != null) { // some other logic failure, including RETRY_ATOMIC -- reach consensus on decision fail reason ASAP atomicOp.decide(decision) - true // atomicOp.isDecided will be true as a result } else { - atomicOp.isDecided // consult with current decision status like in Harris DCSS + atomicOp.consensus // consult with current decision status like in Harris DCSS + } + val update: Any = when { + consensus === NO_DECISION -> atomicOp // desc.onPrepare returned null -> start doing atomic op + consensus == null -> desc.updatedNext(affected, next) // move forward if consensus on success + else -> next // roll back if consensus if failure } - val update: Any = if (isDecided) next else atomicOp // restore if decision was already reached affected._next.compareAndSet(this, update) return null } - public fun finishPrepare() = desc.finishPrepare(this) + public fun finishPrepare(): Unit = desc.finishPrepare(this) override fun toString(): String = "PrepareOp(op=$atomicOp)" } @@ -444,9 +451,10 @@ public actual open class LockFreeLinkedListNode { protected open fun takeAffectedNode(op: OpDescriptor): Node? = affectedNode!! // null for RETRY_ATOMIC protected open fun failure(affected: Node): Any? = null // next: Node | Removed protected open fun retry(affected: Node, next: Any): Boolean = false // next: Node | Removed - protected abstract fun updatedNext(affected: Node, next: Node): Any protected abstract fun finishOnSuccess(affected: Node, next: Node) + public abstract fun updatedNext(affected: Node, next: Node): Any + public abstract fun finishPrepare(prepareOp: PrepareOp) // non-null on failure @@ -455,6 +463,8 @@ public actual open class LockFreeLinkedListNode { return null } + public open fun onRemoved(affected: Node) {} // called once when node was prepared & later removed + @Suppress("UNCHECKED_CAST") final override fun prepare(op: AtomicOp<*>): Any? { while (true) { // lock free loop on next diff --git a/kotlinx-coroutines-core/jvm/src/internal/MainDispatchers.kt b/kotlinx-coroutines-core/jvm/src/internal/MainDispatchers.kt index b3e49752e0..5b2b9ff68c 100644 --- a/kotlinx-coroutines-core/jvm/src/internal/MainDispatchers.kt +++ b/kotlinx-coroutines-core/jvm/src/internal/MainDispatchers.kt @@ -87,17 +87,14 @@ private class MissingMainCoroutineDispatcher( override val immediate: MainCoroutineDispatcher get() = this - override fun isDispatchNeeded(context: CoroutineContext): Boolean { + override fun isDispatchNeeded(context: CoroutineContext): Boolean = missing() - } - override suspend fun delay(time: Long) { + override suspend fun delay(time: Long) = missing() - } - override fun invokeOnTimeout(timeMillis: Long, block: Runnable): DisposableHandle { + override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle = missing() - } override fun dispatch(context: CoroutineContext, block: Runnable) = missing() @@ -114,7 +111,7 @@ private class MissingMainCoroutineDispatcher( } } - override fun toString(): String = "Main[missing${if (cause != null) ", cause=$cause" else ""}]" + override fun toString(): String = "Dispatchers.Main[missing${if (cause != null) ", cause=$cause" else ""}]" } /** diff --git a/kotlinx-coroutines-core/jvm/src/internal/StackTraceRecovery.kt b/kotlinx-coroutines-core/jvm/src/internal/StackTraceRecovery.kt index f5c5c24b47..208d3f2e75 100644 --- a/kotlinx-coroutines-core/jvm/src/internal/StackTraceRecovery.kt +++ b/kotlinx-coroutines-core/jvm/src/internal/StackTraceRecovery.kt @@ -2,7 +2,7 @@ * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ -@file:Suppress("UNCHECKED_CAST") +@file:Suppress("UNCHECKED_CAST", "NO_EXPLICIT_VISIBILITY_IN_API_MODE") package kotlinx.coroutines.internal @@ -191,7 +191,7 @@ private fun createStackTrace(continuation: CoroutineStackFrame): ArrayDeque.frameIndex(methodName: String) = indexOfFirst { methodName == it.className } diff --git a/kotlinx-coroutines-core/jvm/src/scheduling/CoroutineScheduler.kt b/kotlinx-coroutines-core/jvm/src/scheduling/CoroutineScheduler.kt index 815fa26941..62cf80f7f8 100644 --- a/kotlinx-coroutines-core/jvm/src/scheduling/CoroutineScheduler.kt +++ b/kotlinx-coroutines-core/jvm/src/scheduling/CoroutineScheduler.kt @@ -951,3 +951,20 @@ internal class CoroutineScheduler( TERMINATED } } + +/** + * Checks if the thread is part of a thread pool that supports coroutines. + * This function is needed for integration with BlockHound. + */ +@Suppress("UNUSED") +@JvmName("isSchedulerWorker") +internal fun isSchedulerWorker(thread: Thread) = thread is CoroutineScheduler.Worker + +/** + * Checks if the thread is running a CPU-bound task. + * This function is needed for integration with BlockHound. + */ +@Suppress("UNUSED") +@JvmName("mayNotBlock") +internal fun mayNotBlock(thread: Thread) = thread is CoroutineScheduler.Worker && + thread.state == CoroutineScheduler.WorkerState.CPU_ACQUIRED diff --git a/kotlinx-coroutines-core/jvm/src/scheduling/Dispatcher.kt b/kotlinx-coroutines-core/jvm/src/scheduling/Dispatcher.kt index 7398f12fe7..202c6e1d06 100644 --- a/kotlinx-coroutines-core/jvm/src/scheduling/Dispatcher.kt +++ b/kotlinx-coroutines-core/jvm/src/scheduling/Dispatcher.kt @@ -14,13 +14,18 @@ import kotlin.coroutines.* * Default instance of coroutine dispatcher. */ internal object DefaultScheduler : ExperimentalCoroutineDispatcher() { - val IO = blocking(systemProp(IO_PARALLELISM_PROPERTY_NAME, 64.coerceAtLeast(AVAILABLE_PROCESSORS))) + val IO: CoroutineDispatcher = LimitingDispatcher( + this, + systemProp(IO_PARALLELISM_PROPERTY_NAME, 64.coerceAtLeast(AVAILABLE_PROCESSORS)), + "Dispatchers.IO", + TASK_PROBABLY_BLOCKING + ) override fun close() { - throw UnsupportedOperationException("$DEFAULT_SCHEDULER_NAME cannot be closed") + throw UnsupportedOperationException("$DEFAULT_DISPATCHER_NAME cannot be closed") } - override fun toString(): String = DEFAULT_SCHEDULER_NAME + override fun toString(): String = DEFAULT_DISPATCHER_NAME @InternalCoroutinesApi @Suppress("UNUSED") @@ -32,20 +37,20 @@ internal object DefaultScheduler : ExperimentalCoroutineDispatcher() { */ // TODO make internal (and rename) after complete integration @InternalCoroutinesApi -open class ExperimentalCoroutineDispatcher( +public open class ExperimentalCoroutineDispatcher( private val corePoolSize: Int, private val maxPoolSize: Int, private val idleWorkerKeepAliveNs: Long, private val schedulerName: String = "CoroutineScheduler" ) : ExecutorCoroutineDispatcher() { - constructor( + public constructor( corePoolSize: Int = CORE_POOL_SIZE, maxPoolSize: Int = MAX_POOL_SIZE, schedulerName: String = DEFAULT_SCHEDULER_NAME ) : this(corePoolSize, maxPoolSize, IDLE_WORKER_KEEP_ALIVE_NS, schedulerName) @Deprecated(message = "Binary compatibility for Ktor 1.0-beta", level = DeprecationLevel.HIDDEN) - constructor( + public constructor( corePoolSize: Int = CORE_POOL_SIZE, maxPoolSize: Int = MAX_POOL_SIZE ) : this(corePoolSize, maxPoolSize, IDLE_WORKER_KEEP_ALIVE_NS) @@ -60,6 +65,8 @@ open class ExperimentalCoroutineDispatcher( try { coroutineScheduler.dispatch(block) } catch (e: RejectedExecutionException) { + // CoroutineScheduler only rejects execution when it is being closed and this behavior is reserved + // for testing purposes, so we don't have to worry about cancelling the affected Job here. DefaultExecutor.dispatch(context, block) } @@ -67,10 +74,12 @@ open class ExperimentalCoroutineDispatcher( try { coroutineScheduler.dispatch(block, tailDispatch = true) } catch (e: RejectedExecutionException) { + // CoroutineScheduler only rejects execution when it is being closed and this behavior is reserved + // for testing purposes, so we don't have to worry about cancelling the affected Job here. DefaultExecutor.dispatchYield(context, block) } - override fun close() = coroutineScheduler.close() + override fun close(): Unit = coroutineScheduler.close() override fun toString(): String { return "${super.toString()}[scheduler = $coroutineScheduler]" @@ -85,7 +94,7 @@ open class ExperimentalCoroutineDispatcher( */ public fun blocking(parallelism: Int = BLOCKING_DEFAULT_PARALLELISM): CoroutineDispatcher { require(parallelism > 0) { "Expected positive parallelism level, but have $parallelism" } - return LimitingDispatcher(this, parallelism, TASK_PROBABLY_BLOCKING) + return LimitingDispatcher(this, parallelism, null, TASK_PROBABLY_BLOCKING) } /** @@ -98,14 +107,16 @@ open class ExperimentalCoroutineDispatcher( public fun limited(parallelism: Int): CoroutineDispatcher { require(parallelism > 0) { "Expected positive parallelism level, but have $parallelism" } require(parallelism <= corePoolSize) { "Expected parallelism level lesser than core pool size ($corePoolSize), but have $parallelism" } - return LimitingDispatcher(this, parallelism, TASK_NON_BLOCKING) + return LimitingDispatcher(this, parallelism, null, TASK_NON_BLOCKING) } internal fun dispatchWithContext(block: Runnable, context: TaskContext, tailDispatch: Boolean) { try { coroutineScheduler.dispatch(block, context, tailDispatch) } catch (e: RejectedExecutionException) { - // Context shouldn't be lost here to properly invoke before/after task + // CoroutineScheduler only rejects execution when it is being closed and this behavior is reserved + // for testing purposes, so we don't have to worry about cancelling the affected Job here. + // TaskContext shouldn't be lost here to properly invoke before/after task DefaultExecutor.enqueue(coroutineScheduler.createTask(block, context)) } } @@ -130,8 +141,9 @@ open class ExperimentalCoroutineDispatcher( } private class LimitingDispatcher( - val dispatcher: ExperimentalCoroutineDispatcher, - val parallelism: Int, + private val dispatcher: ExperimentalCoroutineDispatcher, + private val parallelism: Int, + private val name: String?, override val taskMode: Int ) : ExecutorCoroutineDispatcher(), TaskContext, Executor { @@ -190,7 +202,7 @@ private class LimitingDispatcher( } override fun toString(): String { - return "${super.toString()}[dispatcher = $dispatcher]" + return name ?: "${super.toString()}[dispatcher = $dispatcher]" } /** diff --git a/kotlinx-coroutines-core/jvm/src/scheduling/Tasks.kt b/kotlinx-coroutines-core/jvm/src/scheduling/Tasks.kt index 247615d777..a317b9754c 100644 --- a/kotlinx-coroutines-core/jvm/src/scheduling/Tasks.kt +++ b/kotlinx-coroutines-core/jvm/src/scheduling/Tasks.kt @@ -11,6 +11,9 @@ import java.util.concurrent.* // TODO most of these fields will be moved to 'object ExperimentalDispatcher' +// User-visible name +internal const val DEFAULT_DISPATCHER_NAME = "Dispatchers.Default" +// Internal debuggability name + thread name prefixes internal const val DEFAULT_SCHEDULER_NAME = "DefaultDispatcher" // 100us as default diff --git a/kotlinx-coroutines-core/jvm/src/test_/TestCoroutineContext.kt b/kotlinx-coroutines-core/jvm/src/test_/TestCoroutineContext.kt index 8a0bd1a187..649c95375d 100644 --- a/kotlinx-coroutines-core/jvm/src/test_/TestCoroutineContext.kt +++ b/kotlinx-coroutines-core/jvm/src/test_/TestCoroutineContext.kt @@ -33,7 +33,7 @@ import kotlin.coroutines.* @Deprecated("This API has been deprecated to integrate with Structured Concurrency.", ReplaceWith("TestCoroutineScope", "kotlin.coroutines.test"), level = DeprecationLevel.WARNING) -class TestCoroutineContext(private val name: String? = null) : CoroutineContext { +public class TestCoroutineContext(private val name: String? = null) : CoroutineContext { private val uncaughtExceptions = mutableListOf() private val ctxDispatcher = Dispatcher() @@ -80,7 +80,7 @@ class TestCoroutineContext(private val name: String? = null) : CoroutineContext * @param unit The [TimeUnit] in which the clock-time must be returned. * @return The virtual clock-time */ - public fun now(unit: TimeUnit = TimeUnit.MILLISECONDS)= + public fun now(unit: TimeUnit = TimeUnit.MILLISECONDS): Long= unit.convert(time, TimeUnit.NANOSECONDS) /** @@ -105,7 +105,7 @@ class TestCoroutineContext(private val name: String? = null) : CoroutineContext * @param targetTime The point in time to which to move the CoroutineContext's clock. * @param unit The [TimeUnit] in which [targetTime] is expressed. */ - fun advanceTimeTo(targetTime: Long, unit: TimeUnit = TimeUnit.MILLISECONDS) { + public fun advanceTimeTo(targetTime: Long, unit: TimeUnit = TimeUnit.MILLISECONDS) { val nanoTime = unit.toNanos(targetTime) triggerActions(nanoTime) if (nanoTime > time) time = nanoTime @@ -115,7 +115,7 @@ class TestCoroutineContext(private val name: String? = null) : CoroutineContext * Triggers any actions that have not yet been triggered and that are scheduled to be triggered at or * before this CoroutineContext's present virtual clock-time. */ - public fun triggerActions() = triggerActions(time) + public fun triggerActions(): Unit = triggerActions(time) /** * Cancels all not yet triggered actions. Be careful calling this, since it can seriously @@ -230,7 +230,7 @@ class TestCoroutineContext(private val name: String? = null) : CoroutineContext }, timeMillis) } - override fun invokeOnTimeout(timeMillis: Long, block: Runnable): DisposableHandle { + override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle { val node = postDelayed(block, timeMillis) return object : DisposableHandle { override fun dispose() { diff --git a/kotlinx-coroutines-core/jvm/test/AtomicCancellationTest.kt b/kotlinx-coroutines-core/jvm/test/AtomicCancellationTest.kt index 8a7dce01ee..2612b84153 100644 --- a/kotlinx-coroutines-core/jvm/test/AtomicCancellationTest.kt +++ b/kotlinx-coroutines-core/jvm/test/AtomicCancellationTest.kt @@ -9,25 +9,24 @@ import kotlinx.coroutines.selects.* import kotlin.test.* class AtomicCancellationTest : TestBase() { - @Test - fun testSendAtomicCancel() = runBlocking { + fun testSendCancellable() = runBlocking { expect(1) val channel = Channel() val job = launch(start = CoroutineStart.UNDISPATCHED) { expect(2) channel.send(42) // suspends - expect(4) // should execute despite cancellation + expectUnreached() // should NOT execute because of cancellation } expect(3) assertEquals(42, channel.receive()) // will schedule sender for further execution job.cancel() // cancel the job next yield() // now yield - finish(5) + finish(4) } @Test - fun testSelectSendAtomicCancel() = runBlocking { + fun testSelectSendCancellable() = runBlocking { expect(1) val channel = Channel() val job = launch(start = CoroutineStart.UNDISPATCHED) { @@ -38,34 +37,33 @@ class AtomicCancellationTest : TestBase() { "OK" } } - assertEquals("OK", result) - expect(5) // should execute despite cancellation + expectUnreached() // should NOT execute because of cancellation } expect(3) assertEquals(42, channel.receive()) // will schedule sender for further execution job.cancel() // cancel the job next yield() // now yield - finish(6) + finish(4) } @Test - fun testReceiveAtomicCancel() = runBlocking { + fun testReceiveCancellable() = runBlocking { expect(1) val channel = Channel() val job = launch(start = CoroutineStart.UNDISPATCHED) { expect(2) assertEquals(42, channel.receive()) // suspends - expect(4) // should execute despite cancellation + expectUnreached() // should NOT execute because of cancellation } expect(3) channel.send(42) // will schedule receiver for further execution job.cancel() // cancel the job next yield() // now yield - finish(5) + finish(4) } @Test - fun testSelectReceiveAtomicCancel() = runBlocking { + fun testSelectReceiveCancellable() = runBlocking { expect(1) val channel = Channel() val job = launch(start = CoroutineStart.UNDISPATCHED) { @@ -77,14 +75,13 @@ class AtomicCancellationTest : TestBase() { "OK" } } - assertEquals("OK", result) - expect(5) // should execute despite cancellation + expectUnreached() // should NOT execute because of cancellation } expect(3) channel.send(42) // will schedule receiver for further execution job.cancel() // cancel the job next yield() // now yield - finish(6) + finish(4) } @Test diff --git a/kotlinx-coroutines-core/jvm/test/CancellableContinuationJvmTest.kt b/kotlinx-coroutines-core/jvm/test/CancellableContinuationJvmTest.kt index 4e25da96f5..f1a957adca 100644 --- a/kotlinx-coroutines-core/jvm/test/CancellableContinuationJvmTest.kt +++ b/kotlinx-coroutines-core/jvm/test/CancellableContinuationJvmTest.kt @@ -20,4 +20,63 @@ class CancellableContinuationJvmTest : TestBase() { } suspend {}() // Eliminate tail-call optimization } + + @Test + fun testExceptionIsNotReported() = runTest({ it is CancellationException }) { + val ctx = coroutineContext + suspendCancellableCoroutine { + ctx.cancel() + it.resumeWith(Result.failure(TestException())) + } + } + + @Test + fun testBlockingIntegration() = runTest { + val source = BlockingSource() + val job = launch(Dispatchers.Default) { + source.await() + } + source.cancelAndJoin(job) + } + + @Test + fun testBlockingIntegrationAlreadyCancelled() = runTest { + val source = BlockingSource() + val job = launch(Dispatchers.Default) { + cancel() + source.await() + } + source.cancelAndJoin(job) + } + + private suspend fun BlockingSource.cancelAndJoin(job: Job) { + while (!hasSubscriber) { + Thread.sleep(10) + } + job.cancelAndJoin() + } + + private suspend fun BlockingSource.await() = suspendCancellableCoroutine { + it.invokeOnCancellation { this.cancel() } + subscribe() + } + + private class BlockingSource { + @Volatile + private var isCancelled = false + + @Volatile + public var hasSubscriber = false + + public fun subscribe() { + hasSubscriber = true + while (!isCancelled) { + Thread.sleep(10) + } + } + + public fun cancel() { + isCancelled = true + } + } } diff --git a/kotlinx-coroutines-core/jvm/test/DefaultExecutorStressTest.kt b/kotlinx-coroutines-core/jvm/test/DefaultExecutorStressTest.kt index b4c6aaed4d..bc2de8c998 100644 --- a/kotlinx-coroutines-core/jvm/test/DefaultExecutorStressTest.kt +++ b/kotlinx-coroutines-core/jvm/test/DefaultExecutorStressTest.kt @@ -4,7 +4,8 @@ package kotlinx.coroutines -import org.junit.* +import org.junit.Test +import kotlin.test.* class DefaultExecutorStressTest : TestBase() { @Test @@ -35,4 +36,30 @@ class DefaultExecutorStressTest : TestBase() { } finish(2 + iterations * 4) } + + @Test + fun testWorkerShutdown() = withVirtualTimeSource { + val iterations = 1_000 * stressTestMultiplier + // wait for the worker to shut down + suspend fun awaitWorkerShutdown() { + val executorTimeoutMs = 1000L + delay(executorTimeoutMs) + while (DefaultExecutor.isThreadPresent) { delay(10) } // hangs if the thread refuses to stop + assertFalse(DefaultExecutor.isThreadPresent) // just to make sure + } + runTest { + awaitWorkerShutdown() // so that the worker shuts down after the initial launch + repeat (iterations) { + val job = launch(Dispatchers.Unconfined) { + // this line runs in the main thread + delay(1) + // this line runs in the DefaultExecutor worker + } + delay(100) // yield the execution, allow the worker to spawn + assertTrue(DefaultExecutor.isThreadPresent) // the worker spawned + job.join() + awaitWorkerShutdown() + } + } + } } diff --git a/kotlinx-coroutines-core/jvm/test/DispatchersToStringTest.kt b/kotlinx-coroutines-core/jvm/test/DispatchersToStringTest.kt new file mode 100644 index 0000000000..7adaa2a4b6 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/DispatchersToStringTest.kt @@ -0,0 +1,18 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines + +import kotlin.test.* + +class DispatchersToStringTest { + @Test + fun testStrings() { + assertEquals("Dispatchers.Unconfined", Dispatchers.Unconfined.toString()) + assertEquals("Dispatchers.Default", Dispatchers.Default.toString()) + assertEquals("Dispatchers.IO", Dispatchers.IO.toString()) + assertEquals("Dispatchers.Main[missing]", Dispatchers.Main.toString()) + assertEquals("Dispatchers.Main[missing]", Dispatchers.Main.immediate.toString()) + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/jvm/test/ExecutorsTest.kt b/kotlinx-coroutines-core/jvm/test/ExecutorsTest.kt index 033b9b7bc9..ebf08a03d0 100644 --- a/kotlinx-coroutines-core/jvm/test/ExecutorsTest.kt +++ b/kotlinx-coroutines-core/jvm/test/ExecutorsTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ package kotlinx.coroutines @@ -29,6 +29,8 @@ class ExecutorsTest : TestBase() { val context = newFixedThreadPoolContext(2, "TestPool") runBlocking(context) { checkThreadName("TestPool") + delay(10) + checkThreadName("TestPool") // should dispatch on the right thread } context.close() } @@ -38,6 +40,8 @@ class ExecutorsTest : TestBase() { val executor = Executors.newSingleThreadExecutor { r -> Thread(r, "TestExecutor") } runBlocking(executor.asCoroutineDispatcher()) { checkThreadName("TestExecutor") + delay(10) + checkThreadName("TestExecutor") // should dispatch on the right thread } executor.shutdown() } diff --git a/kotlinx-coroutines-core/jvm/test/FailingCoroutinesMachineryTest.kt b/kotlinx-coroutines-core/jvm/test/FailingCoroutinesMachineryTest.kt index 9bf8ffad85..c9f722a5b8 100644 --- a/kotlinx-coroutines-core/jvm/test/FailingCoroutinesMachineryTest.kt +++ b/kotlinx-coroutines-core/jvm/test/FailingCoroutinesMachineryTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ package kotlinx.coroutines @@ -15,8 +15,21 @@ import kotlin.test.* @RunWith(Parameterized::class) class FailingCoroutinesMachineryTest( private val element: CoroutineContext.Element, - private val dispatcher: CoroutineDispatcher + private val dispatcher: TestDispatcher ) : TestBase() { + class TestDispatcher(val name: String, val block: () -> CoroutineDispatcher) { + private var _value: CoroutineDispatcher? = null + + val value: CoroutineDispatcher + get() = _value ?: block().also { _value = it } + + override fun toString(): String = name + + fun reset() { + runCatching { (_value as? ExecutorCoroutineDispatcher)?.close() } + _value = null + } + } private var caught: Throwable? = null private val latch = CountDownLatch(1) @@ -75,7 +88,7 @@ class FailingCoroutinesMachineryTest( @After fun tearDown() { - runCatching { (dispatcher as? ExecutorCoroutineDispatcher)?.close() } + dispatcher.reset() if (lazyOuterDispatcher.isInitialized()) lazyOuterDispatcher.value.close() } @@ -84,14 +97,14 @@ class FailingCoroutinesMachineryTest( @Parameterized.Parameters(name = "Element: {0}, dispatcher: {1}") fun dispatchers(): List> { val elements = listOf(FailingRestore, FailingUpdate) - val dispatchers = listOf( - Dispatchers.Unconfined, - Dispatchers.Default, - Executors.newFixedThreadPool(1).asCoroutineDispatcher(), - Executors.newScheduledThreadPool(1).asCoroutineDispatcher(), - ThrowingDispatcher, ThrowingDispatcher2 + val dispatchers = listOf( + TestDispatcher("Dispatchers.Unconfined") { Dispatchers.Unconfined }, + TestDispatcher("Dispatchers.Default") { Dispatchers.Default }, + TestDispatcher("Executors.newFixedThreadPool(1)") { Executors.newFixedThreadPool(1).asCoroutineDispatcher() }, + TestDispatcher("Executors.newScheduledThreadPool(1)") { Executors.newScheduledThreadPool(1).asCoroutineDispatcher() }, + TestDispatcher("ThrowingDispatcher") { ThrowingDispatcher }, + TestDispatcher("ThrowingDispatcher2") { ThrowingDispatcher2 } ) - return elements.flatMap { element -> dispatchers.map { dispatcher -> arrayOf(element, dispatcher) @@ -102,13 +115,13 @@ class FailingCoroutinesMachineryTest( @Test fun testElement() = runTest { - launch(NonCancellable + dispatcher + exceptionHandler + element) {} + launch(NonCancellable + dispatcher.value + exceptionHandler + element) {} checkException() } @Test fun testNestedElement() = runTest { - launch(NonCancellable + dispatcher + exceptionHandler) { + launch(NonCancellable + dispatcher.value + exceptionHandler) { launch(element) { } } checkException() @@ -117,7 +130,7 @@ class FailingCoroutinesMachineryTest( @Test fun testNestedDispatcherAndElement() = runTest { launch(lazyOuterDispatcher.value + NonCancellable + exceptionHandler) { - launch(element + dispatcher) { } + launch(element + dispatcher.value) { } } checkException() } diff --git a/kotlinx-coroutines-core/jvm/test/FieldWalker.kt b/kotlinx-coroutines-core/jvm/test/FieldWalker.kt index 12fd4dea04..e8079ebdfa 100644 --- a/kotlinx-coroutines-core/jvm/test/FieldWalker.kt +++ b/kotlinx-coroutines-core/jvm/test/FieldWalker.kt @@ -4,10 +4,13 @@ package kotlinx.coroutines +import java.lang.ref.* import java.lang.reflect.* +import java.text.* import java.util.* import java.util.Collections.* import java.util.concurrent.atomic.* +import java.util.concurrent.locks.* import kotlin.collections.ArrayList import kotlin.test.* @@ -22,7 +25,11 @@ object FieldWalker { init { // excluded/terminal classes (don't walk them) - fieldsCache += listOf(Any::class, String::class, Thread::class, Throwable::class) + fieldsCache += listOf( + Any::class, String::class, Thread::class, Throwable::class, StackTraceElement::class, + WeakReference::class, ReferenceQueue::class, AbstractMap::class, + ReentrantReadWriteLock::class, SimpleDateFormat::class + ) .map { it.java } .associateWith { emptyList() } } @@ -31,10 +38,10 @@ object FieldWalker { * Reflectively starts to walk through object graph and returns identity set of all reachable objects. * Use [walkRefs] if you need a path from root for debugging. */ - public fun walk(root: Any?): Set = walkRefs(root).keys + public fun walk(root: Any?): Set = walkRefs(root, false).keys - public fun assertReachableCount(expected: Int, root: Any?, predicate: (Any) -> Boolean) { - val visited = walkRefs(root) + public fun assertReachableCount(expected: Int, root: Any?, rootStatics: Boolean = false, predicate: (Any) -> Boolean) { + val visited = walkRefs(root, rootStatics) val actual = visited.keys.filter(predicate) if (actual.size != expected) { val textDump = actual.joinToString("") { "\n\t" + showPath(it, visited) } @@ -49,16 +56,18 @@ object FieldWalker { * Reflectively starts to walk through object graph and map to all the reached object to their path * in from root. Use [showPath] do display a path if needed. */ - private fun walkRefs(root: Any?): Map { + private fun walkRefs(root: Any?, rootStatics: Boolean): Map { val visited = IdentityHashMap() if (root == null) return visited visited[root] = Ref.RootRef val stack = ArrayDeque() stack.addLast(root) + var statics = rootStatics while (stack.isNotEmpty()) { val element = stack.removeLast() try { - visit(element, visited, stack) + visit(element, visited, stack, statics) + statics = false // only scan root static when asked } catch (e: Exception) { error("Failed to visit element ${showPath(element, visited)}: $e") } @@ -75,7 +84,7 @@ object FieldWalker { when (ref) { is Ref.FieldRef -> { cur = ref.parent - path += ".${ref.name}" + path += "|${ref.parent.javaClass.simpleName}::${ref.name}" } is Ref.ArrayRef -> { cur = ref.parent @@ -87,7 +96,7 @@ object FieldWalker { return path.joinToString("") } - private fun visit(element: Any, visited: IdentityHashMap, stack: ArrayDeque) { + private fun visit(element: Any, visited: IdentityHashMap, stack: ArrayDeque, statics: Boolean) { val type = element.javaClass when { // Special code for arrays @@ -111,8 +120,16 @@ object FieldWalker { element is AtomicReference<*> -> { push(element.get(), visited, stack) { Ref.FieldRef(element, "value") } } + element is AtomicReferenceArray<*> -> { + for (index in 0 until element.length()) { + push(element[index], visited, stack) { Ref.ArrayRef(element, index) } + } + } + element is AtomicLongFieldUpdater<*> -> { + /* filter it out here to suppress its subclasses too */ + } // All the other classes are reflectively scanned - else -> fields(type).forEach { field -> + else -> fields(type, statics).forEach { field -> push(field.get(element), visited, stack) { Ref.FieldRef(element, field.name) } // special case to scan Throwable cause (cannot get it reflectively) if (element is Throwable) { @@ -129,19 +146,21 @@ object FieldWalker { } } - private fun fields(type0: Class<*>): List { + private fun fields(type0: Class<*>, rootStatics: Boolean): List { fieldsCache[type0]?.let { return it } val result = ArrayList() var type = type0 + var statics = rootStatics while (true) { val fields = type.declaredFields.filter { !it.type.isPrimitive - && !Modifier.isStatic(it.modifiers) + && (statics || !Modifier.isStatic(it.modifiers)) && !(it.type.isArray && it.type.componentType.isPrimitive) } fields.forEach { it.isAccessible = true } // make them all accessible result.addAll(fields) type = type.superclass + statics = false val superFields = fieldsCache[type] // will stop at Any anyway if (superFields != null) { result.addAll(superFields) diff --git a/kotlinx-coroutines-core/jvm/test/JobStructuredJoinStressTest.kt b/kotlinx-coroutines-core/jvm/test/JobStructuredJoinStressTest.kt index ec3635ca36..50d86f32be 100644 --- a/kotlinx-coroutines-core/jvm/test/JobStructuredJoinStressTest.kt +++ b/kotlinx-coroutines-core/jvm/test/JobStructuredJoinStressTest.kt @@ -5,6 +5,7 @@ package kotlinx.coroutines import org.junit.* +import kotlin.coroutines.* /** * Test a race between job failure and join. @@ -12,22 +13,52 @@ import org.junit.* * See [#1123](https://github.com/Kotlin/kotlinx.coroutines/issues/1123). */ class JobStructuredJoinStressTest : TestBase() { - private val nRepeats = 1_000 * stressTestMultiplier + private val nRepeats = 10_000 * stressTestMultiplier @Test - fun testStress() { - repeat(nRepeats) { + fun testStressRegularJoin() { + stress(Job::join) + } + + @Test + fun testStressSuspendCancellable() { + stress { job -> + suspendCancellableCoroutine { cont -> + job.invokeOnCompletion { cont.resume(Unit) } + } + } + } + + @Test + fun testStressSuspendCancellableReusable() { + stress { job -> + suspendCancellableCoroutineReusable { cont -> + job.invokeOnCompletion { cont.resume(Unit) } + } + } + } + + private fun stress(join: suspend (Job) -> Unit) { + expect(1) + repeat(nRepeats) { index -> assertFailsWith { runBlocking { // launch in background val job = launch(Dispatchers.Default) { throw TestException("OK") // crash } - assertFailsWith { - job.join() + try { + join(job) + error("Should not complete successfully") + } catch (e: CancellationException) { + // must always crash with cancellation exception + expect(2 + index) + } catch (e: Throwable) { + error("Unexpected exception", e) } } } } + finish(2 + nRepeats) } } \ No newline at end of file diff --git a/kotlinx-coroutines-core/jvm/test/RejectedExecutionTest.kt b/kotlinx-coroutines-core/jvm/test/RejectedExecutionTest.kt new file mode 100644 index 0000000000..7f6d6b661c --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/RejectedExecutionTest.kt @@ -0,0 +1,171 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines + +import kotlinx.coroutines.flow.* +import kotlinx.coroutines.internal.* +import kotlinx.coroutines.scheduling.* +import org.junit.* +import org.junit.Test +import java.util.concurrent.* +import kotlin.test.* + +class RejectedExecutionTest : TestBase() { + private val threadName = "RejectedExecutionTest" + private val executor = RejectingExecutor() + + @After + fun tearDown() { + executor.shutdown() + executor.awaitTermination(10, TimeUnit.SECONDS) + } + + @Test + fun testRejectOnLaunch() = runTest { + expect(1) + val job = launch(executor.asCoroutineDispatcher()) { + expectUnreached() + } + assertEquals(1, executor.submittedTasks) + assertTrue(job.isCancelled) + finish(2) + } + + @Test + fun testRejectOnLaunchAtomic() = runTest { + expect(1) + val job = launch(executor.asCoroutineDispatcher(), start = CoroutineStart.ATOMIC) { + expect(2) + assertEquals(true, coroutineContext[Job]?.isCancelled) + assertIoThread() // was rejected on start, but start was atomic + } + assertEquals(1, executor.submittedTasks) + job.join() + finish(3) + } + + @Test + fun testRejectOnWithContext() = runTest { + expect(1) + assertFailsWith { + withContext(executor.asCoroutineDispatcher()) { + expectUnreached() + } + } + assertEquals(1, executor.submittedTasks) + finish(2) + } + + @Test + fun testRejectOnResumeInContext() = runTest { + expect(1) + executor.acceptTasks = 1 // accept one task + assertFailsWith { + withContext(executor.asCoroutineDispatcher()) { + expect(2) + assertExecutorThread() + try { + withContext(Dispatchers.Default) { + expect(3) + assertDefaultDispatcherThread() + // We have to wait until caller executor thread had already suspended (if not running task), + // so that we resume back to it a new task is posted + executor.awaitNotRunningTask() + expect(4) + assertDefaultDispatcherThread() + } + // cancelled on resume back + } finally { + expect(5) + assertIoThread() + } + expectUnreached() + } + } + assertEquals(2, executor.submittedTasks) + finish(6) + } + + @Test + fun testRejectOnDelay() = runTest { + if (!removeFutureOnCancel(executor)) return@runTest // Skip this test on old JDKs + expect(1) + executor.acceptTasks = 1 // accept one task + assertFailsWith { + withContext(executor.asCoroutineDispatcher()) { + expect(2) + assertExecutorThread() + try { + delay(10) // cancelled + } finally { + // Since it was cancelled on attempt to delay, it still stays on the same thread + assertExecutorThread() + } + expectUnreached() + } + } + assertEquals(2, executor.submittedTasks) + finish(3) + } + + @Test + fun testRejectWithTimeout() = runTest { + if (!removeFutureOnCancel(executor)) return@runTest // Skip this test on old JDKs + expect(1) + executor.acceptTasks = 1 // accept one task + assertFailsWith { + withContext(executor.asCoroutineDispatcher()) { + expect(2) + assertExecutorThread() + withTimeout(1000) { + expect(3) // atomic entry into the block (legacy behavior, it seem to be Ok with way) + assertEquals(true, coroutineContext[Job]?.isCancelled) // but the job is already cancelled + } + expectUnreached() + } + } + assertEquals(2, executor.submittedTasks) + finish(4) + } + + private inner class RejectingExecutor : ScheduledThreadPoolExecutor(1, { r -> Thread(r, threadName) }) { + var acceptTasks = 0 + var submittedTasks = 0 + val runningTask = MutableStateFlow(false) + + override fun schedule(command: Runnable, delay: Long, unit: TimeUnit): ScheduledFuture<*> { + submittedTasks++ + if (submittedTasks > acceptTasks) throw RejectedExecutionException() + val wrapper = Runnable { + runningTask.value = true + try { + command.run() + } finally { + runningTask.value = false + } + } + return super.schedule(wrapper, delay, unit) + } + + suspend fun awaitNotRunningTask() = runningTask.first { !it } + } + + private fun assertExecutorThread() { + val thread = Thread.currentThread() + if (!thread.name.startsWith(threadName)) error("Not an executor thread: $thread") + } + + private fun assertDefaultDispatcherThread() { + val thread = Thread.currentThread() + if (thread !is CoroutineScheduler.Worker) error("Not a thread from Dispatchers.Default: $thread") + assertEquals(CoroutineScheduler.WorkerState.CPU_ACQUIRED, thread.state) + } + + private fun assertIoThread() { + val thread = Thread.currentThread() + if (thread !is CoroutineScheduler.Worker) error("Not a thread from Dispatchers.IO: $thread") + assertEquals(CoroutineScheduler.WorkerState.BLOCKING, thread.state) + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/jvm/test/ReusableCancellableContinuationTest.kt b/kotlinx-coroutines-core/jvm/test/ReusableCancellableContinuationTest.kt index 5f5620c632..56f1e28313 100644 --- a/kotlinx-coroutines-core/jvm/test/ReusableCancellableContinuationTest.kt +++ b/kotlinx-coroutines-core/jvm/test/ReusableCancellableContinuationTest.kt @@ -11,15 +11,14 @@ import kotlin.coroutines.* import kotlin.test.* class ReusableCancellableContinuationTest : TestBase() { - @Test fun testReusable() = runTest { - testContinuationsCount(10, 1, ::suspendAtomicCancellableCoroutineReusable) + testContinuationsCount(10, 1, ::suspendCancellableCoroutineReusable) } @Test fun testRegular() = runTest { - testContinuationsCount(10, 10, ::suspendAtomicCancellableCoroutine) + testContinuationsCount(10, 10, ::suspendCancellableCoroutine) } private suspend inline fun CoroutineScope.testContinuationsCount( @@ -51,7 +50,7 @@ class ReusableCancellableContinuationTest : TestBase() { fun testCancelledOnClaimedCancel() = runTest { expect(1) try { - suspendAtomicCancellableCoroutineReusable { + suspendCancellableCoroutineReusable { it.cancel() } expectUnreached() @@ -65,7 +64,7 @@ class ReusableCancellableContinuationTest : TestBase() { expect(1) // Bind child at first var continuation: Continuation<*>? = null - suspendAtomicCancellableCoroutineReusable { + suspendCancellableCoroutineReusable { expect(2) continuation = it launch { // Attach to the parent, avoid fast path @@ -77,13 +76,16 @@ class ReusableCancellableContinuationTest : TestBase() { ensureActive() // Verify child was bound FieldWalker.assertReachableCount(1, coroutineContext[Job]) { it === continuation } - suspendAtomicCancellableCoroutineReusable { - expect(5) - coroutineContext[Job]!!.cancel() - it.resume(Unit) + try { + suspendCancellableCoroutineReusable { + expect(5) + coroutineContext[Job]!!.cancel() + it.resume(Unit) // will not dispatch, will get CancellationException + } + } catch (e: CancellationException) { + assertFalse(isActive) + finish(6) } - assertFalse(isActive) - finish(6) } @Test @@ -93,7 +95,7 @@ class ReusableCancellableContinuationTest : TestBase() { launch { cont!!.resumeWith(Result.success(Unit)) } - suspendAtomicCancellableCoroutineReusable { + suspendCancellableCoroutineReusable { cont = it } ensureActive() @@ -108,7 +110,7 @@ class ReusableCancellableContinuationTest : TestBase() { launch { // Attach to the parent, avoid fast path cont!!.resumeWith(Result.success(Unit)) } - suspendAtomicCancellableCoroutine { + suspendCancellableCoroutine { cont = it } ensureActive() @@ -121,7 +123,7 @@ class ReusableCancellableContinuationTest : TestBase() { expect(1) var cont: Continuation<*>? = null try { - suspendAtomicCancellableCoroutineReusable { + suspendCancellableCoroutineReusable { cont = it it.cancel() } @@ -137,7 +139,7 @@ class ReusableCancellableContinuationTest : TestBase() { val currentJob = coroutineContext[Job]!! expect(1) // Bind child at first - suspendAtomicCancellableCoroutineReusable { + suspendCancellableCoroutineReusable { expect(2) // Attach to the parent, avoid fast path launch { @@ -153,15 +155,23 @@ class ReusableCancellableContinuationTest : TestBase() { assertFalse(isActive) // Child detached FieldWalker.assertReachableCount(0, currentJob) { it is CancellableContinuation<*> } - suspendAtomicCancellableCoroutineReusable { it.resume(Unit) } - suspendAtomicCancellableCoroutineReusable { it.resume(Unit) } - FieldWalker.assertReachableCount(0, currentJob) { it is CancellableContinuation<*> } - + expect(5) + try { + // Resume is non-atomic, so it throws cancellation exception + suspendCancellableCoroutineReusable { + expect(6) // but the code inside the block is executed + it.resume(Unit) + } + } catch (e: CancellationException) { + FieldWalker.assertReachableCount(0, currentJob) { it is CancellableContinuation<*> } + expect(7) + } try { - suspendAtomicCancellableCoroutineReusable {} + // No resume -- still cancellation exception + suspendCancellableCoroutineReusable {} } catch (e: CancellationException) { FieldWalker.assertReachableCount(0, currentJob) { it is CancellableContinuation<*> } - finish(5) + finish(8) } } @@ -203,6 +213,6 @@ class ReusableCancellableContinuationTest : TestBase() { for (value in channel) { delay(1) } - FieldWalker.assertReachableCount(1, coroutineContext[Job], { it is ChildContinuation }) + FieldWalker.assertReachableCount(1, coroutineContext[Job]) { it is ChildContinuation } } } diff --git a/kotlinx-coroutines-core/jvm/test/RunBlockingTest.kt b/kotlinx-coroutines-core/jvm/test/RunBlockingTest.kt index d21a9f895b..e20362ff93 100644 --- a/kotlinx-coroutines-core/jvm/test/RunBlockingTest.kt +++ b/kotlinx-coroutines-core/jvm/test/RunBlockingTest.kt @@ -162,4 +162,13 @@ class RunBlockingTest : TestBase() { handle.dispose() } + + @Test + fun testContract() { + val rb: Int + runBlocking { + rb = 42 + } + rb.hashCode() // unused + } } diff --git a/kotlinx-coroutines-core/jvm/test/RunInterruptibleStressTest.kt b/kotlinx-coroutines-core/jvm/test/RunInterruptibleStressTest.kt new file mode 100644 index 0000000000..701b958afd --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/RunInterruptibleStressTest.kt @@ -0,0 +1,56 @@ +/* + * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines + +import org.junit.* +import org.junit.Test +import java.util.concurrent.atomic.* +import kotlin.test.* + +/** + * Stress test for [runInterruptible]. + * It does not pass on JDK 1.6 on Windows: [Thread.sleep] times out without being interrupted despite the + * fact that thread interruption flag is set. + */ +class RunInterruptibleStressTest : TestBase() { + @get:Rule + val dispatcher = ExecutorRule(4) + private val repeatTimes = 1000 * stressTestMultiplier + + @Test + fun testStress() = runTest { + val enterCount = AtomicInteger(0) + val interruptedCount = AtomicInteger(0) + + repeat(repeatTimes) { + val job = launch(dispatcher) { + try { + runInterruptible { + enterCount.incrementAndGet() + try { + Thread.sleep(10_000) + error("Sleep was not interrupted, Thread.isInterrupted=${Thread.currentThread().isInterrupted}") + } catch (e: InterruptedException) { + interruptedCount.incrementAndGet() + throw e + } + } + } catch (e: CancellationException) { + // Expected + } finally { + assertFalse(Thread.currentThread().isInterrupted, "Interrupt flag should not leak") + } + } + // Add dispatch delay + val cancelJob = launch(dispatcher) { + job.cancel() + } + joinAll(job, cancelJob) + } + println("Entered runInterruptible ${enterCount.get()} times") + assertTrue(enterCount.get() > 0) // ensure timing is Ok and we don't cancel it all prematurely + assertEquals(enterCount.get(), interruptedCount.get()) + } +} diff --git a/kotlinx-coroutines-core/jvm/test/RunInterruptibleTest.kt b/kotlinx-coroutines-core/jvm/test/RunInterruptibleTest.kt new file mode 100644 index 0000000000..e755b17d91 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/RunInterruptibleTest.kt @@ -0,0 +1,63 @@ +/* + * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines + +import kotlinx.coroutines.channels.* +import java.io.* +import java.util.concurrent.* +import java.util.concurrent.atomic.* +import kotlin.test.* + +class RunInterruptibleTest : TestBase() { + + @Test + fun testNormalRun() = runTest { + val result = runInterruptible { + val x = 1 + val y = 2 + Thread.sleep(1) + x + y + } + assertEquals(3, result) + } + + @Test + fun testExceptionalRun() = runTest { + try { + runInterruptible { + expect(1) + throw TestException() + } + } catch (e: TestException) { + finish(2) + } + } + + @Test + fun testInterrupt() = runTest { + val latch = Channel(1) + val job = launch { + runInterruptible(Dispatchers.IO) { + expect(2) + latch.offer(Unit) + try { + Thread.sleep(10_000L) + expectUnreached() + } catch (e: InterruptedException) { + expect(4) + assertFalse { Thread.currentThread().isInterrupted } + } + } + } + + launch(start = CoroutineStart.UNDISPATCHED) { + expect(1) + latch.receive() + expect(3) + job.cancelAndJoin() + }.join() + finish(5) + } +} diff --git a/kotlinx-coroutines-core/jvm/test/TestBase.kt b/kotlinx-coroutines-core/jvm/test/TestBase.kt index bf462cc78f..17238e873c 100644 --- a/kotlinx-coroutines-core/jvm/test/TestBase.kt +++ b/kotlinx-coroutines-core/jvm/test/TestBase.kt @@ -69,6 +69,8 @@ public actual open class TestBase actual constructor() { throw makeError(message, cause) } + public fun hasError() = error.get() != null + private fun makeError(message: Any, cause: Throwable? = null): IllegalStateException = IllegalStateException(message.toString(), cause).also { setError(it) @@ -107,7 +109,7 @@ public actual open class TestBase actual constructor() { * Asserts that this line is never executed. */ public actual fun expectUnreached() { - error("Should not be reached") + error("Should not be reached, current action index is ${actionIndex.get()}") } /** diff --git a/kotlinx-coroutines-core/jvm/test/channels/BroadcastChannelLeakTest.kt b/kotlinx-coroutines-core/jvm/test/channels/BroadcastChannelLeakTest.kt new file mode 100644 index 0000000000..66b08c74e4 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/channels/BroadcastChannelLeakTest.kt @@ -0,0 +1,34 @@ +package kotlinx.coroutines.channels + +import kotlinx.coroutines.* +import org.junit.Test +import kotlin.test.* + +class BroadcastChannelLeakTest : TestBase() { + @Test + fun testArrayBroadcastChannelSubscriptionLeak() { + checkLeak { ArrayBroadcastChannel(1) } + } + + @Test + fun testConflatedBroadcastChannelSubscriptionLeak() { + checkLeak { ConflatedBroadcastChannel() } + } + + enum class TestKind { BROADCAST_CLOSE, SUB_CANCEL, BOTH } + + private fun checkLeak(factory: () -> BroadcastChannel) = runTest { + for (kind in TestKind.values()) { + val broadcast = factory() + val sub = broadcast.openSubscription() + broadcast.send("OK") + assertEquals("OK", sub.receive()) + // now close broadcast + if (kind != TestKind.SUB_CANCEL) broadcast.close() + // and then cancel subscription + if (kind != TestKind.BROADCAST_CLOSE) sub.cancel() + // subscription should not be reachable from the channel anymore + FieldWalker.assertReachableCount(0, broadcast) { it === sub } + } + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/jvm/test/channels/BroadcastChannelMultiReceiveStressTest.kt b/kotlinx-coroutines-core/jvm/test/channels/BroadcastChannelMultiReceiveStressTest.kt index 54ba7b639f..2e73b2432a 100644 --- a/kotlinx-coroutines-core/jvm/test/channels/BroadcastChannelMultiReceiveStressTest.kt +++ b/kotlinx-coroutines-core/jvm/test/channels/BroadcastChannelMultiReceiveStressTest.kt @@ -48,8 +48,9 @@ class BroadcastChannelMultiReceiveStressTest( launch(pool + CoroutineName("Sender")) { var i = 0L while (isActive) { - broadcast.send(++i) - sentTotal.set(i) // set sentTotal only if `send` was not cancelled + i++ + broadcast.send(i) // could be cancelled + sentTotal.set(i) // only was for it if it was not cancelled } } val receivers = mutableListOf() @@ -88,10 +89,8 @@ class BroadcastChannelMultiReceiveStressTest( try { withTimeout(5000) { receivers.forEachIndexed { index, receiver -> - if (lastReceived[index].get() == total) - receiver.cancel() - else - receiver.join() + if (lastReceived[index].get() >= total) receiver.cancel() + receiver.join() } } } catch (e: Exception) { @@ -112,7 +111,7 @@ class BroadcastChannelMultiReceiveStressTest( check(i == last + 1) { "Last was $last, got $i" } receivedTotal.incrementAndGet() lastReceived[receiverIndex].set(i) - return i == stopOnReceive.get() + return i >= stopOnReceive.get() } private suspend fun doReceive(channel: ReceiveChannel, receiverIndex: Int) { diff --git a/kotlinx-coroutines-core/jvm/test/channels/ChannelAtomicCancelStressTest.kt b/kotlinx-coroutines-core/jvm/test/channels/ChannelAtomicCancelStressTest.kt deleted file mode 100644 index 6556888a0f..0000000000 --- a/kotlinx-coroutines-core/jvm/test/channels/ChannelAtomicCancelStressTest.kt +++ /dev/null @@ -1,156 +0,0 @@ -/* - * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package kotlinx.coroutines.channels - -import kotlinx.coroutines.* -import kotlinx.coroutines.selects.* -import org.junit.After -import org.junit.Test -import org.junit.runner.* -import org.junit.runners.* -import kotlin.random.Random -import java.util.concurrent.atomic.* -import kotlin.test.* - -/** - * Tests cancel atomicity for channel send & receive operations, including their select versions. - */ -@RunWith(Parameterized::class) -class ChannelAtomicCancelStressTest(private val kind: TestChannelKind) : TestBase() { - companion object { - @Parameterized.Parameters(name = "{0}") - @JvmStatic - fun params(): Collection> = TestChannelKind.values().map { arrayOf(it) } - } - - private val TEST_DURATION = 1000L * stressTestMultiplier - - private val dispatcher = newFixedThreadPoolContext(2, "ChannelAtomicCancelStressTest") - private val scope = CoroutineScope(dispatcher) - - private val channel = kind.create() - private val senderDone = Channel(1) - private val receiverDone = Channel(1) - - private var lastSent = 0 - private var lastReceived = 0 - - private var stoppedSender = 0 - private var stoppedReceiver = 0 - - private var missedCnt = 0 - private var dupCnt = 0 - - val failed = AtomicReference() - - lateinit var sender: Job - lateinit var receiver: Job - - @After - fun tearDown() { - dispatcher.close() - } - - fun fail(e: Throwable) = failed.compareAndSet(null, e) - - private inline fun cancellable(done: Channel, block: () -> Unit) { - try { - block() - } finally { - if (!done.offer(true)) - fail(IllegalStateException("failed to offer to done channel")) - } - } - - @Test - fun testAtomicCancelStress() = runBlocking { - println("--- ChannelAtomicCancelStressTest $kind") - val deadline = System.currentTimeMillis() + TEST_DURATION - launchSender() - launchReceiver() - while (System.currentTimeMillis() < deadline && failed.get() == null) { - when (Random.nextInt(3)) { - 0 -> { // cancel & restart sender - stopSender() - launchSender() - } - 1 -> { // cancel & restart receiver - stopReceiver() - launchReceiver() - } - 2 -> yield() // just yield (burn a little time) - } - } - stopSender() - stopReceiver() - println(" Sent $lastSent ints to channel") - println(" Received $lastReceived ints from channel") - println(" Stopped sender $stoppedSender times") - println("Stopped receiver $stoppedReceiver times") - println(" Missed $missedCnt ints") - println(" Duplicated $dupCnt ints") - failed.get()?.let { throw it } - assertEquals(0, dupCnt) - if (!kind.isConflated) { - assertEquals(0, missedCnt) - assertEquals(lastSent, lastReceived) - } - } - - private fun launchSender() { - sender = scope.launch(start = CoroutineStart.ATOMIC) { - cancellable(senderDone) { - var counter = 0 - while (true) { - val trySend = lastSent + 1 - when (Random.nextInt(2)) { - 0 -> channel.send(trySend) - 1 -> select { channel.onSend(trySend) {} } - else -> error("cannot happen") - } - lastSent = trySend // update on success - when { - // must artificially slow down LINKED_LIST sender to avoid overwhelming receiver and going OOM - kind == TestChannelKind.LINKED_LIST -> while (lastSent > lastReceived + 100) yield() - // yield periodically to check cancellation on conflated channels - kind.isConflated -> if (counter++ % 100 == 0) yield() - } - } - } - } - } - - private suspend fun stopSender() { - stoppedSender++ - sender.cancel() - senderDone.receive() - } - - private fun launchReceiver() { - receiver = scope.launch(start = CoroutineStart.ATOMIC) { - cancellable(receiverDone) { - while (true) { - val received = when (Random.nextInt(2)) { - 0 -> channel.receive() - 1 -> select { channel.onReceive { it } } - else -> error("cannot happen") - } - val expected = lastReceived + 1 - if (received > expected) - missedCnt++ - if (received < expected) - dupCnt++ - lastReceived = received - } - } - } - } - - private suspend fun stopReceiver() { - stoppedReceiver++ - receiver.cancel() - receiverDone.receive() - } -} diff --git a/kotlinx-coroutines-core/jvm/test/channels/ChannelCancelUndeliveredElementStressTest.kt b/kotlinx-coroutines-core/jvm/test/channels/ChannelCancelUndeliveredElementStressTest.kt new file mode 100644 index 0000000000..76713aa173 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/channels/ChannelCancelUndeliveredElementStressTest.kt @@ -0,0 +1,102 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.channels + +import kotlinx.coroutines.* +import kotlinx.coroutines.selects.* +import java.util.concurrent.atomic.* +import kotlin.random.* +import kotlin.test.* + +class ChannelCancelUndeliveredElementStressTest : TestBase() { + private val repeatTimes = 10_000 * stressTestMultiplier + + // total counters + private var sendCnt = 0 + private var offerFailedCnt = 0 + private var receivedCnt = 0 + private var undeliveredCnt = 0 + + // last operation + private var lastReceived = 0 + private var dSendCnt = 0 + private var dSendExceptionCnt = 0 + private var dOfferFailedCnt = 0 + private var dReceivedCnt = 0 + private val dUndeliveredCnt = AtomicInteger() + + @Test + fun testStress() = runTest { + repeat(repeatTimes) { + val channel = Channel(1) { dUndeliveredCnt.incrementAndGet() } + val j1 = launch(Dispatchers.Default) { + sendOne(channel) // send first + sendOne(channel) // send second + } + val j2 = launch(Dispatchers.Default) { + receiveOne(channel) // receive one element from the channel + channel.cancel() // cancel the channel + } + + joinAll(j1, j2) + + // All elements must be either received or undelivered (IN every run) + if (dSendCnt - dOfferFailedCnt != dReceivedCnt + dUndeliveredCnt.get()) { + println(" Send: $dSendCnt") + println("Send Exception: $dSendExceptionCnt") + println(" Offer failed: $dOfferFailedCnt") + println(" Received: $dReceivedCnt") + println(" Undelivered: ${dUndeliveredCnt.get()}") + error("Failed") + } + offerFailedCnt += dOfferFailedCnt + receivedCnt += dReceivedCnt + undeliveredCnt += dUndeliveredCnt.get() + // clear for next run + dSendCnt = 0 + dSendExceptionCnt = 0 + dOfferFailedCnt = 0 + dReceivedCnt = 0 + dUndeliveredCnt.set(0) + } + // Stats + println(" Send: $sendCnt") + println(" Offer failed: $offerFailedCnt") + println(" Received: $receivedCnt") + println(" Undelivered: $undeliveredCnt") + assertEquals(sendCnt - offerFailedCnt, receivedCnt + undeliveredCnt) + } + + private suspend fun sendOne(channel: Channel) { + dSendCnt++ + val i = ++sendCnt + try { + when (Random.nextInt(2)) { + 0 -> channel.send(i) + 1 -> if (!channel.offer(i)) { + dOfferFailedCnt++ + } + } + } catch(e: Throwable) { + assertTrue(e is CancellationException) // the only exception possible in this test + dSendExceptionCnt++ + throw e + } + } + + private suspend fun receiveOne(channel: Channel) { + val received = when (Random.nextInt(3)) { + 0 -> channel.receive() + 1 -> channel.receiveOrNull() ?: error("Cannot be closed yet") + 2 -> select { + channel.onReceive { it } + } + else -> error("Cannot happen") + } + assertTrue(received > lastReceived) + dReceivedCnt++ + lastReceived = received + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/jvm/test/channels/ChannelSendReceiveStressTest.kt b/kotlinx-coroutines-core/jvm/test/channels/ChannelSendReceiveStressTest.kt index 00c5a6090f..f414c33338 100644 --- a/kotlinx-coroutines-core/jvm/test/channels/ChannelSendReceiveStressTest.kt +++ b/kotlinx-coroutines-core/jvm/test/channels/ChannelSendReceiveStressTest.kt @@ -35,7 +35,7 @@ class ChannelSendReceiveStressTest( private val maxBuffer = 10_000 // artificial limit for LinkedListChannel - val channel = kind.create() + val channel = kind.create() private val sendersCompleted = AtomicInteger() private val receiversCompleted = AtomicInteger() private val dupes = AtomicInteger() diff --git a/kotlinx-coroutines-core/jvm/test/channels/ChannelUndeliveredElementStressTest.kt b/kotlinx-coroutines-core/jvm/test/channels/ChannelUndeliveredElementStressTest.kt new file mode 100644 index 0000000000..1188329a4c --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/channels/ChannelUndeliveredElementStressTest.kt @@ -0,0 +1,255 @@ +/* + * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.channels + +import kotlinx.atomicfu.* +import kotlinx.coroutines.* +import kotlinx.coroutines.selects.* +import org.junit.After +import org.junit.Test +import org.junit.runner.* +import org.junit.runners.* +import kotlin.random.Random +import kotlin.test.* + +/** + * Tests resource transfer via channel send & receive operations, including their select versions, + * using `onUndeliveredElement` to detect lost resources and close them properly. + */ +@RunWith(Parameterized::class) +class ChannelUndeliveredElementStressTest(private val kind: TestChannelKind) : TestBase() { + companion object { + @Parameterized.Parameters(name = "{0}") + @JvmStatic + fun params(): Collection> = + TestChannelKind.values() + .filter { !it.viaBroadcast } + .map { arrayOf(it) } + } + + private val iterationDurationMs = 100L + private val testIterations = 20 * stressTestMultiplier // 2 sec + + private val dispatcher = newFixedThreadPoolContext(2, "ChannelAtomicCancelStressTest") + private val scope = CoroutineScope(dispatcher) + + private val channel = kind.create { it.failedToDeliver() } + private val senderDone = Channel(1) + private val receiverDone = Channel(1) + + @Volatile + private var lastReceived = -1L + + private var stoppedSender = 0L + private var stoppedReceiver = 0L + + private var sentCnt = 0L // total number of send attempts + private var receivedCnt = 0L // actually received successfully + private var dupCnt = 0L // duplicates (should never happen) + private val failedToDeliverCnt = atomic(0L) // out of sent + + private val modulo = 1 shl 25 + private val mask = (modulo - 1).toLong() + private val sentStatus = ItemStatus() // 1 - send norm, 2 - send select, +2 - did not throw exception + private val receivedStatus = ItemStatus() // 1-6 received + private val failedStatus = ItemStatus() // 1 - failed + + lateinit var sender: Job + lateinit var receiver: Job + + @After + fun tearDown() { + dispatcher.close() + } + + private inline fun cancellable(done: Channel, block: () -> Unit) { + try { + block() + } finally { + if (!done.offer(true)) + error(IllegalStateException("failed to offer to done channel")) + } + } + + @Test + fun testAtomicCancelStress() = runBlocking { + println("=== ChannelAtomicCancelStressTest $kind") + var nextIterationTime = System.currentTimeMillis() + iterationDurationMs + var iteration = 0 + launchSender() + launchReceiver() + while (!hasError()) { + if (System.currentTimeMillis() >= nextIterationTime) { + nextIterationTime += iterationDurationMs + iteration++ + verify(iteration) + if (iteration % 10 == 0) printProgressSummary(iteration) + if (iteration >= testIterations) break + launchSender() + launchReceiver() + } + when (Random.nextInt(3)) { + 0 -> { // cancel & restart sender + stopSender() + launchSender() + } + 1 -> { // cancel & restart receiver + stopReceiver() + launchReceiver() + } + 2 -> yield() // just yield (burn a little time) + } + } + } + + private suspend fun verify(iteration: Int) { + stopSender() + drainReceiver() + stopReceiver() + try { + assertEquals(0, dupCnt) + assertEquals(sentCnt - failedToDeliverCnt.value, receivedCnt) + } catch (e: Throwable) { + printProgressSummary(iteration) + printErrorDetails() + throw e + } + sentStatus.clear() + receivedStatus.clear() + failedStatus.clear() + } + + private fun printProgressSummary(iteration: Int) { + println("--- ChannelAtomicCancelStressTest $kind -- $iteration of $testIterations") + println(" Sent $sentCnt times to channel") + println(" Received $receivedCnt times from channel") + println(" Failed to deliver ${failedToDeliverCnt.value} times") + println(" Stopped sender $stoppedSender times") + println(" Stopped receiver $stoppedReceiver times") + println(" Duplicated $dupCnt deliveries") + } + + private fun printErrorDetails() { + val min = minOf(sentStatus.min, receivedStatus.min, failedStatus.min) + val max = maxOf(sentStatus.max, receivedStatus.max, failedStatus.max) + for (x in min..max) { + val sentCnt = if (sentStatus[x] != 0) 1 else 0 + val receivedCnt = if (receivedStatus[x] != 0) 1 else 0 + val failedToDeliverCnt = failedStatus[x] + if (sentCnt - failedToDeliverCnt != receivedCnt) { + println("!!! Error for value $x: " + + "sentStatus=${sentStatus[x]}, " + + "receivedStatus=${receivedStatus[x]}, " + + "failedStatus=${failedStatus[x]}" + ) + } + } + } + + + private fun launchSender() { + sender = scope.launch(start = CoroutineStart.ATOMIC) { + cancellable(senderDone) { + var counter = 0 + while (true) { + val trySendData = Data(sentCnt++) + val sendMode = Random.nextInt(2) + 1 + sentStatus[trySendData.x] = sendMode + when (sendMode) { + 1 -> channel.send(trySendData) + 2 -> select { channel.onSend(trySendData) {} } + else -> error("cannot happen") + } + sentStatus[trySendData.x] = sendMode + 2 + when { + // must artificially slow down LINKED_LIST sender to avoid overwhelming receiver and going OOM + kind == TestChannelKind.LINKED_LIST -> while (sentCnt > lastReceived + 100) yield() + // yield periodically to check cancellation on conflated channels + kind.isConflated -> if (counter++ % 100 == 0) yield() + } + } + } + } + } + + private suspend fun stopSender() { + stoppedSender++ + sender.cancel() + senderDone.receive() + } + + private fun launchReceiver() { + receiver = scope.launch(start = CoroutineStart.ATOMIC) { + cancellable(receiverDone) { + while (true) { + val receiveMode = Random.nextInt(6) + 1 + val receivedData = when (receiveMode) { + 1 -> channel.receive() + 2 -> select { channel.onReceive { it } } + 3 -> channel.receiveOrNull() ?: error("Should not be closed") + 4 -> select { channel.onReceiveOrNull { it ?: error("Should not be closed") } } + 5 -> channel.receiveOrClosed().value + 6 -> { + val iterator = channel.iterator() + check(iterator.hasNext()) { "Should not be closed" } + iterator.next() + } + else -> error("cannot happen") + } + receivedCnt++ + val received = receivedData.x + if (received <= lastReceived) + dupCnt++ + lastReceived = received + receivedStatus[received] = receiveMode + } + } + } + } + + private suspend fun drainReceiver() { + while (!channel.isEmpty) yield() // burn time until receiver gets it all + } + + private suspend fun stopReceiver() { + stoppedReceiver++ + receiver.cancel() + receiverDone.receive() + } + + private inner class Data(val x: Long) { + private val failedToDeliver = atomic(false) + + fun failedToDeliver() { + check(failedToDeliver.compareAndSet(false, true)) { "onUndeliveredElement notified twice" } + failedToDeliverCnt.incrementAndGet() + failedStatus[x] = 1 + } + } + + inner class ItemStatus { + private val a = ByteArray(modulo) + private val _min = atomic(Long.MAX_VALUE) + private val _max = atomic(-1L) + + val min: Long get() = _min.value + val max: Long get() = _max.value + + operator fun set(x: Long, value: Int) { + a[(x and mask).toInt()] = value.toByte() + _min.update { y -> minOf(x, y) } + _max.update { y -> maxOf(x, y) } + } + + operator fun get(x: Long): Int = a[(x and mask).toInt()].toInt() + + fun clear() { + if (_max.value < 0) return + for (x in _min.value.._max.value) a[(x and mask).toInt()] = 0 + _min.value = Long.MAX_VALUE + _max.value = -1L + } + } +} diff --git a/kotlinx-coroutines-core/jvm/test/channels/ChannelsConsumeTest.kt b/kotlinx-coroutines-core/jvm/test/channels/ChannelsConsumeTest.kt deleted file mode 100644 index cb19b36a13..0000000000 --- a/kotlinx-coroutines-core/jvm/test/channels/ChannelsConsumeTest.kt +++ /dev/null @@ -1,908 +0,0 @@ -/* - * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -@file:Suppress("DEPRECATION") - -package kotlinx.coroutines.channels - -import kotlinx.coroutines.* -import kotlin.coroutines.* -import kotlin.test.* - -/** - * Tests that various operators on channels properly consume (close) their source channels. - */ -class ChannelsConsumeTest : TestBase() { - private val sourceList = (1..10).toList() - - // test source with numbers 1..10 - private fun CoroutineScope.testSource() = produce(NonCancellable) { - for (i in sourceList) { - send(i) - } - } - - @Test - fun testConsume() { - checkTerminal { - consume { - assertEquals(1, receive()) - } - } - } - - @Test - fun testConsumeEach() { - checkTerminal { - var sum = 0 - consumeEach { sum += it } - assertEquals(55, sum) - } - } - - @Test - fun testConsumeEachIndexed() { - checkTerminal { - var sum = 0 - consumeEachIndexed { (index, i) -> sum += index * i } - assertEquals(330, sum) - } - } - - @Test - fun testElementAt() { - checkTerminal { - assertEquals(2, elementAt(1)) - } - checkTerminal(expected = { it is IndexOutOfBoundsException }) { - elementAt(10) - } - } - - @Test - fun testElementAtOrElse() { - checkTerminal { - assertEquals(3, elementAtOrElse(2) { error("Cannot happen") }) - } - checkTerminal { - assertEquals(-23, elementAtOrElse(10) { -23 }) - } - } - - @Test - fun testElementOrNull() { - checkTerminal { - assertEquals(4, elementAtOrNull(3)) - } - checkTerminal { - assertNull(elementAtOrNull(10)) - } - } - - @Test - fun testFind() { - checkTerminal { - assertEquals(3, find { it % 3 == 0 }) - } - } - - @Test - fun testFindLast() { - checkTerminal { - assertEquals(9, findLast { it % 3 == 0 }) - } - } - - @Test - fun testFirst() { - checkTerminal { - assertEquals(1, first()) - } - } - - @Test - fun testFirstPredicate() { - checkTerminal { - assertEquals(3, first { it % 3 == 0 }) - } - checkTerminal(expected = { it is NoSuchElementException }) { - first { it > 10 } - } - } - - @Test - fun testFirstOrNull() { - checkTerminal { - assertEquals(1, firstOrNull()) - } - } - - @Test - fun testFirstOrNullPredicate() { - checkTerminal { - assertEquals(3, firstOrNull { it % 3 == 0 }) - } - checkTerminal { - assertNull(firstOrNull { it > 10 }) - } - } - - @Test - fun testIndexOf() { - checkTerminal { - assertEquals(2, indexOf(3)) - } - checkTerminal { - assertEquals(-1, indexOf(11)) - } - } - - @Test - fun testIndexOfFirst() { - checkTerminal { - assertEquals(2, indexOfFirst { it % 3 == 0 }) - } - checkTerminal { - assertEquals(-1, indexOfFirst { it > 10 }) - } - } - - @Test - fun testIndexOfLast() { - checkTerminal { - assertEquals(8, indexOfLast { it % 3 == 0 }) - } - checkTerminal { - assertEquals(-1, indexOfLast { it > 10 }) - } - } - - @Test - fun testLast() { - checkTerminal { - assertEquals(10, last()) - } - } - - @Test - fun testLastPredicate() { - checkTerminal { - assertEquals(9, last { it % 3 == 0 }) - } - checkTerminal(expected = { it is NoSuchElementException }) { - last { it > 10 } - } - } - - @Test - fun testLastIndexOf() { - checkTerminal { - assertEquals(8, lastIndexOf(9)) - } - } - - @Test - fun testLastOrNull() { - checkTerminal { - assertEquals(10, lastOrNull()) - } - } - - @Test - fun testLastOrNullPredicate() { - checkTerminal { - assertEquals(9, lastOrNull { it % 3 == 0 }) - } - checkTerminal { - assertNull(lastOrNull { it > 10 }) - } - } - - @Test - fun testSingle() { - checkTerminal(expected = { it is IllegalArgumentException }) { - single() - } - } - - @Test - fun testSinglePredicate() { - checkTerminal { - assertEquals(7, single { it % 7 == 0 }) - } - checkTerminal(expected = { it is IllegalArgumentException }) { - single { it % 3 == 0 } - } - checkTerminal(expected = { it is NoSuchElementException }) { - single { it > 10 } - } - } - - @Test - fun testSingleOrNull() { - checkTerminal { - assertNull(singleOrNull()) - } - } - - @Test - fun testSingleOrNullPredicate() { - checkTerminal { - assertEquals(7, singleOrNull { it % 7 == 0 }) - } - checkTerminal { - assertNull(singleOrNull { it % 3 == 0 }) - } - checkTerminal { - assertNull(singleOrNull { it > 10 }) - } - } - - @Test - fun testDrop() { - checkTransform(sourceList.drop(3)) { - drop(3) - } - } - - @Test - fun testDropWhile() { - checkTransform(sourceList.dropWhile { it < 4}) { - dropWhile { it < 4 } - } - } - - @Test - fun testFilter() { - checkTransform(sourceList.filter { it % 2 == 0 }) { - filter { it % 2 == 0 } - } - } - - @Test - fun testFilterIndexed() { - checkTransform(sourceList.filterIndexed { index, _ -> index % 2 == 0 }) { - filterIndexed { index, _ -> index % 2 == 0 } - } - } - - @Test - fun testFilterIndexedToCollection() { - checkTerminal { - val list = mutableListOf() - filterIndexedTo(list) { index, _ -> index % 2 == 0 } - assertEquals(listOf(1, 3, 5, 7, 9), list) - } - } - - @Test - fun testFilterIndexedToChannel() { - checkTerminal { - val channel = Channel() - val result = GlobalScope.async { channel.toList() } - filterIndexedTo(channel) { index, _ -> index % 2 == 0 } - channel.close() - assertEquals(listOf(1, 3, 5, 7, 9), result.await()) - } - } - - @Test - fun testFilterNot() { - checkTransform(sourceList.filterNot { it % 2 == 0 }) { - filterNot { it % 2 == 0 } - } - } - - @Test - fun testFilterNotNullToCollection() { - checkTerminal { - val list = mutableListOf() - filterNotNullTo(list) - assertEquals((1..10).toList(), list) - } - } - - @Test - fun testFilterNotNullToChannel() { - checkTerminal { - val channel = Channel() - val result = GlobalScope.async { channel.toList() } - filterNotNullTo(channel) - channel.close() - assertEquals((1..10).toList(), result.await()) - } - } - - @Test - fun testFilterNotToCollection() { - checkTerminal { - val list = mutableListOf() - filterNotTo(list) { it % 2 == 0 } - assertEquals(listOf(1, 3, 5, 7, 9), list) - } - } - - @Test - fun testFilterNotToChannel() { - checkTerminal { - val channel = Channel() - val result = GlobalScope.async { channel.toList() } - filterNotTo(channel) { it % 2 == 0 } - channel.close() - assertEquals(listOf(1, 3, 5, 7, 9), result.await()) - } - } - - @Test - fun testFilterToCollection() { - checkTerminal { - val list = mutableListOf() - filterTo(list) { it % 2 == 0 } - assertEquals(listOf(2, 4, 6, 8, 10), list) - } - } - - @Test - fun testFilterToChannel() { - checkTerminal { - val channel = Channel() - val result = GlobalScope.async { channel.toList() } - filterTo(channel) { it % 2 == 0 } - channel.close() - assertEquals(listOf(2, 4, 6, 8, 10), result.await()) - } - } - - @Test - fun testTake() { - checkTransform(sourceList.take(3)) { - take(3) - } - } - - @Test - fun testTakeWhile() { - checkTransform(sourceList.takeWhile { it < 4 }) { - takeWhile { it < 4 } - } - } - - @Test - fun testAssociate() { - checkTerminal { - assertEquals(sourceList.associate { it to it.toString() }, associate { it to it.toString() }) - } - } - - @Test - fun testAssociateBy() { - checkTerminal { - assertEquals(sourceList.associateBy { it.toString() }, associateBy { it.toString() }) - } - } - - @Test - fun testAssociateByTwo() { - checkTerminal { - assertEquals(sourceList.associateBy({ it.toString() }, { it + 1}), associateBy({ it.toString() }, { it + 1})) - } - } - - @Test - fun testAssociateByToMap() { - checkTerminal { - val map = mutableMapOf() - associateByTo(map) { it.toString() } - assertEquals(sourceList.associateBy { it.toString() }, map) - } - } - - @Test - fun testAssociateByTwoToMap() { - checkTerminal { - val map = mutableMapOf() - associateByTo(map, { it.toString() }, { it + 1}) - assertEquals(sourceList.associateBy({ it.toString() }, { it + 1}), map) - } - } - - @Test - fun testAssociateToMap() { - checkTerminal { - val map = mutableMapOf() - associateTo(map) { it to it.toString() } - assertEquals(sourceList.associate { it to it.toString() }, map) - } - } - - @Test - fun testToChannel() { - checkTerminal { - val channel = Channel() - val result = GlobalScope.async { channel.toList() } - toChannel(channel) - channel.close() - assertEquals(sourceList, result.await()) - } - } - - @Test - fun testToCollection() { - checkTerminal { - val list = mutableListOf() - toCollection(list) - assertEquals(sourceList, list) - } - } - - @Test - fun testToList() { - checkTerminal { - val list = toList() - assertEquals(sourceList, list) - } - } - - @Test - fun testToMap() { - checkTerminal { - val map = map { it to it.toString() }.toMap() - assertEquals(sourceList.map { it to it.toString() }.toMap(), map) - } - } - - @Test - fun testToMapWithMap() { - checkTerminal { - val map = mutableMapOf() - map { it to it.toString() }.toMap(map) - assertEquals(sourceList.map { it to it.toString() }.toMap(), map) - } - } - - @Test - fun testToMutableList() { - checkTerminal { - val list = toMutableList() - assertEquals(sourceList, list) - } - } - - @Test - fun testToSet() { - checkTerminal { - val set = toSet() - assertEquals(sourceList.toSet(), set) - } - } - - @Test - fun testFlatMap() { - checkTransform(sourceList.flatMap { listOf("A$it", "B$it") }) { - flatMap { - GlobalScope.produce(coroutineContext) { - send("A$it") - send("B$it") - } - } - } - } - - @Test - fun testGroupBy() { - checkTerminal { - val map = groupBy { it % 2 } - assertEquals(sourceList.groupBy { it % 2 }, map) - } - } - - @Test - fun testGroupByTwo() { - checkTerminal { - val map = groupBy({ it % 2 }, { it.toString() }) - assertEquals(sourceList.groupBy({ it % 2 }, { it.toString() }), map) - } - } - - @Test - fun testGroupByTo() { - checkTerminal { - val map = mutableMapOf>() - groupByTo(map) { it % 2 } - assertEquals(sourceList.groupBy { it % 2 }, map) - } - } - - @Test - fun testGroupByToTwo() { - checkTerminal { - val map = mutableMapOf>() - groupByTo(map, { it % 2 }, { it.toString() }) - assertEquals(sourceList.groupBy({ it % 2 }, { it.toString() }), map) - } - } - - @Test - fun testMap() { - checkTransform(sourceList.map { it.toString() }) { - map { it.toString() } - } - } - - @Test - fun testMapIndexed() { - checkTransform(sourceList.mapIndexed { index, v -> "$index$v" }) { - mapIndexed { index, v -> "$index$v" } - } - } - - @Test - fun testMapIndexedNotNull() { - checkTransform(sourceList.mapIndexedNotNull { index, v -> "$index$v".takeIf { v % 2 == 0 } }) { - mapIndexedNotNull { index, v -> "$index$v".takeIf { v % 2 == 0 } } - } - } - - @Test - fun testMapIndexedNotNullToCollection() { - checkTerminal { - val list = mutableListOf() - mapIndexedNotNullTo(list) { index, v -> "$index$v".takeIf { v % 2 == 0 } } - assertEquals(sourceList.mapIndexedNotNull { index, v -> "$index$v".takeIf { v % 2 == 0 } }, list) - } - } - - @Test - fun testMapIndexedNotNullToChannel() { - checkTerminal { - val channel = Channel() - val result = GlobalScope.async { channel.toList() } - mapIndexedNotNullTo(channel) { index, v -> "$index$v".takeIf { v % 2 == 0 } } - channel.close() - assertEquals(sourceList.mapIndexedNotNull { index, v -> "$index$v".takeIf { v % 2 == 0 } }, result.await()) - } - } - - @Test - fun testMapIndexedToCollection() { - checkTerminal { - val list = mutableListOf() - mapIndexedTo(list) { index, v -> "$index$v" } - assertEquals(sourceList.mapIndexed { index, v -> "$index$v" }, list) - } - } - - @Test - fun testMapIndexedToChannel() { - checkTerminal { - val channel = Channel() - val result = GlobalScope.async { channel.toList() } - mapIndexedTo(channel) { index, v -> "$index$v" } - channel.close() - assertEquals(sourceList.mapIndexed { index, v -> "$index$v" }, result.await()) - } - } - - @Test - fun testMapNotNull() { - checkTransform(sourceList.mapNotNull { (it + 3).takeIf { it % 2 == 0 } }) { - mapNotNull { (it + 3).takeIf { it % 2 == 0 } } - } - } - - @Test - fun testMapNotNullToCollection() { - checkTerminal { - val list = mutableListOf() - mapNotNullTo(list) { (it + 3).takeIf { it % 2 == 0 } } - assertEquals(sourceList.mapNotNull { (it + 3).takeIf { it % 2 == 0 } }, list) - } - } - - @Test - fun testMapNotNullToChannel() { - checkTerminal { - val channel = Channel() - val result = GlobalScope.async { channel.toList() } - mapNotNullTo(channel) { (it + 3).takeIf { it % 2 == 0 } } - channel.close() - assertEquals(sourceList.mapNotNull { (it + 3).takeIf { it % 2 == 0 } }, result.await()) - } - } - - @Test - fun testMapToCollection() { - checkTerminal { - val list = mutableListOf() - mapTo(list) { it + 3 } - assertEquals(sourceList.map { it + 3 }, list) - } - } - - @Test - fun testMapToChannel() { - checkTerminal { - val channel = Channel() - val result = GlobalScope.async { channel.toList() } - mapTo(channel) { it + 3 } - channel.close() - assertEquals(sourceList.map { it + 3 }, result.await()) - } - } - - @Test - fun testWithIndex() { - checkTransform(sourceList.asSequence().withIndex().toList()) { - withIndex() - } - } - - @Test - fun testDistinctBy() { - checkTransform(sourceList.distinctBy { it / 2 }) { - distinctBy { it / 2 } - } - } - - @Test - fun testToMutableSet() { - checkTerminal { - val set = toMutableSet() - assertEquals(sourceList.toSet(), set) - } - } - - @Test - fun testAll() { - checkTerminal { - val all = all { it < 11 } - assertEquals(sourceList.all { it < 11 }, all) - } - } - - @Test - fun testAny() { - checkTerminal { - val any = any() - assertEquals(sourceList.any(), any) - } - } - - @Test - fun testAnyPredicate() { - checkTerminal { - val any = any { it % 3 == 0 } - assertEquals(sourceList.any { it % 3 == 0 }, any) - } - } - - @Test - fun testCount() { - checkTerminal { - val c = count() - assertEquals(sourceList.count(), c) - } - } - - @Test - fun testCountPredicate() { - checkTerminal { - val c = count { it % 3 == 0 } - assertEquals(sourceList.count { it % 3 == 0 }, c) - } - } - - @Test - fun testFold() { - checkTerminal { - val c = fold(1) { a, b -> a + b } - assertEquals(sourceList.fold(1) { a, b -> a + b }, c) - } - } - - @Test - fun testFoldIndexed() { - checkTerminal { - val c = foldIndexed(1) { i, a, b -> i * a + b } - assertEquals(sourceList.foldIndexed(1) { i, a, b -> i * a + b }, c) - } - } - - @Test - fun testMaxBy() { - checkTerminal { - val c = maxBy { it % 3 } - assertEquals(sourceList.maxBy { it % 3 }, c) - } - } - - @Test - fun testMaxWith() { - checkTerminal { - val c = maxWith(compareBy { it % 3 }) - assertEquals(sourceList.maxWith(compareBy { it % 3 }), c) - } - } - - @Test - fun testMinBy() { - checkTerminal { - val c = maxBy { it % 3 } - assertEquals(sourceList.maxBy { it % 3 }, c) - } - } - - @Test - fun testMinWith() { - checkTerminal { - val c = maxWith(compareBy { it % 3 }) - assertEquals(sourceList.maxWith(compareBy { it % 3 }), c) - } - } - - @Test - fun testNone() { - checkTerminal { - val none = none() - assertEquals(sourceList.none(), none) - } - } - - @Test - fun testNonePredicate() { - checkTerminal { - val none = none { it > 10 } - assertEquals(sourceList.none { it > 10 }, none) - } - } - - @Test - fun testReduce() { - checkTerminal { - val c = reduce { a, b -> a + b } - assertEquals(sourceList.reduce { a, b -> a + b }, c) - } - } - - @Test - fun testReduceIndexed() { - checkTerminal { - val c = reduceIndexed { i, a, b -> i * a + b } - assertEquals(sourceList.reduceIndexed { i, a, b -> i * a + b }, c) - } - } - - @Test - fun testSubBy() { - checkTerminal { - val c = sumBy { it } - assertEquals(sourceList.sumBy { it }, c) - } - } - - @Test - fun testSubByDouble() { - checkTerminal { - val c = sumByDouble { it.toDouble() } - assertEquals(sourceList.sumByDouble { it.toDouble() }, c) - } - } - - @Test - fun testPartition() { - checkTerminal { - val pair = partition { it % 2 == 0 } - assertEquals(sourceList.partition { it % 2 == 0 }, pair) - } - } - - @Test - fun testZip() { - val expect = sourceList.zip(sourceList) { a, b -> a + 2 * b } - checkTransform(expect) { - with(CoroutineScope(coroutineContext)) { - zip(testSource()) { a, b -> a + 2*b } - } - } - checkTransform(expect) { - with(CoroutineScope(coroutineContext)) { - testSource().zip(this@checkTransform) { a, b -> a + 2*b } - } - } - } - - // ------------------ - - private fun checkTerminal( - expected: ((Throwable?) -> Unit)? = null, - terminal: suspend ReceiveChannel.() -> Unit - ) { - checkTerminalCompletion(expected, terminal) - checkTerminalCancellation(expected, terminal) - } - - private fun checkTerminalCompletion( - expected: ((Throwable?) -> Unit)? = null, - terminal: suspend ReceiveChannel.() -> Unit - ) { - val src = runBlocking { - val src = testSource() - try { - // terminal operation - terminal(src) - // source must be cancelled at the end of terminal op - if (expected != null) error("Exception was expected") - } catch (e: Throwable) { - if (expected == null) throw e - expected(e) - } - src - } - assertTrue(src.isClosedForReceive, "Source must be closed") - } - - private fun checkTerminalCancellation( - expected: ((Throwable?) -> Unit)? = null, - terminal: suspend ReceiveChannel.() -> Unit - ) { - val src = runBlocking { - val src = testSource() - // terminal operation in a separate async context started until the first suspension - val d = async(NonCancellable, start = CoroutineStart.UNDISPATCHED) { - terminal(src) - } - // then cancel it - d.cancel() - // and try to get it's result - try { - d.await() - } catch (e: CancellationException) { - // ok -- was cancelled - } catch (e: Throwable) { - // if threw a different exception -- must be an expected one - if (expected == null) throw e - expected(e) - } - src - } - // source must be cancelled at the end of terminal op even if it was cancelled while in process - assertTrue(src.isClosedForReceive, "Source must be closed") - } - - private fun checkTransform( - expect: List, - transform: suspend ReceiveChannel.() -> ReceiveChannel - ) { - // check for varying number of received elements from the channel - for (nReceive in 0..expect.size) { - checkTransform(nReceive, expect, transform) - } - } - - private fun checkTransform( - nReceive: Int, - expect: List, - transform: suspend ReceiveChannel.() -> ReceiveChannel - ) { - val src = runBlocking { - val src = testSource() - // transform - val res = transform(src) - // receive nReceive elements from the result - repeat(nReceive) { i -> - assertEquals(expect[i], res.receive()) - } - if (nReceive < expect.size) { - // then cancel - res.cancel() - } else { - // then check that result is closed - assertNull(res.receiveOrNull(), "Result has unexpected values") - } - src - } - // source must be cancelled when runBlocking processes all the scheduled stuff - assertTrue(src.isClosedForReceive, "Source must be closed") - } -} \ No newline at end of file diff --git a/kotlinx-coroutines-core/jvm/test/channels/ChannelsJvmTest.kt b/kotlinx-coroutines-core/jvm/test/channels/ChannelsJvmTest.kt index 7613f04d29..da20f0c5ee 100644 --- a/kotlinx-coroutines-core/jvm/test/channels/ChannelsJvmTest.kt +++ b/kotlinx-coroutines-core/jvm/test/channels/ChannelsJvmTest.kt @@ -14,7 +14,9 @@ class ChannelsJvmTest : TestBase() { fun testBlocking() { val ch = Channel() val sum = GlobalScope.async { - ch.sumBy { it } + var sum = 0 + ch.consumeEach { sum += it } + sum } repeat(10) { ch.sendBlocking(it) diff --git a/kotlinx-coroutines-core/jvm/test/channels/InvokeOnCloseStressTest.kt b/kotlinx-coroutines-core/jvm/test/channels/InvokeOnCloseStressTest.kt index 864a0b4c2e..888522c63c 100644 --- a/kotlinx-coroutines-core/jvm/test/channels/InvokeOnCloseStressTest.kt +++ b/kotlinx-coroutines-core/jvm/test/channels/InvokeOnCloseStressTest.kt @@ -39,7 +39,7 @@ class InvokeOnCloseStressTest : TestBase(), CoroutineScope { private suspend fun runStressTest(kind: TestChannelKind) { repeat(iterations) { val counter = AtomicInteger(0) - val channel = kind.create() + val channel = kind.create() val latch = CountDownLatch(1) val j1 = async { diff --git a/kotlinx-coroutines-core/jvm/test/channels/SimpleSendReceiveJvmTest.kt b/kotlinx-coroutines-core/jvm/test/channels/SimpleSendReceiveJvmTest.kt index 07c431bb4d..eeddfb5f49 100644 --- a/kotlinx-coroutines-core/jvm/test/channels/SimpleSendReceiveJvmTest.kt +++ b/kotlinx-coroutines-core/jvm/test/channels/SimpleSendReceiveJvmTest.kt @@ -28,7 +28,7 @@ class SimpleSendReceiveJvmTest( } } - val channel = kind.create() + val channel = kind.create() @Test fun testSimpleSendReceive() = runBlocking { diff --git a/kotlinx-coroutines-core/jvm/test/channels/TickerChannelTest.kt b/kotlinx-coroutines-core/jvm/test/channels/TickerChannelTest.kt index c421bd334a..fcdc6bb4ad 100644 --- a/kotlinx-coroutines-core/jvm/test/channels/TickerChannelTest.kt +++ b/kotlinx-coroutines-core/jvm/test/channels/TickerChannelTest.kt @@ -47,17 +47,17 @@ class TickerChannelTest : TestBase() { @Test fun testDelayChannelBackpressure2() = withVirtualTimeSource { runTest { - val delayChannel = ticker(delayMillis = 1000, initialDelayMillis = 0) + val delayChannel = ticker(delayMillis = 200, initialDelayMillis = 0) delayChannel.checkNotEmpty() delayChannel.checkEmpty() - delay(2500) + delay(500) delayChannel.checkNotEmpty() - delay(510) + delay(110) delayChannel.checkNotEmpty() - delay(510) + delay(110) delayChannel.checkEmpty() - delay(510) + delay(110) delayChannel.checkNotEmpty() delayChannel.cancel() } diff --git a/kotlinx-coroutines-core/jvm/test/examples/example-delay-01.kt b/kotlinx-coroutines-core/jvm/test/examples/example-delay-01.kt new file mode 100644 index 0000000000..d2a5d536b2 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/examples/example-delay-01.kt @@ -0,0 +1,24 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +// This file was automatically generated from Delay.kt by Knit tool. Do not edit. +package kotlinx.coroutines.examples.exampleDelay01 + +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* + +fun main() = runBlocking { + +flow { + emit(1) + delay(90) + emit(2) + delay(90) + emit(3) + delay(1010) + emit(4) + delay(1010) + emit(5) +}.debounce(1000) +.toList().joinToString().let { println(it) } } diff --git a/kotlinx-coroutines-core/jvm/test/examples/example-delay-02.kt b/kotlinx-coroutines-core/jvm/test/examples/example-delay-02.kt new file mode 100644 index 0000000000..f74422e6b4 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/examples/example-delay-02.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +// This file was automatically generated from Delay.kt by Knit tool. Do not edit. +package kotlinx.coroutines.examples.exampleDelay02 + +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* + +fun main() = runBlocking { + +flow { + emit(1) + delay(90) + emit(2) + delay(90) + emit(3) + delay(1010) + emit(4) + delay(1010) + emit(5) +}.debounce { + if (it == 1) { + 0L + } else { + 1000L + } +} +.toList().joinToString().let { println(it) } } diff --git a/kotlinx-coroutines-core/jvm/test/examples/example-delay-03.kt b/kotlinx-coroutines-core/jvm/test/examples/example-delay-03.kt new file mode 100644 index 0000000000..edaea74258 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/examples/example-delay-03.kt @@ -0,0 +1,19 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +// This file was automatically generated from Delay.kt by Knit tool. Do not edit. +package kotlinx.coroutines.examples.exampleDelay03 + +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* + +fun main() = runBlocking { + +flow { + repeat(10) { + emit(it) + delay(110) + } +}.sample(200) +.toList().joinToString().let { println(it) } } diff --git a/kotlinx-coroutines-core/jvm/test/examples/example-delay-duration-01.kt b/kotlinx-coroutines-core/jvm/test/examples/example-delay-duration-01.kt new file mode 100644 index 0000000000..a19e6cb181 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/examples/example-delay-duration-01.kt @@ -0,0 +1,26 @@ +@file:OptIn(ExperimentalTime::class) +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +// This file was automatically generated from Delay.kt by Knit tool. Do not edit. +package kotlinx.coroutines.examples.exampleDelayDuration01 + +import kotlin.time.* +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* + +fun main() = runBlocking { + +flow { + emit(1) + delay(90.milliseconds) + emit(2) + delay(90.milliseconds) + emit(3) + delay(1010.milliseconds) + emit(4) + delay(1010.milliseconds) + emit(5) +}.debounce(1000.milliseconds) +.toList().joinToString().let { println(it) } } diff --git a/kotlinx-coroutines-core/jvm/test/examples/example-delay-duration-02.kt b/kotlinx-coroutines-core/jvm/test/examples/example-delay-duration-02.kt new file mode 100644 index 0000000000..10ba88a54d --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/examples/example-delay-duration-02.kt @@ -0,0 +1,32 @@ +@file:OptIn(ExperimentalTime::class) +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +// This file was automatically generated from Delay.kt by Knit tool. Do not edit. +package kotlinx.coroutines.examples.exampleDelayDuration02 + +import kotlin.time.* +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* + +fun main() = runBlocking { + +flow { + emit(1) + delay(90.milliseconds) + emit(2) + delay(90.milliseconds) + emit(3) + delay(1010.milliseconds) + emit(4) + delay(1010.milliseconds) + emit(5) +}.debounce { + if (it == 1) { + 0.milliseconds + } else { + 1000.milliseconds + } +} +.toList().joinToString().let { println(it) } } diff --git a/kotlinx-coroutines-core/jvm/test/examples/example-delay-duration-03.kt b/kotlinx-coroutines-core/jvm/test/examples/example-delay-duration-03.kt new file mode 100644 index 0000000000..5fa980a6f8 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/examples/example-delay-duration-03.kt @@ -0,0 +1,21 @@ +@file:OptIn(ExperimentalTime::class) +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +// This file was automatically generated from Delay.kt by Knit tool. Do not edit. +package kotlinx.coroutines.examples.exampleDelayDuration03 + +import kotlin.time.* +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* + +fun main() = runBlocking { + +flow { + repeat(10) { + emit(it) + delay(110.milliseconds) + } +}.sample(200.milliseconds) +.toList().joinToString().let { println(it) } } diff --git a/kotlinx-coroutines-core/jvm/test/examples/test/FlowDelayTest.kt b/kotlinx-coroutines-core/jvm/test/examples/test/FlowDelayTest.kt new file mode 100644 index 0000000000..99e72eb2c9 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/examples/test/FlowDelayTest.kt @@ -0,0 +1,53 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +// This file was automatically generated from Delay.kt by Knit tool. Do not edit. +package kotlinx.coroutines.examples.test + +import kotlinx.coroutines.knit.* +import org.junit.Test + +class FlowDelayTest { + @Test + fun testExampleDelay01() { + test("ExampleDelay01") { kotlinx.coroutines.examples.exampleDelay01.main() }.verifyLines( + "3, 4, 5" + ) + } + + @Test + fun testExampleDelay02() { + test("ExampleDelay02") { kotlinx.coroutines.examples.exampleDelay02.main() }.verifyLines( + "1, 3, 4, 5" + ) + } + + @Test + fun testExampleDelayDuration01() { + test("ExampleDelayDuration01") { kotlinx.coroutines.examples.exampleDelayDuration01.main() }.verifyLines( + "3, 4, 5" + ) + } + + @Test + fun testExampleDelayDuration02() { + test("ExampleDelayDuration02") { kotlinx.coroutines.examples.exampleDelayDuration02.main() }.verifyLines( + "1, 3, 4, 5" + ) + } + + @Test + fun testExampleDelay03() { + test("ExampleDelay03") { kotlinx.coroutines.examples.exampleDelay03.main() }.verifyLines( + "1, 3, 5, 7, 9" + ) + } + + @Test + fun testExampleDelayDuration03() { + test("ExampleDelayDuration03") { kotlinx.coroutines.examples.exampleDelayDuration03.main() }.verifyLines( + "1, 3, 5, 7, 9" + ) + } +} diff --git a/kotlinx-coroutines-core/jvm/test/flow/FirstJvmTest.kt b/kotlinx-coroutines-core/jvm/test/flow/FirstJvmTest.kt new file mode 100644 index 0000000000..77ad0831f3 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/flow/FirstJvmTest.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.flow + +import kotlinx.coroutines.* +import org.junit.Test +import kotlin.test.* + +class FirstJvmTest : TestBase() { + + @Test + fun testTakeInterference() = runBlocking(Dispatchers.Default) { + /* + * This test tests a racy situation when outer channelFlow is being cancelled, + * inner flow starts atomically in "CANCELLING" state, sends one element and completes + * (=> cancels and drops element away), triggering NSEE in Flow.first operator + */ + val values = (0..10000).asFlow().flatMapMerge(Int.MAX_VALUE) { + channelFlow { + val value = channelFlow { send(1) }.first() + send(value) + } + }.take(1).toList() + assertEquals(listOf(1), values) + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/jvm/test/flow/FlowCancellationTest.kt b/kotlinx-coroutines-core/jvm/test/flow/FlowCancellationTest.kt new file mode 100644 index 0000000000..269805f9b6 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/flow/FlowCancellationTest.kt @@ -0,0 +1,62 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.flow + +import kotlinx.coroutines.* +import kotlinx.coroutines.channels.* +import kotlin.test.* + +class FlowCancellationTest : TestBase() { + + @Test + fun testEmitIsCooperative() = runTest { + val latch = Channel(1) + val job = flow { + expect(1) + latch.send(Unit) + while (true) { + emit(42) + } + }.launchIn(this + Dispatchers.Default) + + latch.receive() + expect(2) + job.cancelAndJoin() + finish(3) + } + + @Test + fun testIsActiveOnCurrentContext() = runTest { + val latch = Channel(1) + val job = flow { + expect(1) + latch.send(Unit) + while (currentCoroutineContext().isActive) { + // Do nothing + } + }.launchIn(this + Dispatchers.Default) + + latch.receive() + expect(2) + job.cancelAndJoin() + finish(3) + } + + @Test + fun testFlowWithEmptyContext() = runTest { + expect(1) + withEmptyContext { + val flow = flow { + expect(2) + emit("OK") + } + flow.collect { + expect(3) + assertEquals("OK", it) + } + } + finish(4) + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/jvm/test/flow/OnCompletionInterceptedReleaseTest.kt b/kotlinx-coroutines-core/jvm/test/flow/OnCompletionInterceptedReleaseTest.kt new file mode 100644 index 0000000000..a6268b5156 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/flow/OnCompletionInterceptedReleaseTest.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.flow + +import kotlinx.coroutines.* +import org.junit.Test +import kotlin.coroutines.* +import kotlin.test.* + +class OnCompletionInterceptedReleaseTest : TestBase() { + @Test + fun testLeak() = runTest { + expect(1) + var cont: Continuation? = null + val interceptor = CountingInterceptor() + val job = launch(interceptor, start = CoroutineStart.UNDISPATCHED) { + emptyFlow() + .onCompletion { emit(1) } + .collect { value -> + expect(2) + assertEquals(1, value) + suspendCoroutine { cont = it } + } + } + cont!!.resume(Unit) + assertTrue(job.isCompleted) + assertEquals(interceptor.intercepted, interceptor.released) + finish(3) + } + + class CountingInterceptor : AbstractCoroutineContextElement(ContinuationInterceptor), ContinuationInterceptor { + var intercepted = 0 + var released = 0 + override fun interceptContinuation(continuation: Continuation): Continuation { + intercepted++ + return Continuation(continuation.context) { continuation.resumeWith(it) } + } + + override fun releaseInterceptedContinuation(continuation: Continuation<*>) { + released++ + } + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/jvm/test/flow/SharingStressTest.kt b/kotlinx-coroutines-core/jvm/test/flow/SharingStressTest.kt new file mode 100644 index 0000000000..7d346bdc33 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/flow/SharingStressTest.kt @@ -0,0 +1,193 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.flow + +import kotlinx.coroutines.* +import org.junit.* +import org.junit.Test +import java.util.* +import java.util.concurrent.atomic.* +import kotlin.random.* +import kotlin.test.* +import kotlin.time.* +import kotlin.time.TimeSource + +@OptIn(ExperimentalTime::class) +class SharingStressTest : TestBase() { + private val testDuration = 1000L * stressTestMultiplier + private val nSubscribers = 5 + private val testStarted = TimeSource.Monotonic.markNow() + + @get:Rule + val emitterDispatcher = ExecutorRule(1) + + @get:Rule + val subscriberDispatcher = ExecutorRule(nSubscribers) + + @Test + public fun testNoReplayLazy() = + testStress(0, started = SharingStarted.Lazily) + + @Test + public fun testNoReplayWhileSubscribed() = + testStress(0, started = SharingStarted.WhileSubscribed()) + + @Test + public fun testNoReplayWhileSubscribedTimeout() = + testStress(0, started = SharingStarted.WhileSubscribed(stopTimeoutMillis = 50L)) + + @Test + public fun testReplay100WhileSubscribed() = + testStress(100, started = SharingStarted.WhileSubscribed()) + + @Test + public fun testReplay100WhileSubscribedReset() = + testStress(100, started = SharingStarted.WhileSubscribed(replayExpirationMillis = 0L)) + + @Test + public fun testReplay100WhileSubscribedTimeout() = + testStress(100, started = SharingStarted.WhileSubscribed(stopTimeoutMillis = 50L)) + + @Test + public fun testStateLazy() = + testStress(1, started = SharingStarted.Lazily) + + @Test + public fun testStateWhileSubscribed() = + testStress(1, started = SharingStarted.WhileSubscribed()) + + @Test + public fun testStateWhileSubscribedReset() = + testStress(1, started = SharingStarted.WhileSubscribed(replayExpirationMillis = 0L)) + + private fun testStress(replay: Int, started: SharingStarted) = runTest { + log("-- Stress with replay=$replay, started=$started") + val random = Random(1) + val emitIndex = AtomicLong() + val cancelledEmits = HashSet() + val missingCollects = Collections.synchronizedSet(LinkedHashSet()) + // at most one copy of upstream can be running at any time + val isRunning = AtomicInteger(0) + val upstream = flow { + assertEquals(0, isRunning.getAndIncrement()) + try { + while (true) { + val value = emitIndex.getAndIncrement() + try { + emit(value) + } catch (e: CancellationException) { + // emission was cancelled -> could be missing + cancelledEmits.add(value) + throw e + } + } + } finally { + assertEquals(1, isRunning.getAndDecrement()) + } + } + val subCount = MutableStateFlow(0) + val sharingJob = Job() + val sharingScope = this + emitterDispatcher + sharingJob + val usingStateFlow = replay == 1 + val sharedFlow = if (usingStateFlow) + upstream.stateIn(sharingScope, started, 0L) + else + upstream.shareIn(sharingScope, started, replay) + try { + val subscribers = ArrayList() + withTimeoutOrNull(testDuration) { + // start and stop subscribers + while (true) { + log("Staring $nSubscribers subscribers") + repeat(nSubscribers) { + subscribers += launchSubscriber(sharedFlow, usingStateFlow, subCount, missingCollects) + } + // wait until they all subscribed + subCount.first { it == nSubscribers } + // let them work a bit more & make sure emitter did not hang + val fromEmitIndex = emitIndex.get() + val waitEmitIndex = fromEmitIndex + 100 // wait until 100 emitted + withTimeout(10000) { // wait for at most 10s for something to be emitted + do { + delay(random.nextLong(50L..100L)) + } while (emitIndex.get() < waitEmitIndex) // Ok, enough was emitted, wait more if not + } + // Stop all subscribers and ensure they collected something + log("Stopping subscribers (emitted = ${emitIndex.get() - fromEmitIndex})") + subscribers.forEach { + it.job.cancelAndJoin() + assertTrue { it.count > 0 } // something must be collected too + } + subscribers.clear() + log("Intermission") + delay(random.nextLong(10L..100L)) // wait a bit before starting them again + } + } + if (!subscribers.isEmpty()) { + log("Stopping subscribers") + subscribers.forEach { it.job.cancelAndJoin() } + } + } finally { + log("--- Finally: Cancelling sharing job") + sharingJob.cancel() + } + sharingJob.join() // make sure sharing job did not hang + log("Emitter was cancelled ${cancelledEmits.size} times") + log("Collectors missed ${missingCollects.size} values") + for (value in missingCollects) { + assertTrue(value in cancelledEmits, "Value $value is missing for no apparent reason") + } + } + + private fun CoroutineScope.launchSubscriber( + sharedFlow: SharedFlow, + usingStateFlow: Boolean, + subCount: MutableStateFlow, + missingCollects: MutableSet + ): SubJob { + val subJob = SubJob() + subJob.job = launch(subscriberDispatcher) { + var last = -1L + sharedFlow + .onSubscription { + subCount.increment(1) + } + .onCompletion { + subCount.increment(-1) + } + .collect { j -> + subJob.count++ + // last must grow sequentially, no jumping or losses + if (last == -1L) { + last = j + } else { + val expected = last + 1 + if (usingStateFlow) + assertTrue(expected <= j) + else { + if (expected != j) { + if (j == expected + 1) { + // if missing just one -- could be race with cancelled emit + missingCollects.add(expected) + } else { + // broken otherwise + assertEquals(expected, j) + } + } + } + last = j + } + } + } + return subJob + } + + private class SubJob { + lateinit var job: Job + var count = 0L + } + + private fun log(msg: String) = println("${testStarted.elapsedNow().toLongMilliseconds()} ms: $msg") +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/jvm/test/flow/StateFlowCancellabilityTest.kt b/kotlinx-coroutines-core/jvm/test/flow/StateFlowCancellabilityTest.kt new file mode 100644 index 0000000000..fc4996c7c0 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/flow/StateFlowCancellabilityTest.kt @@ -0,0 +1,56 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.flow + +import kotlinx.coroutines.* +import java.util.concurrent.* +import kotlin.test.* + +@Suppress("BlockingMethodInNonBlockingContext") +class StateFlowCancellabilityTest : TestBase() { + @Test + fun testCancellabilityNoConflation() = runTest { + expect(1) + val state = MutableStateFlow(0) + var subscribed = true + var lastReceived = -1 + val barrier = CyclicBarrier(2) + val job = state + .onSubscription { + subscribed = true + barrier.await() + } + .onEach { i -> + when (i) { + 0 -> expect(2) // initial value + 1 -> expect(3) + 2 -> { + expect(4) + currentCoroutineContext().cancel() + } + else -> expectUnreached() // shall check for cancellation + } + lastReceived = i + barrier.await() + barrier.await() + } + .launchIn(this + Dispatchers.Default) + barrier.await() + assertTrue(subscribed) // should have subscribed in the first barrier + barrier.await() + assertEquals(0, lastReceived) // should get initial value, too + for (i in 1..3) { // emit after subscription + state.value = i + barrier.await() // let it go + if (i < 3) { + barrier.await() // wait for receive + assertEquals(i, lastReceived) // shall receive it + } + } + job.join() + finish(5) + } +} + diff --git a/kotlinx-coroutines-core/jvm/test/flow/StateFlowStressTest.kt b/kotlinx-coroutines-core/jvm/test/flow/StateFlowStressTest.kt new file mode 100644 index 0000000000..dc3cd43c93 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/flow/StateFlowStressTest.kt @@ -0,0 +1,80 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.flow + +import kotlinx.coroutines.* +import org.junit.* +import kotlin.random.* + +class StateFlowStressTest : TestBase() { + private val nSeconds = 3 * stressTestMultiplier + private val state = MutableStateFlow(0) + private lateinit var pool: ExecutorCoroutineDispatcher + + @After + fun tearDown() { + pool.close() + } + + fun stress(nEmitters: Int, nCollectors: Int) = runTest { + pool = newFixedThreadPoolContext(nEmitters + nCollectors, "StateFlowStressTest") + val collected = Array(nCollectors) { LongArray(nEmitters) } + val collectors = launch { + repeat(nCollectors) { collector -> + launch(pool) { + val c = collected[collector] + // collect, but abort and collect again after every 1000 values to stress allocation/deallocation + do { + val batchSize = Random.nextInt(1..1000) + var index = 0 + val cnt = state.onEach { value -> + val emitter = (value % nEmitters).toInt() + val current = value / nEmitters + // the first value in batch is allowed to repeat, but cannot go back + val ok = if (index++ == 0) current >= c[emitter] else current > c[emitter] + check(ok) { + "Values must be monotonic, but $current is not, " + + "was ${c[emitter]} in collector #$collector from emitter #$emitter" + } + c[emitter] = current + + }.take(batchSize).map { 1 }.sum() + } while (cnt == batchSize) + } + } + } + val emitted = LongArray(nEmitters) + val emitters = launch { + repeat(nEmitters) { emitter -> + launch(pool) { + var current = 1L + while (true) { + state.value = current * nEmitters + emitter + emitted[emitter] = current + current++ + if (current % 1000 == 0L) yield() // make it cancellable + } + } + } + } + for (second in 1..nSeconds) { + delay(1000) + val cs = collected.map { it.sum() } + println("$second: emitted=${emitted.sum()}, collected=${cs.min()}..${cs.max()}") + } + emitters.cancelAndJoin() + collectors.cancelAndJoin() + // make sure nothing hanged up + require(collected.all { c -> + c.withIndex().all { (emitter, current) -> current > emitted[emitter] / 2 } + }) + } + + @Test + fun testSingleEmitterAndCollector() = stress(1, 1) + + @Test + fun testTenEmittersAndCollectors() = stress(10, 10) +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-basic-08.kt b/kotlinx-coroutines-core/jvm/test/guide/example-basic-08.kt index ff11eb70d7..bb7786f28a 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-basic-08.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-basic-08.kt @@ -10,7 +10,7 @@ import kotlinx.coroutines.* fun main() = runBlocking { repeat(100_000) { // launch a lot of coroutines launch { - delay(1000L) + delay(5000L) print(".") } } diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-cancel-08.kt b/kotlinx-coroutines-core/jvm/test/guide/example-cancel-08.kt new file mode 100644 index 0000000000..e7def132ae --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/guide/example-cancel-08.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +// This file was automatically generated from cancellation-and-timeouts.md by Knit tool. Do not edit. +package kotlinx.coroutines.guide.exampleCancel08 + +import kotlinx.coroutines.* + +var acquired = 0 + +class Resource { + init { acquired++ } // Acquire the resource + fun close() { acquired-- } // Release the resource +} + +fun main() { + runBlocking { + repeat(100_000) { // Launch 100K coroutines + launch { + val resource = withTimeout(60) { // Timeout of 60 ms + delay(50) // Delay for 50 ms + Resource() // Acquire a resource and return it from withTimeout block + } + resource.close() // Release the resource + } + } + } + // Outside of runBlocking all coroutines have completed + println(acquired) // Print the number of resources still acquired +} diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-cancel-09.kt b/kotlinx-coroutines-core/jvm/test/guide/example-cancel-09.kt new file mode 100644 index 0000000000..95424f5108 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/guide/example-cancel-09.kt @@ -0,0 +1,36 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +// This file was automatically generated from cancellation-and-timeouts.md by Knit tool. Do not edit. +package kotlinx.coroutines.guide.exampleCancel09 + +import kotlinx.coroutines.* + +var acquired = 0 + +class Resource { + init { acquired++ } // Acquire the resource + fun close() { acquired-- } // Release the resource +} + +fun main() { + runBlocking { + repeat(100_000) { // Launch 100K coroutines + launch { + var resource: Resource? = null // Not acquired yet + try { + withTimeout(60) { // Timeout of 60 ms + delay(50) // Delay for 50 ms + resource = Resource() // Store a resource to the variable if acquired + } + // We can do something else with the resource here + } finally { + resource?.close() // Release the resource if it was acquired + } + } + } + } + // Outside of runBlocking all coroutines have completed + println(acquired) // Print the number of resources still acquired +} diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-channel-10.kt b/kotlinx-coroutines-core/jvm/test/guide/example-channel-10.kt index ce0424471a..1f48b02e4c 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-channel-10.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-channel-10.kt @@ -11,9 +11,9 @@ import kotlinx.coroutines.channels.* fun main() = runBlocking { val tickerChannel = ticker(delayMillis = 100, initialDelayMillis = 0) // create ticker channel var nextElement = withTimeoutOrNull(1) { tickerChannel.receive() } - println("Initial element is available immediately: $nextElement") // initial delay hasn't passed yet + println("Initial element is available immediately: $nextElement") // no initial delay - nextElement = withTimeoutOrNull(50) { tickerChannel.receive() } // all subsequent elements has 100ms delay + nextElement = withTimeoutOrNull(50) { tickerChannel.receive() } // all subsequent elements have 100ms delay println("Next element is not ready in 50 ms: $nextElement") nextElement = withTimeoutOrNull(60) { tickerChannel.receive() } diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-context-10.kt b/kotlinx-coroutines-core/jvm/test/guide/example-context-10.kt index 0256004405..73ecbd2b3e 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-context-10.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-context-10.kt @@ -5,21 +5,19 @@ // This file was automatically generated from coroutine-context-and-dispatchers.md by Knit tool. Do not edit. package kotlinx.coroutines.guide.exampleContext10 -import kotlin.coroutines.* import kotlinx.coroutines.* -class Activity : CoroutineScope by CoroutineScope(Dispatchers.Default) { - +class Activity { + private val mainScope = CoroutineScope(Dispatchers.Default) // use Default for test purposes + fun destroy() { - cancel() // Extension on CoroutineScope + mainScope.cancel() } - // to be continued ... - // class Activity continues fun doSomething() { // launch ten coroutines for a demo, each working for a different time repeat(10) { i -> - launch { + mainScope.launch { delay((i + 1) * 200L) // variable delay 200ms, 400ms, ... etc println("Coroutine $i is done") } diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-exceptions-01.kt b/kotlinx-coroutines-core/jvm/test/guide/example-exceptions-01.kt index 34d7b68c82..e08ddd0811 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-exceptions-01.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-exceptions-01.kt @@ -8,13 +8,13 @@ package kotlinx.coroutines.guide.exampleExceptions01 import kotlinx.coroutines.* fun main() = runBlocking { - val job = GlobalScope.launch { + val job = GlobalScope.launch { // root coroutine with launch println("Throwing exception from launch") throw IndexOutOfBoundsException() // Will be printed to the console by Thread.defaultUncaughtExceptionHandler } job.join() println("Joined failed job") - val deferred = GlobalScope.async { + val deferred = GlobalScope.async { // root coroutine with async println("Throwing exception from async") throw ArithmeticException() // Nothing is printed, relying on user to call await } diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-exceptions-02.kt b/kotlinx-coroutines-core/jvm/test/guide/example-exceptions-02.kt index 359eff60e4..67fdaa7177 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-exceptions-02.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-exceptions-02.kt @@ -9,12 +9,12 @@ import kotlinx.coroutines.* fun main() = runBlocking { val handler = CoroutineExceptionHandler { _, exception -> - println("Caught $exception") + println("CoroutineExceptionHandler got $exception") } - val job = GlobalScope.launch(handler) { + val job = GlobalScope.launch(handler) { // root coroutine, running in GlobalScope throw AssertionError() } - val deferred = GlobalScope.async(handler) { + val deferred = GlobalScope.async(handler) { // also root, but async instead of launch throw ArithmeticException() // Nothing will be printed, relying on user to call deferred.await() } joinAll(job, deferred) diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-exceptions-04.kt b/kotlinx-coroutines-core/jvm/test/guide/example-exceptions-04.kt index e1fc22d725..9c9b43d22e 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-exceptions-04.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-exceptions-04.kt @@ -9,7 +9,7 @@ import kotlinx.coroutines.* fun main() = runBlocking { val handler = CoroutineExceptionHandler { _, exception -> - println("Caught $exception") + println("CoroutineExceptionHandler got $exception") } val job = GlobalScope.launch(handler) { launch { // the first child diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-exceptions-05.kt b/kotlinx-coroutines-core/jvm/test/guide/example-exceptions-05.kt index e97572aba8..04f9385f06 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-exceptions-05.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-exceptions-05.kt @@ -12,19 +12,19 @@ import java.io.* fun main() = runBlocking { val handler = CoroutineExceptionHandler { _, exception -> - println("Caught $exception with suppressed ${exception.suppressed.contentToString()}") + println("CoroutineExceptionHandler got $exception with suppressed ${exception.suppressed.contentToString()}") } val job = GlobalScope.launch(handler) { launch { try { - delay(Long.MAX_VALUE) + delay(Long.MAX_VALUE) // it gets cancelled when another sibling fails with IOException } finally { - throw ArithmeticException() + throw ArithmeticException() // the second exception } } launch { delay(100) - throw IOException() + throw IOException() // the first exception } delay(Long.MAX_VALUE) } diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-exceptions-06.kt b/kotlinx-coroutines-core/jvm/test/guide/example-exceptions-06.kt index eec27840e5..5a5b276bc3 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-exceptions-06.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-exceptions-06.kt @@ -10,13 +10,13 @@ import java.io.* fun main() = runBlocking { val handler = CoroutineExceptionHandler { _, exception -> - println("Caught original $exception") + println("CoroutineExceptionHandler got $exception") } val job = GlobalScope.launch(handler) { - val inner = launch { + val inner = launch { // all this stack of coroutines will get cancelled launch { launch { - throw IOException() + throw IOException() // the original exception } } } @@ -24,7 +24,7 @@ fun main() = runBlocking { inner.join() } catch (e: CancellationException) { println("Rethrowing CancellationException with original cause") - throw e + throw e // cancellation exception is rethrown, yet the original IOException gets to the handler } } job.join() diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-flow-01.kt b/kotlinx-coroutines-core/jvm/test/guide/example-flow-01.kt index df14603db0..7c6c1578f8 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-flow-01.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-flow-01.kt @@ -5,8 +5,8 @@ // This file was automatically generated from flow.md by Knit tool. Do not edit. package kotlinx.coroutines.guide.exampleFlow01 -fun foo(): List = listOf(1, 2, 3) +fun simple(): List = listOf(1, 2, 3) fun main() { - foo().forEach { value -> println(value) } + simple().forEach { value -> println(value) } } diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-flow-02.kt b/kotlinx-coroutines-core/jvm/test/guide/example-flow-02.kt index fcb61b9d1d..e3fabe37f8 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-flow-02.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-flow-02.kt @@ -5,7 +5,7 @@ // This file was automatically generated from flow.md by Knit tool. Do not edit. package kotlinx.coroutines.guide.exampleFlow02 -fun foo(): Sequence = sequence { // sequence builder +fun simple(): Sequence = sequence { // sequence builder for (i in 1..3) { Thread.sleep(100) // pretend we are computing it yield(i) // yield next value @@ -13,5 +13,5 @@ fun foo(): Sequence = sequence { // sequence builder } fun main() { - foo().forEach { value -> println(value) } + simple().forEach { value -> println(value) } } diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-flow-03.kt b/kotlinx-coroutines-core/jvm/test/guide/example-flow-03.kt index ba94b2f8f6..61601dd94d 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-flow-03.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-flow-03.kt @@ -7,11 +7,11 @@ package kotlinx.coroutines.guide.exampleFlow03 import kotlinx.coroutines.* -suspend fun foo(): List { +suspend fun simple(): List { delay(1000) // pretend we are doing something asynchronous here return listOf(1, 2, 3) } fun main() = runBlocking { - foo().forEach { value -> println(value) } + simple().forEach { value -> println(value) } } diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-flow-04.kt b/kotlinx-coroutines-core/jvm/test/guide/example-flow-04.kt index 3e3aea0f55..c91f70403e 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-flow-04.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-flow-04.kt @@ -8,7 +8,7 @@ package kotlinx.coroutines.guide.exampleFlow04 import kotlinx.coroutines.* import kotlinx.coroutines.flow.* -fun foo(): Flow = flow { // flow builder +fun simple(): Flow = flow { // flow builder for (i in 1..3) { delay(100) // pretend we are doing something useful here emit(i) // emit next value @@ -24,5 +24,5 @@ fun main() = runBlocking { } } // Collect the flow - foo().collect { value -> println(value) } + simple().collect { value -> println(value) } } diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-flow-05.kt b/kotlinx-coroutines-core/jvm/test/guide/example-flow-05.kt index 6d0e451923..788d941d53 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-flow-05.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-flow-05.kt @@ -8,7 +8,7 @@ package kotlinx.coroutines.guide.exampleFlow05 import kotlinx.coroutines.* import kotlinx.coroutines.flow.* -fun foo(): Flow = flow { +fun simple(): Flow = flow { println("Flow started") for (i in 1..3) { delay(100) @@ -17,8 +17,8 @@ fun foo(): Flow = flow { } fun main() = runBlocking { - println("Calling foo...") - val flow = foo() + println("Calling simple function...") + val flow = simple() println("Calling collect...") flow.collect { value -> println(value) } println("Calling collect again...") diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-flow-06.kt b/kotlinx-coroutines-core/jvm/test/guide/example-flow-06.kt index 9d9348ea5c..bd4058e757 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-flow-06.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-flow-06.kt @@ -8,7 +8,7 @@ package kotlinx.coroutines.guide.exampleFlow06 import kotlinx.coroutines.* import kotlinx.coroutines.flow.* -fun foo(): Flow = flow { +fun simple(): Flow = flow { for (i in 1..3) { delay(100) println("Emitting $i") @@ -18,7 +18,7 @@ fun foo(): Flow = flow { fun main() = runBlocking { withTimeoutOrNull(250) { // Timeout after 250ms - foo().collect { value -> println(value) } + simple().collect { value -> println(value) } } println("Done") } diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-flow-13.kt b/kotlinx-coroutines-core/jvm/test/guide/example-flow-13.kt index 4feacc6d25..945ce8954d 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-flow-13.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-flow-13.kt @@ -10,13 +10,13 @@ import kotlinx.coroutines.flow.* fun log(msg: String) = println("[${Thread.currentThread().name}] $msg") -fun foo(): Flow = flow { - log("Started foo flow") +fun simple(): Flow = flow { + log("Started simple flow") for (i in 1..3) { emit(i) } } fun main() = runBlocking { - foo().collect { value -> log("Collected $value") } + simple().collect { value -> log("Collected $value") } } diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-flow-14.kt b/kotlinx-coroutines-core/jvm/test/guide/example-flow-14.kt index c0f2320490..b5fc35e24e 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-flow-14.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-flow-14.kt @@ -8,7 +8,7 @@ package kotlinx.coroutines.guide.exampleFlow14 import kotlinx.coroutines.* import kotlinx.coroutines.flow.* -fun foo(): Flow = flow { +fun simple(): Flow = flow { // The WRONG way to change context for CPU-consuming code in flow builder kotlinx.coroutines.withContext(Dispatchers.Default) { for (i in 1..3) { @@ -19,5 +19,5 @@ fun foo(): Flow = flow { } fun main() = runBlocking { - foo().collect { value -> println(value) } + simple().collect { value -> println(value) } } diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-flow-15.kt b/kotlinx-coroutines-core/jvm/test/guide/example-flow-15.kt index 8f0e395ce4..0218e99532 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-flow-15.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-flow-15.kt @@ -10,7 +10,7 @@ import kotlinx.coroutines.flow.* fun log(msg: String) = println("[${Thread.currentThread().name}] $msg") -fun foo(): Flow = flow { +fun simple(): Flow = flow { for (i in 1..3) { Thread.sleep(100) // pretend we are computing it in CPU-consuming way log("Emitting $i") @@ -19,7 +19,7 @@ fun foo(): Flow = flow { }.flowOn(Dispatchers.Default) // RIGHT way to change context for CPU-consuming code in flow builder fun main() = runBlocking { - foo().collect { value -> + simple().collect { value -> log("Collected $value") } } diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-flow-16.kt b/kotlinx-coroutines-core/jvm/test/guide/example-flow-16.kt index d2f41ff6cc..7f3414fff4 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-flow-16.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-flow-16.kt @@ -9,7 +9,7 @@ import kotlinx.coroutines.* import kotlinx.coroutines.flow.* import kotlin.system.* -fun foo(): Flow = flow { +fun simple(): Flow = flow { for (i in 1..3) { delay(100) // pretend we are asynchronously waiting 100 ms emit(i) // emit next value @@ -18,7 +18,7 @@ fun foo(): Flow = flow { fun main() = runBlocking { val time = measureTimeMillis { - foo().collect { value -> + simple().collect { value -> delay(300) // pretend we are processing it for 300 ms println(value) } diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-flow-17.kt b/kotlinx-coroutines-core/jvm/test/guide/example-flow-17.kt index 5db79df185..ed7161783f 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-flow-17.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-flow-17.kt @@ -9,7 +9,7 @@ import kotlinx.coroutines.* import kotlinx.coroutines.flow.* import kotlin.system.* -fun foo(): Flow = flow { +fun simple(): Flow = flow { for (i in 1..3) { delay(100) // pretend we are asynchronously waiting 100 ms emit(i) // emit next value @@ -18,7 +18,7 @@ fun foo(): Flow = flow { fun main() = runBlocking { val time = measureTimeMillis { - foo() + simple() .buffer() // buffer emissions, don't wait .collect { value -> delay(300) // pretend we are processing it for 300 ms diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-flow-18.kt b/kotlinx-coroutines-core/jvm/test/guide/example-flow-18.kt index 3c1a8a1b7c..fc7bdef561 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-flow-18.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-flow-18.kt @@ -9,7 +9,7 @@ import kotlinx.coroutines.* import kotlinx.coroutines.flow.* import kotlin.system.* -fun foo(): Flow = flow { +fun simple(): Flow = flow { for (i in 1..3) { delay(100) // pretend we are asynchronously waiting 100 ms emit(i) // emit next value @@ -18,7 +18,7 @@ fun foo(): Flow = flow { fun main() = runBlocking { val time = measureTimeMillis { - foo() + simple() .conflate() // conflate emissions, don't process each one .collect { value -> delay(300) // pretend we are processing it for 300 ms diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-flow-19.kt b/kotlinx-coroutines-core/jvm/test/guide/example-flow-19.kt index 1725276bf8..f4ee2193c0 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-flow-19.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-flow-19.kt @@ -9,7 +9,7 @@ import kotlinx.coroutines.* import kotlinx.coroutines.flow.* import kotlin.system.* -fun foo(): Flow = flow { +fun simple(): Flow = flow { for (i in 1..3) { delay(100) // pretend we are asynchronously waiting 100 ms emit(i) // emit next value @@ -18,7 +18,7 @@ fun foo(): Flow = flow { fun main() = runBlocking { val time = measureTimeMillis { - foo() + simple() .collectLatest { value -> // cancel & restart on the latest value println("Collecting $value") delay(300) // pretend we are processing it for 300 ms diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-flow-26.kt b/kotlinx-coroutines-core/jvm/test/guide/example-flow-26.kt index e489c3f2ed..95f9a113b5 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-flow-26.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-flow-26.kt @@ -8,7 +8,7 @@ package kotlinx.coroutines.guide.exampleFlow26 import kotlinx.coroutines.* import kotlinx.coroutines.flow.* -fun foo(): Flow = flow { +fun simple(): Flow = flow { for (i in 1..3) { println("Emitting $i") emit(i) // emit next value @@ -17,7 +17,7 @@ fun foo(): Flow = flow { fun main() = runBlocking { try { - foo().collect { value -> + simple().collect { value -> println(value) check(value <= 1) { "Collected $value" } } diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-flow-27.kt b/kotlinx-coroutines-core/jvm/test/guide/example-flow-27.kt index f9ef9793cf..3f34a7674f 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-flow-27.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-flow-27.kt @@ -8,7 +8,7 @@ package kotlinx.coroutines.guide.exampleFlow27 import kotlinx.coroutines.* import kotlinx.coroutines.flow.* -fun foo(): Flow = +fun simple(): Flow = flow { for (i in 1..3) { println("Emitting $i") @@ -22,7 +22,7 @@ fun foo(): Flow = fun main() = runBlocking { try { - foo().collect { value -> println(value) } + simple().collect { value -> println(value) } } catch (e: Throwable) { println("Caught $e") } diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-flow-28.kt b/kotlinx-coroutines-core/jvm/test/guide/example-flow-28.kt index 84fc69fd7b..02b231e985 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-flow-28.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-flow-28.kt @@ -8,7 +8,7 @@ package kotlinx.coroutines.guide.exampleFlow28 import kotlinx.coroutines.* import kotlinx.coroutines.flow.* -fun foo(): Flow = +fun simple(): Flow = flow { for (i in 1..3) { println("Emitting $i") @@ -21,7 +21,7 @@ fun foo(): Flow = } fun main() = runBlocking { - foo() + simple() .catch { e -> emit("Caught $e") } // emit on exception .collect { value -> println(value) } } diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-flow-29.kt b/kotlinx-coroutines-core/jvm/test/guide/example-flow-29.kt index 6c60c5d9d2..5ec37afd1e 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-flow-29.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-flow-29.kt @@ -8,7 +8,7 @@ package kotlinx.coroutines.guide.exampleFlow29 import kotlinx.coroutines.* import kotlinx.coroutines.flow.* -fun foo(): Flow = flow { +fun simple(): Flow = flow { for (i in 1..3) { println("Emitting $i") emit(i) @@ -16,7 +16,7 @@ fun foo(): Flow = flow { } fun main() = runBlocking { - foo() + simple() .catch { e -> println("Caught $e") } // does not catch downstream exceptions .collect { value -> check(value <= 1) { "Collected $value" } diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-flow-30.kt b/kotlinx-coroutines-core/jvm/test/guide/example-flow-30.kt index e21c77fcf3..e787ca39ae 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-flow-30.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-flow-30.kt @@ -8,7 +8,7 @@ package kotlinx.coroutines.guide.exampleFlow30 import kotlinx.coroutines.* import kotlinx.coroutines.flow.* -fun foo(): Flow = flow { +fun simple(): Flow = flow { for (i in 1..3) { println("Emitting $i") emit(i) @@ -16,7 +16,7 @@ fun foo(): Flow = flow { } fun main() = runBlocking { - foo() + simple() .onEach { value -> check(value <= 1) { "Collected $value" } println(value) diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-flow-31.kt b/kotlinx-coroutines-core/jvm/test/guide/example-flow-31.kt index 9b2855ef09..19fcb1c626 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-flow-31.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-flow-31.kt @@ -8,11 +8,11 @@ package kotlinx.coroutines.guide.exampleFlow31 import kotlinx.coroutines.* import kotlinx.coroutines.flow.* -fun foo(): Flow = (1..3).asFlow() +fun simple(): Flow = (1..3).asFlow() fun main() = runBlocking { try { - foo().collect { value -> println(value) } + simple().collect { value -> println(value) } } finally { println("Done") } diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-flow-32.kt b/kotlinx-coroutines-core/jvm/test/guide/example-flow-32.kt index 3ad74ae5f1..984895753d 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-flow-32.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-flow-32.kt @@ -8,10 +8,10 @@ package kotlinx.coroutines.guide.exampleFlow32 import kotlinx.coroutines.* import kotlinx.coroutines.flow.* -fun foo(): Flow = (1..3).asFlow() +fun simple(): Flow = (1..3).asFlow() fun main() = runBlocking { - foo() + simple() .onCompletion { println("Done") } .collect { value -> println(value) } } diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-flow-33.kt b/kotlinx-coroutines-core/jvm/test/guide/example-flow-33.kt index c0e0ab3d5b..9f86765afd 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-flow-33.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-flow-33.kt @@ -8,13 +8,13 @@ package kotlinx.coroutines.guide.exampleFlow33 import kotlinx.coroutines.* import kotlinx.coroutines.flow.* -fun foo(): Flow = flow { +fun simple(): Flow = flow { emit(1) throw RuntimeException() } fun main() = runBlocking { - foo() + simple() .onCompletion { cause -> if (cause != null) println("Flow completed exceptionally") } .catch { cause -> println("Caught exception") } .collect { value -> println(value) } diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-flow-34.kt b/kotlinx-coroutines-core/jvm/test/guide/example-flow-34.kt index 4b79a73683..b2cee7f9fb 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-flow-34.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-flow-34.kt @@ -8,10 +8,10 @@ package kotlinx.coroutines.guide.exampleFlow34 import kotlinx.coroutines.* import kotlinx.coroutines.flow.* -fun foo(): Flow = (1..3).asFlow() +fun simple(): Flow = (1..3).asFlow() fun main() = runBlocking { - foo() + simple() .onCompletion { cause -> println("Flow completed with $cause") } .collect { value -> check(value <= 1) { "Collected $value" } diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-flow-37.kt b/kotlinx-coroutines-core/jvm/test/guide/example-flow-37.kt new file mode 100644 index 0000000000..229e55b130 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/guide/example-flow-37.kt @@ -0,0 +1,23 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +// This file was automatically generated from flow.md by Knit tool. Do not edit. +package kotlinx.coroutines.guide.exampleFlow37 + +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* + +fun foo(): Flow = flow { + for (i in 1..5) { + println("Emitting $i") + emit(i) + } +} + +fun main() = runBlocking { + foo().collect { value -> + if (value == 3) cancel() + println(value) + } +} diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-flow-38.kt b/kotlinx-coroutines-core/jvm/test/guide/example-flow-38.kt new file mode 100644 index 0000000000..fcbbb24ef7 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/guide/example-flow-38.kt @@ -0,0 +1,16 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +// This file was automatically generated from flow.md by Knit tool. Do not edit. +package kotlinx.coroutines.guide.exampleFlow38 + +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* + +fun main() = runBlocking { + (1..5).asFlow().collect { value -> + if (value == 3) cancel() + println(value) + } +} diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-flow-39.kt b/kotlinx-coroutines-core/jvm/test/guide/example-flow-39.kt new file mode 100644 index 0000000000..86275c7c4a --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/guide/example-flow-39.kt @@ -0,0 +1,16 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +// This file was automatically generated from flow.md by Knit tool. Do not edit. +package kotlinx.coroutines.guide.exampleFlow39 + +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* + +fun main() = runBlocking { + (1..5).asFlow().cancellable().collect { value -> + if (value == 3) cancel() + println(value) + } +} diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-supervision-01.kt b/kotlinx-coroutines-core/jvm/test/guide/example-supervision-01.kt index ffd56a8f85..db819e4fc6 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-supervision-01.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-supervision-01.kt @@ -12,24 +12,24 @@ fun main() = runBlocking { with(CoroutineScope(coroutineContext + supervisor)) { // launch the first child -- its exception is ignored for this example (don't do this in practice!) val firstChild = launch(CoroutineExceptionHandler { _, _ -> }) { - println("First child is failing") - throw AssertionError("First child is cancelled") + println("The first child is failing") + throw AssertionError("The first child is cancelled") } // launch the second child val secondChild = launch { firstChild.join() // Cancellation of the first child is not propagated to the second child - println("First child is cancelled: ${firstChild.isCancelled}, but second one is still active") + println("The first child is cancelled: ${firstChild.isCancelled}, but the second one is still active") try { delay(Long.MAX_VALUE) } finally { // But cancellation of the supervisor is propagated - println("Second child is cancelled because supervisor is cancelled") + println("The second child is cancelled because the supervisor was cancelled") } } // wait until the first child fails & completes firstChild.join() - println("Cancelling supervisor") + println("Cancelling the supervisor") supervisor.cancel() secondChild.join() } diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-supervision-02.kt b/kotlinx-coroutines-core/jvm/test/guide/example-supervision-02.kt index dc3a0f2844..50deff3d0b 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-supervision-02.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-supervision-02.kt @@ -13,18 +13,18 @@ fun main() = runBlocking { supervisorScope { val child = launch { try { - println("Child is sleeping") + println("The child is sleeping") delay(Long.MAX_VALUE) } finally { - println("Child is cancelled") + println("The child is cancelled") } } // Give our child a chance to execute and print using yield yield() - println("Throwing exception from scope") + println("Throwing an exception from the scope") throw AssertionError() } } catch(e: AssertionError) { - println("Caught assertion error") + println("Caught an assertion error") } } diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-supervision-03.kt b/kotlinx-coroutines-core/jvm/test/guide/example-supervision-03.kt index b32a004639..943c64f6ee 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-supervision-03.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-supervision-03.kt @@ -10,14 +10,14 @@ import kotlinx.coroutines.* fun main() = runBlocking { val handler = CoroutineExceptionHandler { _, exception -> - println("Caught $exception") + println("CoroutineExceptionHandler got $exception") } supervisorScope { val child = launch(handler) { - println("Child throws an exception") + println("The child throws an exception") throw AssertionError() } - println("Scope is completing") + println("The scope is completing") } - println("Scope is completed") + println("The scope is completed") } diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-sync-03.kt b/kotlinx-coroutines-core/jvm/test/guide/example-sync-03.kt index 6c34149c93..8153c026b4 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-sync-03.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-sync-03.kt @@ -24,7 +24,7 @@ suspend fun massiveRun(action: suspend () -> Unit) { println("Completed ${n * k} actions in $time ms") } -var counter = AtomicInteger() +val counter = AtomicInteger() fun main() = runBlocking { withContext(Dispatchers.Default) { diff --git a/kotlinx-coroutines-core/jvm/test/guide/test/BasicsGuideTest.kt b/kotlinx-coroutines-core/jvm/test/guide/test/BasicsGuideTest.kt index 7fc57c2ee3..ea5003b0c7 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/test/BasicsGuideTest.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/test/BasicsGuideTest.kt @@ -5,6 +5,7 @@ // This file was automatically generated from basics.md by Knit tool. Do not edit. package kotlinx.coroutines.guide.test +import kotlinx.coroutines.knit.* import org.junit.Test class BasicsGuideTest { diff --git a/kotlinx-coroutines-core/jvm/test/guide/test/CancellationGuideTest.kt b/kotlinx-coroutines-core/jvm/test/guide/test/CancellationGuideTest.kt index a2e91de82d..0cff63a834 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/test/CancellationGuideTest.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/test/CancellationGuideTest.kt @@ -5,6 +5,7 @@ // This file was automatically generated from cancellation-and-timeouts.md by Knit tool. Do not edit. package kotlinx.coroutines.guide.test +import kotlinx.coroutines.knit.* import org.junit.Test class CancellationGuideTest { @@ -87,4 +88,11 @@ class CancellationGuideTest { "Result is null" ) } + + @Test + fun testExampleCancel09() { + test("ExampleCancel09") { kotlinx.coroutines.guide.exampleCancel09.main() }.verifyLines( + "0" + ) + } } diff --git a/kotlinx-coroutines-core/jvm/test/guide/test/ChannelsGuideTest.kt b/kotlinx-coroutines-core/jvm/test/guide/test/ChannelsGuideTest.kt index 209d439663..d15a550adb 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/test/ChannelsGuideTest.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/test/ChannelsGuideTest.kt @@ -5,6 +5,7 @@ // This file was automatically generated from channels.md by Knit tool. Do not edit. package kotlinx.coroutines.guide.test +import kotlinx.coroutines.knit.* import org.junit.Test class ChannelsGuideTest { diff --git a/kotlinx-coroutines-core/jvm/test/guide/test/ComposingGuideTest.kt b/kotlinx-coroutines-core/jvm/test/guide/test/ComposingGuideTest.kt index 50c3fd7e62..1f9692d56b 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/test/ComposingGuideTest.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/test/ComposingGuideTest.kt @@ -5,6 +5,7 @@ // This file was automatically generated from composing-suspending-functions.md by Knit tool. Do not edit. package kotlinx.coroutines.guide.test +import kotlinx.coroutines.knit.* import org.junit.Test class ComposingGuideTest { diff --git a/kotlinx-coroutines-core/jvm/test/guide/test/DispatcherGuideTest.kt b/kotlinx-coroutines-core/jvm/test/guide/test/DispatcherGuideTest.kt index c0c32410d5..d6f1c21dc0 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/test/DispatcherGuideTest.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/test/DispatcherGuideTest.kt @@ -5,6 +5,7 @@ // This file was automatically generated from coroutine-context-and-dispatchers.md by Knit tool. Do not edit. package kotlinx.coroutines.guide.test +import kotlinx.coroutines.knit.* import org.junit.Test class DispatcherGuideTest { diff --git a/kotlinx-coroutines-core/jvm/test/guide/test/ExceptionsGuideTest.kt b/kotlinx-coroutines-core/jvm/test/guide/test/ExceptionsGuideTest.kt index 4a140208f9..f34fd3f83b 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/test/ExceptionsGuideTest.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/test/ExceptionsGuideTest.kt @@ -5,6 +5,7 @@ // This file was automatically generated from exception-handling.md by Knit tool. Do not edit. package kotlinx.coroutines.guide.test +import kotlinx.coroutines.knit.* import org.junit.Test class ExceptionsGuideTest { @@ -22,7 +23,7 @@ class ExceptionsGuideTest { @Test fun testExampleExceptions02() { test("ExampleExceptions02") { kotlinx.coroutines.guide.exampleExceptions02.main() }.verifyLines( - "Caught java.lang.AssertionError" + "CoroutineExceptionHandler got java.lang.AssertionError" ) } @@ -41,14 +42,14 @@ class ExceptionsGuideTest { "Second child throws an exception", "Children are cancelled, but exception is not handled until all children terminate", "The first child finished its non cancellable block", - "Caught java.lang.ArithmeticException" + "CoroutineExceptionHandler got java.lang.ArithmeticException" ) } @Test fun testExampleExceptions05() { test("ExampleExceptions05") { kotlinx.coroutines.guide.exampleExceptions05.main() }.verifyLines( - "Caught java.io.IOException with suppressed [java.lang.ArithmeticException]" + "CoroutineExceptionHandler got java.io.IOException with suppressed [java.lang.ArithmeticException]" ) } @@ -56,37 +57,37 @@ class ExceptionsGuideTest { fun testExampleExceptions06() { test("ExampleExceptions06") { kotlinx.coroutines.guide.exampleExceptions06.main() }.verifyLines( "Rethrowing CancellationException with original cause", - "Caught original java.io.IOException" + "CoroutineExceptionHandler got java.io.IOException" ) } @Test fun testExampleSupervision01() { test("ExampleSupervision01") { kotlinx.coroutines.guide.exampleSupervision01.main() }.verifyLines( - "First child is failing", - "First child is cancelled: true, but second one is still active", - "Cancelling supervisor", - "Second child is cancelled because supervisor is cancelled" + "The first child is failing", + "The first child is cancelled: true, but the second one is still active", + "Cancelling the supervisor", + "The second child is cancelled because the supervisor was cancelled" ) } @Test fun testExampleSupervision02() { test("ExampleSupervision02") { kotlinx.coroutines.guide.exampleSupervision02.main() }.verifyLines( - "Child is sleeping", - "Throwing exception from scope", - "Child is cancelled", - "Caught assertion error" + "The child is sleeping", + "Throwing an exception from the scope", + "The child is cancelled", + "Caught an assertion error" ) } @Test fun testExampleSupervision03() { test("ExampleSupervision03") { kotlinx.coroutines.guide.exampleSupervision03.main() }.verifyLines( - "Scope is completing", - "Child throws an exception", - "Caught java.lang.AssertionError", - "Scope is completed" + "The scope is completing", + "The child throws an exception", + "CoroutineExceptionHandler got java.lang.AssertionError", + "The scope is completed" ) } } diff --git a/kotlinx-coroutines-core/jvm/test/guide/test/FlowGuideTest.kt b/kotlinx-coroutines-core/jvm/test/guide/test/FlowGuideTest.kt index 5d320f2124..c7d4a72082 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/test/FlowGuideTest.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/test/FlowGuideTest.kt @@ -5,6 +5,7 @@ // This file was automatically generated from flow.md by Knit tool. Do not edit. package kotlinx.coroutines.guide.test +import kotlinx.coroutines.knit.* import org.junit.Test class FlowGuideTest { @@ -50,7 +51,7 @@ class FlowGuideTest { @Test fun testExampleFlow05() { test("ExampleFlow05") { kotlinx.coroutines.guide.exampleFlow05.main() }.verifyLines( - "Calling foo...", + "Calling simple function...", "Calling collect...", "Flow started", "1", @@ -139,7 +140,7 @@ class FlowGuideTest { @Test fun testExampleFlow13() { test("ExampleFlow13") { kotlinx.coroutines.guide.exampleFlow13.main() }.verifyLinesFlexibleThread( - "[main @coroutine#1] Started foo flow", + "[main @coroutine#1] Started simple flow", "[main @coroutine#1] Collected 1", "[main @coroutine#1] Collected 2", "[main @coroutine#1] Collected 3" @@ -151,7 +152,7 @@ class FlowGuideTest { test("ExampleFlow14") { kotlinx.coroutines.guide.exampleFlow14.main() }.verifyExceptions( "Exception in thread \"main\" java.lang.IllegalStateException: Flow invariant is violated:", "\t\tFlow was collected in [CoroutineId(1), \"coroutine#1\":BlockingCoroutine{Active}@5511c7f8, BlockingEventLoop@2eac3323],", - "\t\tbut emission happened in [CoroutineId(1), \"coroutine#1\":DispatchedCoroutine{Active}@2dae0000, DefaultDispatcher].", + "\t\tbut emission happened in [CoroutineId(1), \"coroutine#1\":DispatchedCoroutine{Active}@2dae0000, Dispatchers.Default].", "\t\tPlease refer to 'flow' documentation or use 'flowOn' instead", "\tat ..." ) @@ -357,7 +358,7 @@ class FlowGuideTest { fun testExampleFlow34() { test("ExampleFlow34") { kotlinx.coroutines.guide.exampleFlow34.main() }.verifyExceptions( "1", - "Flow completed with null", + "Flow completed with java.lang.IllegalStateException: Collected 2", "Exception in thread \"main\" java.lang.IllegalStateException: Collected 2" ) } @@ -381,4 +382,40 @@ class FlowGuideTest { "Event: 3" ) } + + @Test + fun testExampleFlow37() { + test("ExampleFlow37") { kotlinx.coroutines.guide.exampleFlow37.main() }.verifyExceptions( + "Emitting 1", + "1", + "Emitting 2", + "2", + "Emitting 3", + "3", + "Emitting 4", + "Exception in thread \"main\" kotlinx.coroutines.JobCancellationException: BlockingCoroutine was cancelled; job=\"coroutine#1\":BlockingCoroutine{Cancelled}@6d7b4f4c" + ) + } + + @Test + fun testExampleFlow38() { + test("ExampleFlow38") { kotlinx.coroutines.guide.exampleFlow38.main() }.verifyExceptions( + "1", + "2", + "3", + "4", + "5", + "Exception in thread \"main\" kotlinx.coroutines.JobCancellationException: BlockingCoroutine was cancelled; job=\"coroutine#1\":BlockingCoroutine{Cancelled}@3327bd23" + ) + } + + @Test + fun testExampleFlow39() { + test("ExampleFlow39") { kotlinx.coroutines.guide.exampleFlow39.main() }.verifyExceptions( + "1", + "2", + "3", + "Exception in thread \"main\" kotlinx.coroutines.JobCancellationException: BlockingCoroutine was cancelled; job=\"coroutine#1\":BlockingCoroutine{Cancelled}@5ec0a365" + ) + } } diff --git a/kotlinx-coroutines-core/jvm/test/guide/test/SelectGuideTest.kt b/kotlinx-coroutines-core/jvm/test/guide/test/SelectGuideTest.kt index e3f47b9648..55650d4c6a 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/test/SelectGuideTest.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/test/SelectGuideTest.kt @@ -5,6 +5,7 @@ // This file was automatically generated from select-expression.md by Knit tool. Do not edit. package kotlinx.coroutines.guide.test +import kotlinx.coroutines.knit.* import org.junit.Test class SelectGuideTest { diff --git a/kotlinx-coroutines-core/jvm/test/guide/test/SharedStateGuideTest.kt b/kotlinx-coroutines-core/jvm/test/guide/test/SharedStateGuideTest.kt index 8d534a09ea..3162b24cbc 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/test/SharedStateGuideTest.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/test/SharedStateGuideTest.kt @@ -5,6 +5,7 @@ // This file was automatically generated from shared-mutable-state-and-concurrency.md by Knit tool. Do not edit. package kotlinx.coroutines.guide.test +import kotlinx.coroutines.knit.* import org.junit.Test class SharedStateGuideTest { diff --git a/kotlinx-coroutines-core/jvm/test/internal/ConcurrentWeakMapCollectionStressTest.kt b/kotlinx-coroutines-core/jvm/test/internal/ConcurrentWeakMapCollectionStressTest.kt new file mode 100644 index 0000000000..d9a2a96d81 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/internal/ConcurrentWeakMapCollectionStressTest.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.internal + +import junit.framework.Assert.* +import kotlinx.coroutines.* +import kotlinx.coroutines.debug.internal.* +import org.junit.* +import kotlin.concurrent.* + +class ConcurrentWeakMapCollectionStressTest : TestBase() { + private data class Key(val i: Int) + private val nElements = 100_000 * stressTestMultiplier + private val size = 100_000 + + @Test + fun testCollected() { + // use very big arrays as values, we'll need a queue and a cleaner thread to handle them + val m = ConcurrentWeakMap(weakRefQueue = true) + val cleaner = thread(name = "ConcurrentWeakMapCollectionStressTest-Cleaner") { + m.runWeakRefQueueCleaningLoopUntilInterrupted() + } + for (i in 1..nElements) { + m.put(Key(i), ByteArray(size)) + } + assertTrue(m.size < nElements) // some of it was collected for sure + cleaner.interrupt() + cleaner.join() + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/jvm/test/internal/ConcurrentWeakMapOperationStressTest.kt b/kotlinx-coroutines-core/jvm/test/internal/ConcurrentWeakMapOperationStressTest.kt new file mode 100644 index 0000000000..49e6cccd77 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/internal/ConcurrentWeakMapOperationStressTest.kt @@ -0,0 +1,73 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.internal + +import kotlinx.atomicfu.* +import kotlinx.coroutines.* +import kotlinx.coroutines.debug.internal.* +import org.junit.Test +import kotlin.concurrent.* +import kotlin.test.* + +/** + * Concurrent test for [ConcurrentWeakMap] that tests put/get/remove from concurrent threads and is + * arranged so that concurrent rehashing is also happening. + */ +class ConcurrentWeakMapOperationStressTest : TestBase() { + private val nThreads = 10 + private val batchSize = 1000 + private val nSeconds = 3 * stressTestMultiplier + + private val count = atomic(0L) + private val stop = atomic(false) + + private data class Key(val i: Long) + + @Test + fun testOperations() { + // We don't create queue here, because concurrent operations are enough to make it clean itself + val m = ConcurrentWeakMap() + val threads = Array(nThreads) { index -> + thread(start = false, name = "ConcurrentWeakMapOperationStressTest-$index") { + var generationOffset = 0L + while (!stop.value) { + val kvs = (generationOffset + batchSize * index until generationOffset + batchSize * (index + 1)) + .associateBy({ Key(it) }, { it * it }) + generationOffset += batchSize * nThreads + for ((k, v) in kvs) { + assertEquals(null, m.put(k, v)) + } + for ((k, v) in kvs) { + assertEquals(v, m[k]) + } + for ((k, v) in kvs) { + assertEquals(v, m.remove(k)) + } + for ((k, v) in kvs) { + assertEquals(null, m.get(k)) + } + count.incrementAndGet() + } + } + } + val uncaughtExceptionHandler = Thread.UncaughtExceptionHandler { t, ex -> + ex.printStackTrace() + error("Error in thread $t", ex) + } + threads.forEach { it.uncaughtExceptionHandler = uncaughtExceptionHandler } + threads.forEach { it.start() } + var lastCount = -1L + for (sec in 1..nSeconds) { + Thread.sleep(1000) + val count = count.value + println("$sec: done $count batches") + assertTrue(count > lastCount) // ensure progress + lastCount = count + } + stop.value = true + threads.forEach { it.join() } + assertEquals(0, m.size) + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/jvm/test/internal/ConcurrentWeakMapTest.kt b/kotlinx-coroutines-core/jvm/test/internal/ConcurrentWeakMapTest.kt new file mode 100644 index 0000000000..e4fa5e9bfe --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/internal/ConcurrentWeakMapTest.kt @@ -0,0 +1,46 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.internal + +import junit.framework.Assert.* +import kotlinx.coroutines.* +import kotlinx.coroutines.debug.internal.* +import org.junit.* + +class ConcurrentWeakMapTest : TestBase() { + @Test + fun testSimple() { + val expect = (1..1000).associate { it.toString().let { it to it } } + val m = ConcurrentWeakMap() + // repeat adding/removing a few times + repeat(5) { + assertEquals(0, m.size) + assertEquals(emptySet(), m.keys) + assertEquals(emptyList(), m.values.toList()) + assertEquals(emptySet>(), m.entries) + for ((k, v) in expect) { + assertNull(m.put(k, v)) + } + assertEquals(expect.size, m.size) + assertEquals(expect.keys, m.keys) + assertEquals(expect.entries, m.entries) + for ((k, v) in expect) { + assertEquals(v, m[k]) + } + assertEquals(expect.size, m.size) + if (it % 2 == 0) { + for ((k, v) in expect) { + assertEquals(v, m.remove(k)) + } + } else { + m.clear() + } + assertEquals(0, m.size) + for ((k, _) in expect) { + assertNull(m[k]) + } + } + } +} diff --git a/kotlinx-coroutines-core/jvm/test/internal/SegmentBasedQueue.kt b/kotlinx-coroutines-core/jvm/test/internal/SegmentBasedQueue.kt index 293be7a59e..a125bec25c 100644 --- a/kotlinx-coroutines-core/jvm/test/internal/SegmentBasedQueue.kt +++ b/kotlinx-coroutines-core/jvm/test/internal/SegmentBasedQueue.kt @@ -1,9 +1,13 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + package kotlinx.coroutines.internal -import kotlinx.atomicfu.atomic +import kotlinx.atomicfu.* /** - * This queue implementation is based on [SegmentQueue] for testing purposes and is organized as follows. Essentially, + * This queue implementation is based on [SegmentList] for testing purposes and is organized as follows. Essentially, * the [SegmentBasedQueue] is represented as an infinite array of segments, each stores one element (see [OneElementSegment]). * Both [enqueue] and [dequeue] operations increment the corresponding global index ([enqIdx] for [enqueue] and * [deqIdx] for [dequeue]) and work with the indexed by this counter cell. Since both operations increment the indices @@ -13,60 +17,109 @@ import kotlinx.atomicfu.atomic * the cell with [BROKEN] token and retry the operation, [enqueue] at the same time should restart as well; this way, * the queue is obstruction-free. */ -internal class SegmentBasedQueue : SegmentQueue>() { - override fun newSegment(id: Long, prev: OneElementSegment?): OneElementSegment = OneElementSegment(id, prev) +internal class SegmentBasedQueue { + private val head: AtomicRef> + private val tail: AtomicRef> private val enqIdx = atomic(0L) private val deqIdx = atomic(0L) - // Returns the segments associated with the enqueued element. - fun enqueue(element: T): OneElementSegment { + init { + val s = OneElementSegment(0, null, 2) + head = atomic(s) + tail = atomic(s) + } + + // Returns the segments associated with the enqueued element, or `null` if the queue is closed. + fun enqueue(element: T): OneElementSegment? { while (true) { - var tail = this.tail + val curTail = this.tail.value val enqIdx = this.enqIdx.getAndIncrement() - tail = getSegment(tail, enqIdx) ?: continue - if (tail.element.value === BROKEN) continue - if (tail.element.compareAndSet(null, element)) return tail + val segmentOrClosed = this.tail.findSegmentAndMoveForward(id = enqIdx, startFrom = curTail, createNewSegment = ::createSegment) + if (segmentOrClosed.isClosed) return null + val s = segmentOrClosed.segment + if (s.element.value === BROKEN) continue + if (s.element.compareAndSet(null, element)) return s } } fun dequeue(): T? { while (true) { if (this.deqIdx.value >= this.enqIdx.value) return null - var firstSegment = this.head + val curHead = this.head.value val deqIdx = this.deqIdx.getAndIncrement() - firstSegment = getSegmentAndMoveHead(firstSegment, deqIdx) ?: continue - var el = firstSegment.element.value + val segmentOrClosed = this.head.findSegmentAndMoveForward(id = deqIdx, startFrom = curHead, createNewSegment = ::createSegment) + if (segmentOrClosed.isClosed) return null + val s = segmentOrClosed.segment + if (s.id > deqIdx) continue + var el = s.element.value if (el === null) { - if (firstSegment.element.compareAndSet(null, BROKEN)) continue - else el = firstSegment.element.value + if (s.element.compareAndSet(null, BROKEN)) continue + else el = s.element.value } - if (el === REMOVED) continue + // The link to the previous segment should be cleaned after retrieving the element; + // otherwise, `close()` cannot clean the slot. + s.cleanPrev() + if (el === BROKEN) continue + @Suppress("UNCHECKED_CAST") return el as T } } + // `enqueue` should return `null` after the queue is closed + fun close(): OneElementSegment { + val s = this.tail.value.close() + var cur = s + while (true) { + cur.element.compareAndSet(null, BROKEN) + cur = cur.prev ?: break + } + return s + } + val numberOfSegments: Int get() { - var s: OneElementSegment? = head - var i = 0 - while (s != null) { - s = s.next + var cur = head.value + var i = 1 + while (true) { + cur = cur.next ?: return i i++ } - return i } + + fun checkHeadPrevIsCleaned() { + check(head.value.prev === null) { "head.prev is not null"} + } + + fun checkAllSegmentsAreNotLogicallyRemoved() { + var prev: OneElementSegment? = null + var cur = head.value + while (true) { + check(!cur.logicallyRemoved || cur.isTail) { + "This queue contains removed segments, memory leak detected" + } + check(cur.prev === prev) { + "Two neighbour segments are incorrectly linked: S.next.prev != S" + } + prev = cur + cur = cur.next ?: return + } + } + } -internal class OneElementSegment(id: Long, prev: OneElementSegment?) : Segment>(id, prev) { +private fun createSegment(id: Long, prev: OneElementSegment?) = OneElementSegment(id, prev, 0) + +internal class OneElementSegment(id: Long, prev: OneElementSegment?, pointers: Int) : Segment>(id, prev, pointers) { val element = atomic(null) - override val removed get() = element.value === REMOVED + override val maxSlots get() = 1 + + val logicallyRemoved get() = element.value === BROKEN fun removeSegment() { - element.value = REMOVED - remove() + val old = element.getAndSet(BROKEN) + if (old !== BROKEN) onSlotCleaned() } } -private val BROKEN = Symbol("BROKEN") -private val REMOVED = Symbol("REMOVED") \ No newline at end of file +private val BROKEN = Symbol("BROKEN") \ No newline at end of file diff --git a/kotlinx-coroutines-core/jvm/test/internal/SegmentListTest.kt b/kotlinx-coroutines-core/jvm/test/internal/SegmentListTest.kt new file mode 100644 index 0000000000..ff6a346cda --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/internal/SegmentListTest.kt @@ -0,0 +1,41 @@ +package kotlinx.coroutines.internal + +import kotlinx.atomicfu.* +import org.junit.Test +import kotlin.test.* + +class SegmentListTest { + @Test + fun testRemoveTail() { + val initialSegment = TestSegment(0, null, 2) + val head = AtomicRefHolder(initialSegment) + val tail = AtomicRefHolder(initialSegment) + val s1 = tail.ref.findSegmentAndMoveForward(1, tail.ref.value, ::createTestSegment).segment + assertFalse(s1.removed) + tail.ref.value.onSlotCleaned() + assertFalse(s1.removed) + head.ref.findSegmentAndMoveForward(2, head.ref.value, ::createTestSegment) + assertFalse(s1.removed) + tail.ref.findSegmentAndMoveForward(2, head.ref.value, ::createTestSegment) + assertTrue(s1.removed) + } + + @Test + fun testClose() { + val initialSegment = TestSegment(0, null, 2) + val head = AtomicRefHolder(initialSegment) + val tail = AtomicRefHolder(initialSegment) + tail.ref.findSegmentAndMoveForward(1, tail.ref.value, ::createTestSegment) + assertEquals(tail.ref.value, tail.ref.value.close()) + assertTrue(head.ref.findSegmentAndMoveForward(2, head.ref.value, ::createTestSegment).isClosed) + } +} + +private class AtomicRefHolder(initialValue: T) { + val ref = atomic(initialValue) +} + +private class TestSegment(id: Long, prev: TestSegment?, pointers: Int) : Segment(id, prev, pointers) { + override val maxSlots: Int get() = 1 +} +private fun createTestSegment(id: Long, prev: TestSegment?) = TestSegment(id, prev, 0) \ No newline at end of file diff --git a/kotlinx-coroutines-core/jvm/test/internal/SegmentQueueTest.kt b/kotlinx-coroutines-core/jvm/test/internal/SegmentQueueTest.kt index b59a6488a0..fd2d329088 100644 --- a/kotlinx-coroutines-core/jvm/test/internal/SegmentQueueTest.kt +++ b/kotlinx-coroutines-core/jvm/test/internal/SegmentQueueTest.kt @@ -33,7 +33,7 @@ class SegmentQueueTest : TestBase() { val s = q.enqueue(2) q.enqueue(3) assertEquals(3, q.numberOfSegments) - s.removeSegment() + s!!.removeSegment() assertEquals(2, q.numberOfSegments) assertEquals(1, q.dequeue()) assertEquals(3, q.dequeue()) @@ -47,11 +47,21 @@ class SegmentQueueTest : TestBase() { val s = q.enqueue(2) assertEquals(1, q.dequeue()) q.enqueue(3) - s.removeSegment() + s!!.removeSegment() assertEquals(3, q.dequeue()) assertNull(q.dequeue()) } + @Test + fun testClose() { + val q = SegmentBasedQueue() + q.enqueue(1) + assertEquals(0, q.close().id) + assertEquals(null, q.enqueue(2)) + assertEquals(1, q.dequeue()) + assertEquals(null, q.dequeue()) + } + @Test fun stressTest() { val q = SegmentBasedQueue() @@ -64,6 +74,7 @@ class SegmentQueueTest : TestBase() { expectedQueue.add(el) } else { // remove assertEquals(expectedQueue.poll(), q.dequeue()) + q.checkHeadPrevIsCleaned() } } } @@ -78,7 +89,7 @@ class SegmentQueueTest : TestBase() { val N = 100_000 * stressTestMultiplier val T = 10 val q = SegmentBasedQueue() - val segments = (1..N).map { q.enqueue(it) }.toMutableList() + val segments = (1..N).map { q.enqueue(it)!! }.toMutableList() if (random) segments.shuffle() assertEquals(N, q.numberOfSegments) val nextSegmentIndex = AtomicInteger() diff --git a/kotlinx-coroutines-core/jvm/test/guide/test/TestUtil.kt b/kotlinx-coroutines-core/jvm/test/knit/TestUtil.kt similarity index 98% rename from kotlinx-coroutines-core/jvm/test/guide/test/TestUtil.kt rename to kotlinx-coroutines-core/jvm/test/knit/TestUtil.kt index fb1c85bce1..7eda9043db 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/test/TestUtil.kt +++ b/kotlinx-coroutines-core/jvm/test/knit/TestUtil.kt @@ -1,8 +1,8 @@ /* - * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ -package kotlinx.coroutines.guide.test +package kotlinx.coroutines.knit import kotlinx.coroutines.* import kotlinx.coroutines.internal.* diff --git a/kotlinx-coroutines-core/jvm/test/linearizability/MutexLCStressTest.kt b/kotlinx-coroutines-core/jvm/test/linearizability/MutexLCStressTest.kt new file mode 100644 index 0000000000..9542b5d8de --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/linearizability/MutexLCStressTest.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ +@file:Suppress("unused") +package kotlinx.coroutines.linearizability + +import kotlinx.coroutines.* +import kotlinx.coroutines.sync.* +import org.jetbrains.kotlinx.lincheck.annotations.Operation +import org.jetbrains.kotlinx.lincheck.verifier.* +import org.junit.* + +class MutexLCStressTest : VerifierState() { + private val mutex = Mutex() + + @Operation + fun tryLock() = mutex.tryLock() + + @Operation + suspend fun lock() = mutex.lock() + + @Operation(handleExceptionsAsResult = [IllegalStateException::class]) + fun unlock() = mutex.unlock() + + @Test + fun test() = LCStressOptionsDefault() + .actorsBefore(0) + .check(this::class) + + override fun extractState() = mutex.isLocked +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/jvm/test/linearizability/SegmentListRemoveLCStressTest.kt b/kotlinx-coroutines-core/jvm/test/linearizability/SegmentListRemoveLCStressTest.kt new file mode 100644 index 0000000000..5daed99829 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/linearizability/SegmentListRemoveLCStressTest.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +@file:Suppress("unused") + +package kotlinx.coroutines.linearizability + +import kotlinx.coroutines.* +import kotlinx.coroutines.internal.* +import org.jetbrains.kotlinx.lincheck.annotations.* +import org.jetbrains.kotlinx.lincheck.annotations.Operation +import org.jetbrains.kotlinx.lincheck.paramgen.* +import org.jetbrains.kotlinx.lincheck.verifier.* +import org.junit.* + + +class SegmentListRemoveLCStressTest : VerifierState() { + private val q = SegmentBasedQueue() + private val segments: Array> + + init { + segments = (0..5).map { q.enqueue(it)!! }.toTypedArray() + q.enqueue(6) + } + + @Operation + fun removeSegment(@Param(gen = IntGen::class, conf = "1:5") index: Int) { + segments[index].removeSegment() + } + + override fun extractState() = segments.map { it.logicallyRemoved } + + @Validate + fun checkAllRemoved() { + q.checkHeadPrevIsCleaned() + q.checkAllSegmentsAreNotLogicallyRemoved() + } + + @Test + fun test() = LCStressOptionsDefault() + .actorsBefore(0) + .actorsAfter(0) + .check(this::class) +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/jvm/test/linearizability/SegmentQueueLCStressTest.kt b/kotlinx-coroutines-core/jvm/test/linearizability/SegmentQueueLCStressTest.kt index 1bb51a568f..89bf8dfaa4 100644 --- a/kotlinx-coroutines-core/jvm/test/linearizability/SegmentQueueLCStressTest.kt +++ b/kotlinx-coroutines-core/jvm/test/linearizability/SegmentQueueLCStressTest.kt @@ -18,12 +18,17 @@ class SegmentQueueLCStressTest : VerifierState() { private val q = SegmentBasedQueue() @Operation - fun add(@Param(name = "value") value: Int) { - q.enqueue(value) + fun enqueue(@Param(name = "value") x: Int): Boolean { + return q.enqueue(x) !== null } @Operation - fun poll(): Int? = q.dequeue() + fun dequeue(): Int? = q.dequeue() + + @Operation + fun close() { + q.close() + } override fun extractState(): Any { val elements = ArrayList() @@ -31,8 +36,8 @@ class SegmentQueueLCStressTest : VerifierState() { val x = q.dequeue() ?: break elements.add(x) } - - return elements + val closed = q.enqueue(0) === null + return elements to closed } @Test diff --git a/kotlinx-coroutines-core/jvm/test/linearizability/SemaphoreLCStressTest.kt b/kotlinx-coroutines-core/jvm/test/linearizability/SemaphoreLCStressTest.kt new file mode 100644 index 0000000000..52902f4987 --- /dev/null +++ b/kotlinx-coroutines-core/jvm/test/linearizability/SemaphoreLCStressTest.kt @@ -0,0 +1,34 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ +@file:Suppress("unused") +package kotlinx.coroutines.linearizability + +import kotlinx.coroutines.* +import kotlinx.coroutines.sync.* +import org.jetbrains.kotlinx.lincheck.annotations.Operation +import org.jetbrains.kotlinx.lincheck.verifier.* +import org.junit.* + +abstract class SemaphoreLCStressTestBase(permits: Int) : VerifierState() { + private val semaphore = Semaphore(permits) + + @Operation + fun tryAcquire() = semaphore.tryAcquire() + + @Operation + suspend fun acquire() = semaphore.acquire() + + @Operation(handleExceptionsAsResult = [IllegalStateException::class]) + fun release() = semaphore.release() + + @Test + fun test() = LCStressOptionsDefault() + .actorsBefore(0) + .check(this::class) + + override fun extractState() = semaphore.availablePermits +} + +class Semaphore1LCStressTest : SemaphoreLCStressTestBase(1) +class Semaphore2LCStressTest : SemaphoreLCStressTestBase(2) \ No newline at end of file diff --git a/kotlinx-coroutines-core/jvm/test/sync/MutexStressTest.kt b/kotlinx-coroutines-core/jvm/test/sync/MutexStressTest.kt index 8ecb8fd741..bb713b258d 100644 --- a/kotlinx-coroutines-core/jvm/test/sync/MutexStressTest.kt +++ b/kotlinx-coroutines-core/jvm/test/sync/MutexStressTest.kt @@ -5,6 +5,7 @@ package kotlinx.coroutines.sync import kotlinx.coroutines.* +import kotlinx.coroutines.selects.* import kotlin.test.* class MutexStressTest : TestBase() { @@ -26,4 +27,67 @@ class MutexStressTest : TestBase() { jobs.forEach { it.join() } assertEquals(n * k, shared) } + + @Test + fun stressUnlockCancelRace() = runTest { + val n = 10_000 * stressTestMultiplier + val mutex = Mutex(true) // create a locked mutex + newSingleThreadContext("SemaphoreStressTest").use { pool -> + repeat (n) { + // Initially, we hold the lock and no one else can `lock`, + // otherwise it's a bug. + assertTrue(mutex.isLocked) + var job1EnteredCriticalSection = false + val job1 = launch(start = CoroutineStart.UNDISPATCHED) { + mutex.lock() + job1EnteredCriticalSection = true + mutex.unlock() + } + // check that `job1` didn't finish the call to `acquire()` + assertEquals(false, job1EnteredCriticalSection) + val job2 = launch(pool) { + mutex.unlock() + } + // Because `job2` executes in a separate thread, this + // cancellation races with the call to `unlock()`. + job1.cancelAndJoin() + job2.join() + assertFalse(mutex.isLocked) + mutex.lock() + } + } + } + + @Test + fun stressUnlockCancelRaceWithSelect() = runTest { + val n = 10_000 * stressTestMultiplier + val mutex = Mutex(true) // create a locked mutex + newSingleThreadContext("SemaphoreStressTest").use { pool -> + repeat (n) { + // Initially, we hold the lock and no one else can `lock`, + // otherwise it's a bug. + assertTrue(mutex.isLocked) + var job1EnteredCriticalSection = false + val job1 = launch(start = CoroutineStart.UNDISPATCHED) { + select { + mutex.onLock { + job1EnteredCriticalSection = true + mutex.unlock() + } + } + } + // check that `job1` didn't finish the call to `acquire()` + assertEquals(false, job1EnteredCriticalSection) + val job2 = launch(pool) { + mutex.unlock() + } + // Because `job2` executes in a separate thread, this + // cancellation races with the call to `unlock()`. + job1.cancelAndJoin() + job2.join() + assertFalse(mutex.isLocked) + mutex.lock() + } + } + } } \ No newline at end of file diff --git a/kotlinx-coroutines-core/jvm/test/sync/SemaphoreStressTest.kt b/kotlinx-coroutines-core/jvm/test/sync/SemaphoreStressTest.kt index 9c77990862..374a1e3d7c 100644 --- a/kotlinx-coroutines-core/jvm/test/sync/SemaphoreStressTest.kt +++ b/kotlinx-coroutines-core/jvm/test/sync/SemaphoreStressTest.kt @@ -5,7 +5,6 @@ import org.junit.Test import kotlin.test.assertEquals class SemaphoreStressTest : TestBase() { - @Test fun stressTestAsMutex() = runBlocking(Dispatchers.Default) { val n = 10_000 * stressTestMultiplier @@ -71,14 +70,14 @@ class SemaphoreStressTest : TestBase() { // Initially, we hold the permit and no one else can `acquire`, // otherwise it's a bug. assertEquals(0, semaphore.availablePermits) - var job1_entered_critical_section = false + var job1EnteredCriticalSection = false val job1 = launch(start = CoroutineStart.UNDISPATCHED) { semaphore.acquire() - job1_entered_critical_section = true + job1EnteredCriticalSection = true semaphore.release() } // check that `job1` didn't finish the call to `acquire()` - assertEquals(false, job1_entered_critical_section) + assertEquals(false, job1EnteredCriticalSection) val job2 = launch(pool) { semaphore.release() } @@ -91,5 +90,4 @@ class SemaphoreStressTest : TestBase() { } } } - } diff --git a/kotlinx-coroutines-core/knit.properties b/kotlinx-coroutines-core/knit.properties new file mode 100644 index 0000000000..93ce87db8f --- /dev/null +++ b/kotlinx-coroutines-core/knit.properties @@ -0,0 +1,10 @@ +# +# Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. +# + +knit.package=kotlinx.coroutines.examples +knit.dir=jvm/test/examples/ + +test.package=kotlinx.coroutines.examples.test +test.dir=jvm/test/examples/test/ + diff --git a/kotlinx-coroutines-core/native/src/Builders.kt b/kotlinx-coroutines-core/native/src/Builders.kt index 975fc98db7..3554dc604e 100644 --- a/kotlinx-coroutines-core/native/src/Builders.kt +++ b/kotlinx-coroutines-core/native/src/Builders.kt @@ -73,7 +73,7 @@ private class BlockingCoroutine( eventLoop?.decrementUseCount() } // now return result - val state = state + val state = state.unboxState() (state as? CompletedExceptionally)?.let { throw it.cause } state as T } diff --git a/kotlinx-coroutines-core/native/src/CoroutineContext.kt b/kotlinx-coroutines-core/native/src/CoroutineContext.kt index bcc7f48963..4ec1289ee7 100644 --- a/kotlinx-coroutines-core/native/src/CoroutineContext.kt +++ b/kotlinx-coroutines-core/native/src/CoroutineContext.kt @@ -16,8 +16,8 @@ internal actual object DefaultExecutor : CoroutineDispatcher(), Delay { takeEventLoop().dispatch(context, block) override fun scheduleResumeAfterDelay(timeMillis: Long, continuation: CancellableContinuation) = takeEventLoop().scheduleResumeAfterDelay(timeMillis, continuation) - override fun invokeOnTimeout(timeMillis: Long, block: Runnable): DisposableHandle = - takeEventLoop().invokeOnTimeout(timeMillis, block) + override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle = + takeEventLoop().invokeOnTimeout(timeMillis, block, context) actual fun enqueue(task: Runnable): Unit = loopWasShutDown() } diff --git a/kotlinx-coroutines-core/native/src/Debug.kt b/kotlinx-coroutines-core/native/src/Debug.kt index 26787f865a..1fa0ec7ff3 100644 --- a/kotlinx-coroutines-core/native/src/Debug.kt +++ b/kotlinx-coroutines-core/native/src/Debug.kt @@ -13,6 +13,6 @@ internal actual val Any.hexAddress: String get() = abs(id().let { if (it == Int. internal actual val Any.classSimpleName: String get() = this::class.simpleName ?: "Unknown" @SymbolName("Kotlin_Any_hashCode") -external fun Any.id(): Int // Note: can return negative value on K/N +public external fun Any.id(): Int // Note: can return negative value on K/N internal actual inline fun assert(value: () -> Boolean) {} diff --git a/kotlinx-coroutines-core/native/src/Dispatchers.kt b/kotlinx-coroutines-core/native/src/Dispatchers.kt index 6c650046a0..c06b7c2f0a 100644 --- a/kotlinx-coroutines-core/native/src/Dispatchers.kt +++ b/kotlinx-coroutines-core/native/src/Dispatchers.kt @@ -7,24 +7,16 @@ package kotlinx.coroutines import kotlin.coroutines.* public actual object Dispatchers { - public actual val Default: CoroutineDispatcher = createDefaultDispatcher() - public actual val Main: MainCoroutineDispatcher = NativeMainDispatcher(Default) - public actual val Unconfined: CoroutineDispatcher get() = kotlinx.coroutines.Unconfined // Avoid freezing } private class NativeMainDispatcher(val delegate: CoroutineDispatcher) : MainCoroutineDispatcher() { - override val immediate: MainCoroutineDispatcher get() = throw UnsupportedOperationException("Immediate dispatching is not supported on Native") - override fun dispatch(context: CoroutineContext, block: Runnable) = delegate.dispatch(context, block) - override fun isDispatchNeeded(context: CoroutineContext): Boolean = delegate.isDispatchNeeded(context) - override fun dispatchYield(context: CoroutineContext, block: Runnable) = delegate.dispatchYield(context, block) - - override fun toString(): String = delegate.toString() + override fun toString(): String = toStringInternalImpl() ?: delegate.toString() } diff --git a/kotlinx-coroutines-core/native/src/EventLoop.kt b/kotlinx-coroutines-core/native/src/EventLoop.kt index d6c6525504..b397d6f182 100644 --- a/kotlinx-coroutines-core/native/src/EventLoop.kt +++ b/kotlinx-coroutines-core/native/src/EventLoop.kt @@ -4,6 +4,7 @@ package kotlinx.coroutines +import kotlin.coroutines.* import kotlin.system.* internal actual abstract class EventLoopImplPlatform: EventLoop() { @@ -13,7 +14,7 @@ internal actual abstract class EventLoopImplPlatform: EventLoop() { } internal class EventLoopImpl: EventLoopImplBase() { - override fun invokeOnTimeout(timeMillis: Long, block: Runnable): DisposableHandle = + override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle = scheduleInvokeOnTimeout(timeMillis, block) } diff --git a/kotlinx-coroutines-core/native/src/Exceptions.kt b/kotlinx-coroutines-core/native/src/Exceptions.kt index 39b3344ac1..c82199a429 100644 --- a/kotlinx-coroutines-core/native/src/Exceptions.kt +++ b/kotlinx-coroutines-core/native/src/Exceptions.kt @@ -14,7 +14,7 @@ public actual open class CancellationException( message: String?, cause: Throwable? ) : IllegalStateException(message, cause) { - actual constructor(message: String?) : this(message, null) + public actual constructor(message: String?) : this(message, null) } /** diff --git a/kotlinx-coroutines-core/native/src/WorkerMain.native.kt b/kotlinx-coroutines-core/native/src/WorkerMain.native.kt new file mode 100644 index 0000000000..84cc9f42b9 --- /dev/null +++ b/kotlinx-coroutines-core/native/src/WorkerMain.native.kt @@ -0,0 +1,8 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines + +// It is used in the main sources of native-mt branch +internal expect inline fun workerMain(block: () -> Unit) diff --git a/kotlinx-coroutines-core/native/src/flow/internal/SafeCollector.kt b/kotlinx-coroutines-core/native/src/flow/internal/SafeCollector.kt index 78f1bdb2a9..ace633ccc6 100644 --- a/kotlinx-coroutines-core/native/src/flow/internal/SafeCollector.kt +++ b/kotlinx-coroutines-core/native/src/flow/internal/SafeCollector.kt @@ -4,6 +4,7 @@ package kotlinx.coroutines.flow.internal +import kotlinx.coroutines.* import kotlinx.coroutines.flow.* import kotlin.coroutines.* @@ -17,7 +18,8 @@ internal actual class SafeCollector actual constructor( private var lastEmissionContext: CoroutineContext? = null override suspend fun emit(value: T) { - val currentContext = coroutineContext + val currentContext = currentCoroutineContext() + currentContext.ensureActive() if (lastEmissionContext !== currentContext) { checkContext(currentContext) lastEmissionContext = currentContext diff --git a/kotlinx-coroutines-core/native/src/internal/LinkedList.kt b/kotlinx-coroutines-core/native/src/internal/LinkedList.kt index 60d0857be5..99ab042f3c 100644 --- a/kotlinx-coroutines-core/native/src/internal/LinkedList.kt +++ b/kotlinx-coroutines-core/native/src/internal/LinkedList.kt @@ -1,6 +1,7 @@ /* * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ +@file:Suppress("NO_EXPLICIT_RETURN_TYPE_IN_API_MODE", "NO_EXPLICIT_VISIBILITY_IN_API_MODE") package kotlinx.coroutines.internal @@ -123,6 +124,8 @@ public actual abstract class AbstractAtomicDesc : AtomicDesc() { return null } + actual open fun onRemoved(affected: Node) {} + actual final override fun prepare(op: AtomicOp<*>): Any? { val affected = affectedNode val failure = failure(affected) diff --git a/kotlinx-coroutines-core/native/src/internal/LocalAtomics.kt b/kotlinx-coroutines-core/native/src/internal/LocalAtomics.kt new file mode 100644 index 0000000000..398cb63bc2 --- /dev/null +++ b/kotlinx-coroutines-core/native/src/internal/LocalAtomics.kt @@ -0,0 +1,20 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.internal + +import kotlinx.atomicfu.* + +internal actual class LocalAtomicInt actual constructor(value: Int) { + + private val iRef = atomic(value) + + actual fun set(value: Int) { + iRef.value = value + } + + actual fun get(): Int = iRef.value + + actual fun decrementAndGet(): Int = iRef.decrementAndGet() +} diff --git a/kotlinx-coroutines-core/native/src/internal/ThreadLocal.kt b/kotlinx-coroutines-core/native/src/internal/ThreadLocal.kt index db62371c1f..09f501a4f5 100644 --- a/kotlinx-coroutines-core/native/src/internal/ThreadLocal.kt +++ b/kotlinx-coroutines-core/native/src/internal/ThreadLocal.kt @@ -4,9 +4,6 @@ package kotlinx.coroutines.internal -@Suppress("ACTUAL_WITHOUT_EXPECT") -internal actual typealias NativeThreadLocal = kotlin.native.concurrent.ThreadLocal - internal actual class CommonThreadLocal actual constructor() { private var value: T? = null @Suppress("UNCHECKED_CAST") diff --git a/kotlinx-coroutines-core/native/test/RunBlockingTest.kt b/kotlinx-coroutines-core/native/test/RunBlockingTest.kt new file mode 100644 index 0000000000..c5d08af5f3 --- /dev/null +++ b/kotlinx-coroutines-core/native/test/RunBlockingTest.kt @@ -0,0 +1,18 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines +import kotlin.test.* + +class RunBlockingTest : TestBase() { + + @Test + fun testIncompleteState() { + val handle = runBlocking { + coroutineContext[Job]!!.invokeOnCompletion { } + } + + handle.dispose() + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/native/test/WorkerTest.kt b/kotlinx-coroutines-core/native/test/WorkerTest.kt index 84acedac94..d6b5fad182 100644 --- a/kotlinx-coroutines-core/native/test/WorkerTest.kt +++ b/kotlinx-coroutines-core/native/test/WorkerTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ package kotlinx.coroutines @@ -19,6 +19,7 @@ class WorkerTest : TestBase() { delay(1) } }.result + worker.requestTermination() } @Test @@ -31,5 +32,6 @@ class WorkerTest : TestBase() { }.join() } }.result + worker.requestTermination() } } diff --git a/kotlinx-coroutines-core/nativeDarwin/src/WorkerMain.kt b/kotlinx-coroutines-core/nativeDarwin/src/WorkerMain.kt new file mode 100644 index 0000000000..3445cb9897 --- /dev/null +++ b/kotlinx-coroutines-core/nativeDarwin/src/WorkerMain.kt @@ -0,0 +1,13 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines + +import kotlinx.cinterop.* + +internal actual inline fun workerMain(block: () -> Unit) { + autoreleasepool { + block() + } +} diff --git a/kotlinx-coroutines-core/nativeDarwin/test/Launcher.kt b/kotlinx-coroutines-core/nativeDarwin/test/Launcher.kt new file mode 100644 index 0000000000..78ed765967 --- /dev/null +++ b/kotlinx-coroutines-core/nativeDarwin/test/Launcher.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines + +import platform.CoreFoundation.* +import kotlin.native.concurrent.* +import kotlin.native.internal.test.* +import kotlin.system.* + +// This is a separate entry point for tests in background +fun mainBackground(args: Array) { + val worker = Worker.start(name = "main-background") + worker.execute(TransferMode.SAFE, { args.freeze() }) { + val result = testLauncherEntryPoint(it) + exitProcess(result) + } + CFRunLoopRun() + error("CFRunLoopRun should never return") +} + +// This is a separate entry point for tests with leak checker +fun mainNoExit(args: Array) { + workerMain { // autoreleasepool to make sure interop objects are properly freed + testLauncherEntryPoint(args) + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-core/nativeOther/src/WorkerMain.kt b/kotlinx-coroutines-core/nativeOther/src/WorkerMain.kt new file mode 100644 index 0000000000..cac0530e4e --- /dev/null +++ b/kotlinx-coroutines-core/nativeOther/src/WorkerMain.kt @@ -0,0 +1,7 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines + +internal actual inline fun workerMain(block: () -> Unit) = block() diff --git a/kotlinx-coroutines-core/nativeOther/test/Launcher.kt b/kotlinx-coroutines-core/nativeOther/test/Launcher.kt new file mode 100644 index 0000000000..feddd4c097 --- /dev/null +++ b/kotlinx-coroutines-core/nativeOther/test/Launcher.kt @@ -0,0 +1,23 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines + +import kotlin.native.concurrent.* +import kotlin.native.internal.test.* +import kotlin.system.* + +// This is a separate entry point for tests in background +fun mainBackground(args: Array) { + val worker = Worker.start(name = "main-background") + worker.execute(TransferMode.SAFE, { args.freeze() }) { + val result = testLauncherEntryPoint(it) + exitProcess(result) + }.result // block main thread +} + +// This is a separate entry point for tests with leak checker +fun mainNoExit(args: Array) { + testLauncherEntryPoint(args) +} \ No newline at end of file diff --git a/kotlinx-coroutines-debug/README.md b/kotlinx-coroutines-debug/README.md index c552715c01..0c01400f1f 100644 --- a/kotlinx-coroutines-debug/README.md +++ b/kotlinx-coroutines-debug/README.md @@ -13,12 +13,17 @@ suspension stacktraces. Additionally, it is possible to process the list of such coroutines via [DebugProbes.dumpCoroutinesInfo] or dump isolated parts of coroutines hierarchy referenced by a [Job] or [CoroutineScope] instances using [DebugProbes.printJob] and [DebugProbes.printScope] respectively. +This module also provides an automatic [BlockHound](https://github.com/reactor/BlockHound) integration +that detects when a blocking operation was called in a coroutine context that prohibits it. In order to use it, +please follow the BlockHound [quick start guide]( +https://github.com/reactor/BlockHound/blob/1.0.2.RELEASE/docs/quick_start.md). + ### Using in your project Add `kotlinx-coroutines-debug` to your project test dependencies: ``` dependencies { - testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-debug:1.3.5' + testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-debug:1.4.0' } ``` @@ -56,7 +61,7 @@ stacktraces will be dumped to the console. ### Using as JVM agent Debug module can also be used as a standalone JVM agent to enable debug probes on the application startup. -You can run your application with an additional argument: `-javaagent:kotlinx-coroutines-debug-1.3.5.jar`. +You can run your application with an additional argument: `-javaagent:kotlinx-coroutines-debug-1.4.0.jar`. Additionally, on Linux and Mac OS X you can use `kill -5 $pid` command in order to force your application to print all alive coroutines. When used as Java agent, `"kotlinx.coroutines.debug.enable.creation.stack.trace"` system property can be used to control [DebugProbes.enableCreationStackTraces] along with agent startup. @@ -165,6 +170,98 @@ java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/management/Mana at kotlinx.coroutines.debug.DebugProbes.install(DebugProbes.kt:49) --> +#### Build failures due to duplicate resource files + +Building an Android project that depends on `kotlinx-coroutines-debug` (usually introduced by being a transitive +dependency of `kotlinx-coroutines-test`) may fail with `DuplicateRelativeFileException` for `META-INF/AL2.0`, +`META-INF/LGPL2.1`, or `win32-x86/attach_hotspot_windows.dll` when trying to merge the Android resource. + +The problem is that Android merges the resources of all its dependencies into a single directory and complains about +conflicts, but: +* `kotlinx-coroutines-debug` transitively depends on JNA and JNA-platform, both of which include license files in their + META-INF directories. Trying to merge these files leads to conflicts, which means that any Android project that + depends on JNA and JNA-platform will experience build failures. +* Additionally, `kotlinx-coroutines-debug` embeds `byte-buddy-agent` and `byte-buddy`, along with their resource files. + Then, if the project separately depends on `byte-buddy`, merging the resources of `kotlinx-coroutines-debug` with ones + from `byte-buddy` and `byte-buddy-agent` will lead to conflicts as the resource files are duplicated. + +One possible workaround for these issues is to add the following to the `android` block in your gradle file for the +application subproject: +```groovy + packagingOptions { + // for JNA and JNA-platform + exclude "META-INF/AL2.0" + exclude "META-INF/LGPL2.1" + // for byte-buddy + exclude "META-INF/licenses/ASM" + pickFirst "win32-x86-64/attach_hotspot_windows.dll" + pickFirst "win32-x86/attach_hotspot_windows.dll" + } +``` +This will cause the resource merge algorithm to exclude the problematic license files altogether and only leave a single +copy of the files needed for `byte-buddy-agent` to work. + +Alternatively, avoid depending on `kotlinx-coroutines-debug`. In particular, if the only reason why this library a +dependency of your project is that `kotlinx-coroutines-test` in turn depends on it, you may change your dependency on +`kotlinx.coroutines.test` to exclude `kotlinx-coroutines-debug`. For example, you could replace +```kotlin +androidTestImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version") +``` +with +```groovy +androidTestImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version") { + exclude group: "org.jetbrains.kotlinx", module: "kotlinx-coroutines-debug" +} +``` + [Job]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/index.html diff --git a/kotlinx-coroutines-debug/api/kotlinx-coroutines-debug.api b/kotlinx-coroutines-debug/api/kotlinx-coroutines-debug.api index 6061f03899..b6056c410c 100644 --- a/kotlinx-coroutines-debug/api/kotlinx-coroutines-debug.api +++ b/kotlinx-coroutines-debug/api/kotlinx-coroutines-debug.api @@ -1,5 +1,4 @@ public final class kotlinx/coroutines/debug/CoroutineInfo { - public final fun copy ()Lkotlinx/coroutines/debug/CoroutineInfo; public final fun getContext ()Lkotlin/coroutines/CoroutineContext; public final fun getCreationStackTrace ()Ljava/util/List; public final fun getJob ()Lkotlinx/coroutines/Job; @@ -8,6 +7,11 @@ public final class kotlinx/coroutines/debug/CoroutineInfo { public fun toString ()Ljava/lang/String; } +public final class kotlinx/coroutines/debug/CoroutinesBlockHoundIntegration : reactor/blockhound/integration/BlockHoundIntegration { + public fun ()V + public fun applyTo (Lreactor/blockhound/BlockHound$Builder;)V +} + public final class kotlinx/coroutines/debug/DebugProbes { public static final field INSTANCE Lkotlinx/coroutines/debug/DebugProbes; public final fun dumpCoroutines (Ljava/io/PrintStream;)V @@ -37,10 +41,6 @@ public final class kotlinx/coroutines/debug/State : java/lang/Enum { public static fun values ()[Lkotlinx/coroutines/debug/State; } -public synthetic class kotlinx/coroutines/debug/internal/DebugProbesImplSequenceNumberRefVolatile { - public fun (J)V -} - public final class kotlinx/coroutines/debug/junit4/CoroutinesTimeout : org/junit/rules/TestRule { public static final field Companion Lkotlinx/coroutines/debug/junit4/CoroutinesTimeout$Companion; public fun (JZ)V diff --git a/kotlinx-coroutines-debug/build.gradle b/kotlinx-coroutines-debug/build.gradle index 7fc2e22369..ab7f28c6a8 100644 --- a/kotlinx-coroutines-debug/build.gradle +++ b/kotlinx-coroutines-debug/build.gradle @@ -14,7 +14,7 @@ configurations { * but in that case it changes dependency type to "runtime" and resolves it * (so it cannot be further modified). Otherwise, shadow just ignores all dependencies. */ - shadow.extendsFrom(compile) // shadow - resulting configuration with shaded jar file + shadow.extendsFrom(api) // shadow - resulting configuration with shaded jar file configureKotlinJvmPlatform(shadow) } @@ -22,6 +22,10 @@ dependencies { compileOnly "junit:junit:$junit_version" shadowDeps "net.bytebuddy:byte-buddy:$byte_buddy_version" shadowDeps "net.bytebuddy:byte-buddy-agent:$byte_buddy_version" + compileOnly "io.projectreactor.tools:blockhound:$blockhound_version" + testCompile "io.projectreactor.tools:blockhound:$blockhound_version" + api "net.java.dev.jna:jna:$jna_version" + api "net.java.dev.jna:jna-platform:$jna_version" } jar { @@ -35,5 +39,5 @@ shadowJar { classifier null // Shadow only byte buddy, do not package kotlin stdlib configurations = [project.configurations.shadowDeps] - relocate 'net.bytebuddy', 'kotlinx.coroutines.repackaged.net.bytebuddy' + relocate('net.bytebuddy', 'kotlinx.coroutines.repackaged.net.bytebuddy') } diff --git a/kotlinx-coroutines-debug/resources/META-INF/services/reactor.blockhound.integration.BlockHoundIntegration b/kotlinx-coroutines-debug/resources/META-INF/services/reactor.blockhound.integration.BlockHoundIntegration new file mode 100644 index 0000000000..c2f1e9cf38 --- /dev/null +++ b/kotlinx-coroutines-debug/resources/META-INF/services/reactor.blockhound.integration.BlockHoundIntegration @@ -0,0 +1 @@ +kotlinx.coroutines.debug.CoroutinesBlockHoundIntegration \ No newline at end of file diff --git a/kotlinx-coroutines-debug/src/AgentPremain.kt b/kotlinx-coroutines-debug/src/AgentPremain.kt deleted file mode 100644 index aa842ce327..0000000000 --- a/kotlinx-coroutines-debug/src/AgentPremain.kt +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package kotlinx.coroutines.debug - -import net.bytebuddy.agent.* -import sun.misc.* -import java.lang.instrument.* - -@Suppress("unused") -internal object AgentPremain { - - private val enableCreationStackTraces = - System.getProperty("kotlinx.coroutines.debug.enable.creation.stack.trace")?.toBoolean() - ?: DebugProbes.enableCreationStackTraces - - @JvmStatic - public fun premain(args: String?, instrumentation: Instrumentation) { - Installer.premain(args, instrumentation) - DebugProbes.enableCreationStackTraces = enableCreationStackTraces - DebugProbes.install() - installSignalHandler() - } - - private fun installSignalHandler() { - try { - Signal.handle(Signal("TRAP")) { // kill -5 - if (DebugProbes.isInstalled) { - // Case with 'isInstalled' changed between this check-and-act is not considered - // a real debug probes use-case, thus is not guarded against. - DebugProbes.dumpCoroutines() - } else { - println("""Cannot perform coroutines dump, debug probes are disabled""") - } - } - } catch (t: Throwable) { - System.err.println("Failed to install signal handler: $t") - } - } -} diff --git a/kotlinx-coroutines-debug/src/CoroutineInfo.kt b/kotlinx-coroutines-debug/src/CoroutineInfo.kt index d92d9b6b8b..ce1478ad07 100644 --- a/kotlinx-coroutines-debug/src/CoroutineInfo.kt +++ b/kotlinx-coroutines-debug/src/CoroutineInfo.kt @@ -1,12 +1,11 @@ /* * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ - -@file:Suppress("PropertyName") - +@file:Suppress("NO_EXPLICIT_VISIBILITY_IN_API_MODE", "INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "UNUSED") package kotlinx.coroutines.debug import kotlinx.coroutines.* +import kotlinx.coroutines.debug.internal.* import kotlin.coroutines.* import kotlin.coroutines.jvm.internal.* @@ -14,11 +13,18 @@ import kotlin.coroutines.jvm.internal.* * Class describing coroutine info such as its context, state and stacktrace. */ @ExperimentalCoroutinesApi -public class CoroutineInfo internal constructor( - val context: CoroutineContext, - private val creationStackBottom: CoroutineStackFrame?, - @JvmField internal val sequenceNumber: Long -) { +public class CoroutineInfo internal constructor(delegate: DebugCoroutineInfo) { + /** + * [Coroutine context][coroutineContext] of the coroutine + */ + public val context: CoroutineContext = delegate.context + + /** + * Last observed state of the coroutine + */ + public val state: State = State.valueOf(delegate.state) + + private val creationStackBottom: CoroutineStackFrame? = delegate.creationStackBottom /** * [Job] associated with a current coroutine or null. @@ -32,24 +38,7 @@ public class CoroutineInfo internal constructor( */ public val creationStackTrace: List get() = creationStackTrace() - /** - * Last observed [state][State] of the coroutine. - */ - public val state: State get() = _state - - private var _state: State = State.CREATED - - @JvmField - internal var lastObservedThread: Thread? = null - - @JvmField - internal var lastObservedFrame: CoroutineStackFrame? = null - - public fun copy(): CoroutineInfo = CoroutineInfo(context, creationStackBottom, sequenceNumber).also { - it._state = _state - it.lastObservedFrame = lastObservedFrame - it.lastObservedThread = lastObservedThread - } + private val lastObservedFrame: CoroutineStackFrame? = delegate.lastObservedFrame /** * Last observed stacktrace of the coroutine captured on its suspension or resumption point. @@ -81,18 +70,6 @@ public class CoroutineInfo internal constructor( } } - internal fun updateState(state: State, frame: Continuation<*>) { - // Propagate only duplicating transitions to running for KT-29997 - if (_state == state && state == State.SUSPENDED && lastObservedFrame != null) return - _state = state - lastObservedFrame = frame as? CoroutineStackFrame - if (state == State.RUNNING) { - lastObservedThread = Thread.currentThread() - } else { - lastObservedThread = null - } - } - override fun toString(): String = "CoroutineInfo(state=$state,context=$context)" } diff --git a/kotlinx-coroutines-debug/src/CoroutinesBlockHoundIntegration.kt b/kotlinx-coroutines-debug/src/CoroutinesBlockHoundIntegration.kt new file mode 100644 index 0000000000..091e8eb16e --- /dev/null +++ b/kotlinx-coroutines-debug/src/CoroutinesBlockHoundIntegration.kt @@ -0,0 +1,168 @@ +@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") + +package kotlinx.coroutines.debug + +import kotlinx.coroutines.scheduling.* +import reactor.blockhound.* +import reactor.blockhound.integration.* + +@Suppress("UNUSED") +public class CoroutinesBlockHoundIntegration : BlockHoundIntegration { + + override fun applyTo(builder: BlockHound.Builder): Unit = with(builder) { + allowBlockingCallsInPrimitiveImplementations() + allowBlockingWhenEnqueuingTasks() + allowServiceLoaderInvocationsOnInit() + allowBlockingCallsInReflectionImpl() + /* The predicates that define that BlockHound should only report blocking calls from threads that are part of + the coroutine thread pool and currently execute a CPU-bound coroutine computation. */ + addDynamicThreadPredicate { isSchedulerWorker(it) } + nonBlockingThreadPredicate { p -> p.or { mayNotBlock(it) } } + } + + /** + * Allows blocking calls in various coroutine structures, such as flows and channels. + * + * They use locks in implementations, though only for protecting short pieces of fast and well-understood code, so + * locking in such places doesn't affect the program liveness. + */ + private fun BlockHound.Builder.allowBlockingCallsInPrimitiveImplementations() { + allowBlockingCallsInJobSupport() + allowBlockingCallsInThreadSafeHeap() + allowBlockingCallsInFlow() + allowBlockingCallsInChannels() + } + + /** + * Allows blocking inside [kotlinx.coroutines.JobSupport]. + */ + private fun BlockHound.Builder.allowBlockingCallsInJobSupport() { + for (method in listOf("finalizeFinishingState", "invokeOnCompletion", "makeCancelling", + "tryMakeCompleting")) + { + allowBlockingCallsInside("kotlinx.coroutines.JobSupport", method) + } + } + + /** + * Allows blocking inside [kotlinx.coroutines.internal.ThreadSafeHeap]. + */ + private fun BlockHound.Builder.allowBlockingCallsInThreadSafeHeap() { + for (method in listOf("clear", "peek", "removeFirstOrNull", "addLast")) { + allowBlockingCallsInside("kotlinx.coroutines.internal.ThreadSafeHeap", method) + } + // [addLastIf] is only used in [EventLoop.common]. Users of [removeFirstIf]: + allowBlockingCallsInside("kotlinx.coroutines.test.TestCoroutineDispatcher", "doActionsUntil") + allowBlockingCallsInside("kotlinx.coroutines.test.TestCoroutineContext", "triggerActions") + } + + private fun BlockHound.Builder.allowBlockingCallsInFlow() { + allowBlockingCallsInsideStateFlow() + allowBlockingCallsInsideSharedFlow() + } + + /** + * Allows blocking inside the implementation of [kotlinx.coroutines.flow.StateFlow]. + */ + private fun BlockHound.Builder.allowBlockingCallsInsideStateFlow() { + allowBlockingCallsInside("kotlinx.coroutines.flow.StateFlowImpl", "updateState") + } + + /** + * Allows blocking inside the implementation of [kotlinx.coroutines.flow.SharedFlow]. + */ + private fun BlockHound.Builder.allowBlockingCallsInsideSharedFlow() { + for (method in listOf("emitSuspend", "awaitValue", "getReplayCache", "tryEmit", "cancelEmitter", + "tryTakeValue", "resetReplayCache")) + { + allowBlockingCallsInside("kotlinx.coroutines.flow.SharedFlowImpl", method) + } + for (method in listOf("getSubscriptionCount", "allocateSlot", "freeSlot")) { + allowBlockingCallsInside("kotlinx.coroutines.flow.internal.AbstractSharedFlow", method) + } + } + + private fun BlockHound.Builder.allowBlockingCallsInChannels() { + allowBlockingCallsInArrayChannel() + allowBlockingCallsInBroadcastChannel() + allowBlockingCallsInConflatedChannel() + } + + /** + * Allows blocking inside [kotlinx.coroutines.channels.ArrayChannel]. + */ + private fun BlockHound.Builder.allowBlockingCallsInArrayChannel() { + for (method in listOf( + "pollInternal", "isEmpty", "isFull", "isClosedForReceive", "offerInternal", "offerSelectInternal", + "enqueueSend", "pollInternal", "pollSelectInternal", "enqueueReceiveInternal", "onCancelIdempotent")) + { + allowBlockingCallsInside("kotlinx.coroutines.channels.ArrayChannel", method) + } + } + + /** + * Allows blocking inside [kotlinx.coroutines.channels.ArrayBroadcastChannel]. + */ + private fun BlockHound.Builder.allowBlockingCallsInBroadcastChannel() { + for (method in listOf("offerInternal", "offerSelectInternal", "updateHead")) { + allowBlockingCallsInside("kotlinx.coroutines.channels.ArrayBroadcastChannel", method) + } + for (method in listOf("checkOffer", "pollInternal", "pollSelectInternal")) { + allowBlockingCallsInside("kotlinx.coroutines.channels.ArrayBroadcastChannel\$Subscriber", method) + } + } + + /** + * Allows blocking inside [kotlinx.coroutines.channels.ConflatedChannel]. + */ + private fun BlockHound.Builder.allowBlockingCallsInConflatedChannel() { + for (method in listOf("offerInternal", "offerSelectInternal", "pollInternal", "pollSelectInternal", + "onCancelIdempotent")) + { + allowBlockingCallsInside("kotlinx.coroutines.channels.ConflatedChannel", method) + } + } + + /** + * Allows blocking when enqueuing tasks into a thread pool. + * + * Without this, the following code breaks: + * ``` + * withContext(Dispatchers.Default) { + * withContext(newSingleThreadContext("singleThreadedContext")) { + * } + * } + * ``` + */ + private fun BlockHound.Builder.allowBlockingWhenEnqueuingTasks() { + /* This method may block as part of its implementation, but is probably safe. */ + allowBlockingCallsInside("java.util.concurrent.ScheduledThreadPoolExecutor", "execute") + } + + /** + * Allows instances of [java.util.ServiceLoader] being called. + * + * Each instance is listed separately; another approach could be to generally allow the operations performed by + * service loaders, as they can generally be considered safe. This was not done here because ServiceLoader has a + * large API surface, with some methods being hidden as implementation details (in particular, the implementation of + * its iterator is completely opaque). Relying on particular names being used in ServiceLoader's implementation + * would be brittle, so here we only provide clearance rules for some specific instances. + */ + private fun BlockHound.Builder.allowServiceLoaderInvocationsOnInit() { + allowBlockingCallsInside("kotlinx.coroutines.reactive.ReactiveFlowKt", "") + allowBlockingCallsInside("kotlinx.coroutines.CoroutineExceptionHandlerImplKt", "") + // not part of the coroutines library, but it would be nice if reflection also wasn't considered blocking + allowBlockingCallsInside("kotlin.reflect.jvm.internal.impl.resolve.OverridingUtil", "") + } + + /** + * Allows some blocking calls from the reflection API. + * + * The API is big, so surely some other blocking calls will show up, but with these rules in place, at least some + * simple examples work without problems. + */ + private fun BlockHound.Builder.allowBlockingCallsInReflectionImpl() { + allowBlockingCallsInside("kotlin.reflect.jvm.internal.impl.builtins.jvm.JvmBuiltInsPackageFragmentProvider", "findPackage") + } + +} diff --git a/kotlinx-coroutines-debug/src/DebugProbes.kt b/kotlinx-coroutines-debug/src/DebugProbes.kt index f164e7ccdc..254385f942 100644 --- a/kotlinx-coroutines-debug/src/DebugProbes.kt +++ b/kotlinx-coroutines-debug/src/DebugProbes.kt @@ -2,7 +2,7 @@ * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ -@file:Suppress("unused") +@file:Suppress("UNUSED", "INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") package kotlinx.coroutines.debug @@ -10,7 +10,6 @@ import kotlinx.coroutines.* import kotlinx.coroutines.debug.internal.* import java.io.* import java.lang.management.* -import java.util.* import kotlin.coroutines.* /** @@ -41,7 +40,11 @@ public object DebugProbes { * Sanitization removes all frames from `kotlinx.coroutines` package except * the first one and the last one to simplify diagnostic. */ - public var sanitizeStackTraces: Boolean = true + public var sanitizeStackTraces: Boolean + get() = DebugProbesImpl.sanitizeStackTraces + set(value) { + DebugProbesImpl.sanitizeStackTraces = value + } /** * Whether coroutine creation stack traces should be captured. @@ -50,7 +53,11 @@ public object DebugProbes { * This option can be useful during local debug sessions, but is recommended * to be disabled in production environments to avoid stack trace dumping overhead. */ - public var enableCreationStackTraces: Boolean = true + public var enableCreationStackTraces: Boolean + get() = DebugProbesImpl.enableCreationStackTraces + set(value) { + DebugProbesImpl.enableCreationStackTraces = value + } /** * Determines whether debug probes were [installed][DebugProbes.install]. @@ -114,7 +121,7 @@ public object DebugProbes { * Returns all existing coroutines info. * The resulting collection represents a consistent snapshot of all existing coroutines at the moment of invocation. */ - public fun dumpCoroutinesInfo(): List = DebugProbesImpl.dumpCoroutinesInfo() + public fun dumpCoroutinesInfo(): List = DebugProbesImpl.dumpCoroutinesInfo().map { CoroutineInfo(it) } /** * Dumps all active coroutines into the given output stream, providing a consistent snapshot of all existing coroutines at the moment of invocation. @@ -131,11 +138,10 @@ public object DebugProbes { * at MyClass.createIoRequest(MyClass.kt:142) * at MyClass.fetchData(MyClass.kt:154) * at MyClass.showData(MyClass.kt:31) - * * ... * ``` */ - public fun dumpCoroutines(out: PrintStream = System.out) = DebugProbesImpl.dumpCoroutines(out) + public fun dumpCoroutines(out: PrintStream = System.out): Unit = DebugProbesImpl.dumpCoroutines(out) } // Stubs which are injected as coroutine probes. Require direct match of signatures diff --git a/kotlinx-coroutines-debug/src/internal/Attach.kt b/kotlinx-coroutines-debug/src/internal/Attach.kt new file mode 100644 index 0000000000..cd4cc2a52e --- /dev/null +++ b/kotlinx-coroutines-debug/src/internal/Attach.kt @@ -0,0 +1,41 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ +@file:Suppress("unused") +package kotlinx.coroutines.debug.internal + +import net.bytebuddy.* +import net.bytebuddy.agent.* +import net.bytebuddy.dynamic.loading.* + +/* + * This class is used reflectively from kotlinx-coroutines-core when this module is present in the classpath. + * It is a substitute for service loading. + */ +internal class ByteBuddyDynamicAttach : Function1 { + override fun invoke(value: Boolean) { + if (value) attach() else detach() + } + + private fun attach() { + ByteBuddyAgent.install(ByteBuddyAgent.AttachmentProvider.ForEmulatedAttachment.INSTANCE) + val cl = Class.forName("kotlin.coroutines.jvm.internal.DebugProbesKt") + val cl2 = Class.forName("kotlinx.coroutines.debug.DebugProbesKt") + + ByteBuddy() + .redefine(cl2) + .name(cl.name) + .make() + .load(cl.classLoader, ClassReloadingStrategy.fromInstalledAgent()) + } + + private fun detach() { + val cl = Class.forName("kotlin.coroutines.jvm.internal.DebugProbesKt") + val cl2 = Class.forName("kotlinx.coroutines.debug.internal.NoOpProbesKt") + ByteBuddy() + .redefine(cl2) + .name(cl.name) + .make() + .load(cl.classLoader, ClassReloadingStrategy.fromInstalledAgent()) + } +} diff --git a/kotlinx-coroutines-debug/src/junit4/CoroutinesTimeout.kt b/kotlinx-coroutines-debug/src/junit4/CoroutinesTimeout.kt index 0f14ade89c..0510764a0c 100644 --- a/kotlinx-coroutines-debug/src/junit4/CoroutinesTimeout.kt +++ b/kotlinx-coroutines-debug/src/junit4/CoroutinesTimeout.kt @@ -42,7 +42,11 @@ public class CoroutinesTimeout( ) : TestRule { @Suppress("UNUSED") // Binary compatibility - constructor(testTimeoutMs: Long, cancelOnTimeout: Boolean = false) : this(testTimeoutMs, cancelOnTimeout, true) + public constructor(testTimeoutMs: Long, cancelOnTimeout: Boolean = false) : this( + testTimeoutMs, + cancelOnTimeout, + true + ) init { require(testTimeoutMs > 0) { "Expected positive test timeout, but had $testTimeoutMs" } @@ -55,7 +59,7 @@ public class CoroutinesTimeout( DebugProbes.install() } - companion object { + public companion object { /** * Creates [CoroutinesTimeout] rule with the given timeout in seconds. */ diff --git a/kotlinx-coroutines-debug/test/BlockHoundTest.kt b/kotlinx-coroutines-debug/test/BlockHoundTest.kt new file mode 100644 index 0000000000..571daca12f --- /dev/null +++ b/kotlinx-coroutines-debug/test/BlockHoundTest.kt @@ -0,0 +1,95 @@ +package kotlinx.coroutines.debug +import kotlinx.coroutines.* +import kotlinx.coroutines.channels.* +import org.junit.* +import reactor.blockhound.* + +class BlockHoundTest : TestBase() { + + @Before + fun init() { + BlockHound.install() + } + + @Test(expected = BlockingOperationError::class) + fun shouldDetectBlockingInDefault() = runTest { + withContext(Dispatchers.Default) { + Thread.sleep(1) + } + } + + @Test + fun shouldNotDetectBlockingInIO() = runTest { + withContext(Dispatchers.IO) { + Thread.sleep(1) + } + } + + @Test + fun shouldNotDetectNonblocking() = runTest { + withContext(Dispatchers.Default) { + val a = 1 + val b = 2 + assert(a + b == 3) + } + } + + @Test + fun testReusingThreads() = runTest { + val n = 100 + repeat(n) { + async(Dispatchers.IO) { + Thread.sleep(1) + } + } + repeat(n) { + async(Dispatchers.Default) { + } + } + repeat(n) { + async(Dispatchers.IO) { + Thread.sleep(1) + } + } + } + + @Test + fun testChannelsNotBeingConsideredBlocking() = runTest { + withContext(Dispatchers.Default) { + // Copy of kotlinx.coroutines.channels.ArrayChannelTest.testSimple + val q = Channel(1) + check(q.isEmpty) + check(!q.isClosedForReceive) + check(!q.isClosedForSend) + val sender = launch { + q.send(1) + q.send(2) + } + val receiver = launch { + q.receive() == 1 + q.receive() == 2 + } + sender.join() + receiver.join() + } + } + + @Test(expected = BlockingOperationError::class) + fun testReusingThreadsFailure() = runTest { + val n = 100 + repeat(n) { + async(Dispatchers.IO) { + Thread.sleep(1) + } + } + async(Dispatchers.Default) { + Thread.sleep(1) + } + repeat(n) { + async(Dispatchers.IO) { + Thread.sleep(1) + } + } + } + +} diff --git a/kotlinx-coroutines-debug/test/CoroutinesDumpTest.kt b/kotlinx-coroutines-debug/test/CoroutinesDumpTest.kt index 91bd4f287d..fd0279123f 100644 --- a/kotlinx-coroutines-debug/test/CoroutinesDumpTest.kt +++ b/kotlinx-coroutines-debug/test/CoroutinesDumpTest.kt @@ -39,7 +39,7 @@ class CoroutinesDumpTest : DebugTestBase() { @Test fun testRunningCoroutine() = runBlocking { - val deferred = async(Dispatchers.Default) { + val deferred = async(Dispatchers.IO) { activeMethod(shouldSuspend = false) assertTrue(true) } @@ -70,7 +70,7 @@ class CoroutinesDumpTest : DebugTestBase() { @Test fun testRunningCoroutineWithSuspensionPoint() = runBlocking { - val deferred = async(Dispatchers.Default) { + val deferred = async(Dispatchers.IO) { activeMethod(shouldSuspend = true) yield() // tail-call } @@ -100,7 +100,7 @@ class CoroutinesDumpTest : DebugTestBase() { @Test fun testCreationStackTrace() = runBlocking { - val deferred = async(Dispatchers.Default) { + val deferred = async(Dispatchers.IO) { activeMethod(shouldSuspend = true) } @@ -115,21 +115,27 @@ class CoroutinesDumpTest : DebugTestBase() { coroutineThread!!.interrupt() val expected = - ("kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsJvmKt.createCoroutineUnintercepted(IntrinsicsJvm.kt:116)\n" + - "kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:23)\n" + - "kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:109)\n" + - "kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:160)\n" + - "kotlinx.coroutines.BuildersKt__Builders_commonKt.async(Builders.common.kt:88)\n" + - "kotlinx.coroutines.BuildersKt.async(Unknown Source)\n" + - "kotlinx.coroutines.BuildersKt__Builders_commonKt.async\$default(Builders.common.kt:81)\n" + - "kotlinx.coroutines.BuildersKt.async\$default(Unknown Source)\n" + - "kotlinx.coroutines.debug.CoroutinesDumpTest\$testCreationStackTrace\$1.invokeSuspend(CoroutinesDumpTest.kt)").trimStackTrace() - assertTrue(result.startsWith(expected)) + "kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsJvmKt.createCoroutineUnintercepted(IntrinsicsJvm.kt)\n" + + "kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt)\n" + + "kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable\$default(Cancellable.kt)\n" + + "kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt)\n" + + "kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt)\n" + + "kotlinx.coroutines.BuildersKt__Builders_commonKt.async(Builders.common.kt)\n" + + "kotlinx.coroutines.BuildersKt.async(Unknown Source)\n" + + "kotlinx.coroutines.BuildersKt__Builders_commonKt.async\$default(Builders.common.kt)\n" + + "kotlinx.coroutines.BuildersKt.async\$default(Unknown Source)\n" + + "kotlinx.coroutines.debug.CoroutinesDumpTest\$testCreationStackTrace\$1.invokeSuspend(CoroutinesDumpTest.kt)" + if (!result.startsWith(expected)) { + println("=== Actual result") + println(result) + error("Does not start with expected lines") + } + } @Test fun testFinishedCoroutineRemoved() = runBlocking { - val deferred = async(Dispatchers.Default) { + val deferred = async(Dispatchers.IO) { activeMethod(shouldSuspend = true) } @@ -149,7 +155,10 @@ class CoroutinesDumpTest : DebugTestBase() { if (shouldSuspend) yield() notifyCoroutineStarted() while (coroutineContext[Job]!!.isActive) { - runCatching { Thread.sleep(60_000) } + try { + Thread.sleep(60_000) + } catch (_ : InterruptedException) { + } } } diff --git a/kotlinx-coroutines-debug/test/DebugLeaksTest.kt b/kotlinx-coroutines-debug/test/DebugLeaksTest.kt new file mode 100644 index 0000000000..a43b33be67 --- /dev/null +++ b/kotlinx-coroutines-debug/test/DebugLeaksTest.kt @@ -0,0 +1,57 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +import kotlinx.coroutines.* +import kotlinx.coroutines.debug.* +import kotlinx.coroutines.debug.internal.* +import org.junit.* + +/** + * This is fast but fragile version of [DebugLeaksStressTest] that check reachability of a captured object + * in [DebugProbesImpl] via [FieldWalker]. + */ +@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") +class DebugLeaksTest : DebugTestBase() { + private class Captured + + @Test + fun testIteratorLeak() { + val captured = Captured() + iterator { yield(captured) } + assertNoCapturedReference() + } + + @Test + fun testLazyGlobalCoroutineLeak() { + val captured = Captured() + GlobalScope.launch(start = CoroutineStart.LAZY) { println(captured) } + assertNoCapturedReference() + } + + @Test + fun testLazyCancelledChildCoroutineLeak() = runTest { + val captured = Captured() + coroutineScope { + val child = launch(start = CoroutineStart.LAZY) { println(captured) } + child.cancel() + } + assertNoCapturedReference() + } + + @Test + fun testAbandonedGlobalCoroutineLeak() { + val captured = Captured() + GlobalScope.launch { + suspendForever() + println(captured) + } + assertNoCapturedReference() + } + + private suspend fun suspendForever() = suspendCancellableCoroutine { } + + private fun assertNoCapturedReference() { + FieldWalker.assertReachableCount(0, DebugProbesImpl, rootStatics = true) { it is Captured } + } +} \ No newline at end of file diff --git a/kotlinx-coroutines-debug/test/DebugProbesTest.kt b/kotlinx-coroutines-debug/test/DebugProbesTest.kt index 24050e563c..3b32db3a5a 100644 --- a/kotlinx-coroutines-debug/test/DebugProbesTest.kt +++ b/kotlinx-coroutines-debug/test/DebugProbesTest.kt @@ -40,24 +40,25 @@ class DebugProbesTest : DebugTestBase() { val deferred = createDeferred() val traces = listOf( "java.util.concurrent.ExecutionException\n" + - "\tat kotlinx.coroutines.debug.DebugProbesTest\$createDeferred\$1.invokeSuspend(DebugProbesTest.kt:16)\n" + + "\tat kotlinx.coroutines.debug.DebugProbesTest\$createDeferred\$1.invokeSuspend(DebugProbesTest.kt)\n" + "\t(Coroutine boundary)\n" + "\tat kotlinx.coroutines.DeferredCoroutine.await\$suspendImpl(Builders.common.kt)\n" + - "\tat kotlinx.coroutines.debug.DebugProbesTest.oneMoreNestedMethod(DebugProbesTest.kt:71)\n" + - "\tat kotlinx.coroutines.debug.DebugProbesTest.nestedMethod(DebugProbesTest.kt:66)\n" + + "\tat kotlinx.coroutines.debug.DebugProbesTest.oneMoreNestedMethod(DebugProbesTest.kt)\n" + + "\tat kotlinx.coroutines.debug.DebugProbesTest.nestedMethod(DebugProbesTest.kt)\n" + "\t(Coroutine creation stacktrace)\n" + - "\tat kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsJvmKt.createCoroutineUnintercepted(IntrinsicsJvm.kt:116)\n" + - "\tat kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:23)\n" + - "\tat kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:99)\n" + - "\tat kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:148)\n" + - "\tat kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:45)\n" + + "\tat kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsJvmKt.createCoroutineUnintercepted(IntrinsicsJvm.kt)\n" + + "\tat kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt)\n" + + "\tat kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable\$default(Cancellable.kt)\n" + + "\tat kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt)\n" + + "\tat kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt)\n" + + "\tat kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt)\n" + "\tat kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)\n" + - "\tat kotlinx.coroutines.TestBase.runTest(TestBase.kt:138)\n" + - "\tat kotlinx.coroutines.TestBase.runTest\$default(TestBase.kt:19)\n" + - "\tat kotlinx.coroutines.debug.DebugProbesTest.testAsyncWithProbes(DebugProbesTest.kt:38)", + "\tat kotlinx.coroutines.TestBase.runTest(TestBase.kt)\n" + + "\tat kotlinx.coroutines.TestBase.runTest\$default(TestBase.kt)\n" + + "\tat kotlinx.coroutines.debug.DebugProbesTest.testAsyncWithProbes(DebugProbesTest.kt)", "Caused by: java.util.concurrent.ExecutionException\n" + - "\tat kotlinx.coroutines.debug.DebugProbesTest\$createDeferred\$1.invokeSuspend(DebugProbesTest.kt:16)\n" + - "\tat kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)\n") + "\tat kotlinx.coroutines.debug.DebugProbesTest\$createDeferred\$1.invokeSuspend(DebugProbesTest.kt)\n" + + "\tat kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt)\n") nestedMethod(deferred, traces) deferred.join() } diff --git a/kotlinx-coroutines-debug/test/DumpWithoutCreationStackTraceTest.kt b/kotlinx-coroutines-debug/test/DumpWithoutCreationStackTraceTest.kt index 89782e4c45..6e405ca293 100644 --- a/kotlinx-coroutines-debug/test/DumpWithoutCreationStackTraceTest.kt +++ b/kotlinx-coroutines-debug/test/DumpWithoutCreationStackTraceTest.kt @@ -21,13 +21,13 @@ class DumpWithoutCreationStackTraceTest : DebugTestBase() { yield() verifyDump( "Coroutine \"coroutine#1\":BlockingCoroutine{Active}@70d1cb56, state: RUNNING\n" + - "\tat java.lang.Thread.getStackTrace(Thread.java:1559)\n" + - "\tat kotlinx.coroutines.debug.internal.DebugProbesImpl.enhanceStackTraceWithThreadDump(DebugProbesImpl.kt:188)\n" + - "\tat kotlinx.coroutines.debug.internal.DebugProbesImpl.dumpCoroutinesSynchronized(DebugProbesImpl.kt:153)\n" + - "\tat kotlinx.coroutines.debug.internal.DebugProbesImpl.dumpCoroutines(DebugProbesImpl.kt:141)", + "\tat java.lang.Thread.getStackTrace(Thread.java)\n" + + "\tat kotlinx.coroutines.debug.internal.DebugProbesImpl.enhanceStackTraceWithThreadDumpImpl(DebugProbesImpl.kt)\n" + + "\tat kotlinx.coroutines.debug.internal.DebugProbesImpl.dumpCoroutinesSynchronized(DebugProbesImpl.kt)\n" + + "\tat kotlinx.coroutines.debug.internal.DebugProbesImpl.dumpCoroutines(DebugProbesImpl.kt)", "Coroutine \"coroutine#2\":DeferredCoroutine{Active}@383fa309, state: SUSPENDED\n" + - "\tat kotlinx.coroutines.debug.DumpWithoutCreationStackTraceTest\$createActiveDeferred\$1.invokeSuspend(DumpWithoutCreationStackTraceTest.kt:63)" + "\tat kotlinx.coroutines.debug.DumpWithoutCreationStackTraceTest\$createActiveDeferred\$1.invokeSuspend(DumpWithoutCreationStackTraceTest.kt)" ) deferred.cancelAndJoin() } diff --git a/kotlinx-coroutines-debug/test/LazyCoroutineTest.kt b/kotlinx-coroutines-debug/test/LazyCoroutineTest.kt new file mode 100644 index 0000000000..c872b6a53d --- /dev/null +++ b/kotlinx-coroutines-debug/test/LazyCoroutineTest.kt @@ -0,0 +1,23 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ +package kotlinx.coroutines.debug + +import kotlinx.coroutines.* +import org.junit.Test +import kotlin.test.* + +class LazyCoroutineTest : DebugTestBase() { + + @Test + fun testLazyCompletedCoroutine() = runTest { + val job = launch(start = CoroutineStart.LAZY) {} + job.invokeOnCompletion { expect(2) } + expect(1) + job.cancelAndJoin() + expect(3) + assertEquals(1, DebugProbes.dumpCoroutinesInfo().size) // Outer runBlocking + verifyPartialDump(1, "BlockingCoroutine{Active}") + finish(4) + } +} diff --git a/kotlinx-coroutines-debug/test/RunningThreadStackMergeTest.kt b/kotlinx-coroutines-debug/test/RunningThreadStackMergeTest.kt index c0b7f50134..e7fdeede79 100644 --- a/kotlinx-coroutines-debug/test/RunningThreadStackMergeTest.kt +++ b/kotlinx-coroutines-debug/test/RunningThreadStackMergeTest.kt @@ -1,9 +1,11 @@ /* * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ +@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") package kotlinx.coroutines.debug import kotlinx.coroutines.* +import kotlinx.coroutines.debug.internal.* import org.junit.Test import java.util.concurrent.* import kotlin.test.* @@ -133,7 +135,7 @@ class RunningThreadStackMergeTest : DebugTestBase() { } private fun CoroutineScope.launchEscapingCoroutineWithoutContext() { - launch(Dispatchers.Default) { + launch(Dispatchers.IO) { suspendingFunctionWithoutContext() assertTrue(true) } @@ -148,16 +150,16 @@ class RunningThreadStackMergeTest : DebugTestBase() { @Test fun testRunBlocking() = runBlocking { verifyDump("Coroutine \"coroutine#1\":BlockingCoroutine{Active}@4bcd176c, state: RUNNING\n" + - "\tat java.lang.Thread.getStackTrace(Thread.java:1552)\n" + - "\tat kotlinx.coroutines.debug.internal.DebugProbesImpl.enhanceStackTraceWithThreadDump(DebugProbesImpl.kt:147)\n" + - "\tat kotlinx.coroutines.debug.internal.DebugProbesImpl.dumpCoroutinesSynchronized(DebugProbesImpl.kt:122)\n" + - "\tat kotlinx.coroutines.debug.internal.DebugProbesImpl.dumpCoroutines(DebugProbesImpl.kt:109)\n" + - "\tat kotlinx.coroutines.debug.DebugProbes.dumpCoroutines(DebugProbes.kt:122)\n" + - "\tat kotlinx.coroutines.debug.StracktraceUtilsKt.verifyDump(StracktraceUtils.kt)\n" + - "\tat kotlinx.coroutines.debug.StracktraceUtilsKt.verifyDump\$default(StracktraceUtils.kt)\n" + - "\tat kotlinx.coroutines.debug.RunningThreadStackMergeTest\$testRunBlocking\$1.invokeSuspend(RunningThreadStackMergeTest.kt:112)\n" + + "\tat java.lang.Thread.getStackTrace(Thread.java)\n" + + "\tat kotlinx.coroutines.debug.internal.DebugProbesImpl.enhanceStackTraceWithThreadDumpImpl(DebugProbesImpl.kt)\n" + + "\tat kotlinx.coroutines.debug.internal.DebugProbesImpl.dumpCoroutinesSynchronized(DebugProbesImpl.kt)\n" + + "\tat kotlinx.coroutines.debug.internal.DebugProbesImpl.dumpCoroutines(DebugProbesImpl.kt)\n" + + "\tat kotlinx.coroutines.debug.DebugProbes.dumpCoroutines(DebugProbes.kt)\n" + + "\tat kotlinx.coroutines.debug.StacktraceUtilsKt.verifyDump(StacktraceUtils.kt)\n" + + "\tat kotlinx.coroutines.debug.StacktraceUtilsKt.verifyDump\$default(StacktraceUtils.kt)\n" + + "\tat kotlinx.coroutines.debug.RunningThreadStackMergeTest\$testRunBlocking\$1.invokeSuspend(RunningThreadStackMergeTest.kt)\n" + "\t(Coroutine creation stacktrace)\n" + - "\tat kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsJvmKt.createCoroutineUnintercepted(IntrinsicsJvm.kt:116)\n") + "\tat kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsJvmKt.createCoroutineUnintercepted(IntrinsicsJvm.kt)\n") } @@ -175,10 +177,9 @@ class RunningThreadStackMergeTest : DebugTestBase() { fun testActiveThread() = runBlocking { launchCoroutine() awaitCoroutineStarted() - val info = DebugProbes.dumpCoroutinesInfo().find { it.state == State.RUNNING } + val info = DebugProbesImpl.dumpDebuggerInfo().find { it.state == "RUNNING" } assertNotNull(info) - @Suppress("INVISIBLE_MEMBER") // IDEA bug - assertNotNull(info.lastObservedThread) + assertNotNull(info.lastObservedThreadName) coroutineBlocker.await() } } diff --git a/kotlinx-coroutines-debug/test/StracktraceUtils.kt b/kotlinx-coroutines-debug/test/StacktraceUtils.kt similarity index 76% rename from kotlinx-coroutines-debug/test/StracktraceUtils.kt rename to kotlinx-coroutines-debug/test/StacktraceUtils.kt index 12a39c0041..8c591ebd44 100644 --- a/kotlinx-coroutines-debug/test/StracktraceUtils.kt +++ b/kotlinx-coroutines-debug/test/StacktraceUtils.kt @@ -13,7 +13,7 @@ public fun String.trimStackTrace(): String = .replace(Regex("#[0-9]+"), "") .replace(Regex("(?<=\tat )[^\n]*/"), "") .replace(Regex("\t"), "") - .replace("sun.misc.Unsafe.park", "jdk.internal.misc.Unsafe.park") // JDK8->JDK11 + .replace("sun.misc.Unsafe.", "jdk.internal.misc.Unsafe.") // JDK8->JDK11 .applyBackspace() public fun String.applyBackspace(): String { @@ -62,6 +62,31 @@ public fun verifyDump(vararg traces: String, ignoredCoroutine: String? = null, f } } +/** Clean the stacktraces from artifacts of BlockHound instrumentation + * + * BlockHound works by switching a native call by a class generated with ByteBuddy, which, if the blocking + * call is allowed in this context, in turn calls the real native call that is now available under a + * different name. + * + * The traces thus undergo the following two changes when the execution is instrumented: + * - The original native call is replaced with a non-native one with the same FQN, and + * - An additional native call is placed on top of the stack, with the original name that also has + * `$$BlockHound$$_` prepended at the last component. + */ +private fun cleanBlockHoundTraces(frames: List): List { + var result = mutableListOf() + val blockHoundSubstr = "\$\$BlockHound\$\$_" + var i = 0 + while (i < frames.size) { + result.add(frames[i].replace(blockHoundSubstr, "")) + if (frames[i].contains(blockHoundSubstr)) { + i += 1 + } + i += 1 + } + return result +} + public fun verifyDump(vararg traces: String, ignoredCoroutine: String? = null) { val baos = ByteArrayOutputStream() DebugProbes.dumpCoroutines(PrintStream(baos)) @@ -85,7 +110,7 @@ public fun verifyDump(vararg traces: String, ignoredCoroutine: String? = null) { expected.withIndex().forEach { (index, trace) -> val actualTrace = actual[index].trimStackTrace().sanitizeAddresses() val expectedTrace = trace.trimStackTrace().sanitizeAddresses() - val actualLines = actualTrace.split("\n") + val actualLines = cleanBlockHoundTraces(actualTrace.split("\n")) val expectedLines = expectedTrace.split("\n") for (i in expectedLines.indices) { assertEquals(expectedLines[i], actualLines[i]) diff --git a/kotlinx-coroutines-debug/test/WithContextUndispatchedTest.kt b/kotlinx-coroutines-debug/test/WithContextUndispatchedTest.kt new file mode 100644 index 0000000000..e803c980cf --- /dev/null +++ b/kotlinx-coroutines-debug/test/WithContextUndispatchedTest.kt @@ -0,0 +1,67 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ +package kotlinx.coroutines.debug + +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* +import org.junit.* + +// Test four our internal optimization "withContextUndispatched" +class WithContextUndispatchedTest : DebugTestBase() { + + @Test + fun testZip() = runTest { + val f1 = flowOf("a") + val f2 = flow { + nestedEmit() + yield() + } + f1.zip(f2) { i, j -> i + j }.collect { + bar(false) + } + } + + private suspend fun FlowCollector.nestedEmit() { + emit(1) + emit(2) + } + + @Test + fun testUndispatchedFlowOn() = runTest { + val flow = flowOf(1, 2, 3).flowOn(CoroutineName("...")) + flow.collect { + bar(true) + } + } + + @Test + fun testUndispatchedFlowOnWithNestedCaller() = runTest { + val flow = flow { + nestedEmit() + }.flowOn(CoroutineName("...")) + flow.collect { + bar(true) + } + } + + private suspend fun bar(forFlowOn: Boolean) { + yield() + if (forFlowOn) { + verifyFlowOn() + } else { + verifyZip() + } + yield() + } + + private suspend fun verifyFlowOn() { + yield() // suspend + verifyPartialDump(1, "verifyFlowOn", "bar") + } + + private suspend fun verifyZip() { + yield() // suspend + verifyPartialDump(2, "verifyZip", "bar", "nestedEmit") + } +} diff --git a/kotlinx-coroutines-test/README.md b/kotlinx-coroutines-test/README.md index 2b3ed25d06..0b1c23938e 100644 --- a/kotlinx-coroutines-test/README.md +++ b/kotlinx-coroutines-test/README.md @@ -9,7 +9,7 @@ This package provides testing utilities for effectively testing coroutines. Add `kotlinx-coroutines-test` to your project test dependencies: ``` dependencies { - testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.5' + testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.4.0' } ``` diff --git a/kotlinx-coroutines-test/api/kotlinx-coroutines-test.api b/kotlinx-coroutines-test/api/kotlinx-coroutines-test.api index e3b1f73e4f..c99ec5cbf1 100644 --- a/kotlinx-coroutines-test/api/kotlinx-coroutines-test.api +++ b/kotlinx-coroutines-test/api/kotlinx-coroutines-test.api @@ -25,7 +25,7 @@ public final class kotlinx/coroutines/test/TestCoroutineDispatcher : kotlinx/cor public fun dispatch (Lkotlin/coroutines/CoroutineContext;Ljava/lang/Runnable;)V public fun dispatchYield (Lkotlin/coroutines/CoroutineContext;Ljava/lang/Runnable;)V public fun getCurrentTime ()J - public fun invokeOnTimeout (JLjava/lang/Runnable;)Lkotlinx/coroutines/DisposableHandle; + public fun invokeOnTimeout (JLjava/lang/Runnable;Lkotlin/coroutines/CoroutineContext;)Lkotlinx/coroutines/DisposableHandle; public fun pauseDispatcher ()V public fun pauseDispatcher (Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun resumeDispatcher ()V diff --git a/kotlinx-coroutines-test/build.gradle b/kotlinx-coroutines-test/build.gradle deleted file mode 100644 index e13946fbb4..0000000000 --- a/kotlinx-coroutines-test/build.gradle +++ /dev/null @@ -1,3 +0,0 @@ -dependencies { - implementation project(":kotlinx-coroutines-debug") -} diff --git a/kotlinx-coroutines-test/build.gradle.kts b/kotlinx-coroutines-test/build.gradle.kts new file mode 100644 index 0000000000..825c95f4c8 --- /dev/null +++ b/kotlinx-coroutines-test/build.gradle.kts @@ -0,0 +1,3 @@ +dependencies { + implementation(project(":kotlinx-coroutines-debug")) +} diff --git a/kotlinx-coroutines-test/src/TestBuilders.kt b/kotlinx-coroutines-test/src/TestBuilders.kt index 3b8efb9fe1..88fa01b7b8 100644 --- a/kotlinx-coroutines-test/src/TestBuilders.kt +++ b/kotlinx-coroutines-test/src/TestBuilders.kt @@ -69,13 +69,15 @@ private fun CoroutineContext.activeJobs(): Set { */ // todo: need documentation on how this extension is supposed to be used @ExperimentalCoroutinesApi // Since 1.2.1, tentatively till 1.3.0 -public fun TestCoroutineScope.runBlockingTest(block: suspend TestCoroutineScope.() -> Unit) = runBlockingTest(coroutineContext, block) +public fun TestCoroutineScope.runBlockingTest(block: suspend TestCoroutineScope.() -> Unit): Unit = + runBlockingTest(coroutineContext, block) /** * Convenience method for calling [runBlockingTest] on an existing [TestCoroutineDispatcher]. */ @ExperimentalCoroutinesApi // Since 1.2.1, tentatively till 1.3.0 -public fun TestCoroutineDispatcher.runBlockingTest(block: suspend TestCoroutineScope.() -> Unit) = runBlockingTest(this, block) +public fun TestCoroutineDispatcher.runBlockingTest(block: suspend TestCoroutineScope.() -> Unit): Unit = + runBlockingTest(this, block) private fun CoroutineContext.checkArguments(): Pair { // TODO optimize it diff --git a/kotlinx-coroutines-test/src/TestCoroutineDispatcher.kt b/kotlinx-coroutines-test/src/TestCoroutineDispatcher.kt index aab869c98b..cad2636f97 100644 --- a/kotlinx-coroutines-test/src/TestCoroutineDispatcher.kt +++ b/kotlinx-coroutines-test/src/TestCoroutineDispatcher.kt @@ -65,7 +65,7 @@ public class TestCoroutineDispatcher: CoroutineDispatcher(), Delay, DelayControl } /** @suppress */ - override fun invokeOnTimeout(timeMillis: Long, block: Runnable): DisposableHandle { + override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle { val node = postDelayed(block, timeMillis) return object : DisposableHandle { override fun dispose() { @@ -105,7 +105,7 @@ public class TestCoroutineDispatcher: CoroutineDispatcher(), Delay, DelayControl } /** @suppress */ - override val currentTime get() = _time.value + override val currentTime: Long get() = _time.value /** @suppress */ override fun advanceTimeBy(delayTimeMillis: Long): Long { @@ -136,7 +136,7 @@ public class TestCoroutineDispatcher: CoroutineDispatcher(), Delay, DelayControl } /** @suppress */ - override fun runCurrent() = doActionsUntil(currentTime) + override fun runCurrent(): Unit = doActionsUntil(currentTime) /** @suppress */ override suspend fun pauseDispatcher(block: suspend () -> Unit) { diff --git a/kotlinx-coroutines-test/src/TestCoroutineExceptionHandler.kt b/kotlinx-coroutines-test/src/TestCoroutineExceptionHandler.kt index f585aa03ab..ed08fbcdd4 100644 --- a/kotlinx-coroutines-test/src/TestCoroutineExceptionHandler.kt +++ b/kotlinx-coroutines-test/src/TestCoroutineExceptionHandler.kt @@ -48,7 +48,7 @@ public class TestCoroutineExceptionHandler : } /** @suppress **/ - override val uncaughtExceptions + override val uncaughtExceptions: List get() = synchronized(_exceptions) { _exceptions.toList() } /** @suppress **/ diff --git a/kotlinx-coroutines-test/src/internal/MainTestDispatcher.kt b/kotlinx-coroutines-test/src/internal/MainTestDispatcher.kt index c18e4108bb..baa1aa5fd2 100644 --- a/kotlinx-coroutines-test/src/internal/MainTestDispatcher.kt +++ b/kotlinx-coroutines-test/src/internal/MainTestDispatcher.kt @@ -46,8 +46,8 @@ internal class TestMainDispatcher(private val mainFactory: MainDispatcherFactory delay.delay(time) } - override fun invokeOnTimeout(timeMillis: Long, block: Runnable): DisposableHandle { - return delay.invokeOnTimeout(timeMillis, block) + override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle { + return delay.invokeOnTimeout(timeMillis, block, context) } public fun setDispatcher(dispatcher: CoroutineDispatcher) { diff --git a/kotlinx-coroutines-test/test/TestRunBlockingOrderTest.kt b/kotlinx-coroutines-test/test/TestRunBlockingOrderTest.kt index 0013a654a6..e21c82b95c 100644 --- a/kotlinx-coroutines-test/test/TestRunBlockingOrderTest.kt +++ b/kotlinx-coroutines-test/test/TestRunBlockingOrderTest.kt @@ -54,11 +54,11 @@ class TestRunBlockingOrderTest : TestBase() { } @Test - fun testInfiniteDelay() = runBlockingTest { + fun testVeryLongDelay() = runBlockingTest { expect(1) delay(100) // move time forward a bit some that naive time + delay gives an overflow launch { - delay(Long.MAX_VALUE) // infinite delay + delay(Long.MAX_VALUE / 2) // very long delay finish(4) } launch { diff --git a/publication-validator/README.md b/publication-validator/README.md deleted file mode 100644 index a60ff00e3c..0000000000 --- a/publication-validator/README.md +++ /dev/null @@ -1,13 +0,0 @@ -# Publication validator - -This is a supplementary subproject of kotlinx.coroutines that provides a new -task, `testPublishing`, to test its publication correctness. - -The tests are the following: -* `NpmPublicationValidator` tests that version of NPM artifact is correct and that it has neither source nor package dependencies on atomicfu -* `MavenPublicationValidator` depends on the published artifacts and tests artifacts binary content and absence of atomicfu in the classpath - -To test publication, one needs to run gradle with `-PdryRun=true`, and the -task that actually does the testing is `publication-validator:test`. -`-PdryRun` affects `npmPublish` so that it only provides a packed publication -and does not in fact attempt to send the build for publication. diff --git a/publication-validator/build.gradle b/publication-validator/build.gradle deleted file mode 100644 index a22ccf46d2..0000000000 --- a/publication-validator/build.gradle +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -apply from: rootProject.file("gradle/compile-jvm.gradle") - -repositories { - mavenLocal() - mavenCentral() -} - -dependencies { - testCompile "org.jetbrains.kotlin:kotlin-stdlib-jdk8" - testCompile 'junit:junit:4.12' - testCompile 'org.apache.commons:commons-compress:1.18' - testCompile 'com.google.code.gson:gson:2.8.5' - testCompile project(':kotlinx-coroutines-core') - testCompile project(':kotlinx-coroutines-android') -} - -compileTestKotlin { - kotlinOptions.jvmTarget = "1.8" -} - -def dryRunNpm = properties['dryRun'] - -test { - onlyIf { dryRunNpm == "true" } // so that we don't accidentally publish anything, especially before the test - doFirst { println "Verifying publishing version $version" } // all modules share the same version - environment "projectRoot", project.rootDir - environment "deployVersion", version - if (dryRunNpm == "true") { // `onlyIf` only affects execution of the task, not the dependency subtree - dependsOn(project(':').getTasksByName("publishNpm", true) + - project(':').getTasksByName("publishToMavenLocal", true)) - dependsOn.remove(project(':').getTasksByName("dokka", true)) - } -} diff --git a/reactive/README.md b/reactive/README.md index 8679a2b078..35706ac96e 100644 --- a/reactive/README.md +++ b/reactive/README.md @@ -8,3 +8,4 @@ Module name below corresponds to the artifact name in Maven/Gradle. * [kotlinx-coroutines-reactive](kotlinx-coroutines-reactive/README.md) -- utilities for [Reactive Streams](https://www.reactive-streams.org) * [kotlinx-coroutines-reactor](kotlinx-coroutines-reactor/README.md) -- utilities for [Reactor](https://projectreactor.io) * [kotlinx-coroutines-rx2](kotlinx-coroutines-rx2/README.md) -- utilities for [RxJava 2.x](https://github.com/ReactiveX/RxJava) +* [kotlinx-coroutines-rx3](kotlinx-coroutines-rx3/README.md) -- utilities for [RxJava 3.x](https://github.com/ReactiveX/RxJava) diff --git a/reactive/kotlinx-coroutines-jdk9/api/kotlinx-coroutines-jdk9.api b/reactive/kotlinx-coroutines-jdk9/api/kotlinx-coroutines-jdk9.api index d4bc1698ef..1f5bdec7d0 100644 --- a/reactive/kotlinx-coroutines-jdk9/api/kotlinx-coroutines-jdk9.api +++ b/reactive/kotlinx-coroutines-jdk9/api/kotlinx-coroutines-jdk9.api @@ -15,6 +15,8 @@ public final class kotlinx/coroutines/jdk9/PublishKt { public final class kotlinx/coroutines/jdk9/ReactiveFlowKt { public static final fun asFlow (Ljava/util/concurrent/Flow$Publisher;)Lkotlinx/coroutines/flow/Flow; public static final fun asPublisher (Lkotlinx/coroutines/flow/Flow;)Ljava/util/concurrent/Flow$Publisher; + public static final fun asPublisher (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;)Ljava/util/concurrent/Flow$Publisher; + public static synthetic fun asPublisher$default (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;ILjava/lang/Object;)Ljava/util/concurrent/Flow$Publisher; public static final fun collect (Ljava/util/concurrent/Flow$Publisher;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; } diff --git a/reactive/kotlinx-coroutines-jdk9/build.gradle b/reactive/kotlinx-coroutines-jdk9/build.gradle deleted file mode 100644 index 8737e8ed6d..0000000000 --- a/reactive/kotlinx-coroutines-jdk9/build.gradle +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ -targetCompatibility = 9 - -dependencies { - compile project(":kotlinx-coroutines-reactive") - compile "org.reactivestreams:reactive-streams-flow-adapters:$reactive_streams_version" -} - -compileTestKotlin { - kotlinOptions.jvmTarget = "9" -} - -compileKotlin { - kotlinOptions.jvmTarget = "9" -} - -tasks.withType(dokka.getClass()) { - externalDocumentationLink { - url = new URL("https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/Flow.html") - packageListUrl = projectDir.toPath().resolve("package.list").toUri().toURL() - } -} diff --git a/reactive/kotlinx-coroutines-jdk9/build.gradle.kts b/reactive/kotlinx-coroutines-jdk9/build.gradle.kts new file mode 100644 index 0000000000..c721746f3b --- /dev/null +++ b/reactive/kotlinx-coroutines-jdk9/build.gradle.kts @@ -0,0 +1,22 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +dependencies { + compile(project(":kotlinx-coroutines-reactive")) + compile("org.reactivestreams:reactive-streams-flow-adapters:${version("reactive_streams")}") +} + +tasks { + compileKotlin { + kotlinOptions.jvmTarget = "9" + } + + compileTestKotlin { + kotlinOptions.jvmTarget = "9" + } +} + +externalDocumentationLink( + url = "https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/Flow.html" +) diff --git a/reactive/kotlinx-coroutines-jdk9/src/Publish.kt b/reactive/kotlinx-coroutines-jdk9/src/Publish.kt index d274083668..6fd9a5e75b 100644 --- a/reactive/kotlinx-coroutines-jdk9/src/Publish.kt +++ b/reactive/kotlinx-coroutines-jdk9/src/Publish.kt @@ -28,7 +28,7 @@ import org.reactivestreams.FlowAdapters * **Note: This is an experimental api.** Behaviour of publishers that work as children in a parent scope with respect * to cancellation and error handling may change in the future. */ -@ExperimentalCoroutinesApi +@ExperimentalCoroutinesApi // Since 1.3.x public fun flowPublish( context: CoroutineContext = EmptyCoroutineContext, @BuilderInference block: suspend ProducerScope.() -> Unit diff --git a/reactive/kotlinx-coroutines-jdk9/src/ReactiveFlow.kt b/reactive/kotlinx-coroutines-jdk9/src/ReactiveFlow.kt index 6568c73a4a..5d546dffd3 100644 --- a/reactive/kotlinx-coroutines-jdk9/src/ReactiveFlow.kt +++ b/reactive/kotlinx-coroutines-jdk9/src/ReactiveFlow.kt @@ -4,12 +4,14 @@ package kotlinx.coroutines.jdk9 +import kotlinx.coroutines.* import kotlinx.coroutines.flow.* import kotlinx.coroutines.reactive.asFlow import kotlinx.coroutines.reactive.asPublisher import kotlinx.coroutines.reactive.collect +import org.reactivestreams.* +import kotlin.coroutines.* import java.util.concurrent.Flow as JFlow -import org.reactivestreams.FlowAdapters /** * Transforms the given reactive [Publisher] into [Flow]. @@ -25,9 +27,15 @@ public fun JFlow.Publisher.asFlow(): Flow = /** * Transforms the given flow to a reactive specification compliant [Publisher]. + * + * An optional [context] can be specified to control the execution context of calls to [Subscriber] methods. + * You can set a [CoroutineDispatcher] to confine them to a specific thread and/or various [ThreadContextElement] to + * inject additional context into the caller thread. By default, the [Unconfined][Dispatchers.Unconfined] dispatcher + * is used, so calls are performed from an arbitrary thread. */ -public fun Flow.asPublisher(): JFlow.Publisher { - val reactivePublisher : org.reactivestreams.Publisher = this.asPublisher() +@JvmOverloads // binary compatibility +public fun Flow.asPublisher(context: CoroutineContext = EmptyCoroutineContext): JFlow.Publisher { + val reactivePublisher : org.reactivestreams.Publisher = this.asPublisher(context) return FlowAdapters.toFlowPublisher(reactivePublisher) } @@ -35,5 +43,5 @@ public fun Flow.asPublisher(): JFlow.Publisher { * Subscribes to this [Publisher] and performs the specified action for each received element. * Cancels subscription if any exception happens during collect. */ -public suspend inline fun JFlow.Publisher.collect(action: (T) -> Unit) = +public suspend inline fun JFlow.Publisher.collect(action: (T) -> Unit): Unit = FlowAdapters.toPublisher(this).collect(action) diff --git a/reactive/kotlinx-coroutines-jdk9/test/FlowAsPublisherTest.kt b/reactive/kotlinx-coroutines-jdk9/test/FlowAsPublisherTest.kt index 8017ee5b4f..488695dea2 100644 --- a/reactive/kotlinx-coroutines-jdk9/test/FlowAsPublisherTest.kt +++ b/reactive/kotlinx-coroutines-jdk9/test/FlowAsPublisherTest.kt @@ -16,10 +16,10 @@ class FlowAsPublisherTest : TestBase() { fun testErrorOnCancellationIsReported() { expect(1) flow { - emit(2) try { - hang { expect(3) } + emit(2) } finally { + expect(3) throw TestException() } }.asPublisher().subscribe(object : JFlow.Subscriber { @@ -52,12 +52,11 @@ class FlowAsPublisherTest : TestBase() { expect(1) flow { emit(2) - hang { expect(3) } }.asPublisher().subscribe(object : JFlow.Subscriber { private lateinit var subscription: JFlow.Subscription override fun onComplete() { - expect(4) + expect(3) } override fun onSubscribe(s: JFlow.Subscription?) { @@ -74,6 +73,6 @@ class FlowAsPublisherTest : TestBase() { expectUnreached() } }) - finish(5) + finish(4) } } diff --git a/reactive/kotlinx-coroutines-reactive/README.md b/reactive/kotlinx-coroutines-reactive/README.md index b38202bbdf..aed262263d 100644 --- a/reactive/kotlinx-coroutines-reactive/README.md +++ b/reactive/kotlinx-coroutines-reactive/README.md @@ -6,14 +6,14 @@ Coroutine builders: | **Name** | **Result** | **Scope** | **Description** | --------------- | ----------------------------- | ---------------- | --------------- -| [publish] | `Publisher` | [ProducerScope] | Cold reactive publisher that starts coroutine on subscribe +| [kotlinx.coroutines.reactive.publish] | `Publisher` | [ProducerScope] | Cold reactive publisher that starts the coroutine on subscribe Integration with [Flow]: | **Name** | **Result** | **Description** | --------------- | -------------- | --------------- -| [Publisher.asFlow] | `Flow` | Converts the given publisher to flow -| [Flow.asPublisher] | `Publisher` | Converts the given flow to the TCK-compliant publisher +| [Publisher.asFlow] | `Flow` | Converts the given publisher to a flow +| [Flow.asPublisher] | `Publisher` | Converts the given flow to a TCK-compliant publisher If these adapters are used along with `kotlinx-coroutines-reactor` in the classpath, then Reactor's `Context` is properly propagated as coroutine context element (`ReactorContext`) and vice versa. @@ -37,7 +37,7 @@ Suspending extension functions and suspending iteration: [ProducerScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.channels/-producer-scope/index.html -[publish]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-reactive/kotlinx.coroutines.reactive/publish.html +[kotlinx.coroutines.reactive.publish]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-reactive/kotlinx.coroutines.reactive/publish.html [Publisher.asFlow]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-reactive/kotlinx.coroutines.reactive/org.reactivestreams.-publisher/as-flow.html [Flow.asPublisher]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-reactive/kotlinx.coroutines.reactive/kotlinx.coroutines.flow.-flow/as-publisher.html [org.reactivestreams.Publisher.awaitFirst]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-reactive/kotlinx.coroutines.reactive/org.reactivestreams.-publisher/await-first.html diff --git a/reactive/kotlinx-coroutines-reactive/api/kotlinx-coroutines-reactive.api b/reactive/kotlinx-coroutines-reactive/api/kotlinx-coroutines-reactive.api index bed065d582..961fdbe238 100644 --- a/reactive/kotlinx-coroutines-reactive/api/kotlinx-coroutines-reactive.api +++ b/reactive/kotlinx-coroutines-reactive/api/kotlinx-coroutines-reactive.api @@ -5,6 +5,9 @@ public final class kotlinx/coroutines/reactive/AwaitKt { public static final fun awaitFirstOrNull (Lorg/reactivestreams/Publisher;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static final fun awaitLast (Lorg/reactivestreams/Publisher;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static final fun awaitSingle (Lorg/reactivestreams/Publisher;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun awaitSingleOrDefault (Lorg/reactivestreams/Publisher;Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun awaitSingleOrElse (Lorg/reactivestreams/Publisher;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun awaitSingleOrNull (Lorg/reactivestreams/Publisher;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; } public final class kotlinx/coroutines/reactive/ChannelKt { @@ -32,7 +35,7 @@ public final class kotlinx/coroutines/reactive/FlowKt { public final class kotlinx/coroutines/reactive/FlowSubscription : kotlinx/coroutines/AbstractCoroutine, org/reactivestreams/Subscription { public final field flow Lkotlinx/coroutines/flow/Flow; public final field subscriber Lorg/reactivestreams/Subscriber; - public fun (Lkotlinx/coroutines/flow/Flow;Lorg/reactivestreams/Subscriber;)V + public fun (Lkotlinx/coroutines/flow/Flow;Lorg/reactivestreams/Subscriber;Lkotlin/coroutines/CoroutineContext;)V public fun cancel ()V public fun request (J)V } @@ -65,5 +68,7 @@ public final class kotlinx/coroutines/reactive/PublisherCoroutine : kotlinx/coro public final class kotlinx/coroutines/reactive/ReactiveFlowKt { public static final fun asFlow (Lorg/reactivestreams/Publisher;)Lkotlinx/coroutines/flow/Flow; public static final fun asPublisher (Lkotlinx/coroutines/flow/Flow;)Lorg/reactivestreams/Publisher; + public static final fun asPublisher (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;)Lorg/reactivestreams/Publisher; + public static synthetic fun asPublisher$default (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;ILjava/lang/Object;)Lorg/reactivestreams/Publisher; } diff --git a/reactive/kotlinx-coroutines-reactive/build.gradle.kts b/reactive/kotlinx-coroutines-reactive/build.gradle.kts new file mode 100644 index 0000000000..2ace4f9fcc --- /dev/null +++ b/reactive/kotlinx-coroutines-reactive/build.gradle.kts @@ -0,0 +1,36 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +val reactiveStreamsVersion = property("reactive_streams_version") + +dependencies { + compile("org.reactivestreams:reactive-streams:$reactiveStreamsVersion") + testCompile("org.reactivestreams:reactive-streams-tck:$reactiveStreamsVersion") +} + +val testNG by tasks.registering(Test::class) { + useTestNG() + reports.html.destination = file("$buildDir/reports/testng") + include("**/*ReactiveStreamTckTest.*") + // Skip testNG when tests are filtered with --tests, otherwise it simply fails + onlyIf { + filter.includePatterns.isEmpty() + } + doFirst { + // Classic gradle, nothing works without doFirst + println("TestNG tests: ($includes)") + } +} + +tasks.test { + reports.html.destination = file("$buildDir/reports/junit") +} + +tasks.check { + dependsOn(testNG) +} + +externalDocumentationLink( + url = "https://www.reactive-streams.org/reactive-streams-$reactiveStreamsVersion-javadoc/" +) diff --git a/reactive/kotlinx-coroutines-reactive/src/Await.kt b/reactive/kotlinx-coroutines-reactive/src/Await.kt index 9ea2e3c50e..7956c26010 100644 --- a/reactive/kotlinx-coroutines-reactive/src/Await.kt +++ b/reactive/kotlinx-coroutines-reactive/src/Await.kt @@ -80,13 +80,53 @@ public suspend fun Publisher.awaitLast(): T = awaitOne(Mode.LAST) */ public suspend fun Publisher.awaitSingle(): T = awaitOne(Mode.SINGLE) +/** + * Awaits for the single value from the given publisher or the [default] value if none is emitted without blocking a thread and + * returns the resulting value or throws the corresponding exception if this publisher had produced error. + * + * This suspending function is cancellable. + * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function + * immediately resumes with [CancellationException]. + * + * @throws NoSuchElementException if publisher does not emit any value + * @throws IllegalArgumentException if publisher emits more than one value + */ +public suspend fun Publisher.awaitSingleOrDefault(default: T): T = awaitOne(Mode.SINGLE_OR_DEFAULT, default) + +/** + * Awaits for the single value from the given publisher or `null` value if none is emitted without blocking a thread and + * returns the resulting value or throws the corresponding exception if this publisher had produced error. + * + * This suspending function is cancellable. + * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function + * immediately resumes with [CancellationException]. + * + * @throws NoSuchElementException if publisher does not emit any value + * @throws IllegalArgumentException if publisher emits more than one value + */ +public suspend fun Publisher.awaitSingleOrNull(): T = awaitOne(Mode.SINGLE_OR_DEFAULT) + +/** + * Awaits for the single value from the given publisher or call [defaultValue] to get a value if none is emitted without blocking a thread and + * returns the resulting value or throws the corresponding exception if this publisher had produced error. + * + * This suspending function is cancellable. + * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function + * immediately resumes with [CancellationException]. + * + * @throws NoSuchElementException if publisher does not emit any value + * @throws IllegalArgumentException if publisher emits more than one value + */ +public suspend fun Publisher.awaitSingleOrElse(defaultValue: () -> T): T = awaitOne(Mode.SINGLE_OR_DEFAULT) ?: defaultValue() + // ------------------------ private ------------------------ private enum class Mode(val s: String) { FIRST("awaitFirst"), FIRST_OR_DEFAULT("awaitFirstOrDefault"), LAST("awaitLast"), - SINGLE("awaitSingle"); + SINGLE("awaitSingle"), + SINGLE_OR_DEFAULT("awaitSingleOrDefault"); override fun toString(): String = s } @@ -114,8 +154,8 @@ private suspend fun Publisher.awaitOne( cont.resume(t) } } - Mode.LAST, Mode.SINGLE -> { - if (mode == Mode.SINGLE && seenValue) { + Mode.LAST, Mode.SINGLE, Mode.SINGLE_OR_DEFAULT -> { + if ((mode == Mode.SINGLE || mode == Mode.SINGLE_OR_DEFAULT) && seenValue) { subscription.cancel() if (cont.isActive) cont.resumeWithException(IllegalArgumentException("More than one onNext value for $mode")) @@ -134,7 +174,7 @@ private suspend fun Publisher.awaitOne( return } when { - mode == Mode.FIRST_OR_DEFAULT -> { + (mode == Mode.FIRST_OR_DEFAULT || mode == Mode.SINGLE_OR_DEFAULT) -> { cont.resume(default as T) } cont.isActive -> { diff --git a/reactive/kotlinx-coroutines-reactive/src/Channel.kt b/reactive/kotlinx-coroutines-reactive/src/Channel.kt index 8c8187c130..26f14ec63d 100644 --- a/reactive/kotlinx-coroutines-reactive/src/Channel.kt +++ b/reactive/kotlinx-coroutines-reactive/src/Channel.kt @@ -35,20 +35,20 @@ public fun Publisher.openSubscription(request: Int = 1): ReceiveChannel Publisher.consumeEach(action: (T) -> Unit) = +public suspend inline fun Publisher.consumeEach(action: (T) -> Unit): Unit = openSubscription().consumeEach(action) /** * Subscribes to this [Publisher] and performs the specified action for each received element. * Cancels subscription if any exception happens during collect. */ -public suspend inline fun Publisher.collect(action: (T) -> Unit) = +public suspend inline fun Publisher.collect(action: (T) -> Unit): Unit = openSubscription().consumeEach(action) @Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER", "SubscriberImplementation") private class SubscriptionChannel( private val request: Int -) : LinkedListChannel(), Subscriber { +) : LinkedListChannel(null), Subscriber { init { require(request >= 0) { "Invalid request size: $request" } } diff --git a/reactive/kotlinx-coroutines-reactive/src/Convert.kt b/reactive/kotlinx-coroutines-reactive/src/Convert.kt index fb06ca3a3c..727eff8b48 100644 --- a/reactive/kotlinx-coroutines-reactive/src/Convert.kt +++ b/reactive/kotlinx-coroutines-reactive/src/Convert.kt @@ -16,7 +16,7 @@ import kotlin.coroutines.* * @param context -- the coroutine context from which the resulting observable is going to be signalled */ @Deprecated(message = "Deprecated in the favour of consumeAsFlow()", - level = DeprecationLevel.WARNING, + level = DeprecationLevel.WARNING, // Error in 1.4 replaceWith = ReplaceWith("this.consumeAsFlow().asPublisher()")) public fun ReceiveChannel.asPublisher(context: CoroutineContext = EmptyCoroutineContext): Publisher = publish(context) { for (t in this@asPublisher) diff --git a/reactive/kotlinx-coroutines-reactive/src/Publish.kt b/reactive/kotlinx-coroutines-reactive/src/Publish.kt index 68c1702913..ddfd7f8aa3 100644 --- a/reactive/kotlinx-coroutines-reactive/src/Publish.kt +++ b/reactive/kotlinx-coroutines-reactive/src/Publish.kt @@ -93,7 +93,7 @@ public class PublisherCoroutine( override val isClosedForSend: Boolean get() = isCompleted override val isFull: Boolean = mutex.isLocked override fun close(cause: Throwable?): Boolean = cancelCoroutine(cause) - override fun invokeOnClose(handler: (Throwable?) -> Unit) = + override fun invokeOnClose(handler: (Throwable?) -> Unit): Nothing = throw UnsupportedOperationException("PublisherCoroutine doesn't support invokeOnClose") override fun offer(element: T): Boolean { diff --git a/reactive/kotlinx-coroutines-reactive/src/ReactiveFlow.kt b/reactive/kotlinx-coroutines-reactive/src/ReactiveFlow.kt index 20e165e705..5834220c40 100644 --- a/reactive/kotlinx-coroutines-reactive/src/ReactiveFlow.kt +++ b/reactive/kotlinx-coroutines-reactive/src/ReactiveFlow.kt @@ -34,16 +34,24 @@ public fun Publisher.asFlow(): Flow = * * This function is integrated with `ReactorContext` from `kotlinx-coroutines-reactor` module, * see its documentation for additional details. + * + * An optional [context] can be specified to control the execution context of calls to [Subscriber] methods. + * You can set a [CoroutineDispatcher] to confine them to a specific thread and/or various [ThreadContextElement] to + * inject additional context into the caller thread. By default, the [Unconfined][Dispatchers.Unconfined] dispatcher + * is used, so calls are performed from an arbitrary thread. */ -public fun Flow.asPublisher(): Publisher = FlowAsPublisher(this) +@JvmOverloads // binary compatibility +public fun Flow.asPublisher(context: CoroutineContext = EmptyCoroutineContext): Publisher = + FlowAsPublisher(this, Dispatchers.Unconfined + context) private class PublisherAsFlow( private val publisher: Publisher, context: CoroutineContext = EmptyCoroutineContext, - capacity: Int = Channel.BUFFERED -) : ChannelFlow(context, capacity) { - override fun create(context: CoroutineContext, capacity: Int): ChannelFlow = - PublisherAsFlow(publisher, context, capacity) + capacity: Int = Channel.BUFFERED, + onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND +) : ChannelFlow(context, capacity, onBufferOverflow) { + override fun create(context: CoroutineContext, capacity: Int, onBufferOverflow: BufferOverflow): ChannelFlow = + PublisherAsFlow(publisher, context, capacity, onBufferOverflow) /* * Suppress for Channel.CHANNEL_DEFAULT_CAPACITY. @@ -52,13 +60,15 @@ private class PublisherAsFlow( */ @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") private val requestSize: Long - get() = when (capacity) { - Channel.CONFLATED -> Long.MAX_VALUE // request all and conflate incoming - Channel.RENDEZVOUS -> 1L // need to request at least one anyway - Channel.UNLIMITED -> Long.MAX_VALUE // reactive streams way to say "give all" must be Long.MAX_VALUE - Channel.BUFFERED -> Channel.CHANNEL_DEFAULT_CAPACITY.toLong() - else -> capacity.toLong().also { check(it >= 1) } - } + get() = + if (onBufferOverflow != BufferOverflow.SUSPEND) { + Long.MAX_VALUE // request all, since buffering strategy is to never suspend + } else when (capacity) { + Channel.RENDEZVOUS -> 1L // need to request at least one anyway + Channel.UNLIMITED -> Long.MAX_VALUE // reactive streams way to say "give all", must be Long.MAX_VALUE + Channel.BUFFERED -> Channel.CHANNEL_DEFAULT_CAPACITY.toLong() + else -> capacity.toLong().also { check(it >= 1) } + } override suspend fun collect(collector: FlowCollector) { val collectContext = coroutineContext @@ -78,13 +88,14 @@ private class PublisherAsFlow( } private suspend fun collectImpl(injectContext: CoroutineContext, collector: FlowCollector) { - val subscriber = ReactiveSubscriber(capacity, requestSize) + val subscriber = ReactiveSubscriber(capacity, onBufferOverflow, requestSize) // inject subscribe context into publisher publisher.injectCoroutineContext(injectContext).subscribe(subscriber) try { var consumed = 0L while (true) { val value = subscriber.takeNextOrNull() ?: break + coroutineContext.ensureActive() collector.emit(value) if (++consumed == requestSize) { consumed = 0L @@ -104,10 +115,14 @@ private class PublisherAsFlow( @Suppress("SubscriberImplementation") private class ReactiveSubscriber( capacity: Int, + onBufferOverflow: BufferOverflow, private val requestSize: Long ) : Subscriber { private lateinit var subscription: Subscription - private val channel = Channel(capacity) + + // This implementation of ReactiveSubscriber always uses "offer" in its onNext implementation and it cannot + // be reliable with rendezvous channel, so a rendezvous channel is replaced with buffer=1 channel + private val channel = Channel(if (capacity == Channel.RENDEZVOUS) 1 else capacity, onBufferOverflow) suspend fun takeNextOrNull(): T? = channel.receiveOrNull() @@ -152,24 +167,29 @@ internal fun Publisher.injectCoroutineContext(coroutineContext: Coroutine * Adapter that transforms [Flow] into TCK-complaint [Publisher]. * [cancel] invocation cancels the original flow. */ -@Suppress("PublisherImplementation") -private class FlowAsPublisher(private val flow: Flow) : Publisher { +@Suppress("ReactiveStreamsPublisherImplementation") +private class FlowAsPublisher( + private val flow: Flow, + private val context: CoroutineContext +) : Publisher { override fun subscribe(subscriber: Subscriber?) { if (subscriber == null) throw NullPointerException() - subscriber.onSubscribe(FlowSubscription(flow, subscriber)) + subscriber.onSubscribe(FlowSubscription(flow, subscriber, context)) } } /** @suppress */ @InternalCoroutinesApi public class FlowSubscription( - @JvmField val flow: Flow, - @JvmField val subscriber: Subscriber -) : Subscription, AbstractCoroutine(Dispatchers.Unconfined, false) { + @JvmField public val flow: Flow, + @JvmField public val subscriber: Subscriber, + context: CoroutineContext +) : Subscription, AbstractCoroutine(context, true) { private val requested = atomic(0L) - private val producer = atomic?>(null) + private val producer = atomic?>(createInitialContinuation()) - override fun onStart() { + // This code wraps startCoroutineCancellable into continuation + private fun createInitialContinuation(): Continuation = Continuation(coroutineContext) { ::flowProcessing.startCoroutineCancellable(this) } @@ -196,19 +216,17 @@ public class FlowSubscription( */ private suspend fun consumeFlow() { flow.collect { value -> - /* - * Flow is scopeless, thus if it's not active, its subscription was cancelled. - * No intermediate "child failed, but flow coroutine is not" states are allowed. - */ - coroutineContext.ensureActive() - if (requested.value <= 0L) { + // Emit the value + subscriber.onNext(value) + // Suspend if needed before requesting the next value + if (requested.decrementAndGet() <= 0) { suspendCancellableCoroutine { producer.value = it - if (requested.value != 0L) it.resumeSafely() } + } else { + // check for cancellation if we don't suspend + coroutineContext.ensureActive() } - requested.decrementAndGet() - subscriber.onNext(value) } } @@ -217,22 +235,19 @@ public class FlowSubscription( } override fun request(n: Long) { - if (n <= 0) { - return - } - start() - requested.update { value -> + if (n <= 0) return + val old = requested.getAndUpdate { value -> val newValue = value + n if (newValue <= 0L) Long.MAX_VALUE else newValue } - val producer = producer.getAndSet(null) ?: return - producer.resumeSafely() - } - - private fun CancellableContinuation.resumeSafely() { - val token = tryResume(Unit) - if (token != null) { - completeResume(token) + if (old <= 0L) { + assert(old == 0L) + // Emitter is not started yet or has suspended -- spin on race with suspendCancellableCoroutine + while(true) { + val producer = producer.getAndSet(null) ?: continue // spin if not set yet + producer.resume(Unit) + break + } } } } diff --git a/reactive/kotlinx-coroutines-reactive/test/FlowAsPublisherTest.kt b/reactive/kotlinx-coroutines-reactive/test/FlowAsPublisherTest.kt index 8633492810..e7b8cb17ae 100644 --- a/reactive/kotlinx-coroutines-reactive/test/FlowAsPublisherTest.kt +++ b/reactive/kotlinx-coroutines-reactive/test/FlowAsPublisherTest.kt @@ -8,18 +8,18 @@ import kotlinx.coroutines.* import kotlinx.coroutines.flow.* import org.junit.Test import org.reactivestreams.* +import java.util.concurrent.* import kotlin.test.* class FlowAsPublisherTest : TestBase() { - @Test fun testErrorOnCancellationIsReported() { expect(1) flow { - emit(2) try { - hang { expect(3) } + emit(2) } finally { + expect(3) throw TestException() } }.asPublisher().subscribe(object : Subscriber { @@ -52,12 +52,11 @@ class FlowAsPublisherTest : TestBase() { expect(1) flow { emit(2) - hang { expect(3) } }.asPublisher().subscribe(object : Subscriber { private lateinit var subscription: Subscription override fun onComplete() { - expect(4) + expect(3) } override fun onSubscribe(s: Subscription?) { @@ -74,6 +73,80 @@ class FlowAsPublisherTest : TestBase() { expectUnreached() } }) + finish(4) + } + + @Test + fun testUnconfinedDefaultContext() { + expect(1) + val thread = Thread.currentThread() + fun checkThread() { + assertSame(thread, Thread.currentThread()) + } + flowOf(42).asPublisher().subscribe(object : Subscriber { + private lateinit var subscription: Subscription + + override fun onSubscribe(s: Subscription) { + expect(2) + subscription = s + subscription.request(2) + } + + override fun onNext(t: Int) { + checkThread() + expect(3) + assertEquals(42, t) + } + + override fun onComplete() { + checkThread() + expect(4) + } + + override fun onError(t: Throwable?) { + expectUnreached() + } + }) + finish(5) + } + + @Test + fun testConfinedContext() { + expect(1) + val threadName = "FlowAsPublisherTest.testConfinedContext" + fun checkThread() { + val currentThread = Thread.currentThread() + assertTrue(currentThread.name.startsWith(threadName), "Unexpected thread $currentThread") + } + val completed = CountDownLatch(1) + newSingleThreadContext(threadName).use { dispatcher -> + flowOf(42).asPublisher(dispatcher).subscribe(object : Subscriber { + private lateinit var subscription: Subscription + + override fun onSubscribe(s: Subscription) { + expect(2) + subscription = s + subscription.request(2) + } + + override fun onNext(t: Int) { + checkThread() + expect(3) + assertEquals(42, t) + } + + override fun onComplete() { + checkThread() + expect(4) + completed.countDown() + } + + override fun onError(t: Throwable?) { + expectUnreached() + } + }) + completed.await() + } finish(5) } } diff --git a/reactive/kotlinx-coroutines-reactive/test/IntegrationTest.kt b/reactive/kotlinx-coroutines-reactive/test/IntegrationTest.kt index 6f7d98480b..18cd012d16 100644 --- a/reactive/kotlinx-coroutines-reactive/test/IntegrationTest.kt +++ b/reactive/kotlinx-coroutines-reactive/test/IntegrationTest.kt @@ -48,6 +48,9 @@ class IntegrationTest( assertEquals("ELSE", pub.awaitFirstOrElse { "ELSE" }) assertFailsWith { pub.awaitLast() } assertFailsWith { pub.awaitSingle() } + assertEquals("OK", pub.awaitSingleOrDefault("OK")) + assertNull(pub.awaitSingleOrNull()) + assertEquals("ELSE", pub.awaitSingleOrElse { "ELSE" }) var cnt = 0 pub.collect { cnt++ } assertEquals(0, cnt) @@ -65,6 +68,9 @@ class IntegrationTest( assertEquals("OK", pub.awaitFirstOrElse { "ELSE" }) assertEquals("OK", pub.awaitLast()) assertEquals("OK", pub.awaitSingle()) + assertEquals("OK", pub.awaitSingleOrDefault("!")) + assertEquals("OK", pub.awaitSingleOrNull()) + assertEquals("OK", pub.awaitSingleOrElse { "ELSE" }) var cnt = 0 pub.collect { assertEquals("OK", it) @@ -84,10 +90,13 @@ class IntegrationTest( } assertEquals(1, pub.awaitFirst()) assertEquals(1, pub.awaitFirstOrDefault(0)) - assertEquals(n, pub.awaitLast()) assertEquals(1, pub.awaitFirstOrNull()) assertEquals(1, pub.awaitFirstOrElse { 0 }) + assertEquals(n, pub.awaitLast()) assertFailsWith { pub.awaitSingle() } + assertFailsWith { pub.awaitSingleOrDefault(0) } + assertFailsWith { pub.awaitSingleOrNull() } + assertFailsWith { pub.awaitSingleOrElse { 0 } } checkNumbers(n, pub) val channel = pub.openSubscription() checkNumbers(n, channel.asPublisher(ctx(coroutineContext))) diff --git a/reactive/kotlinx-coroutines-reactive/test/PublisherAsFlowTest.kt b/reactive/kotlinx-coroutines-reactive/test/PublisherAsFlowTest.kt index 61f88f6af3..04833e9814 100644 --- a/reactive/kotlinx-coroutines-reactive/test/PublisherAsFlowTest.kt +++ b/reactive/kotlinx-coroutines-reactive/test/PublisherAsFlowTest.kt @@ -7,6 +7,7 @@ package kotlinx.coroutines.reactive import kotlinx.coroutines.* import kotlinx.coroutines.channels.* import kotlinx.coroutines.flow.* +import org.reactivestreams.* import kotlin.test.* class PublisherAsFlowTest : TestBase() { @@ -181,4 +182,85 @@ class PublisherAsFlowTest : TestBase() { } finish(6) } + + @Test + fun testRequestRendezvous() = + testRequestSizeWithBuffer(Channel.RENDEZVOUS, BufferOverflow.SUSPEND, 1) + + @Test + fun testRequestBuffer1() = + testRequestSizeWithBuffer(1, BufferOverflow.SUSPEND, 1) + + @Test + fun testRequestBuffer10() = + testRequestSizeWithBuffer(10, BufferOverflow.SUSPEND, 10) + + @Test + fun testRequestBufferUnlimited() = + testRequestSizeWithBuffer(Channel.UNLIMITED, BufferOverflow.SUSPEND, Long.MAX_VALUE) + + @Test + fun testRequestBufferOverflowSuspend() = + testRequestSizeWithBuffer(Channel.BUFFERED, BufferOverflow.SUSPEND, 64) + + @Test + fun testRequestBufferOverflowDropOldest() = + testRequestSizeWithBuffer(Channel.BUFFERED, BufferOverflow.DROP_OLDEST, Long.MAX_VALUE) + + @Test + fun testRequestBufferOverflowDropLatest() = + testRequestSizeWithBuffer(Channel.BUFFERED, BufferOverflow.DROP_LATEST, Long.MAX_VALUE) + + @Test + fun testRequestBuffer10OverflowDropOldest() = + testRequestSizeWithBuffer(10, BufferOverflow.DROP_OLDEST, Long.MAX_VALUE) + + @Test + fun testRequestBuffer10OverflowDropLatest() = + testRequestSizeWithBuffer(10, BufferOverflow.DROP_LATEST, Long.MAX_VALUE) + + /** + * Tests `publisher.asFlow.buffer(...)` chain, verifying expected requests size and that only expected + * values are delivered. + */ + private fun testRequestSizeWithBuffer( + capacity: Int, + onBufferOverflow: BufferOverflow, + expectedRequestSize: Long + ) = runTest { + val m = 50 + // publishers numbers from 1 to m + val publisher = Publisher { s -> + s.onSubscribe(object : Subscription { + var lastSent = 0 + var remaining = 0L + override fun request(n: Long) { + assertEquals(expectedRequestSize, n) + remaining += n + check(remaining >= 0) + while (lastSent < m && remaining > 0) { + s.onNext(++lastSent) + remaining-- + } + if (lastSent == m) s.onComplete() + } + + override fun cancel() {} + }) + } + val flow = publisher + .asFlow() + .buffer(capacity, onBufferOverflow) + val list = flow.toList() + val runSize = if (capacity == Channel.BUFFERED) 1 else capacity + val expected = when (onBufferOverflow) { + // Everything is expected to be delivered + BufferOverflow.SUSPEND -> (1..m).toList() + // Only the last one (by default) or the last "capacity" items delivered + BufferOverflow.DROP_OLDEST -> (m - runSize + 1..m).toList() + // Only the first one (by default) or the first "capacity" items delivered + BufferOverflow.DROP_LATEST -> (1..runSize).toList() + } + assertEquals(expected, list) + } } diff --git a/reactive/kotlinx-coroutines-reactive/test/PublisherRequestStressTest.kt b/reactive/kotlinx-coroutines-reactive/test/PublisherRequestStressTest.kt new file mode 100644 index 0000000000..736a66404f --- /dev/null +++ b/reactive/kotlinx-coroutines-reactive/test/PublisherRequestStressTest.kt @@ -0,0 +1,141 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.reactive + +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* +import kotlinx.coroutines.flow.Flow +import org.junit.* +import org.reactivestreams.* +import java.util.concurrent.* +import java.util.concurrent.atomic.* +import kotlin.coroutines.* +import kotlin.random.* + +/** + * This stress-test is self-contained reproducer for the race in [Flow.asPublisher] extension + * that was originally reported in the issue + * [#2109](https://github.com/Kotlin/kotlinx.coroutines/issues/2109). + * The original reproducer used a flow that loads a file using AsynchronousFileChannel + * (that issues completion callbacks from multiple threads) + * and uploads it to S3 via Amazon SDK, which internally uses netty for I/O + * (which uses a single thread for connection-related callbacks). + * + * This stress-test essentially mimics the logic in multiple interacting threads: several emitter threads that form + * the flow and a single requesting thread works on the subscriber's side to periodically request more + * values when the number of items requested drops below the threshold. + */ +@Suppress("ReactiveStreamsSubscriberImplementation") +class PublisherRequestStressTest : TestBase() { + private val testDurationSec = 3 * stressTestMultiplier + + // Original code in Amazon SDK uses 4 and 16 as low/high watermarks. + // There constants were chosen so that problem reproduces asap with particular this code. + private val minDemand = 8L + private val maxDemand = 16L + + private val nEmitThreads = 4 + + private val emitThreadNo = AtomicInteger() + + private val emitPool = Executors.newFixedThreadPool(nEmitThreads) { r -> + Thread(r, "PublisherRequestStressTest-emit-${emitThreadNo.incrementAndGet()}") + } + + private val reqPool = Executors.newSingleThreadExecutor { r -> + Thread(r, "PublisherRequestStressTest-req") + } + + private val nextValue = AtomicLong(0) + + @After + fun tearDown() { + emitPool.shutdown() + reqPool.shutdown() + emitPool.awaitTermination(10, TimeUnit.SECONDS) + reqPool.awaitTermination(10, TimeUnit.SECONDS) + } + + private lateinit var subscription: Subscription + + @Test + fun testRequestStress() { + val expectedValue = AtomicLong(0) + val requestedTill = AtomicLong(0) + val completionLatch = CountDownLatch(1) + val callingOnNext = AtomicInteger() + + val publisher = mtFlow().asPublisher() + var error = false + + publisher.subscribe(object : Subscriber { + private var demand = 0L // only updated from reqPool + + override fun onComplete() { + completionLatch.countDown() + } + + override fun onSubscribe(sub: Subscription) { + subscription = sub + maybeRequestMore() + } + + private fun maybeRequestMore() { + if (demand >= minDemand) return + val nextDemand = Random.nextLong(minDemand + 1..maxDemand) + val more = nextDemand - demand + demand = nextDemand + requestedTill.addAndGet(more) + subscription.request(more) + } + + override fun onNext(value: Long) { + check(callingOnNext.getAndIncrement() == 0) // make sure it is not concurrent + // check for expected value + check(value == expectedValue.get()) + // check that it does not exceed requested values + check(value < requestedTill.get()) + val nextExpected = value + 1 + expectedValue.set(nextExpected) + // send more requests from request thread + reqPool.execute { + demand-- // processed an item + maybeRequestMore() + } + callingOnNext.decrementAndGet() + } + + override fun onError(ex: Throwable?) { + error = true + error("Failed", ex) + } + }) + var prevExpected = -1L + for (second in 1..testDurationSec) { + if (error) break + Thread.sleep(1000) + val expected = expectedValue.get() + println("$second: expectedValue = $expected") + check(expected > prevExpected) // should have progress + prevExpected = expected + } + if (!error) { + subscription.cancel() + completionLatch.await() + } + } + + private fun mtFlow(): Flow = flow { + while (currentCoroutineContext().isActive) { + emit(aWait()) + } + } + + private suspend fun aWait(): Long = suspendCancellableCoroutine { cont -> + emitPool.execute(Runnable { + cont.resume(nextValue.getAndIncrement()) + }) + } +} \ No newline at end of file diff --git a/reactive/kotlinx-coroutines-reactor/README.md b/reactive/kotlinx-coroutines-reactor/README.md index 50e1602a3a..cd4a42a34c 100644 --- a/reactive/kotlinx-coroutines-reactor/README.md +++ b/reactive/kotlinx-coroutines-reactor/README.md @@ -6,29 +6,29 @@ Coroutine builders: | **Name** | **Result** | **Scope** | **Description** | --------------- | ------------| ---------------- | --------------- -| [mono] | `Mono` | [CoroutineScope] | Cold mono that starts coroutine on subscribe -| [flux] | `Flux` | [CoroutineScope] | Cold flux that starts coroutine on subscribe +| [mono] | `Mono` | [CoroutineScope] | A cold Mono that starts the coroutine on subscription +| [flux] | `Flux` | [CoroutineScope] | A cold Flux that starts the coroutine on subscription -Note that `Mono` and `Flux` are a subclass of [Reactive Streams](https://www.reactive-streams.org) -`Publisher` and extensions for it are covered by +Note that `Mono` and `Flux` are subclasses of [Reactive Streams](https://www.reactive-streams.org)' +`Publisher` and extensions for it are covered by the [kotlinx-coroutines-reactive](../kotlinx-coroutines-reactive) module. Integration with [Flow]: | **Name** | **Result** | **Description** | --------------- | -------------- | --------------- -| [Flow.asFlux] | `Flux` | Converts the given flow to the TCK-compliant Flux. +| [Flow.asFlux] | `Flux` | Converts the given flow to a TCK-compliant Flux. -This adapter is integrated with Reactor's `Context` and coroutines [ReactorContext]. +This adapter is integrated with Reactor's `Context` and coroutines' [ReactorContext]. Conversion functions: | **Name** | **Description** | -------- | --------------- -| [Job.asMono][kotlinx.coroutines.Job.asMono] | Converts job to hot mono -| [Deferred.asMono][kotlinx.coroutines.Deferred.asMono] | Converts deferred value to hot mono -| [ReceiveChannel.asFlux][kotlinx.coroutines.channels.ReceiveChannel.asFlux] | Converts streaming channel to hot flux -| [Scheduler.asCoroutineDispatcher][reactor.core.scheduler.Scheduler.asCoroutineDispatcher] | Converts scheduler to [CoroutineDispatcher] +| [Job.asMono][kotlinx.coroutines.Job.asMono] | Converts a job to a hot Mono +| [Deferred.asMono][kotlinx.coroutines.Deferred.asMono] | Converts a deferred value to a hot Mono +| [ReceiveChannel.asFlux][kotlinx.coroutines.channels.ReceiveChannel.asFlux] | Converts a streaming channel to a hot Flux +| [Scheduler.asCoroutineDispatcher][reactor.core.scheduler.Scheduler.asCoroutineDispatcher] | Converts a scheduler to a [CoroutineDispatcher] diff --git a/reactive/kotlinx-coroutines-reactor/api/kotlinx-coroutines-reactor.api b/reactive/kotlinx-coroutines-reactor/api/kotlinx-coroutines-reactor.api index 422f36b1ea..b46fe338e5 100644 --- a/reactive/kotlinx-coroutines-reactor/api/kotlinx-coroutines-reactor.api +++ b/reactive/kotlinx-coroutines-reactor/api/kotlinx-coroutines-reactor.api @@ -38,6 +38,8 @@ public final class kotlinx/coroutines/reactor/ReactorContextKt { public final class kotlinx/coroutines/reactor/ReactorFlowKt { public static final fun asFlux (Lkotlinx/coroutines/flow/Flow;)Lreactor/core/publisher/Flux; + public static final fun asFlux (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;)Lreactor/core/publisher/Flux; + public static synthetic fun asFlux$default (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;ILjava/lang/Object;)Lreactor/core/publisher/Flux; } public final class kotlinx/coroutines/reactor/SchedulerCoroutineDispatcher : kotlinx/coroutines/CoroutineDispatcher, kotlinx/coroutines/Delay { @@ -47,7 +49,7 @@ public final class kotlinx/coroutines/reactor/SchedulerCoroutineDispatcher : kot public fun equals (Ljava/lang/Object;)Z public final fun getScheduler ()Lreactor/core/scheduler/Scheduler; public fun hashCode ()I - public fun invokeOnTimeout (JLjava/lang/Runnable;)Lkotlinx/coroutines/DisposableHandle; + public fun invokeOnTimeout (JLjava/lang/Runnable;Lkotlin/coroutines/CoroutineContext;)Lkotlinx/coroutines/DisposableHandle; public fun scheduleResumeAfterDelay (JLkotlinx/coroutines/CancellableContinuation;)V public fun toString ()Ljava/lang/String; } diff --git a/reactive/kotlinx-coroutines-reactor/build.gradle b/reactive/kotlinx-coroutines-reactor/build.gradle deleted file mode 100644 index a33d3e687a..0000000000 --- a/reactive/kotlinx-coroutines-reactor/build.gradle +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -dependencies { - compile "io.projectreactor:reactor-core:$reactor_vesion" - compile project(':kotlinx-coroutines-reactive') -} - -tasks.withType(dokka.getClass()) { - externalDocumentationLink { - url = new URL("https://projectreactor.io/docs/core/$reactor_vesion/api/") - packageListUrl = projectDir.toPath().resolve("package.list").toUri().toURL() - } -} - -compileTestKotlin { - kotlinOptions.jvmTarget = "1.8" -} - -compileKotlin { - kotlinOptions.jvmTarget = "1.8" -} diff --git a/reactive/kotlinx-coroutines-reactor/build.gradle.kts b/reactive/kotlinx-coroutines-reactor/build.gradle.kts new file mode 100644 index 0000000000..d5fd208a27 --- /dev/null +++ b/reactive/kotlinx-coroutines-reactor/build.gradle.kts @@ -0,0 +1,25 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +val reactorVersion = version("reactor") + +dependencies { + compile("io.projectreactor:reactor-core:$reactorVersion") + compile(project(":kotlinx-coroutines-reactive")) +} + + +tasks { + compileKotlin { + kotlinOptions.jvmTarget = "1.8" + } + + compileTestKotlin { + kotlinOptions.jvmTarget = "1.8" + } +} + +externalDocumentationLink( + url = "https://projectreactor.io/docs/core/$reactorVersion/api/" +) diff --git a/reactive/kotlinx-coroutines-reactor/src/ReactorContext.kt b/reactive/kotlinx-coroutines-reactor/src/ReactorContext.kt index 9f5eb23169..69467ad8b1 100644 --- a/reactive/kotlinx-coroutines-reactor/src/ReactorContext.kt +++ b/reactive/kotlinx-coroutines-reactor/src/ReactorContext.kt @@ -49,7 +49,7 @@ import kotlinx.coroutines.reactive.* */ @ExperimentalCoroutinesApi public class ReactorContext(public val context: Context) : AbstractCoroutineContextElement(ReactorContext) { - companion object Key : CoroutineContext.Key + public companion object Key : CoroutineContext.Key } /** diff --git a/reactive/kotlinx-coroutines-reactor/src/ReactorFlow.kt b/reactive/kotlinx-coroutines-reactor/src/ReactorFlow.kt index d665c88d35..a478ab1ef8 100644 --- a/reactive/kotlinx-coroutines-reactor/src/ReactorFlow.kt +++ b/reactive/kotlinx-coroutines-reactor/src/ReactorFlow.kt @@ -4,25 +4,38 @@ package kotlinx.coroutines.reactor +import kotlinx.coroutines.* import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.reactive.FlowSubscription +import org.reactivestreams.* import reactor.core.CoreSubscriber import reactor.core.publisher.Flux +import kotlin.coroutines.* /** * Converts the given flow to a cold flux. * The original flow is cancelled when the flux subscriber is disposed. * * This function is integrated with [ReactorContext], see its documentation for additional details. + * + * An optional [context] can be specified to control the execution context of calls to [Subscriber] methods. + * You can set a [CoroutineDispatcher] to confine them to a specific thread and/or various [ThreadContextElement] to + * inject additional context into the caller thread. By default, the [Unconfined][Dispatchers.Unconfined] dispatcher + * is used, so calls are performed from an arbitrary thread. */ -public fun Flow.asFlux(): Flux = FlowAsFlux(this) +@JvmOverloads // binary compatibility +public fun Flow.asFlux(context: CoroutineContext = EmptyCoroutineContext): Flux = + FlowAsFlux(this, Dispatchers.Unconfined + context) -private class FlowAsFlux(private val flow: Flow) : Flux() { +private class FlowAsFlux( + private val flow: Flow, + private val context: CoroutineContext +) : Flux() { override fun subscribe(subscriber: CoreSubscriber?) { if (subscriber == null) throw NullPointerException() val hasContext = !subscriber.currentContext().isEmpty val source = if (hasContext) flow.flowOn(subscriber.currentContext().asCoroutineContext()) else flow - subscriber.onSubscribe(FlowSubscription(source, subscriber)) + subscriber.onSubscribe(FlowSubscription(source, subscriber, context)) } } diff --git a/reactive/kotlinx-coroutines-reactor/src/Scheduler.kt b/reactive/kotlinx-coroutines-reactor/src/Scheduler.kt index 833ceb2d6a..4fb5514322 100644 --- a/reactive/kotlinx-coroutines-reactor/src/Scheduler.kt +++ b/reactive/kotlinx-coroutines-reactor/src/Scheduler.kt @@ -13,7 +13,7 @@ import kotlin.coroutines.CoroutineContext /** * Converts an instance of [Scheduler] to an implementation of [CoroutineDispatcher]. */ -fun Scheduler.asCoroutineDispatcher(): SchedulerCoroutineDispatcher = SchedulerCoroutineDispatcher(this) +public fun Scheduler.asCoroutineDispatcher(): SchedulerCoroutineDispatcher = SchedulerCoroutineDispatcher(this) /** * Implements [CoroutineDispatcher] on top of an arbitrary [Scheduler]. @@ -39,7 +39,7 @@ public class SchedulerCoroutineDispatcher( } /** @suppress */ - override fun invokeOnTimeout(timeMillis: Long, block: Runnable): DisposableHandle = + override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle = scheduler.schedule(block, timeMillis, TimeUnit.MILLISECONDS).asDisposableHandle() /** @suppress */ diff --git a/reactive/kotlinx-coroutines-reactor/test/BackpressureTest.kt b/reactive/kotlinx-coroutines-reactor/test/BackpressureTest.kt index 80feaeb865..3d6398aa13 100644 --- a/reactive/kotlinx-coroutines-reactor/test/BackpressureTest.kt +++ b/reactive/kotlinx-coroutines-reactor/test/BackpressureTest.kt @@ -5,6 +5,7 @@ package kotlinx.coroutines.reactor import kotlinx.coroutines.* +import kotlinx.coroutines.channels.* import kotlinx.coroutines.flow.* import kotlinx.coroutines.reactive.* import org.junit.Test @@ -36,4 +37,17 @@ class BackpressureTest : TestBase() { } finish(3) } + + @Test + fun testCooperativeCancellation() = runTest { + val flow = Flux.fromIterable((0L..Long.MAX_VALUE)).asFlow() + flow.onEach { if (it > 10) currentCoroutineContext().cancel() }.launchIn(this + Dispatchers.Default).join() + } + + @Test + fun testCooperativeCancellationForBuffered() = runTest(expected = { it is CancellationException }) { + val flow = Flux.fromIterable((0L..Long.MAX_VALUE)).asFlow() + val channel = flow.onEach { if (it > 10) currentCoroutineContext().cancel() }.produceIn(this + Dispatchers.Default) + channel.consumeEach { /* Do nothing, just consume elements */ } + } } \ No newline at end of file diff --git a/reactive/kotlinx-coroutines-reactor/test/FlowAsFluxTest.kt b/reactive/kotlinx-coroutines-reactor/test/FlowAsFluxTest.kt index e4bd8b315b..cecc89592e 100644 --- a/reactive/kotlinx-coroutines-reactor/test/FlowAsFluxTest.kt +++ b/reactive/kotlinx-coroutines-reactor/test/FlowAsFluxTest.kt @@ -4,10 +4,13 @@ import kotlinx.coroutines.* import kotlinx.coroutines.flow.* import kotlinx.coroutines.reactive.* import org.junit.Test +import org.reactivestreams.* import reactor.core.publisher.* import reactor.util.context.Context +import java.util.concurrent.* import kotlin.test.* +@Suppress("ReactiveStreamsSubscriberImplementation") class FlowAsFluxTest : TestBase() { @Test fun testFlowAsFluxContextPropagation() { @@ -68,4 +71,78 @@ class FlowAsFluxTest : TestBase() { } finish(4) } -} \ No newline at end of file + + @Test + fun testUnconfinedDefaultContext() { + expect(1) + val thread = Thread.currentThread() + fun checkThread() { + assertSame(thread, Thread.currentThread()) + } + flowOf(42).asFlux().subscribe(object : Subscriber { + private lateinit var subscription: Subscription + + override fun onSubscribe(s: Subscription) { + expect(2) + subscription = s + subscription.request(2) + } + + override fun onNext(t: Int) { + checkThread() + expect(3) + assertEquals(42, t) + } + + override fun onComplete() { + checkThread() + expect(4) + } + + override fun onError(t: Throwable?) { + expectUnreached() + } + }) + finish(5) + } + + @Test + fun testConfinedContext() { + expect(1) + val threadName = "FlowAsFluxTest.testConfinedContext" + fun checkThread() { + val currentThread = Thread.currentThread() + assertTrue(currentThread.name.startsWith(threadName), "Unexpected thread $currentThread") + } + val completed = CountDownLatch(1) + newSingleThreadContext(threadName).use { dispatcher -> + flowOf(42).asFlux(dispatcher).subscribe(object : Subscriber { + private lateinit var subscription: Subscription + + override fun onSubscribe(s: Subscription) { + expect(2) + subscription = s + subscription.request(2) + } + + override fun onNext(t: Int) { + checkThread() + expect(3) + assertEquals(42, t) + } + + override fun onComplete() { + checkThread() + expect(4) + completed.countDown() + } + + override fun onError(t: Throwable?) { + expectUnreached() + } + }) + completed.await() + } + finish(5) + } +} diff --git a/reactive/kotlinx-coroutines-reactor/test/FluxSingleTest.kt b/reactive/kotlinx-coroutines-reactor/test/FluxSingleTest.kt index 3879c62c71..cc336ba6b5 100644 --- a/reactive/kotlinx-coroutines-reactor/test/FluxSingleTest.kt +++ b/reactive/kotlinx-coroutines-reactor/test/FluxSingleTest.kt @@ -68,6 +68,72 @@ class FluxSingleTest : TestBase() { } } + @Test + fun testAwaitSingleOrDefault() { + val flux = flux { + send(Flux.empty().awaitSingleOrDefault("O") + "K") + } + + checkSingleValue(flux) { + assertEquals("OK", it) + } + } + + @Test + fun testAwaitSingleOrDefaultException() { + val flux = flux { + send(Flux.just("O", "#").awaitSingleOrDefault("!") + "K") + } + + checkErroneous(flux) { + assert(it is IllegalArgumentException) + } + } + + @Test + fun testAwaitSingleOrNull() { + val flux = flux { + send(Flux.empty().awaitSingleOrNull() ?: "OK") + } + + checkSingleValue(flux) { + assertEquals("OK", it) + } + } + + @Test + fun testAwaitSingleOrNullException() { + val flux = flux { + send((Flux.just("O", "#").awaitSingleOrNull() ?: "!") + "K") + } + + checkErroneous(flux) { + assert(it is IllegalArgumentException) + } + } + + @Test + fun testAwaitSingleOrElse() { + val flux = flux { + send(Flux.empty().awaitSingleOrElse { "O" } + "K") + } + + checkSingleValue(flux) { + assertEquals("OK", it) + } + } + + @Test + fun testAwaitSingleOrElseException() { + val flux = flux { + send(Flux.just("O", "#").awaitSingleOrElse { "!" } + "K") + } + + checkErroneous(flux) { + assert(it is IllegalArgumentException) + } + } + @Test fun testAwaitFirst() { val flux = flux { diff --git a/reactive/kotlinx-coroutines-rx2/api/kotlinx-coroutines-rx2.api b/reactive/kotlinx-coroutines-rx2/api/kotlinx-coroutines-rx2.api index 22f40384f0..4370325f58 100644 --- a/reactive/kotlinx-coroutines-rx2/api/kotlinx-coroutines-rx2.api +++ b/reactive/kotlinx-coroutines-rx2/api/kotlinx-coroutines-rx2.api @@ -30,11 +30,19 @@ public final class kotlinx/coroutines/rx2/RxCompletableKt { public final class kotlinx/coroutines/rx2/RxConvertKt { public static final fun asCompletable (Lkotlinx/coroutines/Job;Lkotlin/coroutines/CoroutineContext;)Lio/reactivex/Completable; public static final fun asFlow (Lio/reactivex/ObservableSource;)Lkotlinx/coroutines/flow/Flow; + public static final fun asFlowable (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;)Lio/reactivex/Flowable; + public static synthetic fun asFlowable$default (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;ILjava/lang/Object;)Lio/reactivex/Flowable; public static final fun asMaybe (Lkotlinx/coroutines/Deferred;Lkotlin/coroutines/CoroutineContext;)Lio/reactivex/Maybe; public static final fun asObservable (Lkotlinx/coroutines/channels/ReceiveChannel;Lkotlin/coroutines/CoroutineContext;)Lio/reactivex/Observable; + public static final fun asObservable (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;)Lio/reactivex/Observable; + public static synthetic fun asObservable$default (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;ILjava/lang/Object;)Lio/reactivex/Observable; public static final fun asSingle (Lkotlinx/coroutines/Deferred;Lkotlin/coroutines/CoroutineContext;)Lio/reactivex/Single; - public static final fun from (Lkotlinx/coroutines/flow/Flow;)Lio/reactivex/Flowable; - public static final fun from (Lkotlinx/coroutines/flow/Flow;)Lio/reactivex/Observable; + public static final synthetic fun from (Lkotlinx/coroutines/flow/Flow;)Lio/reactivex/Flowable; + public static final synthetic fun from (Lkotlinx/coroutines/flow/Flow;)Lio/reactivex/Observable; + public static final synthetic fun from (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;)Lio/reactivex/Flowable; + public static final synthetic fun from (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;)Lio/reactivex/Observable; + public static synthetic fun from$default (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;ILjava/lang/Object;)Lio/reactivex/Flowable; + public static synthetic fun from$default (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;ILjava/lang/Object;)Lio/reactivex/Observable; } public final class kotlinx/coroutines/rx2/RxFlowableKt { @@ -76,7 +84,7 @@ public final class kotlinx/coroutines/rx2/SchedulerCoroutineDispatcher : kotlinx public fun equals (Ljava/lang/Object;)Z public final fun getScheduler ()Lio/reactivex/Scheduler; public fun hashCode ()I - public fun invokeOnTimeout (JLjava/lang/Runnable;)Lkotlinx/coroutines/DisposableHandle; + public fun invokeOnTimeout (JLjava/lang/Runnable;Lkotlin/coroutines/CoroutineContext;)Lkotlinx/coroutines/DisposableHandle; public fun scheduleResumeAfterDelay (JLkotlinx/coroutines/CancellableContinuation;)V public fun toString ()Ljava/lang/String; } diff --git a/reactive/kotlinx-coroutines-rx2/build.gradle b/reactive/kotlinx-coroutines-rx2/build.gradle index b583025ce3..6d2c4abcc8 100644 --- a/reactive/kotlinx-coroutines-rx2/build.gradle +++ b/reactive/kotlinx-coroutines-rx2/build.gradle @@ -11,7 +11,7 @@ dependencies { tasks.withType(dokka.getClass()) { externalDocumentationLink { - url = new URL('http://reactivex.io/RxJava/javadoc/') + url = new URL('http://reactivex.io/RxJava/2.x/javadoc/') packageListUrl = projectDir.toPath().resolve("package.list").toUri().toURL() } } diff --git a/reactive/kotlinx-coroutines-rx2/src/RxChannel.kt b/reactive/kotlinx-coroutines-rx2/src/RxChannel.kt index 9f31b2aaa8..633693e756 100644 --- a/reactive/kotlinx-coroutines-rx2/src/RxChannel.kt +++ b/reactive/kotlinx-coroutines-rx2/src/RxChannel.kt @@ -40,31 +40,31 @@ public fun ObservableSource.openSubscription(): ReceiveChannel { // Will be promoted to error in 1.3.0, removed in 1.4.0 @Deprecated(message = "Use collect instead", level = DeprecationLevel.ERROR, replaceWith = ReplaceWith("this.collect(action)")) -public suspend inline fun MaybeSource.consumeEach(action: (T) -> Unit) = +public suspend inline fun MaybeSource.consumeEach(action: (T) -> Unit): Unit = openSubscription().consumeEach(action) // Will be promoted to error in 1.3.0, removed in 1.4.0 @Deprecated(message = "Use collect instead", level = DeprecationLevel.ERROR, replaceWith = ReplaceWith("this.collect(action)")) -public suspend inline fun ObservableSource.consumeEach(action: (T) -> Unit) = +public suspend inline fun ObservableSource.consumeEach(action: (T) -> Unit): Unit = openSubscription().consumeEach(action) /** * Subscribes to this [MaybeSource] and performs the specified action for each received element. * Cancels subscription if any exception happens during collect. */ -public suspend inline fun MaybeSource.collect(action: (T) -> Unit) = +public suspend inline fun MaybeSource.collect(action: (T) -> Unit): Unit = openSubscription().consumeEach(action) /** * Subscribes to this [ObservableSource] and performs the specified action for each received element. * Cancels subscription if any exception happens during collect. */ -public suspend inline fun ObservableSource.collect(action: (T) -> Unit) = +public suspend inline fun ObservableSource.collect(action: (T) -> Unit): Unit = openSubscription().consumeEach(action) @Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") private class SubscriptionChannel : - LinkedListChannel(), Observer, MaybeObserver + LinkedListChannel(null), Observer, MaybeObserver { private val _subscription = atomic(null) diff --git a/reactive/kotlinx-coroutines-rx2/src/RxConvert.kt b/reactive/kotlinx-coroutines-rx2/src/RxConvert.kt index 0be606ffc2..41c82ed0e8 100644 --- a/reactive/kotlinx-coroutines-rx2/src/RxConvert.kt +++ b/reactive/kotlinx-coroutines-rx2/src/RxConvert.kt @@ -10,12 +10,13 @@ import kotlinx.coroutines.* import kotlinx.coroutines.channels.* import kotlinx.coroutines.flow.* import kotlinx.coroutines.reactive.* +import org.reactivestreams.* import java.util.concurrent.atomic.* import kotlin.coroutines.* /** * Converts this job to the hot reactive completable that signals - * with [onCompleted][CompletableSubscriber.onCompleted] when the corresponding job completes. + * with [onCompleted][CompletableObserver.onComplete] when the corresponding job completes. * * Every subscriber gets the signal at the same time. * Unsubscribing from the resulting completable **does not** affect the original job in any way. @@ -49,7 +50,7 @@ public fun Deferred.asMaybe(context: CoroutineContext): Maybe = rxMay /** * Converts this deferred value to the hot reactive single that signals either - * [onSuccess][SingleSubscriber.onSuccess] or [onError][SingleSubscriber.onError]. + * [onSuccess][SingleObserver.onSuccess] or [onError][SingleObserver.onError]. * * Every subscriber gets the same completion value. * Unsubscribing from the resulting single **does not** affect the original deferred value in any way. @@ -64,21 +65,6 @@ public fun Deferred.asSingle(context: CoroutineContext): Single this@asSingle.await() } -/** - * Converts a stream of elements received from the channel to the hot reactive observable. - * - * Every subscriber receives values from this channel in **fan-out** fashion. If the are multiple subscribers, - * they'll receive values in round-robin way. - */ -@Deprecated( - message = "Deprecated in the favour of Flow", - level = DeprecationLevel.WARNING, replaceWith = ReplaceWith("this.consumeAsFlow().asObservable()") -) -public fun ReceiveChannel.asObservable(context: CoroutineContext): Observable = rxObservable(context) { - for (t in this@asObservable) - send(t) -} - /** * Transforms given cold [ObservableSource] into cold [Flow]. * @@ -95,7 +81,13 @@ public fun ObservableSource.asFlow(): Flow = callbackFlow { val observer = object : Observer { override fun onComplete() { close() } override fun onSubscribe(d: Disposable) { if (!disposableRef.compareAndSet(null, d)) d.dispose() } - override fun onNext(t: T) { sendBlocking(t) } + override fun onNext(t: T) { + try { + sendBlocking(t) + } catch (ignored: Throwable) { // TODO: Replace when this issue is fixed: https://github.com/Kotlin/kotlinx.coroutines/issues/974 + // Is handled by the downstream flow + } + } override fun onError(e: Throwable) { close(e) } } @@ -106,15 +98,19 @@ public fun ObservableSource.asFlow(): Flow = callbackFlow { /** * Converts the given flow to a cold observable. * The original flow is cancelled when the observable subscriber is disposed. + * + * An optional [context] can be specified to control the execution context of calls to [Observer] methods. + * You can set a [CoroutineDispatcher] to confine them to a specific thread and/or various [ThreadContextElement] to + * inject additional context into the caller thread. By default, the [Unconfined][Dispatchers.Unconfined] dispatcher + * is used, so calls are performed from an arbitrary thread. */ -@JvmName("from") @ExperimentalCoroutinesApi -public fun Flow.asObservable() : Observable = Observable.create { emitter -> +public fun Flow.asObservable(context: CoroutineContext = EmptyCoroutineContext) : Observable = Observable.create { emitter -> /* * ATOMIC is used here to provide stable behaviour of subscribe+dispose pair even if * asObservable is already invoked from unconfined */ - val job = GlobalScope.launch(Dispatchers.Unconfined, start = CoroutineStart.ATOMIC) { + val job = GlobalScope.launch(Dispatchers.Unconfined + context, start = CoroutineStart.ATOMIC) { try { collect { value -> emitter.onNext(value) } emitter.onComplete() @@ -135,7 +131,35 @@ public fun Flow.asObservable() : Observable = Observable.create { /** * Converts the given flow to a cold flowable. * The original flow is cancelled when the flowable subscriber is disposed. + * + * An optional [context] can be specified to control the execution context of calls to [Subscriber] methods. + * You can set a [CoroutineDispatcher] to confine them to a specific thread and/or various [ThreadContextElement] to + * inject additional context into the caller thread. By default, the [Unconfined][Dispatchers.Unconfined] dispatcher + * is used, so calls are performed from an arbitrary thread. */ -@JvmName("from") @ExperimentalCoroutinesApi -public fun Flow.asFlowable(): Flowable = Flowable.fromPublisher(asPublisher()) +public fun Flow.asFlowable(context: CoroutineContext = EmptyCoroutineContext): Flowable = + Flowable.fromPublisher(asPublisher(context)) + +@Deprecated( + message = "Deprecated in the favour of Flow", + level = DeprecationLevel.ERROR, + replaceWith = ReplaceWith("this.consumeAsFlow().asObservable(context)", "kotlinx.coroutines.flow.consumeAsFlow") +) // Deprecated since 1.4.0 +public fun ReceiveChannel.asObservable(context: CoroutineContext): Observable = rxObservable(context) { + for (t in this@asObservable) + send(t) +} + +@Suppress("UNUSED") // KT-42513 +@JvmOverloads // binary compatibility +@JvmName("from") +@Deprecated(level = DeprecationLevel.HIDDEN, message = "") // Since 1.4, was experimental prior to that +public fun Flow._asFlowable(context: CoroutineContext = EmptyCoroutineContext): Flowable = + asFlowable(context) + +@Suppress("UNUSED") // KT-42513 +@JvmOverloads // binary compatibility +@JvmName("from") +@Deprecated(level = DeprecationLevel.HIDDEN, message = "") // Since 1.4, was experimental prior to that +public fun Flow._asObservable(context: CoroutineContext = EmptyCoroutineContext) : Observable = asObservable(context) diff --git a/reactive/kotlinx-coroutines-rx2/src/RxScheduler.kt b/reactive/kotlinx-coroutines-rx2/src/RxScheduler.kt index 3ddb67649e..9952eb91a0 100644 --- a/reactive/kotlinx-coroutines-rx2/src/RxScheduler.kt +++ b/reactive/kotlinx-coroutines-rx2/src/RxScheduler.kt @@ -38,7 +38,7 @@ public class SchedulerCoroutineDispatcher( } /** @suppress */ - override fun invokeOnTimeout(timeMillis: Long, block: Runnable): DisposableHandle { + override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle { val disposable = scheduler.scheduleDirect(block, timeMillis, TimeUnit.MILLISECONDS) return DisposableHandle { disposable.dispose() } } diff --git a/reactive/kotlinx-coroutines-rx2/test/ConvertTest.kt b/reactive/kotlinx-coroutines-rx2/test/ConvertTest.kt index a43366555e..cfc3240741 100644 --- a/reactive/kotlinx-coroutines-rx2/test/ConvertTest.kt +++ b/reactive/kotlinx-coroutines-rx2/test/ConvertTest.kt @@ -6,6 +6,7 @@ package kotlinx.coroutines.rx2 import kotlinx.coroutines.* import kotlinx.coroutines.channels.* +import kotlinx.coroutines.flow.* import org.junit.Assert import org.junit.Test import kotlin.test.* @@ -126,7 +127,7 @@ class ConvertTest : TestBase() { delay(50) send("K") } - val observable = c.asObservable(Dispatchers.Unconfined) + val observable = c.consumeAsFlow().asObservable(Dispatchers.Unconfined) checkSingleValue(observable.reduce { t1, t2 -> t1 + t2 }.toSingle()) { assertEquals("OK", it) } @@ -140,7 +141,7 @@ class ConvertTest : TestBase() { delay(50) throw TestException("K") } - val observable = c.asObservable(Dispatchers.Unconfined) + val observable = c.consumeAsFlow().asObservable(Dispatchers.Unconfined) val single = rxSingle(Dispatchers.Unconfined) { var result = "" try { @@ -155,4 +156,4 @@ class ConvertTest : TestBase() { assertEquals("OK", it) } } -} \ No newline at end of file +} diff --git a/reactive/kotlinx-coroutines-rx2/test/FlowAsFlowableTest.kt b/reactive/kotlinx-coroutines-rx2/test/FlowAsFlowableTest.kt new file mode 100644 index 0000000000..1cbded6dc3 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx2/test/FlowAsFlowableTest.kt @@ -0,0 +1,89 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx2 + +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* +import org.junit.Test +import org.reactivestreams.* +import java.util.concurrent.* +import kotlin.test.* + +@Suppress("ReactiveStreamsSubscriberImplementation") +class FlowAsFlowableTest : TestBase() { + @Test + fun testUnconfinedDefaultContext() { + expect(1) + val thread = Thread.currentThread() + fun checkThread() { + assertSame(thread, Thread.currentThread()) + } + flowOf(42).asFlowable().subscribe(object : Subscriber { + private lateinit var subscription: Subscription + + override fun onSubscribe(s: Subscription) { + expect(2) + subscription = s + subscription.request(2) + } + + override fun onNext(t: Int) { + checkThread() + expect(3) + assertEquals(42, t) + } + + override fun onComplete() { + checkThread() + expect(4) + } + + override fun onError(t: Throwable?) { + expectUnreached() + } + }) + finish(5) + } + + @Test + fun testConfinedContext() { + expect(1) + val threadName = "FlowAsFlowableTest.testConfinedContext" + fun checkThread() { + val currentThread = Thread.currentThread() + assertTrue(currentThread.name.startsWith(threadName), "Unexpected thread $currentThread") + } + val completed = CountDownLatch(1) + newSingleThreadContext(threadName).use { dispatcher -> + flowOf(42).asFlowable(dispatcher).subscribe(object : Subscriber { + private lateinit var subscription: Subscription + + override fun onSubscribe(s: Subscription) { + expect(2) + subscription = s + subscription.request(2) + } + + override fun onNext(t: Int) { + checkThread() + expect(3) + assertEquals(42, t) + } + + override fun onComplete() { + checkThread() + expect(4) + completed.countDown() + } + + override fun onError(t: Throwable?) { + expectUnreached() + } + }) + completed.await() + } + finish(5) + } +} diff --git a/reactive/kotlinx-coroutines-rx2/test/FlowAsObservableTest.kt b/reactive/kotlinx-coroutines-rx2/test/FlowAsObservableTest.kt index 0908b34cf2..3cde182260 100644 --- a/reactive/kotlinx-coroutines-rx2/test/FlowAsObservableTest.kt +++ b/reactive/kotlinx-coroutines-rx2/test/FlowAsObservableTest.kt @@ -4,9 +4,12 @@ package kotlinx.coroutines.rx2 +import io.reactivex.* +import io.reactivex.disposables.* import kotlinx.coroutines.* import kotlinx.coroutines.flow.* import org.junit.Test +import java.util.concurrent.* import kotlin.test.* class FlowAsObservableTest : TestBase() { @@ -139,4 +142,70 @@ class FlowAsObservableTest : TestBase() { observable.subscribe({ expect(2) }, { expectUnreached() }, { finish(3) }) } + + @Test + fun testUnconfinedDefaultContext() { + expect(1) + val thread = Thread.currentThread() + fun checkThread() { + assertSame(thread, Thread.currentThread()) + } + flowOf(42).asObservable().subscribe(object : Observer { + override fun onSubscribe(d: Disposable) { + expect(2) + } + + override fun onNext(t: Int) { + checkThread() + expect(3) + assertEquals(42, t) + } + + override fun onComplete() { + checkThread() + expect(4) + } + + override fun onError(t: Throwable) { + expectUnreached() + } + }) + finish(5) + } + + @Test + fun testConfinedContext() { + expect(1) + val threadName = "FlowAsObservableTest.testConfinedContext" + fun checkThread() { + val currentThread = Thread.currentThread() + assertTrue(currentThread.name.startsWith(threadName), "Unexpected thread $currentThread") + } + val completed = CountDownLatch(1) + newSingleThreadContext(threadName).use { dispatcher -> + flowOf(42).asObservable(dispatcher).subscribe(object : Observer { + override fun onSubscribe(d: Disposable) { + expect(2) + } + + override fun onNext(t: Int) { + checkThread() + expect(3) + assertEquals(42, t) + } + + override fun onComplete() { + checkThread() + expect(4) + completed.countDown() + } + + override fun onError(e: Throwable) { + expectUnreached() + } + }) + completed.await() + } + finish(5) + } } diff --git a/reactive/kotlinx-coroutines-rx2/test/IntegrationTest.kt b/reactive/kotlinx-coroutines-rx2/test/IntegrationTest.kt index 22e0e72191..540fa76b7e 100644 --- a/reactive/kotlinx-coroutines-rx2/test/IntegrationTest.kt +++ b/reactive/kotlinx-coroutines-rx2/test/IntegrationTest.kt @@ -6,6 +6,7 @@ package kotlinx.coroutines.rx2 import io.reactivex.* import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* import org.junit.Test import org.junit.runner.* import org.junit.runners.* @@ -92,7 +93,7 @@ class IntegrationTest( assertFailsWith { observable.awaitSingle() } checkNumbers(n, observable) val channel = observable.openSubscription() - checkNumbers(n, channel.asObservable(ctx(coroutineContext))) + checkNumbers(n, channel.consumeAsFlow().asObservable(ctx(coroutineContext))) channel.cancel() } @@ -131,4 +132,4 @@ class IntegrationTest( assertEquals(n, last) } -} \ No newline at end of file +} diff --git a/reactive/kotlinx-coroutines-rx2/test/ObservableSourceAsFlowStressTest.kt b/reactive/kotlinx-coroutines-rx2/test/ObservableSourceAsFlowStressTest.kt new file mode 100644 index 0000000000..159f3729c8 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx2/test/ObservableSourceAsFlowStressTest.kt @@ -0,0 +1,35 @@ +/* + * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx2 + +import io.reactivex.* +import kotlinx.coroutines.* +import kotlinx.coroutines.channels.* +import kotlinx.coroutines.flow.* +import org.junit.* +import java.util.concurrent.* + +class ObservableSourceAsFlowStressTest : TestBase() { + + private val iterations = 100 * stressTestMultiplierSqrt + + @Before + fun setup() { + ignoreLostThreads("RxComputationThreadPool-", "RxCachedWorkerPoolEvictor-", "RxSchedulerPurge-") + } + + @Test + fun testAsFlowCancellation() = runTest { + repeat(iterations) { + val latch = Channel(1) + var i = 0 + val observable = Observable.interval(100L, TimeUnit.MICROSECONDS) + .doOnNext { if (++i > 100) latch.offer(Unit) } + val job = observable.asFlow().launchIn(CoroutineScope(Dispatchers.Default)) + latch.receive() + job.cancelAndJoin() + } + } +} diff --git a/reactive/kotlinx-coroutines-rx3/README.md b/reactive/kotlinx-coroutines-rx3/README.md new file mode 100644 index 0000000000..3aa73eb969 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/README.md @@ -0,0 +1,84 @@ +# Module kotlinx-coroutines-rx3 + +Utilities for [RxJava 3.x](https://github.com/ReactiveX/RxJava). + +Coroutine builders: + +| **Name** | **Result** | **Scope** | **Description** +| --------------- | --------------------------------------- | ---------------- | --------------- +| [rxCompletable] | `Completable` | [CoroutineScope] | Cold completable that starts coroutine on subscribe +| [rxMaybe] | `Maybe` | [CoroutineScope] | Cold maybe that starts coroutine on subscribe +| [rxSingle] | `Single` | [CoroutineScope] | Cold single that starts coroutine on subscribe +| [rxObservable] | `Observable` | [ProducerScope] | Cold observable that starts coroutine on subscribe +| [rxFlowable] | `Flowable` | [ProducerScope] | Cold observable that starts coroutine on subscribe with **backpressure** support + +Integration with [Flow]: + +| **Name** | **Result** | **Description** +| --------------- | -------------- | --------------- +| [Flow.asFlowable] | `Flowable` | Converts the given flow to a cold Flowable. +| [Flow.asObservable] | `Observable` | Converts the given flow to a cold Observable. +| [ObservableSource.asFlow] | `Flow` | Converts the given cold ObservableSource to flow + +Suspending extension functions and suspending iteration: + +| **Name** | **Description** +| -------- | --------------- +| [CompletableSource.await][io.reactivex.rxjava3.core.CompletableSource.await] | Awaits for completion of the completable value +| [MaybeSource.await][io.reactivex.rxjava3.core.MaybeSource.await] | Awaits for the value of the maybe and returns it or null +| [MaybeSource.awaitOrDefault][io.reactivex.rxjava3.core.MaybeSource.awaitOrDefault] | Awaits for the value of the maybe and returns it or default +| [SingleSource.await][io.reactivex.rxjava3.core.SingleSource.await] | Awaits for completion of the single value and returns it +| [ObservableSource.awaitFirst][io.reactivex.rxjava3.core.ObservableSource.awaitFirst] | Awaits for the first value from the given observable +| [ObservableSource.awaitFirstOrDefault][io.reactivex.rxjava3.core.ObservableSource.awaitFirstOrDefault] | Awaits for the first value from the given observable or default +| [ObservableSource.awaitFirstOrElse][io.reactivex.rxjava3.core.ObservableSource.awaitFirstOrElse] | Awaits for the first value from the given observable or default from a function +| [ObservableSource.awaitFirstOrNull][io.reactivex.rxjava3.core.ObservableSource.awaitFirstOrNull] | Awaits for the first value from the given observable or null +| [ObservableSource.awaitLast][io.reactivex.rxjava3.core.ObservableSource.awaitFirst] | Awaits for the last value from the given observable +| [ObservableSource.awaitSingle][io.reactivex.rxjava3.core.ObservableSource.awaitSingle] | Awaits for the single value from the given observable + +Note that `Flowable` is a subclass of [Reactive Streams](https://www.reactive-streams.org) +`Publisher` and extensions for it are covered by +[kotlinx-coroutines-reactive](../kotlinx-coroutines-reactive) module. + +Conversion functions: + +| **Name** | **Description** +| -------- | --------------- +| [Job.asCompletable][kotlinx.coroutines.Job.asCompletable] | Converts job to hot completable +| [Deferred.asSingle][kotlinx.coroutines.Deferred.asSingle] | Converts deferred value to hot single +| [Scheduler.asCoroutineDispatcher][io.reactivex.rxjava3.core.Scheduler.asCoroutineDispatcher] | Converts scheduler to [CoroutineDispatcher] + + + +[CoroutineScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html +[CoroutineDispatcher]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-dispatcher/index.html + +[ProducerScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.channels/-producer-scope/index.html + +[Flow]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html + + +[rxCompletable]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx3/kotlinx.coroutines.rx3/rx-completable.html +[rxMaybe]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx3/kotlinx.coroutines.rx3/rx-maybe.html +[rxSingle]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx3/kotlinx.coroutines.rx3/rx-single.html +[rxObservable]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx3/kotlinx.coroutines.rx3/rx-observable.html +[rxFlowable]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx3/kotlinx.coroutines.rx3/rx-flowable.html +[Flow.asFlowable]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx3/kotlinx.coroutines.rx3/kotlinx.coroutines.flow.-flow/as-flowable.html +[Flow.asObservable]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx3/kotlinx.coroutines.rx3/kotlinx.coroutines.flow.-flow/as-observable.html +[ObservableSource.asFlow]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx3/kotlinx.coroutines.rx3/io.reactivex.rxjava3.core.-observable-source/as-flow.html +[io.reactivex.rxjava3.core.CompletableSource.await]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx3/kotlinx.coroutines.rx3/io.reactivex.rxjava3.core.-completable-source/await.html +[io.reactivex.rxjava3.core.MaybeSource.await]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx3/kotlinx.coroutines.rx3/io.reactivex.rxjava3.core.-maybe-source/await.html +[io.reactivex.rxjava3.core.MaybeSource.awaitOrDefault]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx3/kotlinx.coroutines.rx3/io.reactivex.rxjava3.core.-maybe-source/await-or-default.html +[io.reactivex.rxjava3.core.SingleSource.await]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx3/kotlinx.coroutines.rx3/io.reactivex.rxjava3.core.-single-source/await.html +[io.reactivex.rxjava3.core.ObservableSource.awaitFirst]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx3/kotlinx.coroutines.rx3/io.reactivex.rxjava3.core.-observable-source/await-first.html +[io.reactivex.rxjava3.core.ObservableSource.awaitFirstOrDefault]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx3/kotlinx.coroutines.rx3/io.reactivex.rxjava3.core.-observable-source/await-first-or-default.html +[io.reactivex.rxjava3.core.ObservableSource.awaitFirstOrElse]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx3/kotlinx.coroutines.rx3/io.reactivex.rxjava3.core.-observable-source/await-first-or-else.html +[io.reactivex.rxjava3.core.ObservableSource.awaitFirstOrNull]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx3/kotlinx.coroutines.rx3/io.reactivex.rxjava3.core.-observable-source/await-first-or-null.html +[io.reactivex.rxjava3.core.ObservableSource.awaitSingle]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx3/kotlinx.coroutines.rx3/io.reactivex.rxjava3.core.-observable-source/await-single.html +[kotlinx.coroutines.Job.asCompletable]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx3/kotlinx.coroutines.rx3/kotlinx.coroutines.-job/as-completable.html +[kotlinx.coroutines.Deferred.asSingle]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx3/kotlinx.coroutines.rx3/kotlinx.coroutines.-deferred/as-single.html +[io.reactivex.rxjava3.core.Scheduler.asCoroutineDispatcher]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx3/kotlinx.coroutines.rx3/io.reactivex.rxjava3.core.-scheduler/as-coroutine-dispatcher.html + + +# Package kotlinx.coroutines.rx3 + +Utilities for [RxJava 3.x](https://github.com/ReactiveX/RxJava). diff --git a/reactive/kotlinx-coroutines-rx3/api/kotlinx-coroutines-rx3.api b/reactive/kotlinx-coroutines-rx3/api/kotlinx-coroutines-rx3.api new file mode 100644 index 0000000000..6d2dd63d2c --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/api/kotlinx-coroutines-rx3.api @@ -0,0 +1,78 @@ +public final class kotlinx/coroutines/rx3/RxAwaitKt { + public static final fun await (Lio/reactivex/rxjava3/core/CompletableSource;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun await (Lio/reactivex/rxjava3/core/MaybeSource;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun await (Lio/reactivex/rxjava3/core/SingleSource;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun awaitFirst (Lio/reactivex/rxjava3/core/ObservableSource;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun awaitFirstOrDefault (Lio/reactivex/rxjava3/core/ObservableSource;Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun awaitFirstOrElse (Lio/reactivex/rxjava3/core/ObservableSource;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun awaitFirstOrNull (Lio/reactivex/rxjava3/core/ObservableSource;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun awaitLast (Lio/reactivex/rxjava3/core/ObservableSource;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun awaitOrDefault (Lio/reactivex/rxjava3/core/MaybeSource;Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun awaitSingle (Lio/reactivex/rxjava3/core/ObservableSource;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; +} + +public final class kotlinx/coroutines/rx3/RxChannelKt { + public static final fun collect (Lio/reactivex/rxjava3/core/MaybeSource;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun collect (Lio/reactivex/rxjava3/core/ObservableSource;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun openSubscription (Lio/reactivex/rxjava3/core/MaybeSource;)Lkotlinx/coroutines/channels/ReceiveChannel; + public static final fun openSubscription (Lio/reactivex/rxjava3/core/ObservableSource;)Lkotlinx/coroutines/channels/ReceiveChannel; +} + +public final class kotlinx/coroutines/rx3/RxCompletableKt { + public static final fun rxCompletable (Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;)Lio/reactivex/rxjava3/core/Completable; + public static synthetic fun rxCompletable$default (Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lio/reactivex/rxjava3/core/Completable; +} + +public final class kotlinx/coroutines/rx3/RxConvertKt { + public static final fun asCompletable (Lkotlinx/coroutines/Job;Lkotlin/coroutines/CoroutineContext;)Lio/reactivex/rxjava3/core/Completable; + public static final fun asFlow (Lio/reactivex/rxjava3/core/ObservableSource;)Lkotlinx/coroutines/flow/Flow; + public static final fun asFlowable (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;)Lio/reactivex/rxjava3/core/Flowable; + public static synthetic fun asFlowable$default (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;ILjava/lang/Object;)Lio/reactivex/rxjava3/core/Flowable; + public static final fun asMaybe (Lkotlinx/coroutines/Deferred;Lkotlin/coroutines/CoroutineContext;)Lio/reactivex/rxjava3/core/Maybe; + public static final fun asObservable (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;)Lio/reactivex/rxjava3/core/Observable; + public static synthetic fun asObservable$default (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;ILjava/lang/Object;)Lio/reactivex/rxjava3/core/Observable; + public static final fun asSingle (Lkotlinx/coroutines/Deferred;Lkotlin/coroutines/CoroutineContext;)Lio/reactivex/rxjava3/core/Single; + public static final synthetic fun from (Lkotlinx/coroutines/flow/Flow;)Lio/reactivex/rxjava3/core/Flowable; + public static final synthetic fun from (Lkotlinx/coroutines/flow/Flow;)Lio/reactivex/rxjava3/core/Observable; + public static final synthetic fun from (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;)Lio/reactivex/rxjava3/core/Flowable; + public static final synthetic fun from (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;)Lio/reactivex/rxjava3/core/Observable; + public static synthetic fun from$default (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;ILjava/lang/Object;)Lio/reactivex/rxjava3/core/Flowable; + public static synthetic fun from$default (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;ILjava/lang/Object;)Lio/reactivex/rxjava3/core/Observable; +} + +public final class kotlinx/coroutines/rx3/RxFlowableKt { + public static final fun rxFlowable (Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;)Lio/reactivex/rxjava3/core/Flowable; + public static synthetic fun rxFlowable$default (Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lio/reactivex/rxjava3/core/Flowable; +} + +public final class kotlinx/coroutines/rx3/RxMaybeKt { + public static final fun rxMaybe (Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;)Lio/reactivex/rxjava3/core/Maybe; + public static synthetic fun rxMaybe$default (Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lio/reactivex/rxjava3/core/Maybe; +} + +public final class kotlinx/coroutines/rx3/RxObservableKt { + public static final fun rxObservable (Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;)Lio/reactivex/rxjava3/core/Observable; + public static synthetic fun rxObservable$default (Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lio/reactivex/rxjava3/core/Observable; +} + +public final class kotlinx/coroutines/rx3/RxSchedulerKt { + public static final fun asCoroutineDispatcher (Lio/reactivex/rxjava3/core/Scheduler;)Lkotlinx/coroutines/rx3/SchedulerCoroutineDispatcher; +} + +public final class kotlinx/coroutines/rx3/RxSingleKt { + public static final fun rxSingle (Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;)Lio/reactivex/rxjava3/core/Single; + public static synthetic fun rxSingle$default (Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lio/reactivex/rxjava3/core/Single; +} + +public final class kotlinx/coroutines/rx3/SchedulerCoroutineDispatcher : kotlinx/coroutines/CoroutineDispatcher, kotlinx/coroutines/Delay { + public fun (Lio/reactivex/rxjava3/core/Scheduler;)V + public fun delay (JLkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun dispatch (Lkotlin/coroutines/CoroutineContext;Ljava/lang/Runnable;)V + public fun equals (Ljava/lang/Object;)Z + public final fun getScheduler ()Lio/reactivex/rxjava3/core/Scheduler; + public fun hashCode ()I + public fun invokeOnTimeout (JLjava/lang/Runnable;Lkotlin/coroutines/CoroutineContext;)Lkotlinx/coroutines/DisposableHandle; + public fun scheduleResumeAfterDelay (JLkotlinx/coroutines/CancellableContinuation;)V + public fun toString ()Ljava/lang/String; +} + diff --git a/reactive/kotlinx-coroutines-reactive/build.gradle b/reactive/kotlinx-coroutines-rx3/build.gradle similarity index 68% rename from reactive/kotlinx-coroutines-reactive/build.gradle rename to reactive/kotlinx-coroutines-rx3/build.gradle index ad97c63f45..ced694abd3 100644 --- a/reactive/kotlinx-coroutines-reactive/build.gradle +++ b/reactive/kotlinx-coroutines-rx3/build.gradle @@ -1,10 +1,28 @@ /* * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ +targetCompatibility = JavaVersion.VERSION_1_8 dependencies { - compile "org.reactivestreams:reactive-streams:$reactive_streams_version" + compile project(':kotlinx-coroutines-reactive') + testCompile project(':kotlinx-coroutines-reactive').sourceSets.test.output testCompile "org.reactivestreams:reactive-streams-tck:$reactive_streams_version" + compile "io.reactivex.rxjava3:rxjava:$rxjava3_version" +} + +compileTestKotlin { + kotlinOptions.jvmTarget = "1.8" +} + +compileKotlin { + kotlinOptions.jvmTarget = "1.8" +} + +tasks.withType(dokka.getClass()) { + externalDocumentationLink { + url = new URL('http://reactivex.io/RxJava/3.x/javadoc/') + packageListUrl = projectDir.toPath().resolve("package.list").toUri().toURL() + } } task testNG(type: Test) { @@ -25,10 +43,3 @@ test { dependsOn(testNG) reports.html.destination = file("$buildDir/reports/junit") } - -tasks.withType(dokka.getClass()) { - externalDocumentationLink { - url = new URL("https://www.reactive-streams.org/reactive-streams-$reactive_streams_version-javadoc/") - packageListUrl = projectDir.toPath().resolve("package.list").toUri().toURL() - } -} diff --git a/reactive/kotlinx-coroutines-rx3/package.list b/reactive/kotlinx-coroutines-rx3/package.list new file mode 100644 index 0000000000..889916d0db --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/package.list @@ -0,0 +1,14 @@ +io.reactivex.rxjava3.core +io.reactivex.rxjava3.annotations +io.reactivex.rxjava3.disposables +io.reactivex.rxjava3.exceptions +io.reactivex.rxjava3.flowables +io.reactivex.rxjava3.functions +io.reactivex.rxjava3.observables +io.reactivex.rxjava3.observers +io.reactivex.rxjava3.parallel +io.reactivex.rxjava3.plugins +io.reactivex.rxjava3.processors +io.reactivex.rxjava3.schedulers +io.reactivex.rxjava3.subjects +io.reactivex.rxjava3.subscribers diff --git a/reactive/kotlinx-coroutines-rx3/src/RxAwait.kt b/reactive/kotlinx-coroutines-rx3/src/RxAwait.kt new file mode 100644 index 0000000000..e52556e4e9 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/src/RxAwait.kt @@ -0,0 +1,220 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.core.* +import io.reactivex.rxjava3.disposables.Disposable +import kotlinx.coroutines.CancellableContinuation +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.Job +import kotlinx.coroutines.suspendCancellableCoroutine +import kotlin.coroutines.* + +// ------------------------ CompletableSource ------------------------ + +/** + * Awaits for completion of this completable without blocking a thread. + * Returns `Unit` or throws the corresponding exception if this completable had produced error. + * + * This suspending function is cancellable. If the [Job] of the invoking coroutine is cancelled or completed while this + * suspending function is suspended, this function immediately resumes with [CancellationException]. + */ +public suspend fun CompletableSource.await(): Unit = suspendCancellableCoroutine { cont -> + subscribe(object : CompletableObserver { + override fun onSubscribe(d: Disposable) { cont.disposeOnCancellation(d) } + override fun onComplete() { cont.resume(Unit) } + override fun onError(e: Throwable) { cont.resumeWithException(e) } + }) +} + +// ------------------------ MaybeSource ------------------------ + +/** + * Awaits for completion of the maybe without blocking a thread. + * Returns the resulting value, null if no value was produced or throws the corresponding exception if this + * maybe had produced error. + * + * This suspending function is cancellable. + * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function + * immediately resumes with [CancellationException]. + */ +@Suppress("UNCHECKED_CAST") +public suspend fun MaybeSource.await(): T? = (this as MaybeSource).awaitOrDefault(null) + +/** + * Awaits for completion of the maybe without blocking a thread. + * Returns the resulting value, [default] if no value was produced or throws the corresponding exception if this + * maybe had produced error. + * + * This suspending function is cancellable. + * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function + * immediately resumes with [CancellationException]. + */ +public suspend fun MaybeSource.awaitOrDefault(default: T): T = suspendCancellableCoroutine { cont -> + subscribe(object : MaybeObserver { + override fun onSubscribe(d: Disposable) { cont.disposeOnCancellation(d) } + override fun onComplete() { cont.resume(default) } + override fun onSuccess(t: T) { cont.resume(t) } + override fun onError(error: Throwable) { cont.resumeWithException(error) } + }) +} + +// ------------------------ SingleSource ------------------------ + +/** + * Awaits for completion of the single value without blocking a thread. + * Returns the resulting value or throws the corresponding exception if this single had produced error. + * + * This suspending function is cancellable. + * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function + * immediately resumes with [CancellationException]. + */ +public suspend fun SingleSource.await(): T = suspendCancellableCoroutine { cont -> + subscribe(object : SingleObserver { + override fun onSubscribe(d: Disposable) { cont.disposeOnCancellation(d) } + override fun onSuccess(t: T) { cont.resume(t) } + override fun onError(error: Throwable) { cont.resumeWithException(error) } + }) +} + +// ------------------------ ObservableSource ------------------------ + +/** + * Awaits for the first value from the given observable without blocking a thread. + * Returns the resulting value or throws the corresponding exception if this observable had produced error. + * + * This suspending function is cancellable. + * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function + * immediately resumes with [CancellationException]. + * + * @throws NoSuchElementException if observable does not emit any value + */ +public suspend fun ObservableSource.awaitFirst(): T = awaitOne(Mode.FIRST) + +/** + * Awaits for the first value from the given observable or the [default] value if none is emitted without blocking a + * thread and returns the resulting value or throws the corresponding exception if this observable had produced error. + * + * This suspending function is cancellable. + * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function + * immediately resumes with [CancellationException]. + */ +public suspend fun ObservableSource.awaitFirstOrDefault(default: T): T = awaitOne(Mode.FIRST_OR_DEFAULT, default) + +/** + * Awaits for the first value from the given observable or `null` value if none is emitted without blocking a + * thread and returns the resulting value or throws the corresponding exception if this observable had produced error. + * + * This suspending function is cancellable. + * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function + * immediately resumes with [CancellationException]. + */ +public suspend fun ObservableSource.awaitFirstOrNull(): T? = awaitOne(Mode.FIRST_OR_DEFAULT) + +/** + * Awaits for the first value from the given observable or call [defaultValue] to get a value if none is emitted without blocking a + * thread and returns the resulting value or throws the corresponding exception if this observable had produced error. + * + * This suspending function is cancellable. + * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function + * immediately resumes with [CancellationException]. + */ +public suspend fun ObservableSource.awaitFirstOrElse(defaultValue: () -> T): T = awaitOne(Mode.FIRST_OR_DEFAULT) ?: defaultValue() + +/** + * Awaits for the last value from the given observable without blocking a thread. + * Returns the resulting value or throws the corresponding exception if this observable had produced error. + * + * This suspending function is cancellable. + * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function + * immediately resumes with [CancellationException]. + * + * @throws NoSuchElementException if observable does not emit any value + */ +public suspend fun ObservableSource.awaitLast(): T = awaitOne(Mode.LAST) + +/** + * Awaits for the single value from the given observable without blocking a thread. + * Returns the resulting value or throws the corresponding exception if this observable had produced error. + * + * This suspending function is cancellable. + * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function + * immediately resumes with [CancellationException]. + * + * @throws NoSuchElementException if observable does not emit any value + * @throws IllegalArgumentException if observable emits more than one value + */ +public suspend fun ObservableSource.awaitSingle(): T = awaitOne(Mode.SINGLE) + +// ------------------------ private ------------------------ + +internal fun CancellableContinuation<*>.disposeOnCancellation(d: Disposable) = + invokeOnCancellation { d.dispose() } + +private enum class Mode(val s: String) { + FIRST("awaitFirst"), + FIRST_OR_DEFAULT("awaitFirstOrDefault"), + LAST("awaitLast"), + SINGLE("awaitSingle"); + override fun toString(): String = s +} + +private suspend fun ObservableSource.awaitOne( + mode: Mode, + default: T? = null +): T = suspendCancellableCoroutine { cont -> + subscribe(object : Observer { + private lateinit var subscription: Disposable + private var value: T? = null + private var seenValue = false + + override fun onSubscribe(sub: Disposable) { + subscription = sub + cont.invokeOnCancellation { sub.dispose() } + } + + override fun onNext(t: T) { + when (mode) { + Mode.FIRST, Mode.FIRST_OR_DEFAULT -> { + if (!seenValue) { + seenValue = true + cont.resume(t) + subscription.dispose() + } + } + Mode.LAST, Mode.SINGLE -> { + if (mode == Mode.SINGLE && seenValue) { + if (cont.isActive) + cont.resumeWithException(IllegalArgumentException("More than one onNext value for $mode")) + subscription.dispose() + } else { + value = t + seenValue = true + } + } + } + } + + @Suppress("UNCHECKED_CAST") + override fun onComplete() { + if (seenValue) { + if (cont.isActive) cont.resume(value as T) + return + } + when { + mode == Mode.FIRST_OR_DEFAULT -> { + cont.resume(default as T) + } + cont.isActive -> { + cont.resumeWithException(NoSuchElementException("No value received via onNext for $mode")) + } + } + } + + override fun onError(e: Throwable) { + cont.resumeWithException(e) + } + }) +} diff --git a/reactive/kotlinx-coroutines-rx3/src/RxCancellable.kt b/reactive/kotlinx-coroutines-rx3/src/RxCancellable.kt new file mode 100644 index 0000000000..0b57b8bbe4 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/src/RxCancellable.kt @@ -0,0 +1,25 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.functions.* +import io.reactivex.rxjava3.plugins.* +import kotlinx.coroutines.* +import kotlin.coroutines.* + +internal class RxCancellable(private val job: Job) : Cancellable { + override fun cancel() { + job.cancel() + } +} + +internal fun handleUndeliverableException(cause: Throwable, context: CoroutineContext) { + if (cause is CancellationException) return // Async CE should be completely ignored + try { + RxJavaPlugins.onError(cause) + } catch (e: Throwable) { + handleCoroutineException(context, cause) + } +} diff --git a/reactive/kotlinx-coroutines-rx3/src/RxChannel.kt b/reactive/kotlinx-coroutines-rx3/src/RxChannel.kt new file mode 100644 index 0000000000..737cf6710d --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/src/RxChannel.kt @@ -0,0 +1,86 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.core.* +import io.reactivex.rxjava3.disposables.* +import kotlinx.atomicfu.* +import kotlinx.coroutines.channels.* +import kotlinx.coroutines.internal.* + +/** + * Subscribes to this [MaybeSource] and returns a channel to receive elements emitted by it. + * The resulting channel shall be [cancelled][ReceiveChannel.cancel] to unsubscribe from this source. + * + * This API is internal in the favour of [Flow]. + * [MaybeSource] doesn't have a corresponding [Flow] adapter, so it should be transformed to [Observable] first. + */ +@PublishedApi +internal fun MaybeSource.openSubscription(): ReceiveChannel { + val channel = SubscriptionChannel() + subscribe(channel) + return channel +} + +/** + * Subscribes to this [ObservableSource] and returns a channel to receive elements emitted by it. + * The resulting channel shall be [cancelled][ReceiveChannel.cancel] to unsubscribe from this source. + * + * This API is internal in the favour of [Flow]. + * [ObservableSource] doesn't have a corresponding [Flow] adapter, so it should be transformed to [Observable] first. + */ +@PublishedApi +internal fun ObservableSource.openSubscription(): ReceiveChannel { + val channel = SubscriptionChannel() + subscribe(channel) + return channel +} + +/** + * Subscribes to this [MaybeSource] and performs the specified action for each received element. + * Cancels subscription if any exception happens during collect. + */ +public suspend inline fun MaybeSource.collect(action: (T) -> Unit): Unit = + openSubscription().consumeEach(action) + +/** + * Subscribes to this [ObservableSource] and performs the specified action for each received element. + * Cancels subscription if any exception happens during collect. + */ +public suspend inline fun ObservableSource.collect(action: (T) -> Unit): Unit = + openSubscription().consumeEach(action) + +@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") +private class SubscriptionChannel : + LinkedListChannel(null), Observer, MaybeObserver +{ + private val _subscription = atomic(null) + + @Suppress("CANNOT_OVERRIDE_INVISIBLE_MEMBER") + override fun onClosedIdempotent(closed: LockFreeLinkedListNode) { + _subscription.getAndSet(null)?.dispose() // dispose exactly once + } + + // Observer overrider + override fun onSubscribe(sub: Disposable) { + _subscription.value = sub + } + + override fun onSuccess(t: T) { + offer(t) + } + + override fun onNext(t: T) { + offer(t) + } + + override fun onComplete() { + close(cause = null) + } + + override fun onError(e: Throwable) { + close(cause = e) + } +} diff --git a/reactive/kotlinx-coroutines-rx3/src/RxCompletable.kt b/reactive/kotlinx-coroutines-rx3/src/RxCompletable.kt new file mode 100644 index 0000000000..54b412f171 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/src/RxCompletable.kt @@ -0,0 +1,60 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.core.* +import kotlinx.coroutines.* +import kotlin.coroutines.* + +/** + * Creates cold [Completable] that runs a given [block] in a coroutine and emits its result. + * Every time the returned completable is subscribed, it starts a new coroutine. + * Unsubscribing cancels running coroutine. + * Coroutine context can be specified with [context] argument. + * If the context does not have any dispatcher nor any other [ContinuationInterceptor], then [Dispatchers.Default] is used. + * Method throws [IllegalArgumentException] if provided [context] contains a [Job] instance. + */ +public fun rxCompletable( + context: CoroutineContext = EmptyCoroutineContext, + block: suspend CoroutineScope.() -> Unit +): Completable { + require(context[Job] === null) { "Completable context cannot contain job in it." + + "Its lifecycle should be managed via Disposable handle. Had $context" } + return rxCompletableInternal(GlobalScope, context, block) +} + +private fun rxCompletableInternal( + scope: CoroutineScope, // support for legacy rxCompletable in scope + context: CoroutineContext, + block: suspend CoroutineScope.() -> Unit +): Completable = Completable.create { subscriber -> + val newContext = scope.newCoroutineContext(context) + val coroutine = RxCompletableCoroutine(newContext, subscriber) + subscriber.setCancellable(RxCancellable(coroutine)) + coroutine.start(CoroutineStart.DEFAULT, coroutine, block) +} + +private class RxCompletableCoroutine( + parentContext: CoroutineContext, + private val subscriber: CompletableEmitter +) : AbstractCoroutine(parentContext, true) { + override fun onCompleted(value: Unit) { + try { + subscriber.onComplete() + } catch (e: Throwable) { + handleUndeliverableException(e, context) + } + } + + override fun onCancelled(cause: Throwable, handled: Boolean) { + try { + if (!subscriber.tryOnError(cause)) { + handleUndeliverableException(cause, context) + } + } catch (e: Throwable) { + handleUndeliverableException(e, context) + } + } +} diff --git a/reactive/kotlinx-coroutines-rx3/src/RxConvert.kt b/reactive/kotlinx-coroutines-rx3/src/RxConvert.kt new file mode 100644 index 0000000000..0978423ac9 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/src/RxConvert.kt @@ -0,0 +1,155 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.core.* +import io.reactivex.rxjava3.disposables.* +import kotlinx.coroutines.* +import kotlinx.coroutines.channels.* +import kotlinx.coroutines.flow.* +import kotlinx.coroutines.reactive.* +import org.reactivestreams.* +import java.util.concurrent.atomic.* +import kotlin.coroutines.* + +/** + * Converts this job to the hot reactive completable that signals + * with [onCompleted][CompletableObserver.onComplete] when the corresponding job completes. + * + * Every subscriber gets the signal at the same time. + * Unsubscribing from the resulting completable **does not** affect the original job in any way. + * + * **Note: This is an experimental api.** Conversion of coroutines primitives to reactive entities may change + * in the future to account for the concept of structured concurrency. + * + * @param context -- the coroutine context from which the resulting completable is going to be signalled + */ +@ExperimentalCoroutinesApi +public fun Job.asCompletable(context: CoroutineContext): Completable = rxCompletable(context) { + this@asCompletable.join() +} + +/** + * Converts this deferred value to the hot reactive maybe that signals + * [onComplete][MaybeEmitter.onComplete], [onSuccess][MaybeEmitter.onSuccess] or [onError][MaybeEmitter.onError]. + * + * Every subscriber gets the same completion value. + * Unsubscribing from the resulting maybe **does not** affect the original deferred value in any way. + * + * **Note: This is an experimental api.** Conversion of coroutines primitives to reactive entities may change + * in the future to account for the concept of structured concurrency. + * + * @param context -- the coroutine context from which the resulting maybe is going to be signalled + */ +@ExperimentalCoroutinesApi +public fun Deferred.asMaybe(context: CoroutineContext): Maybe = rxMaybe(context) { + this@asMaybe.await() +} + +/** + * Converts this deferred value to the hot reactive single that signals either + * [onSuccess][SingleObserver.onSuccess] or [onError][SingleObserver.onError]. + * + * Every subscriber gets the same completion value. + * Unsubscribing from the resulting single **does not** affect the original deferred value in any way. + * + * **Note: This is an experimental api.** Conversion of coroutines primitives to reactive entities may change + * in the future to account for the concept of structured concurrency. + * + * @param context -- the coroutine context from which the resulting single is going to be signalled + */ +@ExperimentalCoroutinesApi +public fun Deferred.asSingle(context: CoroutineContext): Single = rxSingle(context) { + this@asSingle.await() +} + +/** + * Transforms given cold [ObservableSource] into cold [Flow]. + * + * The resulting flow is _cold_, which means that [ObservableSource.subscribe] is called every time a terminal operator + * is applied to the resulting flow. + * + * A channel with the [default][Channel.BUFFERED] buffer size is used. Use the [buffer] operator on the + * resulting flow to specify a user-defined value and to control what happens when data is produced faster + * than consumed, i.e. to control the back-pressure behavior. Check [callbackFlow] for more details. + */ +@ExperimentalCoroutinesApi +public fun ObservableSource.asFlow(): Flow = callbackFlow { + val disposableRef = AtomicReference() + val observer = object : Observer { + override fun onComplete() { close() } + override fun onSubscribe(d: Disposable) { if (!disposableRef.compareAndSet(null, d)) d.dispose() } + override fun onNext(t: T) { + try { + sendBlocking(t) + } catch (ignored: Throwable) { // TODO: Replace when this issue is fixed: https://github.com/Kotlin/kotlinx.coroutines/issues/974 + // Is handled by the downstream flow + } + } + override fun onError(e: Throwable) { close(e) } + } + + subscribe(observer) + awaitClose { disposableRef.getAndSet(Disposable.disposed())?.dispose() } +} + +/** + * Converts the given flow to a cold observable. + * The original flow is cancelled when the observable subscriber is disposed. + * + * An optional [context] can be specified to control the execution context of calls to [Observer] methods. + * You can set a [CoroutineDispatcher] to confine them to a specific thread and/or various [ThreadContextElement] to + * inject additional context into the caller thread. By default, the [Unconfined][Dispatchers.Unconfined] dispatcher + * is used, so calls are performed from an arbitrary thread. + */ +@ExperimentalCoroutinesApi +public fun Flow.asObservable(context: CoroutineContext = EmptyCoroutineContext) : Observable = Observable.create { emitter -> + /* + * ATOMIC is used here to provide stable behaviour of subscribe+dispose pair even if + * asObservable is already invoked from unconfined + */ + val job = GlobalScope.launch(Dispatchers.Unconfined + context, start = CoroutineStart.ATOMIC) { + try { + collect { value -> emitter.onNext(value) } + emitter.onComplete() + } catch (e: Throwable) { + // 'create' provides safe emitter, so we can unconditionally call on* here if exception occurs in `onComplete` + if (e !is CancellationException) { + if (!emitter.tryOnError(e)) { + handleUndeliverableException(e, coroutineContext) + } + } else { + emitter.onComplete() + } + } + } + emitter.setCancellable(RxCancellable(job)) +} + +/** + * Converts the given flow to a cold flowable. + * The original flow is cancelled when the flowable subscriber is disposed. + * + * An optional [context] can be specified to control the execution context of calls to [Subscriber] methods. + * You can set a [CoroutineDispatcher] to confine them to a specific thread and/or various [ThreadContextElement] to + * inject additional context into the caller thread. By default, the [Unconfined][Dispatchers.Unconfined] dispatcher + * is used, so calls are performed from an arbitrary thread. + */ +@ExperimentalCoroutinesApi +public fun Flow.asFlowable(context: CoroutineContext = EmptyCoroutineContext): Flowable = + Flowable.fromPublisher(asPublisher(context)) + +@Suppress("UNUSED") // KT-42513 +@JvmOverloads // binary compatibility +@JvmName("from") +@Deprecated(level = DeprecationLevel.HIDDEN, message = "") // Since 1.4, was experimental prior to that +public fun Flow._asFlowable(context: CoroutineContext = EmptyCoroutineContext): Flowable = + asFlowable(context) + +@Suppress("UNUSED") // KT-42513 +@JvmOverloads // binary compatibility +@JvmName("from") +@Deprecated(level = DeprecationLevel.HIDDEN, message = "") // Since 1.4, was experimental prior to that +public fun Flow._asObservable(context: CoroutineContext = EmptyCoroutineContext) : Observable = asObservable(context) diff --git a/reactive/kotlinx-coroutines-rx3/src/RxFlowable.kt b/reactive/kotlinx-coroutines-rx3/src/RxFlowable.kt new file mode 100644 index 0000000000..2de46a6a20 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/src/RxFlowable.kt @@ -0,0 +1,41 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.core.* +import kotlinx.coroutines.* +import kotlinx.coroutines.channels.* +import kotlinx.coroutines.reactive.* +import kotlin.coroutines.* + +/** + * Creates cold [flowable][Flowable] that will run a given [block] in a coroutine. + * Every time the returned flowable is subscribed, it starts a new coroutine. + * + * Coroutine emits ([ObservableEmitter.onNext]) values with `send`, completes ([ObservableEmitter.onComplete]) + * when the coroutine completes or channel is explicitly closed and emits error ([ObservableEmitter.onError]) + * if coroutine throws an exception or closes channel with a cause. + * Unsubscribing cancels running coroutine. + * + * Invocations of `send` are suspended appropriately when subscribers apply back-pressure and to ensure that + * `onNext` is not invoked concurrently. + * + * Coroutine context can be specified with [context] argument. + * If the context does not have any dispatcher nor any other [ContinuationInterceptor], then [Dispatchers.Default] is used. + * Method throws [IllegalArgumentException] if provided [context] contains a [Job] instance. + * + * **Note: This is an experimental api.** Behaviour of publishers that work as children in a parent scope with respect + */ +@ExperimentalCoroutinesApi +public fun rxFlowable( + context: CoroutineContext = EmptyCoroutineContext, + @BuilderInference block: suspend ProducerScope.() -> Unit +): Flowable { + require(context[Job] === null) { "Flowable context cannot contain job in it." + + "Its lifecycle should be managed via Disposable handle. Had $context" } + return Flowable.fromPublisher(publishInternal(GlobalScope, context, RX_HANDLER, block)) +} + +private val RX_HANDLER: (Throwable, CoroutineContext) -> Unit = ::handleUndeliverableException diff --git a/reactive/kotlinx-coroutines-rx3/src/RxMaybe.kt b/reactive/kotlinx-coroutines-rx3/src/RxMaybe.kt new file mode 100644 index 0000000000..4d55ef5ffa --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/src/RxMaybe.kt @@ -0,0 +1,61 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.core.* +import kotlinx.coroutines.* +import kotlin.coroutines.* + +/** + * Creates cold [maybe][Maybe] that will run a given [block] in a coroutine and emits its result. + * If [block] result is `null`, [onComplete][MaybeObserver.onComplete] is invoked without a value. + * Every time the returned observable is subscribed, it starts a new coroutine. + * Unsubscribing cancels running coroutine. + * Coroutine context can be specified with [context] argument. + * If the context does not have any dispatcher nor any other [ContinuationInterceptor], then [Dispatchers.Default] is used. + * Method throws [IllegalArgumentException] if provided [context] contains a [Job] instance. + */ +public fun rxMaybe( + context: CoroutineContext = EmptyCoroutineContext, + block: suspend CoroutineScope.() -> T? +): Maybe { + require(context[Job] === null) { "Maybe context cannot contain job in it." + + "Its lifecycle should be managed via Disposable handle. Had $context" } + return rxMaybeInternal(GlobalScope, context, block) +} + +private fun rxMaybeInternal( + scope: CoroutineScope, // support for legacy rxMaybe in scope + context: CoroutineContext, + block: suspend CoroutineScope.() -> T? +): Maybe = Maybe.create { subscriber -> + val newContext = scope.newCoroutineContext(context) + val coroutine = RxMaybeCoroutine(newContext, subscriber) + subscriber.setCancellable(RxCancellable(coroutine)) + coroutine.start(CoroutineStart.DEFAULT, coroutine, block) +} + +private class RxMaybeCoroutine( + parentContext: CoroutineContext, + private val subscriber: MaybeEmitter +) : AbstractCoroutine(parentContext, true) { + override fun onCompleted(value: T) { + try { + if (value == null) subscriber.onComplete() else subscriber.onSuccess(value) + } catch (e: Throwable) { + handleUndeliverableException(e, context) + } + } + + override fun onCancelled(cause: Throwable, handled: Boolean) { + try { + if (!subscriber.tryOnError(cause)) { + handleUndeliverableException(cause, context) + } + } catch (e: Throwable) { + handleUndeliverableException(e, context) + } + } +} diff --git a/reactive/kotlinx-coroutines-rx3/src/RxObservable.kt b/reactive/kotlinx-coroutines-rx3/src/RxObservable.kt new file mode 100644 index 0000000000..102d06ea60 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/src/RxObservable.kt @@ -0,0 +1,195 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.core.* +import io.reactivex.rxjava3.exceptions.* +import kotlinx.atomicfu.* +import kotlinx.coroutines.* +import kotlinx.coroutines.channels.* +import kotlinx.coroutines.selects.* +import kotlinx.coroutines.sync.* +import kotlin.coroutines.* + +/** + * Creates cold [observable][Observable] that will run a given [block] in a coroutine. + * Every time the returned observable is subscribed, it starts a new coroutine. + * + * Coroutine emits ([ObservableEmitter.onNext]) values with `send`, completes ([ObservableEmitter.onComplete]) + * when the coroutine completes or channel is explicitly closed and emits error ([ObservableEmitter.onError]) + * if coroutine throws an exception or closes channel with a cause. + * Unsubscribing cancels running coroutine. + * + * Invocations of `send` are suspended appropriately to ensure that `onNext` is not invoked concurrently. + * Note that Rx 2.x [Observable] **does not support backpressure**. + * + * Coroutine context can be specified with [context] argument. + * If the context does not have any dispatcher nor any other [ContinuationInterceptor], then [Dispatchers.Default] is used. + * Method throws [IllegalArgumentException] if provided [context] contains a [Job] instance. + */ +@ExperimentalCoroutinesApi +public fun rxObservable( + context: CoroutineContext = EmptyCoroutineContext, + @BuilderInference block: suspend ProducerScope.() -> Unit +): Observable { + require(context[Job] === null) { "Observable context cannot contain job in it." + + "Its lifecycle should be managed via Disposable handle. Had $context" } + return rxObservableInternal(GlobalScope, context, block) +} + +private fun rxObservableInternal( + scope: CoroutineScope, // support for legacy rxObservable in scope + context: CoroutineContext, + block: suspend ProducerScope.() -> Unit +): Observable = Observable.create { subscriber -> + val newContext = scope.newCoroutineContext(context) + val coroutine = RxObservableCoroutine(newContext, subscriber) + subscriber.setCancellable(RxCancellable(coroutine)) // do it first (before starting coroutine), to await unnecessary suspensions + coroutine.start(CoroutineStart.DEFAULT, coroutine, block) +} + +private const val OPEN = 0 // open channel, still working +private const val CLOSED = -1 // closed, but have not signalled onCompleted/onError yet +private const val SIGNALLED = -2 // already signalled subscriber onCompleted/onError + +private class RxObservableCoroutine( + parentContext: CoroutineContext, + private val subscriber: ObservableEmitter +) : AbstractCoroutine(parentContext, true), ProducerScope, SelectClause2> { + override val channel: SendChannel get() = this + + // Mutex is locked when while subscriber.onXXX is being invoked + private val mutex = Mutex() + + private val _signal = atomic(OPEN) + + override val isClosedForSend: Boolean get() = isCompleted + override val isFull: Boolean = mutex.isLocked + override fun close(cause: Throwable?): Boolean = cancelCoroutine(cause) + override fun invokeOnClose(handler: (Throwable?) -> Unit) = + throw UnsupportedOperationException("RxObservableCoroutine doesn't support invokeOnClose") + + override fun offer(element: T): Boolean { + if (!mutex.tryLock()) return false + doLockedNext(element) + return true + } + + public override suspend fun send(element: T) { + // fast-path -- try send without suspension + if (offer(element)) return + // slow-path does suspend + return sendSuspend(element) + } + + private suspend fun sendSuspend(element: T) { + mutex.lock() + doLockedNext(element) + } + + override val onSend: SelectClause2> + get() = this + + // registerSelectSend + @Suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE") + override fun registerSelectClause2(select: SelectInstance, element: T, block: suspend (SendChannel) -> R) { + mutex.onLock.registerSelectClause2(select, null) { + doLockedNext(element) + block(this) + } + } + + // assert: mutex.isLocked() + private fun doLockedNext(elem: T) { + // check if already closed for send + if (!isActive) { + doLockedSignalCompleted(completionCause, completionCauseHandled) + throw getCancellationException() + } + // notify subscriber + try { + subscriber.onNext(elem) + } catch (e: Throwable) { + // If onNext fails with exception, then we cancel coroutine (with this exception) and then rethrow it + // to abort the corresponding send/offer invocation. From the standpoint of coroutines machinery, + // this failure is essentially equivalent to a failure of a child coroutine. + cancelCoroutine(e) + mutex.unlock() + throw e + } + /* + * There is no sense to check for `isActive` before doing `unlock`, because cancellation/completion might + * happen after this check and before `unlock` (see signalCompleted that does not do anything + * if it fails to acquire the lock that we are still holding). + * We have to recheck `isCompleted` after `unlock` anyway. + */ + unlockAndCheckCompleted() + } + + private fun unlockAndCheckCompleted() { + mutex.unlock() + // recheck isActive + if (!isActive && mutex.tryLock()) + doLockedSignalCompleted(completionCause, completionCauseHandled) + } + + // assert: mutex.isLocked() + private fun doLockedSignalCompleted(cause: Throwable?, handled: Boolean) { + // cancellation failures + try { + if (_signal.value >= CLOSED) { + _signal.value = SIGNALLED // we'll signal onError/onCompleted (that the final state -- no CAS needed) + try { + if (cause != null && cause !is CancellationException) { + /* + * Reactive frameworks have two types of exceptions: regular and fatal. + * Regular are passed to onError. + * Fatal can be passed to onError, but even the standard implementations **can just swallow it** (e.g. see #1297). + * Such behaviour is inconsistent, leads to silent failures and we can't possibly know whether + * the cause will be handled by onError (and moreover, it depends on whether a fatal exception was + * thrown by subscriber or upstream). + * To make behaviour consistent and least surprising, we always handle fatal exceptions + * by coroutines machinery, anyway, they should not be present in regular program flow, + * thus our goal here is just to expose it as soon as possible. + */ + subscriber.tryOnError(cause) + if (!handled && cause.isFatal()) { + handleUndeliverableException(cause, context) + } + } + else { + subscriber.onComplete() + } + } catch (e: Throwable) { + // Unhandled exception (cannot handle in other way, since we are already complete) + handleUndeliverableException(e, context) + } + } + } finally { + mutex.unlock() + } + } + + private fun signalCompleted(cause: Throwable?, handled: Boolean) { + if (!_signal.compareAndSet(OPEN, CLOSED)) return // abort, other thread invoked doLockedSignalCompleted + if (mutex.tryLock()) // if we can acquire the lock + doLockedSignalCompleted(cause, handled) + } + + override fun onCompleted(value: Unit) { + signalCompleted(null, false) + } + + override fun onCancelled(cause: Throwable, handled: Boolean) { + signalCompleted(cause, handled) + } +} + +internal fun Throwable.isFatal() = try { + Exceptions.throwIfFatal(this) // Rx-consistent behaviour without hardcode + false +} catch (e: Throwable) { + true +} diff --git a/reactive/kotlinx-coroutines-rx3/src/RxScheduler.kt b/reactive/kotlinx-coroutines-rx3/src/RxScheduler.kt new file mode 100644 index 0000000000..a426aea6ba --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/src/RxScheduler.kt @@ -0,0 +1,52 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.core.Scheduler +import kotlinx.coroutines.* +import java.util.concurrent.TimeUnit +import kotlin.coroutines.CoroutineContext + +/** + * Converts an instance of [Scheduler] to an implementation of [CoroutineDispatcher] + * and provides native support of [delay] and [withTimeout]. + */ +public fun Scheduler.asCoroutineDispatcher(): SchedulerCoroutineDispatcher = SchedulerCoroutineDispatcher(this) + +/** + * Implements [CoroutineDispatcher] on top of an arbitrary [Scheduler]. + */ +public class SchedulerCoroutineDispatcher( + /** + * Underlying scheduler of current [CoroutineDispatcher]. + */ + public val scheduler: Scheduler +) : CoroutineDispatcher(), Delay { + /** @suppress */ + override fun dispatch(context: CoroutineContext, block: Runnable) { + scheduler.scheduleDirect(block) + } + + /** @suppress */ + override fun scheduleResumeAfterDelay(timeMillis: Long, continuation: CancellableContinuation) { + val disposable = scheduler.scheduleDirect({ + with(continuation) { resumeUndispatched(Unit) } + }, timeMillis, TimeUnit.MILLISECONDS) + continuation.disposeOnCancellation(disposable) + } + + /** @suppress */ + override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle { + val disposable = scheduler.scheduleDirect(block, timeMillis, TimeUnit.MILLISECONDS) + return DisposableHandle { disposable.dispose() } + } + + /** @suppress */ + override fun toString(): String = scheduler.toString() + /** @suppress */ + override fun equals(other: Any?): Boolean = other is SchedulerCoroutineDispatcher && other.scheduler === scheduler + /** @suppress */ + override fun hashCode(): Int = System.identityHashCode(scheduler) +} diff --git a/reactive/kotlinx-coroutines-rx3/src/RxSingle.kt b/reactive/kotlinx-coroutines-rx3/src/RxSingle.kt new file mode 100644 index 0000000000..225df93ad1 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/src/RxSingle.kt @@ -0,0 +1,60 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.core.* +import kotlinx.coroutines.* +import kotlin.coroutines.* + +/** + * Creates cold [single][Single] that will run a given [block] in a coroutine and emits its result. + * Every time the returned observable is subscribed, it starts a new coroutine. + * Unsubscribing cancels running coroutine. + * Coroutine context can be specified with [context] argument. + * If the context does not have any dispatcher nor any other [ContinuationInterceptor], then [Dispatchers.Default] is used. + * Method throws [IllegalArgumentException] if provided [context] contains a [Job] instance. + */ +public fun rxSingle( + context: CoroutineContext = EmptyCoroutineContext, + block: suspend CoroutineScope.() -> T +): Single { + require(context[Job] === null) { "Single context cannot contain job in it." + + "Its lifecycle should be managed via Disposable handle. Had $context" } + return rxSingleInternal(GlobalScope, context, block) +} + +private fun rxSingleInternal( + scope: CoroutineScope, // support for legacy rxSingle in scope + context: CoroutineContext, + block: suspend CoroutineScope.() -> T +): Single = Single.create { subscriber -> + val newContext = scope.newCoroutineContext(context) + val coroutine = RxSingleCoroutine(newContext, subscriber) + subscriber.setCancellable(RxCancellable(coroutine)) + coroutine.start(CoroutineStart.DEFAULT, coroutine, block) +} + +private class RxSingleCoroutine( + parentContext: CoroutineContext, + private val subscriber: SingleEmitter +) : AbstractCoroutine(parentContext, true) { + override fun onCompleted(value: T) { + try { + subscriber.onSuccess(value) + } catch (e: Throwable) { + handleUndeliverableException(e, context) + } + } + + override fun onCancelled(cause: Throwable, handled: Boolean) { + try { + if (!subscriber.tryOnError(cause)) { + handleUndeliverableException(cause, context) + } + } catch (e: Throwable) { + handleUndeliverableException(e, context) + } + } +} diff --git a/reactive/kotlinx-coroutines-rx3/test/BackpressureTest.kt b/reactive/kotlinx-coroutines-rx3/test/BackpressureTest.kt new file mode 100644 index 0000000000..9cec4833a1 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/test/BackpressureTest.kt @@ -0,0 +1,39 @@ +/* + * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.core.* +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* +import kotlinx.coroutines.reactive.* +import org.junit.Test +import kotlin.test.* + +class BackpressureTest : TestBase() { + @Test + fun testBackpressureDropDirect() = runTest { + expect(1) + Flowable.fromArray(1) + .onBackpressureDrop() + .collect { + assertEquals(1, it) + expect(2) + } + finish(3) + } + + @Test + fun testBackpressureDropFlow() = runTest { + expect(1) + Flowable.fromArray(1) + .onBackpressureDrop() + .asFlow() + .collect { + assertEquals(1, it) + expect(2) + } + finish(3) + } +} diff --git a/reactive/kotlinx-coroutines-rx3/test/Check.kt b/reactive/kotlinx-coroutines-rx3/test/Check.kt new file mode 100644 index 0000000000..3d4704f490 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/test/Check.kt @@ -0,0 +1,76 @@ +/* + * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.core.* +import io.reactivex.rxjava3.plugins.* + +fun checkSingleValue( + observable: Observable, + checker: (T) -> Unit +) { + val singleValue = observable.blockingSingle() + checker(singleValue) +} + +fun checkErroneous( + observable: Observable<*>, + checker: (Throwable) -> Unit +) { + val singleNotification = observable.materialize().blockingSingle() + val error = singleNotification.error ?: error("Excepted error") + checker(error) +} + +fun checkSingleValue( + single: Single, + checker: (T) -> Unit +) { + val singleValue = single.blockingGet() + checker(singleValue) +} + +fun checkErroneous( + single: Single<*>, + checker: (Throwable) -> Unit +) { + try { + single.blockingGet() + error("Should have failed") + } catch (e: Throwable) { + checker(e) + } +} + +fun checkMaybeValue( + maybe: Maybe, + checker: (T?) -> Unit +) { + val maybeValue = maybe.toFlowable().blockingIterable().firstOrNull() + checker(maybeValue) +} + +@Suppress("UNCHECKED_CAST") +fun checkErroneous( + maybe: Maybe<*>, + checker: (Throwable) -> Unit +) { + try { + (maybe as Maybe).blockingGet() + error("Should have failed") + } catch (e: Throwable) { + checker(e) + } +} + +inline fun withExceptionHandler(noinline handler: (Throwable) -> Unit, block: () -> Unit) { + val original = RxJavaPlugins.getErrorHandler() + RxJavaPlugins.setErrorHandler { handler(it) } + try { + block() + } finally { + RxJavaPlugins.setErrorHandler(original) + } +} diff --git a/reactive/kotlinx-coroutines-rx3/test/CompletableTest.kt b/reactive/kotlinx-coroutines-rx3/test/CompletableTest.kt new file mode 100644 index 0000000000..e5399d16d1 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/test/CompletableTest.kt @@ -0,0 +1,181 @@ +/* + * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.core.* +import io.reactivex.rxjava3.disposables.* +import io.reactivex.rxjava3.exceptions.* +import kotlinx.coroutines.* +import org.junit.Test +import kotlin.test.* + +class CompletableTest : TestBase() { + @Test + fun testBasicSuccess() = runBlocking { + expect(1) + val completable = rxCompletable(currentDispatcher()) { + expect(4) + } + expect(2) + completable.subscribe { + expect(5) + } + expect(3) + yield() // to completable coroutine + finish(6) + } + + @Test + fun testBasicFailure() = runBlocking { + expect(1) + val completable = rxCompletable(currentDispatcher()) { + expect(4) + throw RuntimeException("OK") + } + expect(2) + completable.subscribe({ + expectUnreached() + }, { error -> + expect(5) + assertTrue(error is RuntimeException) + assertEquals("OK", error.message) + }) + expect(3) + yield() // to completable coroutine + finish(6) + } + + @Test + fun testBasicUnsubscribe() = runBlocking { + expect(1) + val completable = rxCompletable(currentDispatcher()) { + expect(4) + yield() // back to main, will get cancelled + expectUnreached() + } + expect(2) + // nothing is called on a disposed rx3 completable + val sub = completable.subscribe({ + expectUnreached() + }, { + expectUnreached() + }) + expect(3) + yield() // to started coroutine + expect(5) + sub.dispose() // will cancel coroutine + yield() + finish(6) + } + + @Test + fun testAwaitSuccess() = runBlocking { + expect(1) + val completable = rxCompletable(currentDispatcher()) { + expect(3) + } + expect(2) + completable.await() // shall launch coroutine + finish(4) + } + + @Test + fun testAwaitFailure() = runBlocking { + expect(1) + val completable = rxCompletable(currentDispatcher()) { + expect(3) + throw RuntimeException("OK") + } + expect(2) + try { + completable.await() // shall launch coroutine and throw exception + expectUnreached() + } catch (e: RuntimeException) { + finish(4) + assertEquals("OK", e.message) + } + } + + @Test + fun testSuppressedException() = runTest { + val completable = rxCompletable(currentDispatcher()) { + launch(start = CoroutineStart.ATOMIC) { + throw TestException() // child coroutine fails + } + try { + delay(Long.MAX_VALUE) + } finally { + throw TestException2() // but parent throws another exception while cleaning up + } + } + try { + completable.await() + expectUnreached() + } catch (e: TestException) { + assertTrue(e.suppressed[0] is TestException2) + } + } + + @Test + fun testUnhandledException() = runTest() { + expect(1) + var disposable: Disposable? = null + val handler = { e: Throwable -> + assertTrue(e is UndeliverableException && e.cause is TestException) + expect(5) + } + val completable = rxCompletable(currentDispatcher()) { + expect(4) + disposable!!.dispose() // cancel our own subscription, so that delay will get cancelled + try { + delay(Long.MAX_VALUE) + } finally { + throw TestException() // would not be able to handle it since mono is disposed + } + } + withExceptionHandler(handler) { + completable.subscribe(object : CompletableObserver { + override fun onSubscribe(d: Disposable) { + expect(2) + disposable = d + } + + override fun onComplete() { + expectUnreached() + } + + override fun onError(t: Throwable) { + expectUnreached() + } + }) + expect(3) + yield() // run coroutine + finish(6) + } + } + + @Test + fun testFatalExceptionInSubscribe() = runTest { + val handler: (Throwable) -> Unit = { e -> + assertTrue(e is UndeliverableException && e.cause is LinkageError); expect(2) + } + + withExceptionHandler(handler) { + rxCompletable(Dispatchers.Unconfined) { + expect(1) + 42 + }.subscribe({ throw LinkageError() }) + finish(3) + } + } + + @Test + fun testFatalExceptionInSingle() = runTest { + rxCompletable(Dispatchers.Unconfined) { + throw LinkageError() + }.subscribe({ expectUnreached() }, { expect(1); assertTrue(it is LinkageError) }) + finish(2) + } +} diff --git a/reactive/kotlinx-coroutines-rx3/test/ConvertTest.kt b/reactive/kotlinx-coroutines-rx3/test/ConvertTest.kt new file mode 100644 index 0000000000..e2fe533552 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/test/ConvertTest.kt @@ -0,0 +1,159 @@ +/* + * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import kotlinx.coroutines.* +import kotlinx.coroutines.channels.* +import kotlinx.coroutines.flow.consumeAsFlow +import org.junit.Assert +import org.junit.Test +import kotlin.test.* + +class ConvertTest : TestBase() { + @Test + fun testToCompletableSuccess() = runBlocking { + expect(1) + val job = launch { + expect(3) + } + val completable = job.asCompletable(coroutineContext.minusKey(Job)) + completable.subscribe { + expect(4) + } + expect(2) + yield() + finish(5) + } + + @Test + fun testToCompletableFail() = runBlocking { + expect(1) + val job = async(NonCancellable) { // don't kill parent on exception + expect(3) + throw RuntimeException("OK") + } + val completable = job.asCompletable(coroutineContext.minusKey(Job)) + completable.subscribe { + expect(4) + } + expect(2) + yield() + finish(5) + } + + @Test + fun testToMaybe() { + val d = GlobalScope.async { + delay(50) + "OK" + } + val maybe1 = d.asMaybe(Dispatchers.Unconfined) + checkMaybeValue(maybe1) { + assertEquals("OK", it) + } + val maybe2 = d.asMaybe(Dispatchers.Unconfined) + checkMaybeValue(maybe2) { + assertEquals("OK", it) + } + } + + @Test + fun testToMaybeEmpty() { + val d = GlobalScope.async { + delay(50) + null + } + val maybe1 = d.asMaybe(Dispatchers.Unconfined) + checkMaybeValue(maybe1, Assert::assertNull) + val maybe2 = d.asMaybe(Dispatchers.Unconfined) + checkMaybeValue(maybe2, Assert::assertNull) + } + + @Test + fun testToMaybeFail() { + val d = GlobalScope.async { + delay(50) + throw TestRuntimeException("OK") + } + val maybe1 = d.asMaybe(Dispatchers.Unconfined) + checkErroneous(maybe1) { + check(it is TestRuntimeException && it.message == "OK") { "$it" } + } + val maybe2 = d.asMaybe(Dispatchers.Unconfined) + checkErroneous(maybe2) { + check(it is TestRuntimeException && it.message == "OK") { "$it" } + } + } + + @Test + fun testToSingle() { + val d = GlobalScope.async { + delay(50) + "OK" + } + val single1 = d.asSingle(Dispatchers.Unconfined) + checkSingleValue(single1) { + assertEquals("OK", it) + } + val single2 = d.asSingle(Dispatchers.Unconfined) + checkSingleValue(single2) { + assertEquals("OK", it) + } + } + + @Test + fun testToSingleFail() { + val d = GlobalScope.async { + delay(50) + throw TestRuntimeException("OK") + } + val single1 = d.asSingle(Dispatchers.Unconfined) + checkErroneous(single1) { + check(it is TestRuntimeException && it.message == "OK") { "$it" } + } + val single2 = d.asSingle(Dispatchers.Unconfined) + checkErroneous(single2) { + check(it is TestRuntimeException && it.message == "OK") { "$it" } + } + } + + @Test + fun testToObservable() { + val c = GlobalScope.produce { + delay(50) + send("O") + delay(50) + send("K") + } + val observable = c.consumeAsFlow().asObservable() + checkSingleValue(observable.reduce { t1, t2 -> t1 + t2 }.toSingle()) { + assertEquals("OK", it) + } + } + + @Test + fun testToObservableFail() { + val c = GlobalScope.produce { + delay(50) + send("O") + delay(50) + throw TestException("K") + } + val observable = c.consumeAsFlow().asObservable() + val single = rxSingle(Dispatchers.Unconfined) { + var result = "" + try { + observable.collect { result += it } + } catch(e: Throwable) { + check(e is TestException) + result += e.message + } + result + } + checkSingleValue(single) { + assertEquals("OK", it) + } + } +} diff --git a/reactive/kotlinx-coroutines-rx3/test/FlowAsFlowableTest.kt b/reactive/kotlinx-coroutines-rx3/test/FlowAsFlowableTest.kt new file mode 100644 index 0000000000..a73fee469e --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/test/FlowAsFlowableTest.kt @@ -0,0 +1,89 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* +import org.junit.Test +import org.reactivestreams.* +import java.util.concurrent.* +import kotlin.test.* + +@Suppress("ReactiveStreamsSubscriberImplementation") +class FlowAsFlowableTest : TestBase() { + @Test + fun testUnconfinedDefaultContext() { + expect(1) + val thread = Thread.currentThread() + fun checkThread() { + assertSame(thread, Thread.currentThread()) + } + flowOf(42).asFlowable().subscribe(object : Subscriber { + private lateinit var subscription: Subscription + + override fun onSubscribe(s: Subscription) { + expect(2) + subscription = s + subscription.request(2) + } + + override fun onNext(t: Int) { + checkThread() + expect(3) + assertEquals(42, t) + } + + override fun onComplete() { + checkThread() + expect(4) + } + + override fun onError(t: Throwable?) { + expectUnreached() + } + }) + finish(5) + } + + @Test + fun testConfinedContext() { + expect(1) + val threadName = "FlowAsFlowableTest.testConfinedContext" + fun checkThread() { + val currentThread = Thread.currentThread() + assertTrue(currentThread.name.startsWith(threadName), "Unexpected thread $currentThread") + } + val completed = CountDownLatch(1) + newSingleThreadContext(threadName).use { dispatcher -> + flowOf(42).asFlowable(dispatcher).subscribe(object : Subscriber { + private lateinit var subscription: Subscription + + override fun onSubscribe(s: Subscription) { + expect(2) + subscription = s + subscription.request(2) + } + + override fun onNext(t: Int) { + checkThread() + expect(3) + assertEquals(42, t) + } + + override fun onComplete() { + checkThread() + expect(4) + completed.countDown() + } + + override fun onError(t: Throwable?) { + expectUnreached() + } + }) + completed.await() + } + finish(5) + } +} diff --git a/reactive/kotlinx-coroutines-rx3/test/FlowAsObservableTest.kt b/reactive/kotlinx-coroutines-rx3/test/FlowAsObservableTest.kt new file mode 100644 index 0000000000..5759f9f426 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/test/FlowAsObservableTest.kt @@ -0,0 +1,211 @@ +/* + * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.core.* +import io.reactivex.rxjava3.disposables.* +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* +import org.junit.Test +import java.util.concurrent.* +import kotlin.test.* + +class FlowAsObservableTest : TestBase() { + @Test + fun testBasicSuccess() = runTest { + expect(1) + val observable = flow { + expect(3) + emit("OK") + }.asObservable() + + expect(2) + observable.subscribe { value -> + expect(4) + assertEquals("OK", value) + } + + finish(5) + } + + @Test + fun testBasicFailure() = runTest { + expect(1) + val observable = flow { + expect(3) + throw RuntimeException("OK") + }.asObservable() + + expect(2) + observable.subscribe({ expectUnreached() }, { error -> + expect(4) + assertTrue(error is RuntimeException) + assertEquals("OK", error.message) + }) + finish(5) + } + + @Test + fun testBasicUnsubscribe() = runTest { + expect(1) + val observable = flow { + expect(3) + hang { + expect(4) + } + }.asObservable() + + expect(2) + val sub = observable.subscribe({ expectUnreached() }, { expectUnreached() }) + sub.dispose() // will cancel coroutine + finish(5) + } + + @Test + fun testNotifyOnceOnCancellation() = runTest { + val observable = + flow { + expect(3) + emit("OK") + hang { + expect(7) + } + }.asObservable() + .doOnNext { + expect(4) + assertEquals("OK", it) + } + .doOnDispose { + expect(6) // notified once! + } + + expect(1) + val job = launch(start = CoroutineStart.UNDISPATCHED) { + expect(2) + observable.collect { + expect(5) + assertEquals("OK", it) + } + } + + yield() + job.cancelAndJoin() + finish(8) + } + + @Test + fun testFailingConsumer() = runTest { + expect(1) + val observable = flow { + expect(2) + emit("OK") + hang { + expect(4) + } + + }.asObservable() + + try { + observable.collect { + expect(3) + throw TestException() + } + } catch (e: TestException) { + finish(5) + } + } + + @Test + fun testNonAtomicStart() = runTest { + withContext(Dispatchers.Unconfined) { + val observable = flow { + expect(1) + }.asObservable() + + val disposable = observable.subscribe({ expectUnreached() }, { expectUnreached() }, { expectUnreached() }) + disposable.dispose() + } + finish(2) + } + + @Test + fun testFlowCancelledFromWithin() = runTest { + val observable = flow { + expect(1) + emit(1) + kotlin.coroutines.coroutineContext.cancel() + kotlin.coroutines.coroutineContext.ensureActive() + expectUnreached() + }.asObservable() + + observable.subscribe({ expect(2) }, { expectUnreached() }, { finish(3) }) + } + + @Test + fun testUnconfinedDefaultContext() { + expect(1) + val thread = Thread.currentThread() + fun checkThread() { + assertSame(thread, Thread.currentThread()) + } + flowOf(42).asObservable().subscribe(object : Observer { + override fun onSubscribe(d: Disposable) { + expect(2) + } + + override fun onNext(t: Int) { + checkThread() + expect(3) + assertEquals(42, t) + } + + override fun onComplete() { + checkThread() + expect(4) + } + + override fun onError(t: Throwable) { + expectUnreached() + } + }) + finish(5) + } + + @Test + fun testConfinedContext() { + expect(1) + val threadName = "FlowAsObservableTest.testConfinedContext" + fun checkThread() { + val currentThread = Thread.currentThread() + assertTrue(currentThread.name.startsWith(threadName), "Unexpected thread $currentThread") + } + val completed = CountDownLatch(1) + newSingleThreadContext(threadName).use { dispatcher -> + flowOf(42).asObservable(dispatcher).subscribe(object : Observer { + override fun onSubscribe(d: Disposable) { + expect(2) + } + + override fun onNext(t: Int) { + checkThread() + expect(3) + assertEquals(42, t) + } + + override fun onComplete() { + checkThread() + expect(4) + completed.countDown() + } + + override fun onError(e: Throwable) { + expectUnreached() + } + }) + completed.await() + } + finish(5) + } +} diff --git a/reactive/kotlinx-coroutines-rx3/test/FlowableContextTest.kt b/reactive/kotlinx-coroutines-rx3/test/FlowableContextTest.kt new file mode 100644 index 0000000000..b70e0d5135 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/test/FlowableContextTest.kt @@ -0,0 +1,43 @@ +/* + * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.core.* +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* +import kotlinx.coroutines.reactive.* +import org.junit.* +import org.junit.Test +import kotlin.test.* + +class FlowableContextTest : TestBase() { + private val dispatcher = newSingleThreadContext("FlowableContextTest") + + @After + fun tearDown() { + dispatcher.close() + } + + @Test + fun testFlowableCreateAsFlowThread() = runTest { + expect(1) + val mainThread = Thread.currentThread() + val dispatcherThread = withContext(dispatcher) { Thread.currentThread() } + assertTrue(dispatcherThread != mainThread) + Flowable.create({ + assertEquals(dispatcherThread, Thread.currentThread()) + it.onNext("OK") + it.onComplete() + }, BackpressureStrategy.BUFFER) + .asFlow() + .flowOn(dispatcher) + .collect { + expect(2) + assertEquals("OK", it) + assertEquals(mainThread, Thread.currentThread()) + } + finish(3) + } +} diff --git a/reactive/kotlinx-coroutines-rx3/test/FlowableExceptionHandlingTest.kt b/reactive/kotlinx-coroutines-rx3/test/FlowableExceptionHandlingTest.kt new file mode 100644 index 0000000000..8cbd7ee89f --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/test/FlowableExceptionHandlingTest.kt @@ -0,0 +1,134 @@ +/* + * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.exceptions.* +import kotlinx.coroutines.* +import org.junit.* +import org.junit.Test +import kotlin.test.* + +class FlowableExceptionHandlingTest : TestBase() { + + @Before + fun setup() { + ignoreLostThreads("RxComputationThreadPool-", "RxCachedWorkerPoolEvictor-", "RxSchedulerPurge-") + } + + private inline fun handler(expect: Int) = { t: Throwable -> + assertTrue(t is UndeliverableException && t.cause is T) + expect(expect) + } + + private fun cehUnreached() = CoroutineExceptionHandler { _, _ -> expectUnreached() } + + @Test + fun testException() = withExceptionHandler({ expectUnreached() }) { + rxFlowable(Dispatchers.Unconfined + cehUnreached()) { + expect(1) + throw TestException() + }.subscribe({ + expectUnreached() + }, { + expect(2) // Reported to onError + }) + finish(3) + } + + @Test + fun testFatalException() = withExceptionHandler(handler(3)) { + rxFlowable(Dispatchers.Unconfined) { + expect(1) + throw LinkageError() + }.subscribe({ + expectUnreached() + }, { + expect(2) // Fatal exception is reported to both onError and CEH + }) + finish(4) + } + + @Test + fun testExceptionAsynchronous() = withExceptionHandler({ expectUnreached() }) { + rxFlowable(Dispatchers.Unconfined + cehUnreached()) { + expect(1) + throw TestException() + }.publish() + .refCount() + .subscribe({ + expectUnreached() + }, { + expect(2) // Reported to onError + }) + finish(3) + } + + @Test + fun testFatalExceptionAsynchronous() = withExceptionHandler(handler(3)) { + rxFlowable(Dispatchers.Unconfined) { + expect(1) + throw LinkageError() + }.publish() + .refCount() + .subscribe({ + expectUnreached() + }, { + expect(2) + }) + finish(4) + } + + @Test + fun testFatalExceptionFromSubscribe() = withExceptionHandler(handler(4)) { + rxFlowable(Dispatchers.Unconfined) { + expect(1) + send(Unit) + }.subscribe({ + expect(2) + throw LinkageError() + }, { expect(3) }) // Fatal exception is reported to both onError and CEH + finish(5) + } + + @Test + fun testExceptionFromSubscribe() = withExceptionHandler({ expectUnreached() }) { + rxFlowable(Dispatchers.Unconfined + cehUnreached()) { + expect(1) + send(Unit) + }.subscribe({ + expect(2) + throw TestException() + }, { expect(3) }) // not reported to onError because came from the subscribe itself + finish(4) + } + + @Test + fun testAsynchronousExceptionFromSubscribe() = withExceptionHandler({ expectUnreached() }) { + rxFlowable(Dispatchers.Unconfined + cehUnreached()) { + expect(1) + send(Unit) + }.publish() + .refCount() + .subscribe({ + expect(2) + throw RuntimeException() + }, { expect(3) }) + finish(4) + } + + @Test + fun testAsynchronousFatalExceptionFromSubscribe() = withExceptionHandler(handler(3)) { + rxFlowable(Dispatchers.Unconfined) { + expect(1) + send(Unit) + }.publish() + .refCount() + .subscribe({ + expect(2) + throw LinkageError() + }, { expectUnreached() }) + finish(4) + } +} diff --git a/reactive/kotlinx-coroutines-rx3/test/FlowableTest.kt b/reactive/kotlinx-coroutines-rx3/test/FlowableTest.kt new file mode 100644 index 0000000000..746d6e8b41 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/test/FlowableTest.kt @@ -0,0 +1,126 @@ +/* + * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import kotlinx.coroutines.* +import kotlinx.coroutines.reactive.* +import org.junit.Test +import kotlin.test.* + +class FlowableTest : TestBase() { + @Test + fun testBasicSuccess() = runBlocking { + expect(1) + val observable = rxFlowable(currentDispatcher()) { + expect(4) + send("OK") + } + expect(2) + observable.subscribe { value -> + expect(5) + assertEquals("OK", value) + } + expect(3) + yield() // to started coroutine + finish(6) + } + + @Test + fun testBasicFailure() = runBlocking { + expect(1) + val observable = rxFlowable(currentDispatcher()) { + expect(4) + throw RuntimeException("OK") + } + expect(2) + observable.subscribe({ + expectUnreached() + }, { error -> + expect(5) + assertTrue(error is RuntimeException) + assertEquals("OK", error.message) + }) + expect(3) + yield() // to started coroutine + finish(6) + } + + @Test + fun testBasicUnsubscribe() = runBlocking { + expect(1) + val observable = rxFlowable(currentDispatcher()) { + expect(4) + yield() // back to main, will get cancelled + expectUnreached() + } + expect(2) + val sub = observable.subscribe({ + expectUnreached() + }, { + expectUnreached() + }) + expect(3) + yield() // to started coroutine + expect(5) + sub.dispose() // will cancel coroutine + yield() + finish(6) + } + + @Test + fun testNotifyOnceOnCancellation() = runTest { + expect(1) + val observable = + rxFlowable(currentDispatcher()) { + expect(5) + send("OK") + try { + delay(Long.MAX_VALUE) + } catch (e: CancellationException) { + expect(11) + } + } + .doOnNext { + expect(6) + assertEquals("OK", it) + } + .doOnCancel { + expect(10) // notified once! + } + expect(2) + val job = launch(start = CoroutineStart.UNDISPATCHED) { + expect(3) + observable.collect { + expect(8) + assertEquals("OK", it) + } + } + expect(4) + yield() // to observable code + expect(7) + yield() // to consuming coroutines + expect(9) + job.cancel() + job.join() + finish(12) + } + + @Test + fun testFailingConsumer() = runTest { + val pub = rxFlowable(currentDispatcher()) { + repeat(3) { + expect(it + 1) // expect(1), expect(2) *should* be invoked + send(it) + } + } + try { + pub.collect { + throw TestException() + } + } catch (e: TestException) { + finish(3) + } + } +} diff --git a/reactive/kotlinx-coroutines-rx3/test/IntegrationTest.kt b/reactive/kotlinx-coroutines-rx3/test/IntegrationTest.kt new file mode 100644 index 0000000000..395672cee9 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/test/IntegrationTest.kt @@ -0,0 +1,136 @@ +/* + * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.core.* +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.consumeAsFlow +import org.junit.Test +import org.junit.runner.* +import org.junit.runners.* +import kotlin.coroutines.* +import kotlin.test.* + +@RunWith(Parameterized::class) +class IntegrationTest( + private val ctx: Ctx, + private val delay: Boolean +) : TestBase() { + + enum class Ctx { + MAIN { override fun invoke(context: CoroutineContext): CoroutineContext = context.minusKey(Job) }, + DEFAULT { override fun invoke(context: CoroutineContext): CoroutineContext = Dispatchers.Default }, + UNCONFINED { override fun invoke(context: CoroutineContext): CoroutineContext = Dispatchers.Unconfined }; + + abstract operator fun invoke(context: CoroutineContext): CoroutineContext + } + + companion object { + @Parameterized.Parameters(name = "ctx={0}, delay={1}") + @JvmStatic + fun params(): Collection> = Ctx.values().flatMap { ctx -> + listOf(false, true).map { delay -> + arrayOf(ctx, delay) + } + } + } + + @Test + fun testEmpty(): Unit = runBlocking { + val observable = rxObservable(ctx(coroutineContext)) { + if (delay) delay(1) + // does not send anything + } + assertFailsWith { observable.awaitFirst() } + assertEquals("OK", observable.awaitFirstOrDefault("OK")) + assertNull(observable.awaitFirstOrNull()) + assertEquals("ELSE", observable.awaitFirstOrElse { "ELSE" }) + assertFailsWith { observable.awaitLast() } + assertFailsWith { observable.awaitSingle() } + var cnt = 0 + observable.collect { + cnt++ + } + assertEquals(0, cnt) + } + + @Test + fun testSingle() = runBlocking { + val observable = rxObservable(ctx(coroutineContext)) { + if (delay) delay(1) + send("OK") + } + assertEquals("OK", observable.awaitFirst()) + assertEquals("OK", observable.awaitFirstOrDefault("OK")) + assertEquals("OK", observable.awaitFirstOrNull()) + assertEquals("OK", observable.awaitFirstOrElse { "ELSE" }) + assertEquals("OK", observable.awaitLast()) + assertEquals("OK", observable.awaitSingle()) + var cnt = 0 + observable.collect { + assertEquals("OK", it) + cnt++ + } + assertEquals(1, cnt) + } + + @Test + fun testNumbers() = runBlocking { + val n = 100 * stressTestMultiplier + val observable = rxObservable(ctx(coroutineContext)) { + for (i in 1..n) { + send(i) + if (delay) delay(1) + } + } + assertEquals(1, observable.awaitFirst()) + assertEquals(1, observable.awaitFirstOrDefault(0)) + assertEquals(1, observable.awaitFirstOrNull()) + assertEquals(1, observable.awaitFirstOrElse { 0 }) + assertEquals(n, observable.awaitLast()) + assertFailsWith { observable.awaitSingle() } + checkNumbers(n, observable) + val channel = observable.openSubscription() + ctx(coroutineContext) + checkNumbers(n, channel.consumeAsFlow().asObservable()) + channel.cancel() + } + + @Test + fun testCancelWithoutValue() = runTest { + val job = launch(Job(), start = CoroutineStart.UNDISPATCHED) { + rxObservable { + hang { } + }.awaitFirst() + } + + job.cancel() + job.join() + } + + @Test + fun testEmptySingle() = runTest(unhandled = listOf({e -> e is NoSuchElementException})) { + expect(1) + val job = launch(Job(), start = CoroutineStart.UNDISPATCHED) { + rxObservable { + yield() + expect(2) + // Nothing to emit + }.awaitFirst() + } + + job.join() + finish(3) + } + + private suspend fun checkNumbers(n: Int, observable: Observable) { + var last = 0 + observable.collect { + assertEquals(++last, it) + } + assertEquals(n, last) + } + +} diff --git a/reactive/kotlinx-coroutines-rx3/test/IterableFlowAsFlowableTckTest.kt b/reactive/kotlinx-coroutines-rx3/test/IterableFlowAsFlowableTckTest.kt new file mode 100644 index 0000000000..8e54922d4a --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/test/IterableFlowAsFlowableTckTest.kt @@ -0,0 +1,37 @@ +/* + * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.core.* +import kotlinx.coroutines.flow.* +import org.junit.* +import org.reactivestreams.* +import org.reactivestreams.tck.* + +class IterableFlowAsFlowableTckTest : PublisherVerification(TestEnvironment()) { + + private fun generate(num: Long): Array { + return Array(if (num >= Integer.MAX_VALUE) 1000000 else num.toInt()) { it.toLong() } + } + + override fun createPublisher(elements: Long): Flowable { + return generate(elements).asIterable().asFlow().asFlowable() + } + + override fun createFailedPublisher(): Publisher? = null + + @Ignore + override fun required_spec309_requestZeroMustSignalIllegalArgumentException() { + } + + @Ignore + override fun required_spec309_requestNegativeNumberMustSignalIllegalArgumentException() { + } + + @Ignore + override fun required_spec312_cancelMustMakeThePublisherToEventuallyStopSignaling() { + // + } +} diff --git a/reactive/kotlinx-coroutines-rx3/test/LeakedExceptionTest.kt b/reactive/kotlinx-coroutines-rx3/test/LeakedExceptionTest.kt new file mode 100644 index 0000000000..028ded0dd0 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/test/LeakedExceptionTest.kt @@ -0,0 +1,107 @@ +/* + * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.core.* +import io.reactivex.rxjava3.exceptions.* +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* +import kotlinx.coroutines.reactive.* +import org.junit.Test +import java.util.concurrent.Executors +import java.util.concurrent.TimeUnit +import kotlin.test.* + +// Check that exception is not leaked to the global exception handler +class LeakedExceptionTest : TestBase() { + + private val handler: (Throwable) -> Unit = + { assertTrue { it is UndeliverableException && it.cause is TestException } } + + @Test + fun testSingle() = withExceptionHandler(handler) { + withFixedThreadPool(4) { dispatcher -> + val flow = rxSingle(dispatcher) { throw TestException() }.toFlowable().asFlow() + runBlocking { + repeat(10000) { + combine(flow, flow) { _, _ -> Unit } + .catch {} + .collect {} + } + } + } + } + + @Test + fun testObservable() = withExceptionHandler(handler) { + withFixedThreadPool(4) { dispatcher -> + val flow = rxObservable(dispatcher) { throw TestException() } + .toFlowable(BackpressureStrategy.BUFFER) + .asFlow() + runBlocking { + repeat(10000) { + combine(flow, flow) { _, _ -> Unit } + .catch {} + .collect {} + } + } + } + } + + @Test + fun testFlowable() = withExceptionHandler(handler) { + withFixedThreadPool(4) { dispatcher -> + val flow = rxFlowable(dispatcher) { throw TestException() }.asFlow() + runBlocking { + repeat(10000) { + combine(flow, flow) { _, _ -> Unit } + .catch {} + .collect {} + } + } + } + } + + /** + * This test doesn't test much and was added to display a problem with straighforward use of + * [withExceptionHandler]. + * + * If one was to remove `dispatcher` and launch `rxFlowable` with an empty coroutine context, + * this test would fail fairly often, while other tests were also vulnerable, but the problem is + * much more difficult to reproduce. Thus, this test is a justification for adding `dispatcher` + * to other tests. + * + * See the commit that introduced this test for a better explanation. + */ + @Test + fun testResettingExceptionHandler() = withExceptionHandler(handler) { + withFixedThreadPool(4) { dispatcher -> + val flow = rxFlowable(dispatcher) { + if ((0..1).random() == 0) { + Thread.sleep(100) + } + throw TestException() + }.asFlow() + runBlocking { + combine(flow, flow) { _, _ -> Unit } + .catch {} + .collect {} + } + } + } + + /** + * Run in a thread pool, then wait for all the tasks to finish. + */ + private fun withFixedThreadPool(numberOfThreads: Int, block: (CoroutineDispatcher) -> Unit) { + val pool = Executors.newFixedThreadPool(numberOfThreads) + val dispatcher = pool.asCoroutineDispatcher() + block(dispatcher) + pool.shutdown() + while (!pool.awaitTermination(10, TimeUnit.SECONDS)) { + /* deliberately empty */ + } + } +} diff --git a/reactive/kotlinx-coroutines-rx3/test/MaybeTest.kt b/reactive/kotlinx-coroutines-rx3/test/MaybeTest.kt new file mode 100644 index 0000000000..e0cec748f8 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/test/MaybeTest.kt @@ -0,0 +1,316 @@ +/* + * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.core.* +import io.reactivex.rxjava3.disposables.* +import io.reactivex.rxjava3.exceptions.* +import io.reactivex.rxjava3.functions.* +import io.reactivex.rxjava3.internal.functions.Functions.* +import kotlinx.coroutines.* +import org.junit.* +import org.junit.Test +import java.util.concurrent.* +import java.util.concurrent.CancellationException +import kotlin.test.* + +class MaybeTest : TestBase() { + @Before + fun setup() { + ignoreLostThreads("RxComputationThreadPool-", "RxCachedWorkerPoolEvictor-", "RxSchedulerPurge-") + } + + @Test + fun testBasicSuccess() = runBlocking { + expect(1) + val maybe = rxMaybe(currentDispatcher()) { + expect(4) + "OK" + } + expect(2) + maybe.subscribe { value -> + expect(5) + assertEquals("OK", value) + } + expect(3) + yield() // to started coroutine + finish(6) + } + + @Test + fun testBasicEmpty() = runBlocking { + expect(1) + val maybe = rxMaybe(currentDispatcher()) { + expect(4) + null + } + expect(2) + maybe.subscribe (emptyConsumer(), ON_ERROR_MISSING, Action { + expect(5) + }) + expect(3) + yield() // to started coroutine + finish(6) + } + + @Test + fun testBasicFailure() = runBlocking { + expect(1) + val maybe = rxMaybe(currentDispatcher()) { + expect(4) + throw RuntimeException("OK") + } + expect(2) + maybe.subscribe({ + expectUnreached() + }, { error -> + expect(5) + assertTrue(error is RuntimeException) + assertEquals("OK", error.message) + }) + expect(3) + yield() // to started coroutine + finish(6) + } + + + @Test + fun testBasicUnsubscribe() = runBlocking { + expect(1) + val maybe = rxMaybe(currentDispatcher()) { + expect(4) + yield() // back to main, will get cancelled + expectUnreached() + } + expect(2) + // nothing is called on a disposed rx3 maybe + val sub = maybe.subscribe({ + expectUnreached() + }, { + expectUnreached() + }) + expect(3) + yield() // to started coroutine + expect(5) + sub.dispose() // will cancel coroutine + yield() + finish(6) + } + + @Test + fun testMaybeNoWait() { + val maybe = rxMaybe { + "OK" + } + + checkMaybeValue(maybe) { + assertEquals("OK", it) + } + } + + @Test + fun testMaybeAwait() = runBlocking { + assertEquals("OK", Maybe.just("O").await() + "K") + } + + @Test + fun testMaybeAwaitForNull() = runBlocking { + assertNull(Maybe.empty().await()) + } + + @Test + fun testMaybeEmitAndAwait() { + val maybe = rxMaybe { + Maybe.just("O").await() + "K" + } + + checkMaybeValue(maybe) { + assertEquals("OK", it) + } + } + + @Test + fun testMaybeWithDelay() { + val maybe = rxMaybe { + Observable.timer(50, TimeUnit.MILLISECONDS).map { "O" }.awaitSingle() + "K" + } + + checkMaybeValue(maybe) { + assertEquals("OK", it) + } + } + + @Test + fun testMaybeException() { + val maybe = rxMaybe { + Observable.just("O", "K").awaitSingle() + "K" + } + + checkErroneous(maybe) { + assert(it is IllegalArgumentException) + } + } + + @Test + fun testAwaitFirst() { + val maybe = rxMaybe { + Observable.just("O", "#").awaitFirst() + "K" + } + + checkMaybeValue(maybe) { + assertEquals("OK", it) + } + } + + @Test + fun testAwaitLast() { + val maybe = rxMaybe { + Observable.just("#", "O").awaitLast() + "K" + } + + checkMaybeValue(maybe) { + assertEquals("OK", it) + } + } + + @Test + fun testExceptionFromObservable() { + val maybe = rxMaybe { + try { + Observable.error(RuntimeException("O")).awaitFirst() + } catch (e: RuntimeException) { + Observable.just(e.message!!).awaitLast() + "K" + } + } + + checkMaybeValue(maybe) { + assertEquals("OK", it) + } + } + + @Test + fun testExceptionFromCoroutine() { + val maybe = rxMaybe { + throw IllegalStateException(Observable.just("O").awaitSingle() + "K") + } + + checkErroneous(maybe) { + assert(it is IllegalStateException) + assertEquals("OK", it.message) + } + } + + @Test + fun testCancelledConsumer() = runTest { + expect(1) + val maybe = rxMaybe(currentDispatcher()) { + expect(4) + try { + delay(Long.MAX_VALUE) + } catch (e: CancellationException) { + expect(6) + } + 42 + } + expect(2) + val timeout = withTimeoutOrNull(100) { + expect(3) + maybe.collect { + expectUnreached() + } + expectUnreached() + } + assertNull(timeout) + expect(5) + yield() // must cancel code inside maybe!!! + finish(7) + } + + @Test + fun testSuppressedException() = runTest { + val maybe = rxMaybe(currentDispatcher()) { + launch(start = CoroutineStart.ATOMIC) { + throw TestException() // child coroutine fails + } + try { + delay(Long.MAX_VALUE) + } finally { + throw TestException2() // but parent throws another exception while cleaning up + } + } + try { + maybe.await() + expectUnreached() + } catch (e: TestException) { + assertTrue(e.suppressed[0] is TestException2) + } + } + + @Test + fun testUnhandledException() = runTest { + expect(1) + var disposable: Disposable? = null + val handler = { e: Throwable -> + assertTrue(e is UndeliverableException && e.cause is TestException) + expect(5) + } + val maybe = rxMaybe(currentDispatcher()) { + expect(4) + disposable!!.dispose() // cancel our own subscription, so that delay will get cancelled + try { + delay(Long.MAX_VALUE) + } finally { + throw TestException() // would not be able to handle it since mono is disposed + } + } + withExceptionHandler(handler) { + maybe.subscribe(object : MaybeObserver { + override fun onSubscribe(d: Disposable) { + expect(2) + disposable = d + } + + override fun onComplete() { + expectUnreached() + } + + override fun onSuccess(t: Unit) { + expectUnreached() + } + + override fun onError(t: Throwable) { + expectUnreached() + } + }) + expect(3) + yield() // run coroutine + finish(6) + } + } + + @Test + fun testFatalExceptionInSubscribe() = runTest { + val handler = { e: Throwable -> + assertTrue(e is UndeliverableException && e.cause is LinkageError) + expect(2) + } + + withExceptionHandler(handler) { + rxMaybe(Dispatchers.Unconfined) { + expect(1) + 42 + }.subscribe({ throw LinkageError() }) + finish(3) + } + } + + @Test + fun testFatalExceptionInSingle() = runTest { + rxMaybe(Dispatchers.Unconfined) { + throw LinkageError() + }.subscribe({ expectUnreached() }, { expect(1); assertTrue(it is LinkageError) }) + finish(2) + } +} diff --git a/reactive/kotlinx-coroutines-rx3/test/ObservableAsFlowTest.kt b/reactive/kotlinx-coroutines-rx3/test/ObservableAsFlowTest.kt new file mode 100644 index 0000000000..262da9ac1b --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/test/ObservableAsFlowTest.kt @@ -0,0 +1,185 @@ +/* + * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.core.Observable +import io.reactivex.rxjava3.core.ObservableSource +import io.reactivex.rxjava3.core.Observer +import io.reactivex.rxjava3.disposables.Disposable +import io.reactivex.rxjava3.subjects.PublishSubject +import kotlinx.coroutines.* +import kotlinx.coroutines.channels.* +import kotlinx.coroutines.flow.* +import kotlin.test.* + +class ObservableAsFlowTest : TestBase() { + @Test + fun testCancellation() = runTest { + var onNext = 0 + var onCancelled = 0 + var onError = 0 + + val source = rxObservable(currentDispatcher()) { + coroutineContext[Job]?.invokeOnCompletion { + if (it is CancellationException) ++onCancelled + } + + repeat(100) { + send(it) + } + } + + source.asFlow().launchIn(CoroutineScope(Dispatchers.Unconfined)) { + onEach { + ++onNext + throw RuntimeException() + } + catch { + ++onError + } + }.join() + + + assertEquals(1, onNext) + assertEquals(1, onError) + assertEquals(1, onCancelled) + } + + @Test + fun testImmediateCollection() { + val source = PublishSubject.create() + val flow = source.asFlow() + GlobalScope.launch(Dispatchers.Unconfined) { + expect(1) + flow.collect { expect(it) } + expect(6) + } + expect(2) + source.onNext(3) + expect(4) + source.onNext(5) + source.onComplete() + finish(7) + } + + @Test + fun testOnErrorCancellation() { + val source = PublishSubject.create() + val flow = source.asFlow() + val exception = RuntimeException() + GlobalScope.launch(Dispatchers.Unconfined) { + try { + expect(1) + flow.collect { expect(it) } + expectUnreached() + } + catch (e: Exception) { + assertSame(exception, e.cause) + expect(5) + } + expect(6) + } + expect(2) + source.onNext(3) + expect(4) + source.onError(exception) + finish(7) + } + + @Test + fun testUnsubscribeOnCollectionException() { + val source = PublishSubject.create() + val flow = source.asFlow() + val exception = RuntimeException() + GlobalScope.launch(Dispatchers.Unconfined) { + try { + expect(1) + flow.collect { + expect(it) + if (it == 3) throw exception + } + expectUnreached() + } + catch (e: Exception) { + assertSame(exception, e.cause) + expect(4) + } + expect(5) + } + expect(2) + assertTrue(source.hasObservers()) + source.onNext(3) + assertFalse(source.hasObservers()) + finish(6) + } + + @Test + fun testLateOnSubscribe() { + var observer: Observer? = null + val source = ObservableSource { observer = it } + val flow = source.asFlow() + assertNull(observer) + val job = GlobalScope.launch(Dispatchers.Unconfined) { + expect(1) + flow.collect { expectUnreached() } + expectUnreached() + } + expect(2) + assertNotNull(observer) + job.cancel() + val disposable = Disposable.empty() + observer!!.onSubscribe(disposable) + assertTrue(disposable.isDisposed) + finish(3) + } + + @Test + fun testBufferUnlimited() = runTest { + val source = rxObservable(currentDispatcher()) { + expect(1); send(10) + expect(2); send(11) + expect(3); send(12) + expect(4); send(13) + expect(5); send(14) + expect(6); send(15) + expect(7); send(16) + expect(8); send(17) + expect(9) + } + source.asFlow().buffer(Channel.UNLIMITED).collect { expect(it) } + finish(18) + } + + @Test + fun testConflated() = runTest { + val source = Observable.range(1, 5) + val list = source.asFlow().conflate().toList() + assertEquals(listOf(1, 5), list) + } + + @Test + fun testLongRange() = runTest { + val source = Observable.range(1, 10_000) + val count = source.asFlow().count() + assertEquals(10_000, count) + } + + @Test + fun testProduce() = runTest { + val source = Observable.range(0, 10) + val flow = source.asFlow() + check((0..9).toList(), flow.produceIn(this)) + check((0..9).toList(), flow.buffer(Channel.UNLIMITED).produceIn(this)) + check((0..9).toList(), flow.buffer(2).produceIn(this)) + check((0..9).toList(), flow.buffer(0).produceIn(this)) + check(listOf(0, 9), flow.conflate().produceIn(this)) + } + + private suspend fun check(expected: List, channel: ReceiveChannel) { + val result = ArrayList(10) + channel.consumeEach { result.add(it) } + assertEquals(expected, result) + } +} diff --git a/reactive/kotlinx-coroutines-rx3/test/ObservableCompletionStressTest.kt b/reactive/kotlinx-coroutines-rx3/test/ObservableCompletionStressTest.kt new file mode 100644 index 0000000000..c1d25bcb51 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/test/ObservableCompletionStressTest.kt @@ -0,0 +1,36 @@ +/* + * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import kotlinx.coroutines.* +import org.junit.* +import java.util.* +import kotlin.coroutines.* + +class ObservableCompletionStressTest : TestBase() { + private val N_REPEATS = 10_000 * stressTestMultiplier + + private fun CoroutineScope.range(context: CoroutineContext, start: Int, count: Int) = rxObservable(context) { + for (x in start until start + count) send(x) + } + + @Test + fun testCompletion() { + val rnd = Random() + repeat(N_REPEATS) { + val count = rnd.nextInt(5) + runBlocking { + withTimeout(5000) { + var received = 0 + range(Dispatchers.Default, 1, count).collect { x -> + received++ + if (x != received) error("$x != $received") + } + if (received != count) error("$received != $count") + } + } + } + } +} diff --git a/reactive/kotlinx-coroutines-rx3/test/ObservableExceptionHandlingTest.kt b/reactive/kotlinx-coroutines-rx3/test/ObservableExceptionHandlingTest.kt new file mode 100644 index 0000000000..1183b2ae21 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/test/ObservableExceptionHandlingTest.kt @@ -0,0 +1,134 @@ +/* + * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.exceptions.* +import kotlinx.coroutines.* +import org.junit.* +import org.junit.Test +import kotlin.test.* + +class ObservableExceptionHandlingTest : TestBase() { + + @Before + fun setup() { + ignoreLostThreads("RxComputationThreadPool-", "RxCachedWorkerPoolEvictor-", "RxSchedulerPurge-") + } + + private inline fun handler(expect: Int) = { t: Throwable -> + assertTrue(t is UndeliverableException && t.cause is T) + expect(expect) + } + + private fun cehUnreached() = CoroutineExceptionHandler { _, _ -> expectUnreached() } + + @Test + fun testException() = withExceptionHandler({ expectUnreached() }) { + rxObservable(Dispatchers.Unconfined + cehUnreached()) { + expect(1) + throw TestException() + }.subscribe({ + expectUnreached() + }, { + expect(2) // Reported to onError + }) + finish(3) + } + + @Test + fun testFatalException() = withExceptionHandler(handler(3)) { + rxObservable(Dispatchers.Unconfined) { + expect(1) + throw LinkageError() + }.subscribe({ + expectUnreached() + }, { + expect(2) + }) + finish(4) + } + + @Test + fun testExceptionAsynchronous() = withExceptionHandler({ expectUnreached() }) { + rxObservable(Dispatchers.Unconfined) { + expect(1) + throw TestException() + }.publish() + .refCount() + .subscribe({ + expectUnreached() + }, { + expect(2) // Reported to onError + }) + finish(3) + } + + @Test + fun testFatalExceptionAsynchronous() = withExceptionHandler(handler(3)) { + rxObservable(Dispatchers.Unconfined) { + expect(1) + throw LinkageError() + }.publish() + .refCount() + .subscribe({ + expectUnreached() + }, { + expect(2) // Fatal exception is not reported in onError + }) + finish(4) + } + + @Test + fun testFatalExceptionFromSubscribe() = withExceptionHandler(handler(4)) { + rxObservable(Dispatchers.Unconfined) { + expect(1) + send(Unit) + }.subscribe({ + expect(2) + throw LinkageError() + }, { expect(3) }) // Unreached because fatal errors are rethrown + finish(5) + } + + @Test + fun testExceptionFromSubscribe() = withExceptionHandler({ expectUnreached() }) { + rxObservable(Dispatchers.Unconfined) { + expect(1) + send(Unit) + }.subscribe({ + expect(2) + throw TestException() + }, { expect(3) }) // not reported to onError because came from the subscribe itself + finish(4) + } + + @Test + fun testAsynchronousExceptionFromSubscribe() = withExceptionHandler({ expectUnreached() }) { + rxObservable(Dispatchers.Unconfined) { + expect(1) + send(Unit) + }.publish() + .refCount() + .subscribe({ + expect(2) + throw RuntimeException() + }, { expect(3) }) + finish(4) + } + + @Test + fun testAsynchronousFatalExceptionFromSubscribe() = withExceptionHandler(handler(4)) { + rxObservable(Dispatchers.Unconfined) { + expect(1) + send(Unit) + }.publish() + .refCount() + .subscribe({ + expect(2) + throw LinkageError() + }, { expect(3) }) + finish(5) + } +} diff --git a/reactive/kotlinx-coroutines-rx3/test/ObservableMultiTest.kt b/reactive/kotlinx-coroutines-rx3/test/ObservableMultiTest.kt new file mode 100644 index 0000000000..b4adf7af27 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/test/ObservableMultiTest.kt @@ -0,0 +1,91 @@ +/* + * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.core.* +import kotlinx.coroutines.* +import org.junit.Test +import java.io.* +import kotlin.test.* + +/** + * Test emitting multiple values with [rxObservable]. + */ +class ObservableMultiTest : TestBase() { + @Test + fun testNumbers() { + val n = 100 * stressTestMultiplier + val observable = rxObservable { + repeat(n) { send(it) } + } + checkSingleValue(observable.toList()) { list -> + assertEquals((0 until n).toList(), list) + } + } + + + @Test + fun testConcurrentStress() { + val n = 10_000 * stressTestMultiplier + val observable = rxObservable { + newCoroutineContext(coroutineContext) + // concurrent emitters (many coroutines) + val jobs = List(n) { + // launch + launch { + val i = it + send(i) + } + } + jobs.forEach { it.join() } + } + checkSingleValue(observable.toList()) { list -> + assertEquals(n, list.size) + assertEquals((0 until n).toList(), list.sorted()) + } + } + + @Test + fun testIteratorResendUnconfined() { + val n = 10_000 * stressTestMultiplier + val observable = rxObservable(Dispatchers.Unconfined) { + Observable.range(0, n).collect { send(it) } + } + checkSingleValue(observable.toList()) { list -> + assertEquals((0 until n).toList(), list) + } + } + + @Test + fun testIteratorResendPool() { + val n = 10_000 * stressTestMultiplier + val observable = rxObservable { + Observable.range(0, n).collect { send(it) } + } + checkSingleValue(observable.toList()) { list -> + assertEquals((0 until n).toList(), list) + } + } + + @Test + fun testSendAndCrash() { + val observable = rxObservable { + send("O") + throw IOException("K") + } + val single = rxSingle { + var result = "" + try { + observable.collect { result += it } + } catch(e: IOException) { + result += e.message + } + result + } + checkSingleValue(single) { + assertEquals("OK", it) + } + } +} diff --git a/reactive/kotlinx-coroutines-rx3/test/ObservableSingleTest.kt b/reactive/kotlinx-coroutines-rx3/test/ObservableSingleTest.kt new file mode 100644 index 0000000000..2a3ce04638 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/test/ObservableSingleTest.kt @@ -0,0 +1,212 @@ +/* + * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.core.* +import kotlinx.coroutines.* +import org.junit.* +import org.junit.Test +import java.util.concurrent.* +import kotlin.test.* + +class ObservableSingleTest : TestBase() { + @Before + fun setup() { + ignoreLostThreads("RxComputationThreadPool-", "RxCachedWorkerPoolEvictor-", "RxSchedulerPurge-") + } + + @Test + fun testSingleNoWait() { + val observable = rxObservable { + send("OK") + } + + checkSingleValue(observable) { + assertEquals("OK", it) + } + } + + @Test + fun testSingleAwait() = runBlocking { + assertEquals("OK", Observable.just("O").awaitSingle() + "K") + } + + @Test + fun testSingleEmitAndAwait() { + val observable = rxObservable { + send(Observable.just("O").awaitSingle() + "K") + } + + checkSingleValue(observable) { + assertEquals("OK", it) + } + } + + @Test + fun testSingleWithDelay() { + val observable = rxObservable { + send(Observable.timer(50, TimeUnit.MILLISECONDS).map { "O" }.awaitSingle() + "K") + } + + checkSingleValue(observable) { + assertEquals("OK", it) + } + } + + @Test + fun testSingleException() { + val observable = rxObservable { + send(Observable.just("O", "K").awaitSingle() + "K") + } + + checkErroneous(observable) { + assertTrue(it is IllegalArgumentException) + } + } + + @Test + fun testAwaitFirst() { + val observable = rxObservable { + send(Observable.just("O", "#").awaitFirst() + "K") + } + + checkSingleValue(observable) { + assertEquals("OK", it) + } + } + + @Test + fun testAwaitFirstOrDefault() { + val observable = rxObservable { + send(Observable.empty().awaitFirstOrDefault("O") + "K") + } + + checkSingleValue(observable) { + assertEquals("OK", it) + } + } + + @Test + fun testAwaitFirstOrDefaultWithValues() { + val observable = rxObservable { + send(Observable.just("O", "#").awaitFirstOrDefault("!") + "K") + } + + checkSingleValue(observable) { + assertEquals("OK", it) + } + } + + @Test + fun testAwaitFirstOrNull() { + val observable = rxObservable { + send(Observable.empty().awaitFirstOrNull() ?: "OK") + } + + checkSingleValue(observable) { + assertEquals("OK", it) + } + } + + @Test + fun testAwaitFirstOrNullWithValues() { + val observable = rxObservable { + send((Observable.just("O", "#").awaitFirstOrNull() ?: "!") + "K") + } + + checkSingleValue(observable) { + assertEquals("OK", it) + } + } + + @Test + fun testAwaitFirstOrElse() { + val observable = rxObservable { + send(Observable.empty().awaitFirstOrElse { "O" } + "K") + } + + checkSingleValue(observable) { + assertEquals("OK", it) + } + } + + @Test + fun testAwaitFirstOrElseWithValues() { + val observable = rxObservable { + send(Observable.just("O", "#").awaitFirstOrElse { "!" } + "K") + } + + checkSingleValue(observable) { + assertEquals("OK", it) + } + } + + @Test + fun testAwaitLast() { + val observable = rxObservable { + send(Observable.just("#", "O").awaitLast() + "K") + } + + checkSingleValue(observable) { + assertEquals("OK", it) + } + } + + @Test + fun testExceptionFromObservable() { + val observable = rxObservable { + try { + send(Observable.error(RuntimeException("O")).awaitFirst()) + } catch (e: RuntimeException) { + send(Observable.just(e.message!!).awaitLast() + "K") + } + } + + checkSingleValue(observable) { + assertEquals("OK", it) + } + } + + @Test + fun testExceptionFromCoroutine() { + val observable = rxObservable { + throw IllegalStateException(Observable.just("O").awaitSingle() + "K") + } + + checkErroneous(observable) { + assertTrue(it is IllegalStateException) + assertEquals("OK", it.message) + } + } + + @Test + fun testObservableIteration() { + val observable = rxObservable { + var result = "" + Observable.just("O", "K").collect { result += it } + send(result) + } + + checkSingleValue(observable) { + assertEquals("OK", it) + } + } + + @Test + fun testObservableIterationFailure() { + val observable = rxObservable { + try { + Observable.error(RuntimeException("OK")).collect { fail("Should not be here") } + send("Fail") + } catch (e: RuntimeException) { + send(e.message!!) + } + } + + checkSingleValue(observable) { + assertEquals("OK", it) + } + } +} diff --git a/reactive/kotlinx-coroutines-rx3/test/ObservableSourceAsFlowStressTest.kt b/reactive/kotlinx-coroutines-rx3/test/ObservableSourceAsFlowStressTest.kt new file mode 100644 index 0000000000..431a7a789e --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/test/ObservableSourceAsFlowStressTest.kt @@ -0,0 +1,36 @@ +/* + * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.core.* +import io.reactivex.rxjava3.exceptions.* +import kotlinx.coroutines.* +import kotlinx.coroutines.channels.* +import kotlinx.coroutines.flow.* +import org.junit.* +import java.util.concurrent.* + +class ObservableSourceAsFlowStressTest : TestBase() { + + private val iterations = 100 * stressTestMultiplierSqrt + + @Before + fun setup() { + ignoreLostThreads("RxComputationThreadPool-", "RxCachedWorkerPoolEvictor-", "RxSchedulerPurge-") + } + + @Test + fun testAsFlowCancellation() = runTest { + repeat(iterations) { + val latch = Channel(1) + var i = 0 + val observable = Observable.interval(100L, TimeUnit.MICROSECONDS) + .doOnNext { if (++i > 100) latch.offer(Unit) } + val job = observable.asFlow().launchIn(CoroutineScope(Dispatchers.Default)) + latch.receive() + job.cancelAndJoin() + } + } +} diff --git a/reactive/kotlinx-coroutines-rx3/test/ObservableSubscriptionSelectTest.kt b/reactive/kotlinx-coroutines-rx3/test/ObservableSubscriptionSelectTest.kt new file mode 100644 index 0000000000..2f04316159 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/test/ObservableSubscriptionSelectTest.kt @@ -0,0 +1,51 @@ +/* + * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import kotlinx.coroutines.* +import kotlinx.coroutines.selects.* +import org.junit.Test +import kotlin.test.* + +class ObservableSubscriptionSelectTest : TestBase() { + @Test + fun testSelect() = runTest { + // source with n ints + val n = 1000 * stressTestMultiplier + val source = rxObservable { repeat(n) { send(it) } } + var a = 0 + var b = 0 + // open two subs + val channelA = source.openSubscription() + val channelB = source.openSubscription() + loop@ while (true) { + val done: Int = select { + channelA.onReceiveOrNull { + if (it != null) assertEquals(a++, it) + if (it == null) 0 else 1 + } + channelB.onReceiveOrNull { + if (it != null) assertEquals(b++, it) + if (it == null) 0 else 2 + } + } + when (done) { + 0 -> break@loop + 1 -> { + val r = channelB.receiveOrNull() + if (r != null) assertEquals(b++, r) + } + 2 -> { + val r = channelA.receiveOrNull() + if (r != null) assertEquals(a++, r) + } + } + } + channelA.cancel() + channelB.cancel() + // should receive one of them fully + assertTrue(a == n || b == n) + } +} diff --git a/reactive/kotlinx-coroutines-rx3/test/ObservableTest.kt b/reactive/kotlinx-coroutines-rx3/test/ObservableTest.kt new file mode 100644 index 0000000000..c6a6be56e3 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/test/ObservableTest.kt @@ -0,0 +1,164 @@ +/* + * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.core.* +import io.reactivex.rxjava3.plugins.* +import kotlinx.coroutines.* +import kotlinx.coroutines.CancellationException +import org.junit.* +import org.junit.Test +import java.util.concurrent.* +import kotlin.test.* + +class ObservableTest : TestBase() { + @Before + fun setup() { + ignoreLostThreads("RxComputationThreadPool-", "RxCachedWorkerPoolEvictor-", "RxSchedulerPurge-") + } + + @Test + fun testBasicSuccess() = runBlocking { + expect(1) + val observable = rxObservable(currentDispatcher()) { + expect(4) + send("OK") + } + expect(2) + observable.subscribe { value -> + expect(5) + assertEquals("OK", value) + } + expect(3) + yield() // to started coroutine + finish(6) + } + + @Test + fun testBasicFailure() = runBlocking { + expect(1) + val observable = rxObservable(currentDispatcher()) { + expect(4) + throw RuntimeException("OK") + } + expect(2) + observable.subscribe({ + expectUnreached() + }, { error -> + expect(5) + assertTrue(error is RuntimeException) + assertEquals("OK", error.message) + }) + expect(3) + yield() // to started coroutine + finish(6) + } + + @Test + fun testBasicUnsubscribe() = runBlocking { + expect(1) + val observable = rxObservable(currentDispatcher()) { + expect(4) + yield() // back to main, will get cancelled + expectUnreached() + } + expect(2) + val sub = observable.subscribe({ + expectUnreached() + }, { + expectUnreached() + }) + expect(3) + yield() // to started coroutine + expect(5) + sub.dispose() // will cancel coroutine + yield() + finish(6) + } + + @Test + fun testNotifyOnceOnCancellation() = runTest { + expect(1) + val observable = + rxObservable(currentDispatcher()) { + expect(5) + send("OK") + try { + delay(Long.MAX_VALUE) + } catch (e: CancellationException) { + expect(11) + } + } + .doOnNext { + expect(6) + assertEquals("OK", it) + } + .doOnDispose { + expect(10) // notified once! + } + expect(2) + val job = launch(start = CoroutineStart.UNDISPATCHED) { + expect(3) + observable.collect { + expect(8) + assertEquals("OK", it) + } + } + expect(4) + yield() // to observable code + expect(7) + yield() // to consuming coroutines + expect(9) + job.cancel() + job.join() + finish(12) + } + + @Test + fun testFailingConsumer() = runTest { + expect(1) + val pub = rxObservable(currentDispatcher()) { + expect(2) + send("OK") + try { + delay(Long.MAX_VALUE) + } catch (e: CancellationException) { + finish(5) + } + } + try { + pub.collect { + expect(3) + throw TestException() + } + } catch (e: TestException) { + expect(4) + } + } + + @Test + fun testExceptionAfterCancellation() { + // Test that no exceptions were reported to the global EH (it will fail the test if so) + val handler = { e: Throwable -> + assertFalse(e is CancellationException) + } + withExceptionHandler(handler) { + RxJavaPlugins.setErrorHandler { + require(it !is CancellationException) + } + Observable + .interval(1, TimeUnit.MILLISECONDS) + .take(1000) + .switchMapSingle { + rxSingle { + timeBomb().await() + } + } + .blockingSubscribe({}, {}) + } + } + + private fun timeBomb() = Single.timer(1, TimeUnit.MILLISECONDS).doOnSuccess { throw TestException() } +} diff --git a/reactive/kotlinx-coroutines-rx3/test/SchedulerTest.kt b/reactive/kotlinx-coroutines-rx3/test/SchedulerTest.kt new file mode 100644 index 0000000000..9e95c213d0 --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/test/SchedulerTest.kt @@ -0,0 +1,34 @@ +/* + * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.schedulers.Schedulers +import kotlinx.coroutines.* +import org.junit.Before +import org.junit.Test +import kotlin.test.* + +class SchedulerTest : TestBase() { + @Before + fun setup() { + ignoreLostThreads("RxCachedThreadScheduler-", "RxCachedWorkerPoolEvictor-", "RxSchedulerPurge-") + } + + @Test + fun testIoScheduler(): Unit = runBlocking { + expect(1) + val mainThread = Thread.currentThread() + withContext(Schedulers.io().asCoroutineDispatcher()) { + val t1 = Thread.currentThread() + assertNotSame(t1, mainThread) + expect(2) + delay(100) + val t2 = Thread.currentThread() + assertNotSame(t2, mainThread) + expect(3) + } + finish(4) + } +} diff --git a/reactive/kotlinx-coroutines-rx3/test/SingleTest.kt b/reactive/kotlinx-coroutines-rx3/test/SingleTest.kt new file mode 100644 index 0000000000..46bcaf84dc --- /dev/null +++ b/reactive/kotlinx-coroutines-rx3/test/SingleTest.kt @@ -0,0 +1,266 @@ +/* + * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.rx3 + +import io.reactivex.rxjava3.core.* +import io.reactivex.rxjava3.disposables.* +import io.reactivex.rxjava3.exceptions.* +import io.reactivex.rxjava3.functions.* +import kotlinx.coroutines.* +import org.junit.* +import org.junit.Test +import java.util.concurrent.* +import kotlin.test.* + +class SingleTest : TestBase() { + @Before + fun setup() { + ignoreLostThreads("RxComputationThreadPool-", "RxCachedWorkerPoolEvictor-", "RxSchedulerPurge-") + } + + @Test + fun testBasicSuccess() = runBlocking { + expect(1) + val single = rxSingle(currentDispatcher()) { + expect(4) + "OK" + } + expect(2) + single.subscribe { value -> + expect(5) + assertEquals("OK", value) + } + expect(3) + yield() // to started coroutine + finish(6) + } + + @Test + fun testBasicFailure() = runBlocking { + expect(1) + val single = rxSingle(currentDispatcher()) { + expect(4) + throw RuntimeException("OK") + } + expect(2) + single.subscribe({ + expectUnreached() + }, { error -> + expect(5) + assertTrue(error is RuntimeException) + assertEquals("OK", error.message) + }) + expect(3) + yield() // to started coroutine + finish(6) + } + + + @Test + fun testBasicUnsubscribe() = runBlocking { + expect(1) + val single = rxSingle(currentDispatcher()) { + expect(4) + yield() // back to main, will get cancelled + expectUnreached() + + } + expect(2) + // nothing is called on a disposed rx3 single + val sub = single.subscribe({ + expectUnreached() + }, { + expectUnreached() + }) + expect(3) + yield() // to started coroutine + expect(5) + sub.dispose() // will cancel coroutine + yield() + finish(6) + } + + @Test + fun testSingleNoWait() { + val single = rxSingle { + "OK" + } + + checkSingleValue(single) { + assertEquals("OK", it) + } + } + + @Test + fun testSingleAwait() = runBlocking { + assertEquals("OK", Single.just("O").await() + "K") + } + + @Test + fun testSingleEmitAndAwait() { + val single = rxSingle { + Single.just("O").await() + "K" + } + + checkSingleValue(single) { + assertEquals("OK", it) + } + } + + @Test + fun testSingleWithDelay() { + val single = rxSingle { + Observable.timer(50, TimeUnit.MILLISECONDS).map { "O" }.awaitSingle() + "K" + } + + checkSingleValue(single) { + assertEquals("OK", it) + } + } + + @Test + fun testSingleException() { + val single = rxSingle { + Observable.just("O", "K").awaitSingle() + "K" + } + + checkErroneous(single) { + assert(it is IllegalArgumentException) + } + } + + @Test + fun testAwaitFirst() { + val single = rxSingle { + Observable.just("O", "#").awaitFirst() + "K" + } + + checkSingleValue(single) { + assertEquals("OK", it) + } + } + + @Test + fun testAwaitLast() { + val single = rxSingle { + Observable.just("#", "O").awaitLast() + "K" + } + + checkSingleValue(single) { + assertEquals("OK", it) + } + } + + @Test + fun testExceptionFromObservable() { + val single = rxSingle { + try { + Observable.error(RuntimeException("O")).awaitFirst() + } catch (e: RuntimeException) { + Observable.just(e.message!!).awaitLast() + "K" + } + } + + checkSingleValue(single) { + assertEquals("OK", it) + } + } + + @Test + fun testExceptionFromCoroutine() { + val single = rxSingle { + throw IllegalStateException(Observable.just("O").awaitSingle() + "K") + } + + checkErroneous(single) { + assert(it is IllegalStateException) + assertEquals("OK", it.message) + } + } + + @Test + fun testSuppressedException() = runTest { + val single = rxSingle(currentDispatcher()) { + launch(start = CoroutineStart.ATOMIC) { + throw TestException() // child coroutine fails + } + try { + delay(Long.MAX_VALUE) + } finally { + throw TestException2() // but parent throws another exception while cleaning up + } + } + try { + single.await() + expectUnreached() + } catch (e: TestException) { + assertTrue(e.suppressed[0] is TestException2) + } + } + + @Test + fun testFatalExceptionInSubscribe() = runTest { + val handler = { e: Throwable -> + assertTrue(e is UndeliverableException && e.cause is LinkageError) + expect(2) + } + withExceptionHandler(handler) { + rxSingle(Dispatchers.Unconfined) { + expect(1) + 42 + }.subscribe(Consumer { + throw LinkageError() + }) + finish(3) + } + } + + @Test + fun testFatalExceptionInSingle() = runTest { + rxSingle(Dispatchers.Unconfined) { + throw LinkageError() + }.subscribe({ _, e -> assertTrue(e is LinkageError); expect(1) }) + + finish(2) + } + + @Test + fun testUnhandledException() = runTest { + expect(1) + var disposable: Disposable? = null + val handler = { e: Throwable -> + assertTrue(e is UndeliverableException && e.cause is TestException) + expect(5) + } + val single = rxSingle(currentDispatcher()) { + expect(4) + disposable!!.dispose() // cancel our own subscription, so that delay will get cancelled + try { + delay(Long.MAX_VALUE) + } finally { + throw TestException() // would not be able to handle it since mono is disposed + } + } + withExceptionHandler(handler) { + single.subscribe(object : SingleObserver { + override fun onSubscribe(d: Disposable) { + expect(2) + disposable = d + } + + override fun onSuccess(t: Unit) { + expectUnreached() + } + + override fun onError(t: Throwable) { + expectUnreached() + } + }) + expect(3) + yield() // run coroutine + finish(6) + } + } +} diff --git a/settings.gradle b/settings.gradle index 64ae2ffad3..3a07891799 100644 --- a/settings.gradle +++ b/settings.gradle @@ -2,6 +2,16 @@ * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ +pluginManagement { + plugins { + id "org.openjfx.javafxplugin" version javafx_plugin_version + + // JMH + id "net.ltgt.apt" version "0.21" + id "me.champeau.gradle.jmh" version "0.5.2" + } +} + rootProject.name = 'kotlinx.coroutines' enableFeaturePreview('GRADLE_METADATA') @@ -34,9 +44,12 @@ module('reactive/kotlinx-coroutines-reactive') module('reactive/kotlinx-coroutines-reactor') module('reactive/kotlinx-coroutines-jdk9') module('reactive/kotlinx-coroutines-rx2') +module('reactive/kotlinx-coroutines-rx3') module('ui/kotlinx-coroutines-android') module('ui/kotlinx-coroutines-android/android-unit-tests') -module('ui/kotlinx-coroutines-javafx') +if (JavaVersion.current().isJava11Compatible()) { + module('ui/kotlinx-coroutines-javafx') +} module('ui/kotlinx-coroutines-swing') module('js/js-stub') @@ -45,4 +58,4 @@ if (!build_snapshot_train) { include('site') } -module('publication-validator') +module('integration-testing') diff --git a/site/build.gradle b/site/build.gradle deleted file mode 100644 index 6b2c5b7916..0000000000 --- a/site/build.gradle +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -def buildDocsDir = "$buildDir/docs" -def jekyllDockerImage = "jekyll/jekyll:$jekyll_version" - -task copyDocs(type: Copy, dependsOn: rootProject.getTasksByName("dokka", true)) { - from (rootProject.getTasksByName("dokka", true).collect { "$it.project.buildDir/dokka" }) { - include "**/*.md" - include "**/package-list" - } - from "docs" - into buildDocsDir -} - -task copyExampleFrontendJs(type: Copy, dependsOn: ':example-frontend-js:bundle') { - def srcBuildDir = project(':example-frontend-js').buildDir - from "$srcBuildDir/dist" - into "$buildDocsDir/example-frontend-js" -} - -task site(type: Exec, description: 'Generate github pages', dependsOn: [copyDocs, copyExampleFrontendJs]) { - inputs.files(fileTree(buildDocsDir)) - outputs.dir("$buildDir/dist") - workingDir file(buildDocsDir) - commandLine 'docker', 'run', '--rm', "--volume=$buildDir:/srv/jekyll", - '-t', jekyllDockerImage, - '/bin/bash', '-c', 'cd docs; jekyll build' -} - -// A useful task for local debugging -- serves a site locally -task serve(type: Exec, dependsOn: [copyDocs, copyExampleFrontendJs]) { - commandLine 'docker', 'run', '--rm', "--volume=$buildDir:/srv/jekyll", - '-p', '8080:8080', - '-t', jekyllDockerImage, - '/bin/bash', '-c', 'cd docs; jekyll serve --host 0.0.0.0 --port 8080' -} - -task clean(type: Delete) { - delete buildDir -} - diff --git a/site/build.gradle.kts b/site/build.gradle.kts new file mode 100644 index 0000000000..eba7b1a1bc --- /dev/null +++ b/site/build.gradle.kts @@ -0,0 +1,65 @@ +import groovy.lang.* + +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +val buildDocsDir = "$buildDir/docs" +val jekyllDockerImage = "jekyll/jekyll:${version("jekyll")}" + +val copyDocs by tasks.registering(Copy::class) { + val dokkaTasks = rootProject.getTasksByName("dokka", true) + + from(dokkaTasks.map { "${it.project.buildDir}/dokka" }) { + include("**/*.md") + include("**/package-list") + } + from("docs") + into(buildDocsDir) + filter { it.replace("/index.md\"", "/index.html\"") } + + dependsOn(dokkaTasks) +} + +val copyExampleFrontendJs by tasks.registering(Copy::class) { + val srcBuildDir = project(":example-frontend-js").buildDir + from("$srcBuildDir/dist") + into("$buildDocsDir/example-frontend-js") + + dependsOn(":example-frontend-js:browserDistribution") +} + +tasks.register("site") { + description = "Generate github pages" + + inputs.files(fileTree(buildDocsDir)) + outputs.dir("$buildDir/dist") + workingDir = file(buildDocsDir) + + commandLine( + "docker", "run", "--rm", "--volume=$buildDir:/srv/jekyll", + "-t", jekyllDockerImage, + "/bin/bash", "-c", "cd docs; jekyll build" + ) + + dependsOn(copyDocs) + dependsOn(copyExampleFrontendJs) +} + +// A useful task for local debugging -- serves a site locally +tasks.register("serve") { + commandLine( + "docker", "run", "--rm", "--volume=$buildDir:/srv/jekyll", + "-p", "8080:8080", + "-t", jekyllDockerImage, + "/bin/bash", "-c", "cd docs; jekyll serve --host 0.0.0.0 --port 8080" + ) + + dependsOn(copyDocs) + dependsOn(copyExampleFrontendJs) +} + +tasks.register("clean") { + delete(buildDir) +} + diff --git a/js/js-stub/build.gradle b/stdlib-stubs/build.gradle.kts similarity index 65% rename from js/js-stub/build.gradle rename to stdlib-stubs/build.gradle.kts index b2ca0398d9..6b9d65555a 100644 --- a/js/js-stub/build.gradle +++ b/stdlib-stubs/build.gradle.kts @@ -2,7 +2,9 @@ * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ -compileKotlin { +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile + +tasks.named("compileKotlin") { kotlinOptions { freeCompilerArgs += "-Xallow-kotlin-package" } diff --git a/stdlib-stubs/src/ContinuationInterceptor.kt b/stdlib-stubs/src/ContinuationInterceptor.kt index 47935e3b03..ebf0a33532 100644 --- a/stdlib-stubs/src/ContinuationInterceptor.kt +++ b/stdlib-stubs/src/ContinuationInterceptor.kt @@ -5,7 +5,7 @@ package kotlin.coroutines // DOKKA STUB public interface ContinuationInterceptor : CoroutineContext.Element { - companion object Key : CoroutineContext.Key + public companion object Key : CoroutineContext.Key public fun interceptContinuation(continuation: Continuation): Continuation public fun releaseInterceptedContinuation(continuation: Continuation<*>): Continuation<*> { return continuation diff --git a/stdlib-stubs/src/Result.kt b/stdlib-stubs/src/Result.kt index 6dc8d9c8f7..611074a795 100644 --- a/stdlib-stubs/src/Result.kt +++ b/stdlib-stubs/src/Result.kt @@ -4,7 +4,7 @@ package kotlin -interface Result { +public interface Result { public val value: T public val isSuccess: Boolean public val isFailure: Boolean diff --git a/ui/coroutines-guide-ui.md b/ui/coroutines-guide-ui.md index 20607be21a..297b1fbc98 100644 --- a/ui/coroutines-guide-ui.md +++ b/ui/coroutines-guide-ui.md @@ -70,7 +70,7 @@ fun setup(hello: Text, fab: Circle) { } ``` -> You can get full code [here](kotlinx-coroutines-javafx/test/guide/example-ui-basic-01.kt). +> You can get the full code [here](kotlinx-coroutines-javafx/test/guide/example-ui-basic-01.kt). You can clone [kotlinx.coroutines](https://github.com/Kotlin/kotlinx.coroutines) project from GitHub onto your workstation and open the project in IDE. All the examples from this guide are in the test folder of @@ -110,7 +110,7 @@ Add dependencies on `kotlinx-coroutines-android` module to the `dependencies { . `app/build.gradle` file: ```groovy -implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.5" +implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.0" ``` You can clone [kotlinx.coroutines](https://github.com/Kotlin/kotlinx.coroutines) project from GitHub onto your @@ -152,7 +152,7 @@ fun setup(hello: Text, fab: Circle) { } ``` -> You can get full code [here](kotlinx-coroutines-javafx/test/guide/example-ui-basic-02.kt). +> You can get the full code [here](kotlinx-coroutines-javafx/test/guide/example-ui-basic-02.kt). So, what happens here? Because we are launching coroutine in the main UI context, we can freely update UI from inside this coroutine and invoke _suspending functions_ like [delay] at the same time. UI is not frozen @@ -179,7 +179,7 @@ fun setup(hello: Text, fab: Circle) { } ``` -> You can get full code [here](kotlinx-coroutines-javafx/test/guide/example-ui-basic-03.kt). +> You can get the full code [here](kotlinx-coroutines-javafx/test/guide/example-ui-basic-03.kt). Now, if the circle is clicked while countdown is still running, the countdown stops. Note that [Job.cancel] is completely thread-safe and non-blocking. It just signals the coroutine to cancel @@ -231,7 +231,7 @@ fun Node.onClick(action: suspend (MouseEvent) -> Unit) { } ``` -> You can get full code [here](kotlinx-coroutines-javafx/test/guide/example-ui-actor-01.kt). +> You can get the full code [here](kotlinx-coroutines-javafx/test/guide/example-ui-actor-01.kt). Note that each time the circle is clicked, it starts a new coroutine and they all compete to update the text. Try it. It does not look very good. We'll fix it later. @@ -273,7 +273,7 @@ fun Node.onClick(action: suspend (MouseEvent) -> Unit) { } ``` -> You can get full code [here](kotlinx-coroutines-javafx/test/guide/example-ui-actor-02.kt). +> You can get the full code [here](kotlinx-coroutines-javafx/test/guide/example-ui-actor-02.kt). The key idea that underlies an integration of an actor coroutine and a regular event handler is that there is an [offer][SendChannel.offer] function on [SendChannel] that does not wait. It sends an element to the actor immediately, @@ -310,7 +310,7 @@ processing the previous one. The [actor] coroutine builder accepts an optional controls the implementation of the channel that this actor is using for its mailbox. The description of all the available choices is given in documentation of the [`Channel()`][Channel] factory function. -Let us change the code to use `ConflatedChannel` by passing [Channel.CONFLATED] capacity value. The +Let us change the code to use a conflated channel by passing [Channel.CONFLATED] capacity value. The change is only to the line that creates an actor: ```kotlin @@ -408,47 +408,40 @@ during UI freeze. ### Structured concurrency, lifecycle and coroutine parent-child hierarchy A typical UI application has a number of elements with a lifecycle. Windows, UI controls, activities, views, fragments -and other visual elements are created and destroyed. A long-running coroutine, performing some IO or a background -computation, can retain references to the corresponding UI elements for longer than it is needed, preventing garbage +and other visual elements are created and destroyed. A long-running coroutine, performing some IO or a background +computation, can retain references to the corresponding UI elements for longer than it is needed, preventing garbage collection of the whole trees of UI objects that were already destroyed and will not be displayed anymore. -The natural solution to this problem is to associate a [Job] object with each UI object that has a lifecycle and create -all the coroutines in the context of this job. But passing associated job object to every coroutine builder is error-prone, -it is easy to forget it. For this purpose, [CoroutineScope] interface could be implemented by UI owner, and then every -coroutine builder defined as an extension on [CoroutineScope] inherits UI job without explicitly mentioning it. -For the sake of simplicity, [MainScope()] factory can be used. It automatically provides `Dispatchers.Main` and parent -job. +The natural solution to this problem is to associate a [CoroutineScope] object with each UI object that has a +lifecycle and create all the coroutines in the context of this scope. +For the sake of simplicity, [MainScope()] factory can be used. It automatically provides `Dispatchers.Main` and +a parent job for all the children coroutines. + +For example, in Android application an `Activity` is initially _created_ and is _destroyed_ when it is no longer +needed and when its memory must be released. A natural solution is to attach an +instance of a `CoroutineScope` to an instance of an `Activity`: -For example, in Android application an `Activity` is initially _created_ and is _destroyed_ when it is no longer -needed and when its memory must be released. A natural solution is to attach an -instance of a `Job` to an instance of an `Activity`: ```kotlin -abstract class ScopedAppActivity: AppCompatActivity(), CoroutineScope by MainScope() { +class MainActivity : AppCompatActivity() { + private val scope = MainScope() + override fun onDestroy() { super.onDestroy() - cancel() // CoroutineScope.cancel + scope.cancel() } -} -``` - -Now, an activity that is associated with a job has to extend ScopedAppActivity - -```kotlin -class MainActivity : ScopedAppActivity() { - fun asyncShowData() = launch { // Is invoked in UI context with Activity's job as a parent + fun asyncShowData() = scope.launch { // Is invoked in UI context with Activity's scope as a parent // actual implementation } suspend fun showIOData() { - val deferred = async(Dispatchers.IO) { - // impl + val data = withContext(Dispatchers.IO) { + // compute data in background thread } withContext(Dispatchers.Main) { - val data = deferred.await() - // Show data in UI + // Show data in UI } } } @@ -457,43 +450,14 @@ class MainActivity : ScopedAppActivity() { Every coroutine launched from within a `MainActivity` has its job as a parent and is immediately cancelled when activity is destroyed. -To propagate activity scope to its views and presenters, multiple techniques can be used: -- [coroutineScope] builder to provide a nested scope -- Receive [CoroutineScope] in presenter method parameters -- Make method extension on [CoroutineScope] (applicable only for top-level methods) - -```kotlin -class ActivityWithPresenters: ScopedAppActivity() { - fun init() { - val presenter = Presenter() - val presenter2 = ScopedPresenter(this) - } -} - -class Presenter { - suspend fun loadData() = coroutineScope { - // Nested scope of outer activity - } - - suspend fun loadData(uiScope: CoroutineScope) = uiScope.launch { - // Invoked in the uiScope - } -} - -class ScopedPresenter(scope: CoroutineScope): CoroutineScope by scope { - fun loadData() = launch { // Extension on ActivityWithPresenters's scope - } -} - -suspend fun CoroutineScope.launchInIO() = launch(Dispatchers.IO) { - // Launched in the scope of the caller, but with IO dispatcher -} -``` +> Note, that Android has first-party support for coroutine scope in all entities with the lifecycle. +See [the corresponding documentation](https://developer.android.com/topic/libraries/architecture/coroutines#lifecyclescope). Parent-child relation between jobs forms a hierarchy. A coroutine that performs some background job on behalf of -the view and in its context can create further children coroutines. The whole tree of coroutines gets cancelled +the activity can create further children coroutines. The whole tree of coroutines gets cancelled when the parent job is cancelled. An example of that is shown in the ["Children of a coroutine"](../docs/coroutine-context-and-dispatchers.md#children-of-a-coroutine) section of the guide to coroutines. + ### Blocking operations @@ -532,7 +496,7 @@ suspend fun fib(x: Int): Int = withContext(Dispatchers.Default) { } ``` -> You can get full code [here](kotlinx-coroutines-javafx/test/guide/example-ui-blocking-02.kt). +> You can get the full code [here](kotlinx-coroutines-javafx/test/guide/example-ui-blocking-02.kt). You can run this code and verify that UI is not frozen while large Fibonacci numbers are being computed. However, this code computes `fib` somewhat slower, because every recursive call to `fib` goes via `withContext`. This is @@ -554,7 +518,7 @@ fun fibBlocking(x: Int): Int = if (x <= 1) x else fibBlocking(x - 1) + fibBlocking(x - 2) ``` -> You can get full code [here](kotlinx-coroutines-javafx/test/guide/example-ui-blocking-03.kt). +> You can get the full code [here](kotlinx-coroutines-javafx/test/guide/example-ui-blocking-03.kt). You can now enjoy full-speed naive Fibonacci computation without blocking the main UI thread. All we need is `withContext(Dispatchers.Default)`. @@ -649,7 +613,6 @@ After delay [Job.cancel]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/cancel.html [CoroutineScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html [MainScope()]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-main-scope.html -[coroutineScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/coroutine-scope.html [withContext]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/with-context.html [Dispatchers.Default]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-default.html [CoroutineStart]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-start/index.html diff --git a/ui/kotlinx-coroutines-android/android-unit-tests/build.gradle b/ui/kotlinx-coroutines-android/android-unit-tests/build.gradle deleted file mode 100644 index c927640cd7..0000000000 --- a/ui/kotlinx-coroutines-android/android-unit-tests/build.gradle +++ /dev/null @@ -1,11 +0,0 @@ -/* - * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -dependencies { - testImplementation "com.google.android:android:$android_version" - testImplementation "com.android.support:support-annotations:$android_support_version" - testImplementation "org.robolectric:robolectric:$robolectric_version" - testImplementation project(":kotlinx-coroutines-test") - testImplementation project(":kotlinx-coroutines-android") -} diff --git a/ui/kotlinx-coroutines-android/android-unit-tests/build.gradle.kts b/ui/kotlinx-coroutines-android/android-unit-tests/build.gradle.kts new file mode 100644 index 0000000000..2acc058d73 --- /dev/null +++ b/ui/kotlinx-coroutines-android/android-unit-tests/build.gradle.kts @@ -0,0 +1,10 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +dependencies { + testImplementation("com.google.android:android:${version("android")}") + testImplementation("org.robolectric:robolectric:${version("robolectric")}") + testImplementation(project(":kotlinx-coroutines-test")) + testImplementation(project(":kotlinx-coroutines-android")) +} diff --git a/ui/kotlinx-coroutines-android/android-unit-tests/test/ordered/tests/TestComponent.kt b/ui/kotlinx-coroutines-android/android-unit-tests/test/ordered/tests/TestComponent.kt index 9cf813bc3a..c677d9911a 100644 --- a/ui/kotlinx-coroutines-android/android-unit-tests/test/ordered/tests/TestComponent.kt +++ b/ui/kotlinx-coroutines-android/android-unit-tests/test/ordered/tests/TestComponent.kt @@ -21,7 +21,7 @@ public class TestComponent { fun launchDelayed() { scope.launch { - delay(Long.MAX_VALUE) + delay(Long.MAX_VALUE / 2) delayedLaunchCompleted = true } } diff --git a/ui/kotlinx-coroutines-android/animation-app/app/build.gradle b/ui/kotlinx-coroutines-android/animation-app/app/build.gradle deleted file mode 100644 index 44f4a5f589..0000000000 --- a/ui/kotlinx-coroutines-android/animation-app/app/build.gradle +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -apply plugin: 'com.android.application' -apply plugin: 'kotlin-android' -apply plugin: 'kotlin-android-extensions' - -android { - compileSdkVersion 29 - defaultConfig { - applicationId "org.jetbrains.kotlinx.animation" - minSdkVersion 14 - targetSdkVersion 29 - versionCode 1 - versionName "1.0" - testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" - } -} - -dependencies { - implementation 'androidx.appcompat:appcompat:1.0.2' - implementation 'androidx.constraintlayout:constraintlayout:1.1.3' - implementation 'com.google.android.material:material:1.0.0' - implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0' - - implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" - implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version" - - testImplementation 'junit:junit:4.12' - androidTestImplementation 'androidx.test:runner:1.2.0' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' -} diff --git a/ui/kotlinx-coroutines-android/animation-app/app/build.gradle.kts b/ui/kotlinx-coroutines-android/animation-app/app/build.gradle.kts new file mode 100644 index 0000000000..517f1f6341 --- /dev/null +++ b/ui/kotlinx-coroutines-android/animation-app/app/build.gradle.kts @@ -0,0 +1,35 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +plugins { + id("com.android.application") + kotlin("android") + kotlin("android.extensions") +} + +android { + compileSdkVersion = "29" + defaultConfig { + applicationId = "org.jetbrains.kotlinx.animation" + minSdkVersion(14) + targetSdkVersion(29) + versionCode = 1 + versionName = "1.0" + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + } +} + +dependencies { + implementation("androidx.appcompat:appcompat:1.0.2") + implementation("androidx.constraintlayout:constraintlayout:1.1.3") + implementation("com.google.android.material:material:1.0.0") + implementation("androidx.lifecycle:lifecycle-extensions:2.0.0") + + implementation(kotlin("stdlib-jdk7")) + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:${property("coroutines_version")}") + + testImplementation("junit:junit:4.12") + androidTestImplementation("androidx.test:runner:1.2.0") + androidTestImplementation("androidx.test.espresso:espresso-core:3.2.0") +} diff --git a/ui/kotlinx-coroutines-android/animation-app/build.gradle b/ui/kotlinx-coroutines-android/animation-app/build.gradle.kts similarity index 72% rename from ui/kotlinx-coroutines-android/animation-app/build.gradle rename to ui/kotlinx-coroutines-android/animation-app/build.gradle.kts index d98ab8cfe7..9cd0c592df 100644 --- a/ui/kotlinx-coroutines-android/animation-app/build.gradle +++ b/ui/kotlinx-coroutines-android/animation-app/build.gradle.kts @@ -10,8 +10,8 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.5.0' - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + classpath("com.android.tools.build:gradle:3.5.0") + classpath(kotlin("gradle-plugin", property("kotlin_version") as String)) // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files @@ -25,6 +25,6 @@ allprojects { } } -task clean(type: Delete) { - delete rootProject.buildDir +task("clean") { + delete(rootProject.buildDir) } diff --git a/ui/kotlinx-coroutines-android/animation-app/gradle.properties b/ui/kotlinx-coroutines-android/animation-app/gradle.properties index e9d79d0e50..5ee7794f17 100644 --- a/ui/kotlinx-coroutines-android/animation-app/gradle.properties +++ b/ui/kotlinx-coroutines-android/animation-app/gradle.properties @@ -20,8 +20,8 @@ org.gradle.jvmargs=-Xmx1536m # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true -kotlin_version=1.3.70 -coroutines_version=1.3.5 +kotlin_version=1.4.0 +coroutines_version=1.4.0 android.useAndroidX=true android.enableJetifier=true diff --git a/ui/kotlinx-coroutines-android/animation-app/gradle/wrapper/gradle-wrapper.jar b/ui/kotlinx-coroutines-android/animation-app/gradle/wrapper/gradle-wrapper.jar index e69de29bb2..490fda8577 100644 Binary files a/ui/kotlinx-coroutines-android/animation-app/gradle/wrapper/gradle-wrapper.jar and b/ui/kotlinx-coroutines-android/animation-app/gradle/wrapper/gradle-wrapper.jar differ diff --git a/ui/kotlinx-coroutines-android/animation-app/gradle/wrapper/gradle-wrapper.properties b/ui/kotlinx-coroutines-android/animation-app/gradle/wrapper/gradle-wrapper.properties index dfd98a959a..dbe85eef28 100644 --- a/ui/kotlinx-coroutines-android/animation-app/gradle/wrapper/gradle-wrapper.properties +++ b/ui/kotlinx-coroutines-android/animation-app/gradle/wrapper/gradle-wrapper.properties @@ -1,9 +1,5 @@ -# -# Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. -# - distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.1-all.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip diff --git a/ui/kotlinx-coroutines-android/animation-app/gradlew b/ui/kotlinx-coroutines-android/animation-app/gradlew old mode 100644 new mode 100755 index cccdd3d517..2fe81a7d95 --- a/ui/kotlinx-coroutines-android/animation-app/gradlew +++ b/ui/kotlinx-coroutines-android/animation-app/gradlew @@ -1,5 +1,21 @@ #!/usr/bin/env sh +# +# Copyright 2015 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + ############################################################################## ## ## Gradle start up script for UN*X @@ -28,7 +44,7 @@ APP_NAME="Gradle" APP_BASE_NAME=`basename "$0"` # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS="" +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD="maximum" @@ -109,8 +125,8 @@ if $darwin; then GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" fi -# For Cygwin, switch paths to Windows format before running java -if $cygwin ; then +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then APP_HOME=`cygpath --path --mixed "$APP_HOME"` CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` JAVACMD=`cygpath --unix "$JAVACMD"` @@ -138,19 +154,19 @@ if $cygwin ; then else eval `echo args$i`="\"$arg\"" fi - i=$((i+1)) + i=`expr $i + 1` done case $i in - (0) set -- ;; - (1) set -- "$args0" ;; - (2) set -- "$args0" "$args1" ;; - (3) set -- "$args0" "$args1" "$args2" ;; - (4) set -- "$args0" "$args1" "$args2" "$args3" ;; - (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; - (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; - (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; - (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; - (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + 0) set -- ;; + 1) set -- "$args0" ;; + 2) set -- "$args0" "$args1" ;; + 3) set -- "$args0" "$args1" "$args2" ;; + 4) set -- "$args0" "$args1" "$args2" "$args3" ;; + 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; esac fi @@ -159,14 +175,9 @@ save () { for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done echo " " } -APP_ARGS=$(save "$@") +APP_ARGS=`save "$@"` # Collect all arguments for the java command, following the shell quoting and substitution rules eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" -# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong -if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then - cd "$(dirname "$0")" -fi - exec "$JAVACMD" "$@" diff --git a/ui/kotlinx-coroutines-android/animation-app/gradlew.bat b/ui/kotlinx-coroutines-android/animation-app/gradlew.bat index f9553162f1..62bd9b9cce 100644 --- a/ui/kotlinx-coroutines-android/animation-app/gradlew.bat +++ b/ui/kotlinx-coroutines-android/animation-app/gradlew.bat @@ -1,3 +1,19 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + @if "%DEBUG%" == "" @echo off @rem ########################################################################## @rem @@ -13,8 +29,11 @@ if "%DIRNAME%" == "" set DIRNAME=. set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS= +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" @rem Find java.exe if defined JAVA_HOME goto findJavaFromJavaHome diff --git a/ui/kotlinx-coroutines-android/animation-app/settings.gradle b/ui/kotlinx-coroutines-android/animation-app/settings.gradle.kts similarity index 87% rename from ui/kotlinx-coroutines-android/animation-app/settings.gradle rename to ui/kotlinx-coroutines-android/animation-app/settings.gradle.kts index 0087705ce7..b05d810bc0 100644 --- a/ui/kotlinx-coroutines-android/animation-app/settings.gradle +++ b/ui/kotlinx-coroutines-android/animation-app/settings.gradle.kts @@ -2,4 +2,4 @@ * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ -include ':app' +include(":app") diff --git a/ui/kotlinx-coroutines-android/api/kotlinx-coroutines-android.api b/ui/kotlinx-coroutines-android/api/kotlinx-coroutines-android.api index b97d8462c3..090c14e09c 100644 --- a/ui/kotlinx-coroutines-android/api/kotlinx-coroutines-android.api +++ b/ui/kotlinx-coroutines-android/api/kotlinx-coroutines-android.api @@ -1,7 +1,7 @@ public abstract class kotlinx/coroutines/android/HandlerDispatcher : kotlinx/coroutines/MainCoroutineDispatcher, kotlinx/coroutines/Delay { public fun delay (JLkotlin/coroutines/Continuation;)Ljava/lang/Object; public abstract fun getImmediate ()Lkotlinx/coroutines/android/HandlerDispatcher; - public fun invokeOnTimeout (JLjava/lang/Runnable;)Lkotlinx/coroutines/DisposableHandle; + public fun invokeOnTimeout (JLjava/lang/Runnable;Lkotlin/coroutines/CoroutineContext;)Lkotlinx/coroutines/DisposableHandle; } public final class kotlinx/coroutines/android/HandlerDispatcherKt { diff --git a/ui/kotlinx-coroutines-android/build.gradle b/ui/kotlinx-coroutines-android/build.gradle deleted file mode 100644 index 51702cdbe3..0000000000 --- a/ui/kotlinx-coroutines-android/build.gradle +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -repositories { - google() -} - -configurations { - r8 -} - -dependencies { - compileOnly "com.google.android:android:$android_version" - compileOnly "com.android.support:support-annotations:$android_support_version" - - testImplementation "com.google.android:android:$android_version" - testImplementation "org.robolectric:robolectric:$robolectric_version" - testImplementation "org.smali:baksmali:$baksmali_version" - - r8 'com.android.tools.build:builder:4.0.0-alpha06' // Contains r8-2.0.4-dev -} - -class RunR8Task extends JavaExec { - - @OutputDirectory - File outputDex - - @InputFile - File inputConfig - - @InputFile - final File inputConfigCommon = new File('testdata/r8-test-common.pro') - - @InputFiles - final File jarFile = project.jar.archivePath - - @Override - Task configure(Closure closure) { - super.configure(closure) - classpath = project.configurations.r8 - main = 'com.android.tools.r8.R8' - return this - } - - @Override - void exec() { - // Resolve classpath only during execution - def arguments = [ - '--release', - '--no-desugaring', - '--output', outputDex.absolutePath, - '--pg-conf', inputConfig.absolutePath - ] - arguments.addAll(project.configurations.runtimeClasspath.files.collect { it.absolutePath }) - arguments.addAll(jarFile.absolutePath) - - args = arguments - - if (outputDex.exists()) { - outputDex.deleteDir() - } - outputDex.mkdirs() - - super.exec() - } -} - -def optimizedDexDir = new File(buildDir, "dex-optim/") -def unOptimizedDexDir = new File(buildDir, "dex-unoptim/") - -def optimizedDexFile = new File(optimizedDexDir, "classes.dex") -def unOptimizedDexFile = new File(unOptimizedDexDir, "classes.dex") - -task runR8(type: RunR8Task, dependsOn: 'jar'){ - outputDex = optimizedDexDir - inputConfig = file('testdata/r8-test-rules.pro') -} - -task runR8NoOptim(type: RunR8Task, dependsOn: 'jar') { - outputDex = unOptimizedDexDir - inputConfig = file('testdata/r8-test-rules-no-optim.pro') -} - -test { - // Ensure the R8-processed dex is built and supply its path as a property to the test. - dependsOn(runR8) - dependsOn(runR8NoOptim) - - inputs.files(optimizedDexFile, unOptimizedDexFile) - - systemProperty 'dexPath', optimizedDexFile.absolutePath - systemProperty 'noOptimDexPath', unOptimizedDexFile.absolutePath - - // Output custom metric with the size of the optimized dex - doLast { - println("##teamcity[buildStatisticValue key='optimizedDexSize' value='${optimizedDexFile.length()}']") - } -} - -tasks.withType(dokka.getClass()) { - externalDocumentationLink { - url = new URL("https://developer.android.com/reference/") - packageListUrl = projectDir.toPath().resolve("package.list").toUri().toURL() - } -} diff --git a/ui/kotlinx-coroutines-android/build.gradle.kts b/ui/kotlinx-coroutines-android/build.gradle.kts new file mode 100644 index 0000000000..4f24788359 --- /dev/null +++ b/ui/kotlinx-coroutines-android/build.gradle.kts @@ -0,0 +1,62 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +import org.jetbrains.dokka.DokkaConfiguration.ExternalDocumentationLink +import org.jetbrains.dokka.gradle.DokkaTask +import java.net.URL + +configurations { + create("r8") +} + +dependencies { + compileOnly("com.google.android:android:${version("android")}") + compileOnly("androidx.annotation:annotation:${version("androidx_annotation")}") + + testImplementation("com.google.android:android:${version("android")}") + testImplementation("org.robolectric:robolectric:${version("robolectric")}") + testImplementation("org.smali:baksmali:${version("baksmali")}") + + "r8"("com.android.tools.build:builder:4.0.0-alpha06") // Contains r8-2.0.4-dev +} + +val optimizedDexDir = File(buildDir, "dex-optim/") +val unOptimizedDexDir = File(buildDir, "dex-unoptim/") + +val optimizedDexFile = File(optimizedDexDir, "classes.dex") +val unOptimizedDexFile = File(unOptimizedDexDir, "classes.dex") + +val runR8 by tasks.registering(RunR8::class) { + outputDex = optimizedDexDir + inputConfig = file("testdata/r8-test-rules.pro") + + dependsOn("jar") +} + +val runR8NoOptim by tasks.registering(RunR8::class) { + outputDex = unOptimizedDexDir + inputConfig = file("testdata/r8-test-rules-no-optim.pro") + + dependsOn("jar") +} + +tasks.test { + // Ensure the R8-processed dex is built and supply its path as a property to the test. + dependsOn(runR8) + dependsOn(runR8NoOptim) + + inputs.files(optimizedDexFile, unOptimizedDexFile) + + systemProperty("dexPath", optimizedDexFile.absolutePath) + systemProperty("noOptimDexPath", unOptimizedDexFile.absolutePath) + + // Output custom metric with the size of the optimized dex + doLast { + println("##teamcity[buildStatisticValue key='optimizedDexSize' value='${optimizedDexFile.length()}']") + } +} + +externalDocumentationLink( + url = "https://developer.android.com/reference/" +) diff --git a/ui/kotlinx-coroutines-android/example-app/app/build.gradle b/ui/kotlinx-coroutines-android/example-app/app/build.gradle deleted file mode 100644 index 3f013247d7..0000000000 --- a/ui/kotlinx-coroutines-android/example-app/app/build.gradle +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -apply plugin: 'com.android.application' -apply plugin: 'kotlin-android' -apply plugin: 'kotlin-android-extensions' - -android { - compileSdkVersion 29 - defaultConfig { - applicationId "com.example.app" - minSdkVersion 14 - targetSdkVersion 29 - versionCode 1 - versionName "1.0" - testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" - } -} - -dependencies { - implementation 'androidx.appcompat:appcompat:1.0.2' - implementation 'androidx.constraintlayout:constraintlayout:1.1.3' - implementation 'com.google.android.material:material:1.0.0' - - implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" - implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version" - - testImplementation 'junit:junit:4.12' - androidTestImplementation 'androidx.test:runner:1.2.0' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' -} diff --git a/ui/kotlinx-coroutines-android/example-app/app/build.gradle.kts b/ui/kotlinx-coroutines-android/example-app/app/build.gradle.kts new file mode 100644 index 0000000000..39bba5bf30 --- /dev/null +++ b/ui/kotlinx-coroutines-android/example-app/app/build.gradle.kts @@ -0,0 +1,34 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +plugins { + id("com.android.application") + kotlin("android") + kotlin("android.extensions") +} + +android { + compileSdkVersion = "29" + defaultConfig { + applicationId = "com.example.app" + minSdkVersion(14) + targetSdkVersion(29) + versionCode = 1 + versionName = "1.0" + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + } +} + +dependencies { + implementation("androidx.appcompat:appcompat:1.0.2") + implementation("androidx.constraintlayout:constraintlayout:1.1.3") + implementation("com.google.android.material:material:1.0.0") + + implementation(kotlin("stdlib-jdk7")) + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:${property("coroutines_version")}") + + testImplementation("junit:junit:4.12") + androidTestImplementation("androidx.test:runner:1.2.0") + androidTestImplementation("androidx.test.espresso:espresso-core:3.2.0") +} diff --git a/ui/kotlinx-coroutines-android/example-app/build.gradle b/ui/kotlinx-coroutines-android/example-app/build.gradle.kts similarity index 72% rename from ui/kotlinx-coroutines-android/example-app/build.gradle rename to ui/kotlinx-coroutines-android/example-app/build.gradle.kts index d98ab8cfe7..9cd0c592df 100644 --- a/ui/kotlinx-coroutines-android/example-app/build.gradle +++ b/ui/kotlinx-coroutines-android/example-app/build.gradle.kts @@ -10,8 +10,8 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.5.0' - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + classpath("com.android.tools.build:gradle:3.5.0") + classpath(kotlin("gradle-plugin", property("kotlin_version") as String)) // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files @@ -25,6 +25,6 @@ allprojects { } } -task clean(type: Delete) { - delete rootProject.buildDir +task("clean") { + delete(rootProject.buildDir) } diff --git a/ui/kotlinx-coroutines-android/example-app/gradle.properties b/ui/kotlinx-coroutines-android/example-app/gradle.properties index e9d79d0e50..5ee7794f17 100644 --- a/ui/kotlinx-coroutines-android/example-app/gradle.properties +++ b/ui/kotlinx-coroutines-android/example-app/gradle.properties @@ -20,8 +20,8 @@ org.gradle.jvmargs=-Xmx1536m # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true -kotlin_version=1.3.70 -coroutines_version=1.3.5 +kotlin_version=1.4.0 +coroutines_version=1.4.0 android.useAndroidX=true android.enableJetifier=true diff --git a/ui/kotlinx-coroutines-android/example-app/gradle/wrapper/gradle-wrapper.jar b/ui/kotlinx-coroutines-android/example-app/gradle/wrapper/gradle-wrapper.jar index e69de29bb2..490fda8577 100644 Binary files a/ui/kotlinx-coroutines-android/example-app/gradle/wrapper/gradle-wrapper.jar and b/ui/kotlinx-coroutines-android/example-app/gradle/wrapper/gradle-wrapper.jar differ diff --git a/ui/kotlinx-coroutines-android/example-app/gradle/wrapper/gradle-wrapper.properties b/ui/kotlinx-coroutines-android/example-app/gradle/wrapper/gradle-wrapper.properties index dfd98a959a..dbe85eef28 100644 --- a/ui/kotlinx-coroutines-android/example-app/gradle/wrapper/gradle-wrapper.properties +++ b/ui/kotlinx-coroutines-android/example-app/gradle/wrapper/gradle-wrapper.properties @@ -1,9 +1,5 @@ -# -# Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. -# - distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.1-all.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip diff --git a/ui/kotlinx-coroutines-android/example-app/gradlew b/ui/kotlinx-coroutines-android/example-app/gradlew old mode 100644 new mode 100755 index 9d82f78915..2fe81a7d95 --- a/ui/kotlinx-coroutines-android/example-app/gradlew +++ b/ui/kotlinx-coroutines-android/example-app/gradlew @@ -1,4 +1,20 @@ -#!/usr/bin/env bash +#!/usr/bin/env sh + +# +# Copyright 2015 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ############################################################################## ## @@ -6,20 +22,38 @@ ## ############################################################################## -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS="" +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null APP_NAME="Gradle" APP_BASE_NAME=`basename "$0"` +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD="maximum" -warn ( ) { +warn () { echo "$*" } -die ( ) { +die () { echo echo "$*" echo @@ -30,6 +64,7 @@ die ( ) { cygwin=false msys=false darwin=false +nonstop=false case "`uname`" in CYGWIN* ) cygwin=true @@ -40,26 +75,11 @@ case "`uname`" in MINGW* ) msys=true ;; + NONSTOP* ) + nonstop=true + ;; esac -# Attempt to set APP_HOME -# Resolve links: $0 may be a link -PRG="$0" -# Need this for relative symlinks. -while [ -h "$PRG" ] ; do - ls=`ls -ld "$PRG"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - PRG="$link" - else - PRG=`dirname "$PRG"`"/$link" - fi -done -SAVED="`pwd`" -cd "`dirname \"$PRG\"`/" >/dev/null -APP_HOME="`pwd -P`" -cd "$SAVED" >/dev/null - CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar # Determine the Java command to use to start the JVM. @@ -85,7 +105,7 @@ location of your Java installation." fi # Increase the maximum file descriptors if we can. -if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then MAX_FD_LIMIT=`ulimit -H -n` if [ $? -eq 0 ] ; then if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then @@ -105,8 +125,8 @@ if $darwin; then GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" fi -# For Cygwin, switch paths to Windows format before running java -if $cygwin ; then +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then APP_HOME=`cygpath --path --mixed "$APP_HOME"` CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` JAVACMD=`cygpath --unix "$JAVACMD"` @@ -134,27 +154,30 @@ if $cygwin ; then else eval `echo args$i`="\"$arg\"" fi - i=$((i+1)) + i=`expr $i + 1` done case $i in - (0) set -- ;; - (1) set -- "$args0" ;; - (2) set -- "$args0" "$args1" ;; - (3) set -- "$args0" "$args1" "$args2" ;; - (4) set -- "$args0" "$args1" "$args2" "$args3" ;; - (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; - (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; - (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; - (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; - (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + 0) set -- ;; + 1) set -- "$args0" ;; + 2) set -- "$args0" "$args1" ;; + 3) set -- "$args0" "$args1" "$args2" ;; + 4) set -- "$args0" "$args1" "$args2" "$args3" ;; + 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; esac fi -# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules -function splitJvmOpts() { - JVM_OPTS=("$@") +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " } -eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS -JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" +APP_ARGS=`save "$@"` + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" -exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" +exec "$JAVACMD" "$@" diff --git a/ui/kotlinx-coroutines-android/example-app/gradlew.bat b/ui/kotlinx-coroutines-android/example-app/gradlew.bat index 8a0b282aa6..62bd9b9cce 100644 --- a/ui/kotlinx-coroutines-android/example-app/gradlew.bat +++ b/ui/kotlinx-coroutines-android/example-app/gradlew.bat @@ -1,3 +1,19 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + @if "%DEBUG%" == "" @echo off @rem ########################################################################## @rem @@ -8,14 +24,17 @@ @rem Set local scope for the variables with windows NT shell if "%OS%"=="Windows_NT" setlocal -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS= - set DIRNAME=%~dp0 if "%DIRNAME%" == "" set DIRNAME=. set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + @rem Find java.exe if defined JAVA_HOME goto findJavaFromJavaHome @@ -46,10 +65,9 @@ echo location of your Java installation. goto fail :init -@rem Get command-line arguments, handling Windowz variants +@rem Get command-line arguments, handling Windows variants if not "%OS%" == "Windows_NT" goto win9xME_args -if "%@eval[2+2]" == "4" goto 4NT_args :win9xME_args @rem Slurp the command line arguments. @@ -60,11 +78,6 @@ set _SKIP=2 if "x%~1" == "x" goto execute set CMD_LINE_ARGS=%* -goto execute - -:4NT_args -@rem Get arguments from the 4NT Shell from JP Software -set CMD_LINE_ARGS=%$ :execute @rem Setup the command line diff --git a/ui/kotlinx-coroutines-android/example-app/settings.gradle b/ui/kotlinx-coroutines-android/example-app/settings.gradle deleted file mode 100644 index e7b4def49c..0000000000 --- a/ui/kotlinx-coroutines-android/example-app/settings.gradle +++ /dev/null @@ -1 +0,0 @@ -include ':app' diff --git a/ui/kotlinx-coroutines-android/example-app/settings.gradle.kts b/ui/kotlinx-coroutines-android/example-app/settings.gradle.kts new file mode 100644 index 0000000000..15a801b10a --- /dev/null +++ b/ui/kotlinx-coroutines-android/example-app/settings.gradle.kts @@ -0,0 +1 @@ +include(":app") diff --git a/ui/kotlinx-coroutines-android/gradle/wrapper/gradle-wrapper.jar b/ui/kotlinx-coroutines-android/gradle/wrapper/gradle-wrapper.jar deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/ui/kotlinx-coroutines-android/gradle/wrapper/gradle-wrapper.properties b/ui/kotlinx-coroutines-android/gradle/wrapper/gradle-wrapper.properties deleted file mode 100644 index d5aeee10af..0000000000 --- a/ui/kotlinx-coroutines-android/gradle/wrapper/gradle-wrapper.properties +++ /dev/null @@ -1,6 +0,0 @@ -#Sun Apr 08 03:07:42 CEST 2018 -distributionBase=GRADLE_USER_HOME -distributionPath=wrapper/dists -zipStoreBase=GRADLE_USER_HOME -zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip diff --git a/ui/kotlinx-coroutines-android/gradlew b/ui/kotlinx-coroutines-android/gradlew deleted file mode 100644 index cccdd3d517..0000000000 --- a/ui/kotlinx-coroutines-android/gradlew +++ /dev/null @@ -1,172 +0,0 @@ -#!/usr/bin/env sh - -############################################################################## -## -## Gradle start up script for UN*X -## -############################################################################## - -# Attempt to set APP_HOME -# Resolve links: $0 may be a link -PRG="$0" -# Need this for relative symlinks. -while [ -h "$PRG" ] ; do - ls=`ls -ld "$PRG"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - PRG="$link" - else - PRG=`dirname "$PRG"`"/$link" - fi -done -SAVED="`pwd`" -cd "`dirname \"$PRG\"`/" >/dev/null -APP_HOME="`pwd -P`" -cd "$SAVED" >/dev/null - -APP_NAME="Gradle" -APP_BASE_NAME=`basename "$0"` - -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS="" - -# Use the maximum available, or set MAX_FD != -1 to use that value. -MAX_FD="maximum" - -warn () { - echo "$*" -} - -die () { - echo - echo "$*" - echo - exit 1 -} - -# OS specific support (must be 'true' or 'false'). -cygwin=false -msys=false -darwin=false -nonstop=false -case "`uname`" in - CYGWIN* ) - cygwin=true - ;; - Darwin* ) - darwin=true - ;; - MINGW* ) - msys=true - ;; - NONSTOP* ) - nonstop=true - ;; -esac - -CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar - -# Determine the Java command to use to start the JVM. -if [ -n "$JAVA_HOME" ] ; then - if [ -x "$JAVA_HOME/jre/sh/java" ] ; then - # IBM's JDK on AIX uses strange locations for the executables - JAVACMD="$JAVA_HOME/jre/sh/java" - else - JAVACMD="$JAVA_HOME/bin/java" - fi - if [ ! -x "$JAVACMD" ] ; then - die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME - -Please set the JAVA_HOME variable in your environment to match the -location of your Java installation." - fi -else - JAVACMD="java" - which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. - -Please set the JAVA_HOME variable in your environment to match the -location of your Java installation." -fi - -# Increase the maximum file descriptors if we can. -if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then - MAX_FD_LIMIT=`ulimit -H -n` - if [ $? -eq 0 ] ; then - if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then - MAX_FD="$MAX_FD_LIMIT" - fi - ulimit -n $MAX_FD - if [ $? -ne 0 ] ; then - warn "Could not set maximum file descriptor limit: $MAX_FD" - fi - else - warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" - fi -fi - -# For Darwin, add options to specify how the application appears in the dock -if $darwin; then - GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" -fi - -# For Cygwin, switch paths to Windows format before running java -if $cygwin ; then - APP_HOME=`cygpath --path --mixed "$APP_HOME"` - CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` - JAVACMD=`cygpath --unix "$JAVACMD"` - - # We build the pattern for arguments to be converted via cygpath - ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` - SEP="" - for dir in $ROOTDIRSRAW ; do - ROOTDIRS="$ROOTDIRS$SEP$dir" - SEP="|" - done - OURCYGPATTERN="(^($ROOTDIRS))" - # Add a user-defined pattern to the cygpath arguments - if [ "$GRADLE_CYGPATTERN" != "" ] ; then - OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" - fi - # Now convert the arguments - kludge to limit ourselves to /bin/sh - i=0 - for arg in "$@" ; do - CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` - CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option - - if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition - eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` - else - eval `echo args$i`="\"$arg\"" - fi - i=$((i+1)) - done - case $i in - (0) set -- ;; - (1) set -- "$args0" ;; - (2) set -- "$args0" "$args1" ;; - (3) set -- "$args0" "$args1" "$args2" ;; - (4) set -- "$args0" "$args1" "$args2" "$args3" ;; - (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; - (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; - (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; - (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; - (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; - esac -fi - -# Escape application args -save () { - for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done - echo " " -} -APP_ARGS=$(save "$@") - -# Collect all arguments for the java command, following the shell quoting and substitution rules -eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" - -# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong -if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then - cd "$(dirname "$0")" -fi - -exec "$JAVACMD" "$@" diff --git a/ui/kotlinx-coroutines-android/gradlew.bat b/ui/kotlinx-coroutines-android/gradlew.bat deleted file mode 100644 index f9553162f1..0000000000 --- a/ui/kotlinx-coroutines-android/gradlew.bat +++ /dev/null @@ -1,84 +0,0 @@ -@if "%DEBUG%" == "" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS= - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto init - -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto init - -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:init -@rem Get command-line arguments, handling Windows variants - -if not "%OS%" == "Windows_NT" goto win9xME_args - -:win9xME_args -@rem Slurp the command line arguments. -set CMD_LINE_ARGS= -set _SKIP=2 - -:win9xME_args_slurp -if "x%~1" == "x" goto execute - -set CMD_LINE_ARGS=%* - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% - -:end -@rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega diff --git a/ui/kotlinx-coroutines-android/resources/META-INF/com.android.tools/r8-from-1.6.0/coroutines.pro b/ui/kotlinx-coroutines-android/resources/META-INF/com.android.tools/r8-from-1.6.0/coroutines.pro index fd25b215c3..0d04990ad9 100644 --- a/ui/kotlinx-coroutines-android/resources/META-INF/com.android.tools/r8-from-1.6.0/coroutines.pro +++ b/ui/kotlinx-coroutines-android/resources/META-INF/com.android.tools/r8-from-1.6.0/coroutines.pro @@ -9,6 +9,8 @@ boolean ANDROID_DETECTED return true; } +-keep class kotlinx.coroutines.android.AndroidDispatcherFactory {*;} + # Disable support for "Missing Main Dispatcher", since we always have Android main dispatcher -assumenosideeffects class kotlinx.coroutines.internal.MainDispatchersKt { boolean SUPPORT_MISSING return false; diff --git a/ui/kotlinx-coroutines-android/src/AndroidExceptionPreHandler.kt b/ui/kotlinx-coroutines-android/src/AndroidExceptionPreHandler.kt index 673c3813d8..b105e5b84c 100644 --- a/ui/kotlinx-coroutines-android/src/AndroidExceptionPreHandler.kt +++ b/ui/kotlinx-coroutines-android/src/AndroidExceptionPreHandler.kt @@ -5,7 +5,7 @@ package kotlinx.coroutines.android import android.os.* -import android.support.annotation.* +import androidx.annotation.* import kotlinx.coroutines.* import java.lang.reflect.* import kotlin.coroutines.* diff --git a/ui/kotlinx-coroutines-android/src/HandlerDispatcher.kt b/ui/kotlinx-coroutines-android/src/HandlerDispatcher.kt index d415b003bf..af79da7c97 100644 --- a/ui/kotlinx-coroutines-android/src/HandlerDispatcher.kt +++ b/ui/kotlinx-coroutines-android/src/HandlerDispatcher.kt @@ -7,7 +7,7 @@ package kotlinx.coroutines.android import android.os.* -import android.support.annotation.* +import androidx.annotation.* import android.view.* import kotlinx.coroutines.* import kotlinx.coroutines.internal.* @@ -52,7 +52,7 @@ public sealed class HandlerDispatcher : MainCoroutineDispatcher(), Delay { internal class AndroidDispatcherFactory : MainDispatcherFactory { override fun createDispatcher(allFactories: List) = - HandlerContext(Looper.getMainLooper().asHandler(async = true), "Main") + HandlerContext(Looper.getMainLooper().asHandler(async = true)) override fun hintOnError(): String? = "For tests Dispatchers.setMain from kotlinx-coroutines-test module can be used" @@ -97,7 +97,7 @@ internal fun Looper.asHandler(async: Boolean): Handler { @JvmField @Deprecated("Use Dispatchers.Main instead", level = DeprecationLevel.HIDDEN) -internal val Main: HandlerDispatcher? = runCatching { HandlerContext(Looper.getMainLooper().asHandler(async = true), "Main") }.getOrNull() +internal val Main: HandlerDispatcher? = runCatching { HandlerContext(Looper.getMainLooper().asHandler(async = true)) }.getOrNull() /** * Implements [CoroutineDispatcher] on top of an arbitrary Android [Handler]. @@ -140,7 +140,7 @@ internal class HandlerContext private constructor( continuation.invokeOnCancellation { handler.removeCallbacks(block) } } - override fun invokeOnTimeout(timeMillis: Long, block: Runnable): DisposableHandle { + override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle { handler.postDelayed(block, timeMillis.coerceAtMost(MAX_DELAY)) return object : DisposableHandle { override fun dispose() { @@ -149,12 +149,10 @@ internal class HandlerContext private constructor( } } - override fun toString(): String = - if (name != null) { - if (invokeImmediately) "$name [immediate]" else name - } else { - handler.toString() - } + override fun toString(): String = toStringInternalImpl() ?: run { + val str = name ?: handler.toString() + if (invokeImmediately) "$str.immediate" else str + } override fun equals(other: Any?): Boolean = other is HandlerContext && other.handler === handler override fun hashCode(): Int = System.identityHashCode(handler) diff --git a/ui/kotlinx-coroutines-android/test/HandlerDispatcherTest.kt b/ui/kotlinx-coroutines-android/test/HandlerDispatcherTest.kt index 1501639e5d..55decde61b 100644 --- a/ui/kotlinx-coroutines-android/test/HandlerDispatcherTest.kt +++ b/ui/kotlinx-coroutines-android/test/HandlerDispatcherTest.kt @@ -123,8 +123,8 @@ class HandlerDispatcherTest : TestBase() { ReflectionHelpers.setStaticField(Build.VERSION::class.java, "SDK_INT", 28) val main = Looper.getMainLooper().asHandler(async = true).asCoroutineDispatcher("testName") assertEquals("testName", main.toString()) - assertEquals("testName [immediate]", main.immediate.toString()) - assertEquals("testName [immediate]", main.immediate.immediate.toString()) + assertEquals("testName.immediate", main.immediate.toString()) + assertEquals("testName.immediate", main.immediate.immediate.toString()) } private suspend fun Job.join(mainLooper: ShadowLooper) { @@ -155,4 +155,10 @@ class HandlerDispatcherTest : TestBase() { yield() // yield back finish(5) } + + @Test + fun testMainDispatcherToString() { + assertEquals("Dispatchers.Main", Dispatchers.Main.toString()) + assertEquals("Dispatchers.Main.immediate", Dispatchers.Main.immediate.toString()) + } } diff --git a/ui/kotlinx-coroutines-android/testdata/r8-test-rules.pro b/ui/kotlinx-coroutines-android/testdata/r8-test-rules.pro index dde8600854..63dc24ccd5 100644 --- a/ui/kotlinx-coroutines-android/testdata/r8-test-rules.pro +++ b/ui/kotlinx-coroutines-android/testdata/r8-test-rules.pro @@ -6,9 +6,10 @@ -checkdiscard class kotlinx.coroutines.internal.FastServiceLoader -checkdiscard class kotlinx.coroutines.DebugKt -checkdiscard class kotlinx.coroutines.internal.StackTraceRecoveryKt +-checkdiscard class kotlinx.coroutines.debug.DebugProbesKt # Real android projects do not keep this class, but somehow it is kept in this test (R8 bug) # -checkdiscard class kotlinx.coroutines.internal.MissingMainCoroutineDispatcher # Should not keep this class, but it is still there (R8 bug) -#-checkdiscard class kotlinx.coroutines.CoroutineId \ No newline at end of file +#-checkdiscard class kotlinx.coroutines.CoroutineId diff --git a/ui/kotlinx-coroutines-javafx/api/kotlinx-coroutines-javafx.api b/ui/kotlinx-coroutines-javafx/api/kotlinx-coroutines-javafx.api index 620e904612..e2c3b8f326 100644 --- a/ui/kotlinx-coroutines-javafx/api/kotlinx-coroutines-javafx.api +++ b/ui/kotlinx-coroutines-javafx/api/kotlinx-coroutines-javafx.api @@ -5,7 +5,7 @@ public final class kotlinx/coroutines/javafx/JavaFxConvertKt { public abstract class kotlinx/coroutines/javafx/JavaFxDispatcher : kotlinx/coroutines/MainCoroutineDispatcher, kotlinx/coroutines/Delay { public fun delay (JLkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun dispatch (Lkotlin/coroutines/CoroutineContext;Ljava/lang/Runnable;)V - public fun invokeOnTimeout (JLjava/lang/Runnable;)Lkotlinx/coroutines/DisposableHandle; + public fun invokeOnTimeout (JLjava/lang/Runnable;Lkotlin/coroutines/CoroutineContext;)Lkotlinx/coroutines/DisposableHandle; public fun scheduleResumeAfterDelay (JLkotlinx/coroutines/CancellableContinuation;)V } diff --git a/ui/kotlinx-coroutines-javafx/build.gradle b/ui/kotlinx-coroutines-javafx/build.gradle deleted file mode 100644 index 9d1c128239..0000000000 --- a/ui/kotlinx-coroutines-javafx/build.gradle +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -static int javaVersionMajor() { - String javaVersion = System.properties["java.version"] - int i = javaVersion.indexOf('.') - return (i < 0 ? javaVersion : javaVersion.substring(0, i)).toInteger() -} - -// JDK11+ does not bundle JavaFx and the plugin for JavaFx support is compiled with class file version 55.0 (JDK 11) -if (javaVersionMajor() >= 11) { - apply plugin: 'org.openjfx.javafxplugin' - - javafx { - version = javafx_version - modules = ['javafx.controls'] - configuration = 'compile' - } -} - -task checkJdk8() { - // only fail w/o JDK_18 when actually trying to test, not during project setup phase - doLast { - if (!System.env.JDK_18) { - throw new GradleException("JDK_18 environment variable is not defined. " + - "Can't run JDK 8 compatibility tests. " + - "Please ensure JDK 8 is installed and that JDK_18 points to it.") - } - } -} - -task jdk8Test(type: Test, dependsOn: [compileTestKotlin, checkJdk8]) { - classpath = files { test.classpath } - testClassesDirs = files { test.testClassesDirs } - executable = "$System.env.JDK_18/bin/java" -} - -// Run these tests only during nightly stress test -jdk8Test.onlyIf { project.properties['stressTest'] != null } -build.dependsOn jdk8Test diff --git a/ui/kotlinx-coroutines-javafx/build.gradle.kts b/ui/kotlinx-coroutines-javafx/build.gradle.kts new file mode 100644 index 0000000000..112441e0ed --- /dev/null +++ b/ui/kotlinx-coroutines-javafx/build.gradle.kts @@ -0,0 +1,50 @@ +/* + * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +plugins { + id("org.openjfx.javafxplugin") +} + +javafx { + version = version("javafx") + modules = listOf("javafx.controls") + configuration = "compile" +} + +val JDK_18: String? by lazy { + System.getenv("JDK_18") +} + +val checkJdk8 by tasks.registering { + // only fail w/o JDK_18 when actually trying to test, not during project setup phase + doLast { + if (JDK_18 == null) { + throw GradleException( + """ + JDK_18 environment variable is not defined. + Can't run JDK 8 compatibility tests. + Please ensure JDK 8 is installed and that JDK_18 points to it. + """.trimIndent() + ) + } + } +} + +val jdk8Test by tasks.registering(Test::class) { + // Run these tests only during nightly stress test + onlyIf { project.properties["stressTest"] != null } + + val test = tasks.test.get() + + classpath = test.classpath + testClassesDirs = test.testClassesDirs + executable = "$JDK_18/bin/java" + + dependsOn("compileTestKotlin") + dependsOn(checkJdk8) +} + +tasks.build { + dependsOn(jdk8Test) +} diff --git a/ui/kotlinx-coroutines-javafx/src/JavaFxConvert.kt b/ui/kotlinx-coroutines-javafx/src/JavaFxConvert.kt index 903b60a2cf..c7fcb1c2b6 100644 --- a/ui/kotlinx-coroutines-javafx/src/JavaFxConvert.kt +++ b/ui/kotlinx-coroutines-javafx/src/JavaFxConvert.kt @@ -24,7 +24,7 @@ import kotlinx.coroutines.flow.* * Adjacent applications of [flowOn], [buffer], [conflate], and [produceIn] to the result of `asFlow` are fused. * [conflate] has no effect, as this flow is already conflated; one can use [buffer] to change that instead. */ -@ExperimentalCoroutinesApi +@ExperimentalCoroutinesApi // Since 1.3.x public fun ObservableValue.asFlow(): Flow = callbackFlow { val listener = ChangeListener { _, _, newValue -> try { diff --git a/ui/kotlinx-coroutines-javafx/src/JavaFxDispatcher.kt b/ui/kotlinx-coroutines-javafx/src/JavaFxDispatcher.kt index ed74ad6a56..c3069d636f 100644 --- a/ui/kotlinx-coroutines-javafx/src/JavaFxDispatcher.kt +++ b/ui/kotlinx-coroutines-javafx/src/JavaFxDispatcher.kt @@ -31,7 +31,7 @@ public val Dispatchers.JavaFx: JavaFxDispatcher public sealed class JavaFxDispatcher : MainCoroutineDispatcher(), Delay { /** @suppress */ - override fun dispatch(context: CoroutineContext, block: Runnable) = Platform.runLater(block) + override fun dispatch(context: CoroutineContext, block: Runnable): Unit = Platform.runLater(block) /** @suppress */ override fun scheduleResumeAfterDelay(timeMillis: Long, continuation: CancellableContinuation) { @@ -42,7 +42,7 @@ public sealed class JavaFxDispatcher : MainCoroutineDispatcher(), Delay { } /** @suppress */ - override fun invokeOnTimeout(timeMillis: Long, block: Runnable): DisposableHandle { + override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle { val timeline = schedule(timeMillis, TimeUnit.MILLISECONDS, EventHandler { block.run() }) @@ -70,7 +70,7 @@ private object ImmediateJavaFxDispatcher : JavaFxDispatcher() { override fun isDispatchNeeded(context: CoroutineContext): Boolean = !Platform.isFxApplicationThread() - override fun toString() = "JavaFx [immediate]" + override fun toString() = toStringInternalImpl() ?: "JavaFx.immediate" } /** @@ -85,7 +85,7 @@ internal object JavaFx : JavaFxDispatcher() { override val immediate: MainCoroutineDispatcher get() = ImmediateJavaFxDispatcher - override fun toString() = "JavaFx" + override fun toString() = toStringInternalImpl() ?: "JavaFx" } private val pulseTimer by lazy { diff --git a/ui/kotlinx-coroutines-javafx/test/JavaFxDispatcherTest.kt b/ui/kotlinx-coroutines-javafx/test/JavaFxDispatcherTest.kt index 724be6d77b..24c5c132fd 100644 --- a/ui/kotlinx-coroutines-javafx/test/JavaFxDispatcherTest.kt +++ b/ui/kotlinx-coroutines-javafx/test/JavaFxDispatcherTest.kt @@ -7,6 +7,8 @@ package kotlinx.coroutines.javafx import javafx.application.* import kotlinx.coroutines.* import org.junit.* +import org.junit.Test +import kotlin.test.* class JavaFxDispatcherTest : TestBase() { @Before @@ -56,4 +58,10 @@ class JavaFxDispatcherTest : TestBase() { finish(5) } } + + @Test + fun testMainDispatcherToString() { + assertEquals("Dispatchers.Main", Dispatchers.Main.toString()) + assertEquals("Dispatchers.Main.immediate", Dispatchers.Main.immediate.toString()) + } } \ No newline at end of file diff --git a/ui/kotlinx-coroutines-swing/api/kotlinx-coroutines-swing.api b/ui/kotlinx-coroutines-swing/api/kotlinx-coroutines-swing.api index 09556e807f..d33191fd96 100644 --- a/ui/kotlinx-coroutines-swing/api/kotlinx-coroutines-swing.api +++ b/ui/kotlinx-coroutines-swing/api/kotlinx-coroutines-swing.api @@ -1,7 +1,7 @@ public abstract class kotlinx/coroutines/swing/SwingDispatcher : kotlinx/coroutines/MainCoroutineDispatcher, kotlinx/coroutines/Delay { public fun delay (JLkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun dispatch (Lkotlin/coroutines/CoroutineContext;Ljava/lang/Runnable;)V - public fun invokeOnTimeout (JLjava/lang/Runnable;)Lkotlinx/coroutines/DisposableHandle; + public fun invokeOnTimeout (JLjava/lang/Runnable;Lkotlin/coroutines/CoroutineContext;)Lkotlinx/coroutines/DisposableHandle; public fun scheduleResumeAfterDelay (JLkotlinx/coroutines/CancellableContinuation;)V } diff --git a/ui/kotlinx-coroutines-swing/build.gradle b/ui/kotlinx-coroutines-swing/build.gradle.kts similarity index 70% rename from ui/kotlinx-coroutines-swing/build.gradle rename to ui/kotlinx-coroutines-swing/build.gradle.kts index ad8bef0e2f..b834f29fb7 100644 --- a/ui/kotlinx-coroutines-swing/build.gradle +++ b/ui/kotlinx-coroutines-swing/build.gradle.kts @@ -3,5 +3,5 @@ */ dependencies { - testCompile project(':kotlinx-coroutines-jdk8') + testCompile(project(":kotlinx-coroutines-jdk8")) } diff --git a/ui/kotlinx-coroutines-swing/src/SwingDispatcher.kt b/ui/kotlinx-coroutines-swing/src/SwingDispatcher.kt index 3fad55f314..054ed1f60e 100644 --- a/ui/kotlinx-coroutines-swing/src/SwingDispatcher.kt +++ b/ui/kotlinx-coroutines-swing/src/SwingDispatcher.kt @@ -25,7 +25,7 @@ public val Dispatchers.Swing : SwingDispatcher */ public sealed class SwingDispatcher : MainCoroutineDispatcher(), Delay { /** @suppress */ - override fun dispatch(context: CoroutineContext, block: Runnable) = SwingUtilities.invokeLater(block) + override fun dispatch(context: CoroutineContext, block: Runnable): Unit = SwingUtilities.invokeLater(block) /** @suppress */ override fun scheduleResumeAfterDelay(timeMillis: Long, continuation: CancellableContinuation) { @@ -36,7 +36,7 @@ public sealed class SwingDispatcher : MainCoroutineDispatcher(), Delay { } /** @suppress */ - override fun invokeOnTimeout(timeMillis: Long, block: Runnable): DisposableHandle { + override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle { val timer = schedule(timeMillis, TimeUnit.MILLISECONDS, ActionListener { block.run() }) @@ -67,7 +67,7 @@ private object ImmediateSwingDispatcher : SwingDispatcher() { override fun isDispatchNeeded(context: CoroutineContext): Boolean = !SwingUtilities.isEventDispatchThread() - override fun toString() = "Swing [immediate]" + override fun toString() = toStringInternalImpl() ?: "Swing.immediate" } /** @@ -77,5 +77,5 @@ internal object Swing : SwingDispatcher() { override val immediate: MainCoroutineDispatcher get() = ImmediateSwingDispatcher - override fun toString() = "Swing" + override fun toString() = toStringInternalImpl() ?: "Swing" } diff --git a/ui/kotlinx-coroutines-swing/test/SwingTest.kt b/ui/kotlinx-coroutines-swing/test/SwingTest.kt index cbed5bf1e9..7e53e57b17 100644 --- a/ui/kotlinx-coroutines-swing/test/SwingTest.kt +++ b/ui/kotlinx-coroutines-swing/test/SwingTest.kt @@ -97,4 +97,10 @@ class SwingTest : TestBase() { yield() // yield back finish(5) } + + @Test + fun testMainDispatcherToString() { + assertEquals("Dispatchers.Main", Dispatchers.Main.toString()) + assertEquals("Dispatchers.Main.immediate", Dispatchers.Main.immediate.toString()) + } } \ No newline at end of file