Skip to content

Commit

Permalink
Fix flaky test.
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrtwhite committed Jan 27, 2025
1 parent 43b79e4 commit cf0c2f0
Showing 1 changed file with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -824,12 +824,13 @@ class AsyncImagePainterTest {
val key = Extras.Key(0)
val value = AtomicInteger()
val compositionCount = AtomicInteger()
val maxCompositionCount = AtomicInteger(3)

composeTestRule.setContent {
val painter = rememberAsyncImagePainter(
model = ImageRequest.Builder(LocalContext.current)
.data("https://example.com/image")
.apply { extras[key] = value.getAndIncrement() }
.apply { extras[key] = value.getAndSet(1) }
.build(),
imageLoader = imageLoader,
)
Expand All @@ -841,17 +842,18 @@ class AsyncImagePainterTest {

val state by painter.state.collectAsState()

when (compositionCount.incrementAndGet()) {
1 -> assertIs<State.Empty>(state)
2 -> assertIs<State.Loading>(state)
3 -> assertIs<State.Success>(state)
else -> error("too many compositions")
val compositions = compositionCount.incrementAndGet()
if (compositions > maxCompositionCount.get()) {
error("too many compositions")
}
if (state is State.Success) {
maxCompositionCount.set(compositions)
}
}

waitForRequestComplete()

assertEquals(3, compositionCount.get())
assertEquals(maxCompositionCount.get(), compositionCount.get())
assertEquals(1, requestTracker.startedRequests)
assertEquals(1, requestTracker.finishedRequests)
}
Expand Down

0 comments on commit cf0c2f0

Please sign in to comment.