forked from Kotlin/kotlinx.coroutines
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge kotlinx-coroutines-core and kotlinx-coroutines-jdk8 modules (Ko…
…tlin#3415) * Configure source sets and compilations for module merger * Programmatically split the structure of compilation for the sake of separate compilation and dependencies * Add new integration test * Merge ABI signatures * Exclude jdk8 from animal sniffer Fixes Kotlin#3268
- Loading branch information
Showing
24 changed files
with
128 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
integration-testing/src/jvmCoreTest/kotlin/Jdk8InCoreIntegration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright 2016-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
package kotlinx.coroutines | ||
|
||
import kotlinx.coroutines.future.* | ||
import org.junit.Test | ||
import kotlin.test.* | ||
|
||
/* | ||
* Integration test that ensures signatures from both the jdk8 and the core source sets of the kotlinx-coroutines-core subproject are used. | ||
*/ | ||
class Jdk8InCoreIntegration { | ||
|
||
@Test | ||
fun testFuture() = runBlocking<Unit> { | ||
val future = future { yield(); 42 } | ||
future.whenComplete { r, _ -> assertEquals(42, r) } | ||
assertEquals(42, future.await()) | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,3 @@ | ||
# Module kotlinx-coroutines-jdk8 | ||
# Stub module | ||
|
||
Integration with JDK8 [CompletableFuture] (Android API level 24). | ||
|
||
Coroutine builders: | ||
|
||
| **Name** | **Result** | **Scope** | **Description** | ||
| -------- | ------------------- | ---------------- | --------------- | ||
| [future] | [CompletableFuture] | [CoroutineScope] | Returns a single value with the future result | ||
|
||
Extension functions: | ||
|
||
| **Name** | **Description** | ||
| -------- | --------------- | ||
| [CompletionStage.await][java.util.concurrent.CompletionStage.await] | Awaits for completion of the completion stage | ||
| [CompletionStage.asDeferred][java.util.concurrent.CompletionStage.asDeferred] | Converts completion stage to an instance of [Deferred] | ||
| [Deferred.asCompletableFuture][kotlinx.coroutines.Deferred.asCompletableFuture] | Converts a deferred value to the future | ||
|
||
## Example | ||
|
||
Given the following functions defined in some Java API: | ||
|
||
```java | ||
public CompletableFuture<Image> loadImageAsync(String name); // starts async image loading | ||
public Image combineImages(Image image1, Image image2); // synchronously combines two images using some algorithm | ||
``` | ||
|
||
We can consume this API from Kotlin coroutine to load two images and combine then asynchronously. | ||
The resulting function returns `CompletableFuture<Image>` for ease of use back from Java. | ||
|
||
```kotlin | ||
fun combineImagesAsync(name1: String, name2: String): CompletableFuture<Image> = future { | ||
val future1 = loadImageAsync(name1) // start loading first image | ||
val future2 = loadImageAsync(name2) // start loading second image | ||
combineImages(future1.await(), future2.await()) // wait for both, combine, and return result | ||
} | ||
``` | ||
|
||
Note that this module should be used only for integration with existing Java APIs based on `CompletableFuture`. | ||
Writing pure-Kotlin code that uses `CompletableFuture` is highly not recommended, since the resulting APIs based | ||
on the futures are quite error-prone. See the discussion on | ||
[Asynchronous Programming Styles](https://github.com/Kotlin/KEEP/blob/master/proposals/coroutines.md#asynchronous-programming-styles) | ||
for details on general problems pertaining to any future-based API and keep in mind that `CompletableFuture` exposes | ||
a _blocking_ method | ||
[get](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Future.html#get--) | ||
that makes it especially bad choice for coroutine-based Kotlin code. | ||
|
||
# Package kotlinx.coroutines.future | ||
|
||
Integration with JDK8 [CompletableFuture] (Android API level 24). | ||
|
||
[CompletableFuture]: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html | ||
|
||
<!--- MODULE kotlinx-coroutines-core --> | ||
<!--- INDEX kotlinx.coroutines --> | ||
|
||
[CoroutineScope]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html | ||
[Deferred]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-deferred/index.html | ||
|
||
<!--- MODULE kotlinx-coroutines-jdk8 --> | ||
<!--- INDEX kotlinx.coroutines.future --> | ||
|
||
[future]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-jdk8/kotlinx.coroutines.future/future.html | ||
[java.util.concurrent.CompletionStage.await]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-jdk8/kotlinx.coroutines.future/await.html | ||
[java.util.concurrent.CompletionStage.asDeferred]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-jdk8/kotlinx.coroutines.future/as-deferred.html | ||
[kotlinx.coroutines.Deferred.asCompletableFuture]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-jdk8/kotlinx.coroutines.future/as-completable-future.html | ||
|
||
<!--- END --> | ||
Stub module for backwards compatibility. Since 1.7.0, this module was merged with core. |
22 changes: 0 additions & 22 deletions
22
integration/kotlinx-coroutines-jdk8/api/kotlinx-coroutines-jdk8.api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +0,0 @@ | ||
public final class kotlinx/coroutines/future/FutureKt { | ||
public static final fun asCompletableFuture (Lkotlinx/coroutines/Deferred;)Ljava/util/concurrent/CompletableFuture; | ||
public static final fun asCompletableFuture (Lkotlinx/coroutines/Job;)Ljava/util/concurrent/CompletableFuture; | ||
public static final fun asDeferred (Ljava/util/concurrent/CompletionStage;)Lkotlinx/coroutines/Deferred; | ||
public static final fun await (Ljava/util/concurrent/CompletionStage;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; | ||
public static final fun future (Lkotlinx/coroutines/CoroutineScope;Lkotlin/coroutines/CoroutineContext;Lkotlinx/coroutines/CoroutineStart;Lkotlin/jvm/functions/Function2;)Ljava/util/concurrent/CompletableFuture; | ||
public static synthetic fun future$default (Lkotlinx/coroutines/CoroutineScope;Lkotlin/coroutines/CoroutineContext;Lkotlinx/coroutines/CoroutineStart;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Ljava/util/concurrent/CompletableFuture; | ||
} | ||
|
||
public final class kotlinx/coroutines/stream/StreamKt { | ||
public static final fun consumeAsFlow (Ljava/util/stream/Stream;)Lkotlinx/coroutines/flow/Flow; | ||
} | ||
|
||
public final class kotlinx/coroutines/time/TimeKt { | ||
public static final fun debounce (Lkotlinx/coroutines/flow/Flow;Ljava/time/Duration;)Lkotlinx/coroutines/flow/Flow; | ||
public static final fun delay (Ljava/time/Duration;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; | ||
public static final fun onTimeout (Lkotlinx/coroutines/selects/SelectBuilder;Ljava/time/Duration;Lkotlin/jvm/functions/Function1;)V | ||
public static final fun sample (Lkotlinx/coroutines/flow/Flow;Ljava/time/Duration;)Lkotlinx/coroutines/flow/Flow; | ||
public static final fun withTimeout (Ljava/time/Duration;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; | ||
public static final fun withTimeoutOrNull (Ljava/time/Duration;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; | ||
} | ||
|
||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
...ferredUnhandledCompletionExceptionTest.kt → ...ferredUnhandledCompletionExceptionTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.