-
Notifications
You must be signed in to change notification settings - Fork 685
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix cancellation in
ConstraintsSizeResolver#size()
`suspendCoroutine` does not implement cancellation from kotlinx.coroutines, which might result in Recomposer hanging while awaiting cancellation. See [reported Compose issue](https://issuetracker.google.com/390456358) for more details. Test: ConstraintsSizeResolverTest
- Loading branch information
Showing
2 changed files
with
91 additions
and
7 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
76 changes: 76 additions & 0 deletions
76
coil-compose-core/src/commonTest/kotlin/coil3/compose/ConstraintsSizeResolverTest.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,76 @@ | ||
package coil3.compose | ||
|
||
import androidx.compose.ui.unit.Constraints | ||
import coil3.compose.internal.toSize | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlinx.coroutines.CoroutineStart | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.cancelAndJoin | ||
import kotlinx.coroutines.test.runTest | ||
|
||
class ConstraintsSizeResolverTest { | ||
@Test | ||
fun `resolver size is cancellable`() = runTest { | ||
val resolver = ConstraintsSizeResolver() | ||
|
||
val deferred = async(start = CoroutineStart.UNDISPATCHED) { | ||
resolver.size() | ||
} | ||
|
||
deferred.cancelAndJoin() | ||
assertEquals(true, deferred.isCompleted) | ||
} | ||
|
||
@Test | ||
fun `resolver size is cancellable with two suspension points`() = runTest { | ||
val resolver = ConstraintsSizeResolver() | ||
|
||
val deferred1 = async(start = CoroutineStart.UNDISPATCHED) { | ||
resolver.size() | ||
} | ||
|
||
val deferred2 = async(start = CoroutineStart.UNDISPATCHED) { | ||
resolver.size() | ||
} | ||
|
||
deferred2.cancelAndJoin() | ||
assertEquals(true, deferred2.isCompleted) | ||
assertEquals(false, deferred1.isCancelled) | ||
assertEquals(false, deferred1.isCompleted) | ||
|
||
val c = Constraints() | ||
resolver.setConstraints(c) | ||
val result = deferred1.await() | ||
assertEquals(c.toSize(), result) | ||
} | ||
|
||
@Test | ||
fun `resolver size is cancellable with many suspension points`() = runTest { | ||
val resolver = ConstraintsSizeResolver() | ||
|
||
val deferred1 = async(start = CoroutineStart.UNDISPATCHED) { | ||
resolver.size() | ||
} | ||
|
||
val deferred2 = async(start = CoroutineStart.UNDISPATCHED) { | ||
resolver.size() | ||
} | ||
|
||
val deferred3 = async(start = CoroutineStart.UNDISPATCHED) { | ||
resolver.size() | ||
} | ||
|
||
deferred2.cancelAndJoin() | ||
assertEquals(true, deferred2.isCompleted) | ||
assertEquals(false, deferred1.isCancelled) | ||
assertEquals(false, deferred3.isCancelled) | ||
|
||
val c = Constraints() | ||
resolver.setConstraints(c) | ||
val result1 = deferred1.await() | ||
val result3 = deferred3.await() | ||
assertEquals(c.toSize(), result1) | ||
assertEquals(c.toSize(), result3) | ||
} | ||
} |