Skip to content

Commit

Permalink
Fix flakiness of SubSamplingImageTest#draw_tile_under_centroid_first
Browse files Browse the repository at this point in the history
  • Loading branch information
saket committed Apr 10, 2024
1 parent 3230987 commit 42faef8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@ class SubSamplingImageTest {
override suspend fun decodeRegion(region: BitmapRegionTile): ImageBitmap {
val isBaseTile = region.sampleSize.size == 8
return if (isBaseTile || !firstNonBaseTileReceived.getAndSet(true)) {
if (!isBaseTile) {
check(region.bounds == IntRect(0, 1200, 1216, 3265)) {
"Incorrect first tile received: $region. Possible race condition?"
}
}
real.decodeRegion(region)
} else {
delay(Long.MAX_VALUE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package me.saket.telephoto.subsamplingimage.internal
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.util.fastForEach
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
Expand Down Expand Up @@ -41,7 +43,11 @@ internal class BitmapCache(
.collect { regions ->
val tilesToLoad = regions.fastFilter { it !in cachedBitmaps.value }
tilesToLoad.fastForEach { region ->
val job = launch {
// CoroutineStart.UNDISPATCHED is used to ensure that the coroutines are executed
// in the same order they were launched. Otherwise, the tiles may load in a different
// order than what was requested. SubSamplingImageTest#draw_tile_under_centroid_first()
// test will also become flaky.
val job = launch(start = CoroutineStart.UNDISPATCHED) {
val bitmap = decoder.decodeRegion(region)
cachedBitmaps.update { it + (region to Loaded(bitmap)) }
}
Expand Down

0 comments on commit 42faef8

Please sign in to comment.