Skip to content

Commit 1d9da6c

Browse files
author
ganfra
committed
Tests: do some clean-up and fix bunch of them
1 parent 69720ff commit 1d9da6c

File tree

16 files changed

+607
-731
lines changed

16 files changed

+607
-731
lines changed

matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/common/CommonTestHelper.kt

+74-58
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import android.content.Context
2020
import android.net.Uri
2121
import androidx.lifecycle.Observer
2222
import androidx.test.internal.runner.junit4.statement.UiThreadStatement
23+
import kotlinx.coroutines.CoroutineScope
2324
import kotlinx.coroutines.Dispatchers
24-
import kotlinx.coroutines.GlobalScope
25+
import kotlinx.coroutines.SupervisorJob
2526
import kotlinx.coroutines.delay
2627
import kotlinx.coroutines.launch
2728
import kotlinx.coroutines.runBlocking
@@ -55,6 +56,7 @@ import java.util.concurrent.TimeUnit
5556
class CommonTestHelper(context: Context) {
5657

5758
val matrix: TestMatrix
59+
val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
5860

5961
fun getTestInterceptor(session: Session): MockOkHttpInterceptor? = TestModule.interceptorForSession(session.sessionId) as? MockOkHttpInterceptor
6062

@@ -93,30 +95,45 @@ class CommonTestHelper(context: Context) {
9395
*
9496
* @param session the session to sync
9597
*/
96-
@Suppress("EXPERIMENTAL_API_USAGE")
97-
fun syncSession(session: Session, timeout: Long = TestConstants.timeOutMillis) {
98+
fun syncSession(session: Session, timeout: Long = TestConstants.timeOutMillis * 10) {
9899
val lock = CountDownLatch(1)
99-
100-
val job = GlobalScope.launch(Dispatchers.Main) {
101-
session.open()
100+
coroutineScope.launch {
101+
session.startSync(true)
102+
val syncLiveData = session.getSyncStateLive()
103+
val syncObserver = object : Observer<SyncState> {
104+
override fun onChanged(t: SyncState?) {
105+
if (session.hasAlreadySynced()) {
106+
lock.countDown()
107+
syncLiveData.removeObserver(this)
108+
}
109+
}
110+
}
111+
syncLiveData.observeForever(syncObserver)
102112
}
103-
runBlocking { job.join() }
104-
105-
session.startSync(true)
113+
await(lock, timeout)
114+
}
106115

107-
val syncLiveData = runBlocking(Dispatchers.Main) {
108-
session.getSyncStateLive()
109-
}
110-
val syncObserver = object : Observer<SyncState> {
111-
override fun onChanged(t: SyncState?) {
112-
if (session.hasAlreadySynced()) {
113-
lock.countDown()
114-
syncLiveData.removeObserver(this)
116+
/**
117+
* This methods clear the cache and waits for initialSync
118+
*
119+
* @param session the session to sync
120+
*/
121+
fun clearCacheAndSync(session: Session, timeout: Long = TestConstants.timeOutMillis) {
122+
val lock = CountDownLatch(1)
123+
coroutineScope.launch {
124+
session.clearCache()
125+
val syncLiveData = session.getSyncStateLive()
126+
val syncObserver = object : Observer<SyncState> {
127+
override fun onChanged(t: SyncState?) {
128+
if (session.hasAlreadySynced()) {
129+
lock.countDown()
130+
syncLiveData.removeObserver(this)
131+
}
115132
}
116133
}
134+
syncLiveData.observeForever(syncObserver)
135+
session.startSync(true)
117136
}
118-
GlobalScope.launch(Dispatchers.Main) { syncLiveData.observeForever(syncObserver) }
119-
120137
await(lock, timeout)
121138
}
122139

@@ -130,41 +147,40 @@ class CommonTestHelper(context: Context) {
130147
fun sendTextMessage(room: Room, message: String, nbOfMessages: Int, timeout: Long = TestConstants.timeOutMillis): List<TimelineEvent> {
131148
val timeline = room.createTimeline(null, TimelineSettings(10))
132149
val sentEvents = ArrayList<TimelineEvent>(nbOfMessages)
133-
val latch = CountDownLatch(1)
134-
val timelineListener = object : Timeline.Listener {
135-
override fun onTimelineFailure(throwable: Throwable) {
136-
}
150+
waitWithLatch(timeout ) { latch ->
151+
val timelineListener = object : Timeline.Listener {
152+
override fun onTimelineFailure(throwable: Throwable) {
153+
}
137154

138-
override fun onNewTimelineEvents(eventIds: List<String>) {
139-
// noop
140-
}
155+
override fun onNewTimelineEvents(eventIds: List<String>) {
156+
// noop
157+
}
141158

142-
override fun onTimelineUpdated(snapshot: List<TimelineEvent>) {
143-
val newMessages = snapshot
144-
.filter { it.root.sendState == SendState.SYNCED }
145-
.filter { it.root.getClearType() == EventType.MESSAGE }
146-
.filter { it.root.getClearContent().toModel<MessageContent>()?.body?.startsWith(message) == true }
147-
148-
if (newMessages.size == nbOfMessages) {
149-
sentEvents.addAll(newMessages)
150-
// Remove listener now, if not at the next update sendEvents could change
151-
timeline.removeListener(this)
152-
latch.countDown()
159+
override fun onTimelineUpdated(snapshot: List<TimelineEvent>) {
160+
val newMessages = snapshot
161+
.filter { it.root.sendState == SendState.SYNCED }
162+
.filter { it.root.getClearType() == EventType.MESSAGE }
163+
.filter { it.root.getClearContent().toModel<MessageContent>()?.body?.startsWith(message) == true }
164+
165+
if (newMessages.size == nbOfMessages) {
166+
sentEvents.addAll(newMessages)
167+
// Remove listener now, if not at the next update sendEvents could change
168+
timeline.removeListener(this)
169+
latch.countDown()
170+
}
153171
}
154172
}
173+
timeline.start()
174+
timeline.addListener(timelineListener)
175+
for (i in 0 until nbOfMessages) {
176+
room.sendTextMessage(message + " #" + (i + 1))
177+
// Sleep a bit otherwise database will be flowed and sync won't be live (we might end up with gap then...)
178+
delay(50)
179+
}
155180
}
156-
timeline.start()
157-
timeline.addListener(timelineListener)
158-
for (i in 0 until nbOfMessages) {
159-
room.sendTextMessage(message + " #" + (i + 1))
160-
}
161-
// Wait 3 second more per message
162-
await(latch, timeout = timeout + 3_000L * nbOfMessages)
163181
timeline.dispose()
164-
165182
// Check that all events has been created
166183
assertEquals("Message number do not match $sentEvents", nbOfMessages.toLong(), sentEvents.size.toLong())
167-
168184
return sentEvents
169185
}
170186

@@ -237,10 +253,10 @@ class CommonTestHelper(context: Context) {
237253

238254
assertTrue(registrationResult is RegistrationResult.Success)
239255
val session = (registrationResult as RegistrationResult.Success).session
256+
session.open()
240257
if (sessionTestParams.withInitialSync) {
241258
syncSession(session, 60_000)
242259
}
243-
244260
return session
245261
}
246262

@@ -265,7 +281,7 @@ class CommonTestHelper(context: Context) {
265281
.getLoginWizard()
266282
.login(userName, password, "myDevice")
267283
}
268-
284+
session.open()
269285
if (sessionTestParams.withInitialSync) {
270286
syncSession(session)
271287
}
@@ -331,21 +347,21 @@ class CommonTestHelper(context: Context) {
331347
}
332348

333349
@Suppress("EXPERIMENTAL_API_USAGE")
334-
fun retryPeriodicallyWithLatch(latch: CountDownLatch, condition: (() -> Boolean)) {
335-
GlobalScope.launch {
336-
while (true) {
337-
delay(1000)
338-
if (condition()) {
339-
latch.countDown()
340-
return@launch
341-
}
350+
suspend fun retryPeriodicallyWithLatch(latch: CountDownLatch, condition: (() -> Boolean)) {
351+
while (true) {
352+
delay(1000)
353+
if (condition()) {
354+
latch.countDown()
355+
return
342356
}
343357
}
344358
}
345359

346-
fun waitWithLatch(timeout: Long? = TestConstants.timeOutMillis, block: (CountDownLatch) -> Unit) {
360+
fun waitWithLatch(timeout: Long? = TestConstants.timeOutMillis, block: suspend (CountDownLatch) -> Unit) {
347361
val latch = CountDownLatch(1)
348-
block(latch)
362+
coroutineScope.launch {
363+
block(latch)
364+
}
349365
await(latch, timeout)
350366
}
351367

0 commit comments

Comments
 (0)