Browse Source

Use backgroundScope for TimelineItemsSubscriberTest

pull/3554/head
Benoit Marty 3 weeks ago
parent
commit
b9c9706c48
  1. 46
      libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/timeline/TimelineItemsSubscriberTest.kt

46
libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/timeline/TimelineItemsSubscriberTest.kt

@ -16,7 +16,6 @@ import io.element.android.libraries.matrix.impl.fixtures.fakes.FakeRustTimelineD
import io.element.android.libraries.matrix.impl.fixtures.fakes.FakeRustTimelineItem import io.element.android.libraries.matrix.impl.fixtures.fakes.FakeRustTimelineItem
import io.element.android.tests.testutils.lambda.lambdaError import io.element.android.tests.testutils.lambda.lambdaError
import io.element.android.tests.testutils.lambda.lambdaRecorder import io.element.android.tests.testutils.lambda.lambdaRecorder
import io.element.android.tests.testutils.runCancellableScopeTestWithTestScope
import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
@ -25,6 +24,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runCurrent import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Test import org.junit.Test
import org.matrix.rustcomponents.sdk.Timeline import org.matrix.rustcomponents.sdk.Timeline
import org.matrix.rustcomponents.sdk.TimelineChange import org.matrix.rustcomponents.sdk.TimelineChange
@ -33,60 +33,55 @@ import uniffi.matrix_sdk_ui.EventItemOrigin
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
class TimelineItemsSubscriberTest { class TimelineItemsSubscriberTest {
@Test @Test
fun `when timeline emits an empty list of items, the flow must emits an empty list`() { fun `when timeline emits an empty list of items, the flow must emits an empty list`() = runTest {
runCancellableScopeTestWithTestScope { testScope, cancellableScope ->
val timelineItems: MutableSharedFlow<List<MatrixTimelineItem>> = val timelineItems: MutableSharedFlow<List<MatrixTimelineItem>> =
MutableSharedFlow(replay = 1, extraBufferCapacity = Int.MAX_VALUE) MutableSharedFlow(replay = 1, extraBufferCapacity = Int.MAX_VALUE)
val timeline = FakeRustTimeline() val timeline = FakeRustTimeline()
val timelineItemsSubscriber = testScope.createTimelineItemsSubscriber( val timelineItemsSubscriber = createTimelineItemsSubscriber(
coroutineScope = cancellableScope, coroutineScope = backgroundScope,
timeline = timeline, timeline = timeline,
timelineItems = timelineItems, timelineItems = timelineItems,
) )
timelineItems.test { timelineItems.test {
timelineItemsSubscriber.subscribeIfNeeded() timelineItemsSubscriber.subscribeIfNeeded()
// Wait for the listener to be set. // Wait for the listener to be set.
testScope.runCurrent() runCurrent()
timeline.emitDiff(listOf(FakeRustTimelineDiff(item = null, change = TimelineChange.RESET))) timeline.emitDiff(listOf(FakeRustTimelineDiff(item = null, change = TimelineChange.RESET)))
val final = awaitItem() val final = awaitItem()
assertThat(final).isEmpty() assertThat(final).isEmpty()
timelineItemsSubscriber.unsubscribeIfNeeded() timelineItemsSubscriber.unsubscribeIfNeeded()
} }
} }
}
@Test @Test
fun `when timeline emits a non empty list of items, the flow must emits a non empty list`() { fun `when timeline emits a non empty list of items, the flow must emits a non empty list`() = runTest {
runCancellableScopeTestWithTestScope { testScope, cancellableScope ->
val timelineItems: MutableSharedFlow<List<MatrixTimelineItem>> = val timelineItems: MutableSharedFlow<List<MatrixTimelineItem>> =
MutableSharedFlow(replay = 1, extraBufferCapacity = Int.MAX_VALUE) MutableSharedFlow(replay = 1, extraBufferCapacity = Int.MAX_VALUE)
val timeline = FakeRustTimeline() val timeline = FakeRustTimeline()
val timelineItemsSubscriber = testScope.createTimelineItemsSubscriber( val timelineItemsSubscriber = createTimelineItemsSubscriber(
coroutineScope = cancellableScope, coroutineScope = backgroundScope,
timeline = timeline, timeline = timeline,
timelineItems = timelineItems, timelineItems = timelineItems,
) )
timelineItems.test { timelineItems.test {
timelineItemsSubscriber.subscribeIfNeeded() timelineItemsSubscriber.subscribeIfNeeded()
// Wait for the listener to be set. // Wait for the listener to be set.
testScope.runCurrent() runCurrent()
timeline.emitDiff(listOf(FakeRustTimelineDiff(item = FakeRustTimelineItem(), change = TimelineChange.RESET))) timeline.emitDiff(listOf(FakeRustTimelineDiff(item = FakeRustTimelineItem(), change = TimelineChange.RESET)))
val final = awaitItem() val final = awaitItem()
assertThat(final).isNotEmpty() assertThat(final).isNotEmpty()
timelineItemsSubscriber.unsubscribeIfNeeded() timelineItemsSubscriber.unsubscribeIfNeeded()
} }
} }
}
@Test @Test
fun `when timeline emits an item with SYNC origin, the callback onNewSyncedEvent is invoked`() { fun `when timeline emits an item with SYNC origin, the callback onNewSyncedEvent is invoked`() = runTest {
runCancellableScopeTestWithTestScope { testScope, cancellableScope ->
val timelineItems: MutableSharedFlow<List<MatrixTimelineItem>> = val timelineItems: MutableSharedFlow<List<MatrixTimelineItem>> =
MutableSharedFlow(replay = 1, extraBufferCapacity = Int.MAX_VALUE) MutableSharedFlow(replay = 1, extraBufferCapacity = Int.MAX_VALUE)
val timeline = FakeRustTimeline() val timeline = FakeRustTimeline()
val onNewSyncedEventRecorder = lambdaRecorder<Unit> { } val onNewSyncedEventRecorder = lambdaRecorder<Unit> { }
val timelineItemsSubscriber = testScope.createTimelineItemsSubscriber( val timelineItemsSubscriber = createTimelineItemsSubscriber(
coroutineScope = cancellableScope, coroutineScope = backgroundScope,
timeline = timeline, timeline = timeline,
timelineItems = timelineItems, timelineItems = timelineItems,
onNewSyncedEvent = onNewSyncedEventRecorder, onNewSyncedEvent = onNewSyncedEventRecorder,
@ -94,7 +89,7 @@ class TimelineItemsSubscriberTest {
timelineItems.test { timelineItems.test {
timelineItemsSubscriber.subscribeIfNeeded() timelineItemsSubscriber.subscribeIfNeeded()
// Wait for the listener to be set. // Wait for the listener to be set.
testScope.runCurrent() runCurrent()
timeline.emitDiff( timeline.emitDiff(
listOf( listOf(
FakeRustTimelineDiff( FakeRustTimelineDiff(
@ -111,29 +106,27 @@ class TimelineItemsSubscriberTest {
} }
onNewSyncedEventRecorder.assertions().isCalledOnce() onNewSyncedEventRecorder.assertions().isCalledOnce()
} }
}
@Test @Test
fun `multiple subscriptions does not have side effect`() { fun `multiple subscriptions does not have side effect`() = runTest {
runCancellableScopeTestWithTestScope { testScope, cancellableScope -> val timelineItemsSubscriber = createTimelineItemsSubscriber(
val timelineItemsSubscriber = testScope.createTimelineItemsSubscriber( coroutineScope = backgroundScope,
coroutineScope = cancellableScope,
) )
timelineItemsSubscriber.subscribeIfNeeded() timelineItemsSubscriber.subscribeIfNeeded()
timelineItemsSubscriber.subscribeIfNeeded() timelineItemsSubscriber.subscribeIfNeeded()
timelineItemsSubscriber.unsubscribeIfNeeded() timelineItemsSubscriber.unsubscribeIfNeeded()
timelineItemsSubscriber.unsubscribeIfNeeded() timelineItemsSubscriber.unsubscribeIfNeeded()
} }
} }
private fun TestScope.createTimelineItemsSubscriber( private fun TestScope.createTimelineItemsSubscriber(
coroutineScope: CoroutineScope, coroutineScope: CoroutineScope,
timeline: Timeline = FakeRustTimeline(), timeline: Timeline = FakeRustTimeline(),
timelineItems: MutableSharedFlow<List<MatrixTimelineItem>> = MutableSharedFlow(replay = 1, extraBufferCapacity = Int.MAX_VALUE), timelineItems: MutableSharedFlow<List<MatrixTimelineItem>> = MutableSharedFlow(replay = 1, extraBufferCapacity = Int.MAX_VALUE),
initLatch: CompletableDeferred<Unit> = CompletableDeferred(), initLatch: CompletableDeferred<Unit> = CompletableDeferred(),
isTimelineInitialized: MutableStateFlow<Boolean> = MutableStateFlow(false), isTimelineInitialized: MutableStateFlow<Boolean> = MutableStateFlow(false),
onNewSyncedEvent: () -> Unit = { lambdaError() }, onNewSyncedEvent: () -> Unit = { lambdaError() },
): TimelineItemsSubscriber { ): TimelineItemsSubscriber {
return TimelineItemsSubscriber( return TimelineItemsSubscriber(
timelineCoroutineScope = coroutineScope, timelineCoroutineScope = coroutineScope,
dispatcher = StandardTestDispatcher(testScheduler), dispatcher = StandardTestDispatcher(testScheduler),
@ -143,5 +136,4 @@ class TimelineItemsSubscriberTest {
isTimelineInitialized = isTimelineInitialized, isTimelineInitialized = isTimelineInitialized,
onNewSyncedEvent = onNewSyncedEvent, onNewSyncedEvent = onNewSyncedEvent,
) )
}
} }

Loading…
Cancel
Save