From b263273c878806ba3f474380f438349c7f8b70ac Mon Sep 17 00:00:00 2001 From: Onuray Sahin Date: Wed, 16 Dec 2020 13:45:26 +0300 Subject: [PATCH] Improve test with detailed CryptoError message. --- .../android/sdk/common/CommonTestHelper.kt | 4 ++-- .../android/sdk/common/CryptoTestHelper.kt | 2 +- .../timeline/TimelineWithManyMembersTest.kt | 23 ++++++++++++------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/common/CommonTestHelper.kt b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/common/CommonTestHelper.kt index 0e7088a6a5..7dcf43ba0f 100644 --- a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/common/CommonTestHelper.kt +++ b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/common/CommonTestHelper.kt @@ -119,7 +119,7 @@ class CommonTestHelper(context: Context) { * @param message the message to send * @param nbOfMessages the number of time the message will be sent */ - fun sendTextMessage(room: Room, message: String, nbOfMessages: Int): List { + fun sendTextMessage(room: Room, message: String, nbOfMessages: Int, timeout: Long = TestConstants.timeOutMillis): List { val timeline = room.createTimeline(null, TimelineSettings(10)) val sentEvents = ArrayList(nbOfMessages) val latch = CountDownLatch(1) @@ -151,7 +151,7 @@ class CommonTestHelper(context: Context) { room.sendTextMessage(message + " #" + (i + 1)) } // Wait 3 second more per message - await(latch, timeout = TestConstants.timeOutMillis + 3_000L * nbOfMessages) + await(latch, timeout = timeout + 3_000L * nbOfMessages) timeline.dispose() // Check that all events has been created diff --git a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/common/CryptoTestHelper.kt b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/common/CryptoTestHelper.kt index 66fc1348d5..5d3407dde1 100644 --- a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/common/CryptoTestHelper.kt +++ b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/common/CryptoTestHelper.kt @@ -399,7 +399,7 @@ class CryptoTestHelper(private val mTestHelper: CommonTestHelper) { for (index in 1 until numberOfMembers) { mTestHelper .createAccount("User_$index", defaultSessionParams) - .also { session -> mTestHelper.doSync { room.invite(session.myUserId, null, it) } } + .also { session -> mTestHelper.doSync(timeout = 600_000) { room.invite(session.myUserId, null, it) } } .also { println("TEST -> " + it.myUserId + " invited") } .also { session -> mTestHelper.doSync { session.joinRoom(room.roomId, null, emptyList(), it) } } .also { println("TEST -> " + it.myUserId + " joined") } diff --git a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/session/room/timeline/TimelineWithManyMembersTest.kt b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/session/room/timeline/TimelineWithManyMembersTest.kt index 1a397c1d1c..dacde121b3 100644 --- a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/session/room/timeline/TimelineWithManyMembersTest.kt +++ b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/session/room/timeline/TimelineWithManyMembersTest.kt @@ -16,8 +16,6 @@ package org.matrix.android.sdk.session.room.timeline -import android.os.SystemClock -import android.util.Log import org.junit.FixMethodOrder import org.junit.Test import org.junit.runner.RunWith @@ -31,7 +29,7 @@ import org.matrix.android.sdk.api.session.room.timeline.TimelineSettings import org.matrix.android.sdk.common.CommonTestHelper import org.matrix.android.sdk.common.CryptoTestHelper import java.util.concurrent.CountDownLatch -import kotlin.test.assertEquals +import kotlin.test.fail @RunWith(JUnit4::class) @FixMethodOrder(MethodSorters.JVM) @@ -111,7 +109,9 @@ class TimelineWithManyMembersTest : InstrumentedTest { commonTestHelper.sendTextMessage( roomForFirstMember, firstMessage, - 1) + 1, + 600_000 + ) for (index in 1 until cryptoTestData.sessions.size) { val session = cryptoTestData.sessions[index] @@ -124,12 +124,19 @@ class TimelineWithManyMembersTest : InstrumentedTest { run { val lock = CountDownLatch(1) val eventsListener = commonTestHelper.createEventListener(lock) { snapshot -> - val decryptedMessage = snapshot.firstOrNull()?.root?.getClearContent()?.toModel()?.body - println("Decrypted Message: $decryptedMessage") - return@createEventListener decryptedMessage?.startsWith(firstMessage).orFalse() + snapshot + .find { it.isEncrypted() } + ?.let { + val body = it.root.getClearContent()?.toModel()?.body + if (body?.startsWith(firstMessage).orFalse()) { + return@createEventListener true + } else { + fail("User " + session.myUserId + " decrypted as " + body + " CryptoError: " + it.root.mCryptoError) + } + } ?: return@createEventListener false } timelineForCurrentMember.addListener(eventsListener) - commonTestHelper.await(lock) + commonTestHelper.await(lock, 600_000) } session.stopSync() }