Skip to content

Commit 865f877

Browse files
committed
Fix flaky test by using CompletableDeferred
1 parent 270fcb9 commit 865f877

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

features/preferences/impl/src/test/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsPresenterTest.kt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ import io.element.android.libraries.preferences.test.InMemoryAppPreferencesStore
2626
import io.element.android.tests.testutils.WarmUpRule
2727
import io.element.android.tests.testutils.awaitLastSequentialItem
2828
import io.element.android.tests.testutils.lambda.lambdaRecorder
29-
import kotlinx.coroutines.ExperimentalCoroutinesApi
29+
import kotlinx.coroutines.CompletableDeferred
3030
import kotlinx.coroutines.flow.first
31-
import kotlinx.coroutines.test.advanceUntilIdle
3231
import kotlinx.coroutines.test.runTest
3332
import org.junit.Rule
3433
import org.junit.Test
@@ -155,10 +154,18 @@ class DeveloperSettingsPresenterTest {
155154
}
156155
}
157156

158-
@OptIn(ExperimentalCoroutinesApi::class)
159157
@Test
160158
fun `present - toggling simplified sliding sync changes the preferences and logs out the user`() = runTest {
161-
val logoutCallRecorder = lambdaRecorder<Boolean, String?> { "" }
159+
val latch1 = CompletableDeferred<Unit>()
160+
val latch2 = CompletableDeferred<Unit>()
161+
val logoutCallRecorder = lambdaRecorder<Boolean, String?> {
162+
if (latch1.isActive) {
163+
latch1.complete(Unit)
164+
} else {
165+
latch2.complete(Unit)
166+
}
167+
""
168+
}
162169
val logoutUseCase = FakeLogoutUseCase(logoutLambda = logoutCallRecorder)
163170
val preferences = InMemoryAppPreferencesStore()
164171
val presenter = createDeveloperSettingsPresenter(preferencesStore = preferences, logoutUseCase = logoutUseCase)
@@ -171,13 +178,12 @@ class DeveloperSettingsPresenterTest {
171178
initialState.eventSink(DeveloperSettingsEvents.SetSimplifiedSlidingSyncEnabled(true))
172179
assertThat(awaitItem().isSimpleSlidingSyncEnabled).isTrue()
173180
assertThat(preferences.isSimplifiedSlidingSyncEnabledFlow().first()).isTrue()
174-
advanceUntilIdle()
181+
latch1.await()
175182
logoutCallRecorder.assertions().isCalledOnce()
176-
177183
initialState.eventSink(DeveloperSettingsEvents.SetSimplifiedSlidingSyncEnabled(false))
178184
assertThat(awaitItem().isSimpleSlidingSyncEnabled).isFalse()
179185
assertThat(preferences.isSimplifiedSlidingSyncEnabledFlow().first()).isFalse()
180-
advanceUntilIdle()
186+
latch2.await()
181187
logoutCallRecorder.assertions().isCalledExactly(times = 2)
182188
}
183189
}

0 commit comments

Comments
 (0)