Test: use this new syncOnce in tests

This commit is contained in:
ganfra 2022-07-11 19:10:09 +02:00
parent 4c2b28075c
commit 28a79b4672
2 changed files with 26 additions and 48 deletions

View File

@ -51,7 +51,6 @@ import org.matrix.android.sdk.api.session.room.timeline.Timeline
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
import org.matrix.android.sdk.api.session.room.timeline.TimelineSettings import org.matrix.android.sdk.api.session.room.timeline.TimelineSettings
import org.matrix.android.sdk.api.session.sync.SyncState import org.matrix.android.sdk.api.session.sync.SyncState
import timber.log.Timber
import java.util.UUID import java.util.UUID
import java.util.concurrent.CancellationException import java.util.concurrent.CancellationException
import java.util.concurrent.CountDownLatch import java.util.concurrent.CountDownLatch
@ -142,42 +141,8 @@ class CommonTestHelper internal constructor(context: Context) {
* @param session the session to sync * @param session the session to sync
*/ */
fun syncSession(session: Session, timeout: Long = TestConstants.timeOutMillis * 10) { fun syncSession(session: Session, timeout: Long = TestConstants.timeOutMillis * 10) {
val lock = CountDownLatch(1) runBlockingTest(timeout) {
coroutineScope.launch { session.syncService().syncOnce(0L)
session.syncService().startSync(true)
val syncLiveData = session.syncService().getSyncStateLive()
val syncObserver = object : Observer<SyncState> {
override fun onChanged(t: SyncState?) {
if (session.syncService().hasAlreadySynced()) {
lock.countDown()
syncLiveData.removeObserver(this)
}
}
}
syncLiveData.observeForever(syncObserver)
}
await(lock, timeout)
}
/**
* This methods clear the cache and waits for initialSync
*
* @param session the session to sync
*/
fun clearCacheAndSync(session: Session, timeout: Long = TestConstants.timeOutMillis) {
waitWithLatch(timeout) { latch ->
session.clearCache()
val syncLiveData = session.syncService().getSyncStateLive()
val syncObserver = object : Observer<SyncState> {
override fun onChanged(t: SyncState?) {
if (session.syncService().hasAlreadySynced()) {
Timber.v("Clear cache and synced")
syncLiveData.removeObserver(this)
latch.countDown()
}
}
}
syncLiveData.observeForever(syncObserver)
session.syncService().startSync(true) session.syncService().startSync(true)
} }
} }

View File

@ -17,10 +17,13 @@
package org.matrix.android.sdk.session.room.timeline package org.matrix.android.sdk.session.room.timeline
import androidx.test.filters.LargeTest import androidx.test.filters.LargeTest
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import org.amshove.kluent.internal.assertEquals import org.amshove.kluent.internal.assertEquals
import org.amshove.kluent.shouldBe
import org.amshove.kluent.shouldBeFalse import org.amshove.kluent.shouldBeFalse
import org.amshove.kluent.shouldBeTrue import org.amshove.kluent.shouldBeTrue
import org.amshove.kluent.shouldNotBe
import org.junit.FixMethodOrder import org.junit.FixMethodOrder
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
@ -31,6 +34,7 @@ import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.session.events.model.EventType import org.matrix.android.sdk.api.session.events.model.EventType
import org.matrix.android.sdk.api.session.events.model.toModel import org.matrix.android.sdk.api.session.events.model.toModel
import org.matrix.android.sdk.api.session.getRoom import org.matrix.android.sdk.api.session.getRoom
import org.matrix.android.sdk.api.session.room.model.create.CreateRoomParams
import org.matrix.android.sdk.api.session.room.model.message.MessageContent import org.matrix.android.sdk.api.session.room.model.message.MessageContent
import org.matrix.android.sdk.api.session.room.timeline.Timeline import org.matrix.android.sdk.api.session.room.timeline.Timeline
import org.matrix.android.sdk.api.session.room.timeline.TimelineSettings import org.matrix.android.sdk.api.session.room.timeline.TimelineSettings
@ -61,28 +65,38 @@ class TimelineForwardPaginationTest : InstrumentedTest {
aliceSession.cryptoService().setWarnOnUnknownDevices(false) aliceSession.cryptoService().setWarnOnUnknownDevices(false)
val roomFromAlicePOV = aliceSession.getRoom(aliceRoomId)!!
// Alice sends X messages // Alice sends X messages
val message = "Message from Alice" val message = "Message from Alice"
val sentMessages = commonTestHelper.sendTextMessage( val sentMessages = commonTestHelper.sendTextMessage(
roomFromAlicePOV, aliceSession.getRoom(aliceRoomId)!!,
message, message,
numberOfMessagesToSend numberOfMessagesToSend
) )
Timber.tag("TimelineTest").v("Sent ${sentMessages.size} messages")
// Alice clear the cache and restart the sync // Alice clear the cache and restart the sync
commonTestHelper.clearCacheAndSync(aliceSession) commonTestHelper.runBlockingTest {
val aliceTimeline = roomFromAlicePOV.timelineService().createTimeline(null, TimelineSettings(30)) aliceSession.getRoom(aliceRoomId) shouldNotBe null
Timber.tag("TimelineTest").v("Clear cache")
aliceSession.clearCache()
aliceSession.getRoom(aliceRoomId) shouldBe null
Timber.tag("TimelineTest").v("Launch initial sync")
aliceSession.syncService().syncOnce(0L)
aliceSession.getRoom(aliceRoomId) shouldNotBe null
}
val aliceRoom = aliceSession.getRoom(aliceRoomId)!!
Timber.tag("TimelineTest").v("Got roomSummary : ${aliceRoom.roomSummary()}")
val aliceTimeline = aliceRoom.timelineService().createTimeline(null, TimelineSettings(30))
aliceTimeline.start() aliceTimeline.start()
Timber.tag("TimelineTest").v("Start timeline with initialSize of 30.")
// Alice sees the 10 last message of the room, and can only navigate BACKWARD // Alice sees the 10 last message of the room, and can only navigate BACKWARD
run { run {
val lock = CountDownLatch(1) val lock = CountDownLatch(1)
val eventsListener = commonTestHelper.createEventListener(lock) { snapshot -> val eventsListener = commonTestHelper.createEventListener(lock) { snapshot ->
Timber.e("Alice timeline updated: with ${snapshot.size} events:") Timber.tag("TimelineTest").v("Alice timeline updated: with ${snapshot.size} events:")
snapshot.forEach { snapshot.forEach {
Timber.w(" event ${it.root.content}") Timber.tag("TimelineTest").v(" event ${it.root.content}")
} }
// Ok, we have the 10 last messages of the initial sync // Ok, we have the 10 last messages of the initial sync
@ -104,11 +118,10 @@ class TimelineForwardPaginationTest : InstrumentedTest {
run { run {
val lock = CountDownLatch(1) val lock = CountDownLatch(1)
val aliceEventsListener = commonTestHelper.createEventListener(lock) { snapshot -> val aliceEventsListener = commonTestHelper.createEventListener(lock) { snapshot ->
Timber.e("Alice timeline updated: with ${snapshot.size} events:") Timber.tag("TimelineTest").v("#restartWithEventId Alice timeline updated: with ${snapshot.size} events:")
snapshot.forEach { snapshot.forEach {
Timber.w(" event ${it.root.content}") Timber.tag("TimelineTest").v(" event ${it.root.content}")
} }
// The event is not in db, so it is fetch alone // The event is not in db, so it is fetch alone
snapshot.size == 1 && snapshot.size == 1 &&
snapshot.all { it.root.content.toModel<MessageContent>()?.body?.startsWith("Message from Alice").orFalse() } snapshot.all { it.root.content.toModel<MessageContent>()?.body?.startsWith("Message from Alice").orFalse() }