Skip to content

Commit

Permalink
Fix flaky test InstalledFapsUidsProducerTest (#928)
Browse files Browse the repository at this point in the history
**Background**

Right now our tests sometimes flaky

**Changes**

Fix race condition in InstalledFapsUidsProducerTest

**Test plan**

Try launch InstalledFapsUidsProducerTest test
  • Loading branch information
LionZXY authored Aug 28, 2024
1 parent 10328e5 commit 9ecb62a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Attention: don't forget to add the flag for F-Droid before release
- [FIX] Relocate remote-controls button to infrared remotes screen
- [FIX] Fap manifest caching
- [FIX] Remote controls design issues
- [FIX] Fix flaky test
- [CI] Fix merge-queue files diff
- [CI] Add https://github.com/LionZXY/detekt-decompose-rule
- [CI] Enabling detekt module for android and kmp modules
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.coroutines.plus
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Assert
import org.junit.Before
Expand Down Expand Up @@ -83,7 +81,7 @@ class InstalledFapsUidsProducerTest {
}

@Test
fun `reinvalidate don't refresh cache`() = runTest {
fun `reinvalidate don't refresh cache`() = runTest(UnconfinedTestDispatcher()) {
fapManifestStateFlow.emit(
FapManifestState.Loaded(
items = persistentListOf(getTestManifest("TEST")),
Expand All @@ -92,9 +90,11 @@ class InstalledFapsUidsProducerTest {
)

val states = mutableListOf<FapInstalledUidsState>()
val stateSubscribeJob = underTest.getUidsStateFlow().onEach {
states.add(it)
}.launchIn(this + testScheduler)
val stateSubscribeJob = launch {
underTest.getUidsStateFlow().collect {
states.add(it)
}
}

var job = launch {
underTest.refresh(this, force = true)
Expand All @@ -103,8 +103,6 @@ class InstalledFapsUidsProducerTest {
it is FapInstalledUidsState.Loaded && it.faps.isNotEmpty()
}.first()

advanceUntilIdle()

Assert.assertArrayEquals(
arrayOf(
FapInstalledUidsState.Loaded(faps = persistentListOf(), inProgress = true),
Expand Down Expand Up @@ -134,7 +132,6 @@ class InstalledFapsUidsProducerTest {
underTest.getUidsStateFlow().filter {
it is FapInstalledUidsState.Loaded && it.faps.find { it.applicationUid == "TEST2" } != null
}.first()
advanceUntilIdle()

Assert.assertArrayEquals(
arrayOf(
Expand Down

0 comments on commit 9ecb62a

Please sign in to comment.