From 7f31098ba192e2c17aa699c1e87dcc46e249e0e0 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 21 Sep 2022 14:25:26 +0100 Subject: [PATCH 1/4] lifting the sync timeout to the matrix configuration --- .../android/sdk/api/MatrixConfiguration.kt | 4 ++++ .../org/matrix/android/sdk/api/SyncConfig.kt | 24 +++++++++++++++++++ .../internal/session/sync/job/SyncThread.kt | 7 +++--- 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/SyncConfig.kt diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/MatrixConfiguration.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/MatrixConfiguration.kt index 893e90fb3e..3b4b3b624d 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/MatrixConfiguration.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/MatrixConfiguration.kt @@ -70,4 +70,8 @@ data class MatrixConfiguration( * List of network interceptors, they will be added when building an OkHttp client. */ val networkInterceptors: List = emptyList(), + /** + * Sync configuration. + */ + val syncConfig: SyncConfig = SyncConfig() ) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/SyncConfig.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/SyncConfig.kt new file mode 100644 index 0000000000..b5547896d4 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/SyncConfig.kt @@ -0,0 +1,24 @@ +/* + * Copyright 2022 The Matrix.org Foundation C.I.C. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.matrix.android.sdk.api + +data class SyncConfig( + /** + * Time to keep sync connection alive for before making another request in milliseconds. + */ + val longPollTimeout: Long = 30_000L +) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/job/SyncThread.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/job/SyncThread.kt index b47b215655..d3f2a3f044 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/job/SyncThread.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/job/SyncThread.kt @@ -30,6 +30,7 @@ import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking +import org.matrix.android.sdk.api.MatrixConfiguration import org.matrix.android.sdk.api.extensions.orFalse import org.matrix.android.sdk.api.failure.Failure import org.matrix.android.sdk.api.failure.isTokenError @@ -52,7 +53,6 @@ import javax.inject.Inject import kotlin.concurrent.schedule private const val RETRY_WAIT_TIME_MS = 10_000L -private const val DEFAULT_LONG_POOL_TIMEOUT = 30_000L private val loggerTag = LoggerTag("SyncThread", LoggerTag.SYNC) @@ -61,7 +61,8 @@ internal class SyncThread @Inject constructor( private val networkConnectivityChecker: NetworkConnectivityChecker, private val backgroundDetectionObserver: BackgroundDetectionObserver, private val activeCallHandler: ActiveCallHandler, - private val lightweightSettingsStorage: DefaultLightweightSettingsStorage + private val lightweightSettingsStorage: DefaultLightweightSettingsStorage, + private val matrixConfiguration: MatrixConfiguration, ) : Thread("Matrix-SyncThread"), NetworkConnectivityChecker.Listener, BackgroundDetectionObserver.Listener { private var state: SyncState = SyncState.Idle @@ -181,7 +182,7 @@ internal class SyncThread @Inject constructor( val timeout = when { previousSyncResponseHasToDevice -> 0L /* Force timeout to 0 */ afterPause -> 0L /* No timeout after a pause */ - else -> DEFAULT_LONG_POOL_TIMEOUT + else -> matrixConfiguration.syncConfig.longPollTimeout } Timber.tag(loggerTag.value).d("Execute sync request with timeout $timeout") val presence = lightweightSettingsStorage.getSyncPresenceStatus() From 9ab78c93e2e8fe4ed618926473d734ad65906175 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 21 Sep 2022 14:36:27 +0100 Subject: [PATCH 2/4] reducing sync timeout for instrumentation tests to speed them up --- .../java/org/matrix/android/sdk/common/CommonTestHelper.kt | 4 +++- .../java/im/vector/app/core/utils/TestMatrixHelper.kt | 4 +++- 2 files changed, 6 insertions(+), 2 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 a78953caac..d30cee0eab 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 @@ -36,6 +36,7 @@ import org.junit.Assert.assertNotNull import org.junit.Assert.assertTrue import org.matrix.android.sdk.api.MatrixCallback import org.matrix.android.sdk.api.MatrixConfiguration +import org.matrix.android.sdk.api.SyncConfig import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig import org.matrix.android.sdk.api.auth.registration.RegistrationResult import org.matrix.android.sdk.api.session.Session @@ -103,7 +104,8 @@ class CommonTestHelper internal constructor(context: Context) { context, MatrixConfiguration( applicationFlavor = "TestFlavor", - roomDisplayNameFallbackProvider = TestRoomDisplayNameFallbackProvider() + roomDisplayNameFallbackProvider = TestRoomDisplayNameFallbackProvider(), + syncConfig = SyncConfig(longPollTimeout = 5_000L) ) ) } diff --git a/vector-app/src/androidTest/java/im/vector/app/core/utils/TestMatrixHelper.kt b/vector-app/src/androidTest/java/im/vector/app/core/utils/TestMatrixHelper.kt index 48fc1343b1..9bdb0f863a 100644 --- a/vector-app/src/androidTest/java/im/vector/app/core/utils/TestMatrixHelper.kt +++ b/vector-app/src/androidTest/java/im/vector/app/core/utils/TestMatrixHelper.kt @@ -20,11 +20,13 @@ import androidx.test.platform.app.InstrumentationRegistry import im.vector.app.features.room.VectorRoomDisplayNameFallbackProvider import org.matrix.android.sdk.api.Matrix import org.matrix.android.sdk.api.MatrixConfiguration +import org.matrix.android.sdk.api.SyncConfig fun getMatrixInstance(): Matrix { val context = InstrumentationRegistry.getInstrumentation().targetContext val configuration = MatrixConfiguration( - roomDisplayNameFallbackProvider = VectorRoomDisplayNameFallbackProvider(context) + roomDisplayNameFallbackProvider = VectorRoomDisplayNameFallbackProvider(context), + syncConfig = SyncConfig(longPollTimeout = 5_000L) ) return Matrix(context, configuration) } From d75e37966c318058ca399b99a1ae099e384f93d8 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 21 Sep 2022 15:37:16 +0100 Subject: [PATCH 3/4] adding changelog entry --- changelog.d/7198.sdk | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/7198.sdk diff --git a/changelog.d/7198.sdk b/changelog.d/7198.sdk new file mode 100644 index 0000000000..115b8d6113 --- /dev/null +++ b/changelog.d/7198.sdk @@ -0,0 +1 @@ +Allow the sync timeout to be configured (mainly useful for testing) From 600588dbbba66b736e9b659b735e85de56035d24 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 21 Sep 2022 16:37:31 +0100 Subject: [PATCH 4/4] adding trailing commas --- .../java/org/matrix/android/sdk/common/CommonTestHelper.kt | 2 +- .../main/java/org/matrix/android/sdk/api/MatrixConfiguration.kt | 2 +- .../src/main/java/org/matrix/android/sdk/api/SyncConfig.kt | 2 +- .../java/im/vector/app/core/utils/TestMatrixHelper.kt | 2 +- 4 files changed, 4 insertions(+), 4 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 d30cee0eab..b179c6027e 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 @@ -105,7 +105,7 @@ class CommonTestHelper internal constructor(context: Context) { MatrixConfiguration( applicationFlavor = "TestFlavor", roomDisplayNameFallbackProvider = TestRoomDisplayNameFallbackProvider(), - syncConfig = SyncConfig(longPollTimeout = 5_000L) + syncConfig = SyncConfig(longPollTimeout = 5_000L), ) ) } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/MatrixConfiguration.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/MatrixConfiguration.kt index 3b4b3b624d..7119563617 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/MatrixConfiguration.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/MatrixConfiguration.kt @@ -73,5 +73,5 @@ data class MatrixConfiguration( /** * Sync configuration. */ - val syncConfig: SyncConfig = SyncConfig() + val syncConfig: SyncConfig = SyncConfig(), ) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/SyncConfig.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/SyncConfig.kt index b5547896d4..a9753e2407 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/SyncConfig.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/SyncConfig.kt @@ -20,5 +20,5 @@ data class SyncConfig( /** * Time to keep sync connection alive for before making another request in milliseconds. */ - val longPollTimeout: Long = 30_000L + val longPollTimeout: Long = 30_000L, ) diff --git a/vector-app/src/androidTest/java/im/vector/app/core/utils/TestMatrixHelper.kt b/vector-app/src/androidTest/java/im/vector/app/core/utils/TestMatrixHelper.kt index 9bdb0f863a..d8873a71a4 100644 --- a/vector-app/src/androidTest/java/im/vector/app/core/utils/TestMatrixHelper.kt +++ b/vector-app/src/androidTest/java/im/vector/app/core/utils/TestMatrixHelper.kt @@ -26,7 +26,7 @@ fun getMatrixInstance(): Matrix { val context = InstrumentationRegistry.getInstrumentation().targetContext val configuration = MatrixConfiguration( roomDisplayNameFallbackProvider = VectorRoomDisplayNameFallbackProvider(context), - syncConfig = SyncConfig(longPollTimeout = 5_000L) + syncConfig = SyncConfig(longPollTimeout = 5_000L), ) return Matrix(context, configuration) }