[Gradle] Implement lazyFuture as Future<T> instead of Lazy<Future<T>>

^KT-34662 Verification Pending
This commit is contained in:
Sebastian Sellmair
2023-03-14 15:41:14 +01:00
committed by Space Team
parent 510eca9da1
commit aa969fe764
3 changed files with 41 additions and 11 deletions
@@ -125,7 +125,7 @@ internal open class CInteropCommonizerTask
* For Gradle Configuration Cache support the Group-to-Dependencies relation should be pre-cached.
* It is used during execution phase.
*/
private val groupedCommonizerDependencies: Future<GroupedCommonizerDependencies> by project.lazyFuture {
private val groupedCommonizerDependencies: Future<GroupedCommonizerDependencies> = project.lazyFuture {
val multiplatformExtension = project.multiplatformExtensionOrNull ?: return@lazyFuture emptyMap()
val sourceSetsByTarget = multiplatformExtension.sourceSets.groupBy { sourceSet -> sourceSet.commonizerTarget.getOrThrow() }
@@ -232,7 +232,7 @@ internal open class CInteropCommonizerTask
}
@get:Internal
internal val allInteropGroups: Future<Set<CInteropCommonizerGroup>> by project.lazyFuture {
internal val allInteropGroups: Future<Set<CInteropCommonizerGroup>> = project.lazyFuture {
val dependents = allDependents.await()
val allScopeSets = dependents.map { it.scopes }.toSet()
val rootScopeSets = allScopeSets.filter { scopeSet ->
@@ -267,7 +267,7 @@ internal open class CInteropCommonizerTask
return suitableGroups.firstOrNull()
}
private val allDependents: Future<Set<CInteropCommonizerDependent>> by project.lazyFuture {
private val allDependents: Future<Set<CInteropCommonizerDependent>> = project.lazyFuture {
val multiplatformExtension = project.multiplatformExtensionOrNull ?: return@lazyFuture emptySet()
val fromSharedNativeCompilations = multiplatformExtension
@@ -85,7 +85,7 @@ internal val <T> Future<T>.lenient: LenientFuture<T> get() = LenientFutureImpl(t
*
* basically creating a future, which is launched lazily
*/
internal fun <T> Project.lazyFuture(block: suspend Project.() -> T): Lazy<Future<T>> = lazy { future(block) }
internal fun <T> Project.lazyFuture(block: suspend Project.() -> T): Future<T> = LazyFutureImpl(lazy { future(block) })
internal fun <T> KotlinPluginLifecycle.future(block: suspend () -> T): Future<T> {
return FutureImpl<T>(lifecycle = this).also { future ->
@@ -158,4 +158,24 @@ private class LenientFutureImpl<T>(
return LenientFutureImpl(FutureImpl(CompletableDeferred(value)))
}
}
}
private class LazyFutureImpl<T>(private val future: Lazy<Future<T>>) : Future<T>, Serializable {
override suspend fun await(): T {
return future.value.await()
}
override fun getOrThrow(): T {
return future.value.getOrThrow()
}
private fun writeReplace(): Any {
return Surrogate(getOrThrow())
}
private class Surrogate<T>(private val value: T) : Serializable {
private fun readResolve(): Any {
return FutureImpl(CompletableDeferred(value))
}
}
}
@@ -9,19 +9,15 @@ package org.jetbrains.kotlin.gradle.unitTests
import org.jetbrains.kotlin.gradle.idea.testFixtures.utils.deserialize
import org.jetbrains.kotlin.gradle.idea.testFixtures.utils.serialize
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginLifecycle.IllegalLifecycleException
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginLifecycle.Stage.FinaliseDsl
import org.jetbrains.kotlin.gradle.plugin.await
import org.jetbrains.kotlin.gradle.plugin.currentKotlinPluginLifecycle
import org.jetbrains.kotlin.gradle.plugin.startKotlinPluginLifecycle
import org.jetbrains.kotlin.gradle.util.buildProject
import org.jetbrains.kotlin.gradle.util.runLifecycleAwareTest
import org.jetbrains.kotlin.gradle.utils.CompletableFuture
import org.jetbrains.kotlin.gradle.utils.LenientFuture
import org.jetbrains.kotlin.gradle.utils.future
import org.jetbrains.kotlin.gradle.utils.lenient
import org.jetbrains.kotlin.gradle.utils.*
import org.junit.Test
import org.junit.jupiter.api.assertThrows
import java.util.concurrent.atomic.AtomicInteger
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNull
@@ -104,4 +100,18 @@ class FutureTest {
assertEquals(42, deserializedFuture.getOrNull())
}
}
@Test
fun `test - lazy future`() = project.runLifecycleAwareTest {
val futureInvocations = AtomicInteger(0)
val future = project.lazyFuture {
assertEquals(1, futureInvocations.incrementAndGet())
}
project.launchInStage(KotlinPluginLifecycle.Stage.last) {
assertEquals(0, futureInvocations.get())
future.await()
assertEquals(1, futureInvocations.get())
}
}
}