From c6a493848e87b623ccf36730cde23278e652c7e9 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 16 Apr 2021 16:13:16 +0200 Subject: [PATCH] Fix a race condition: Push can be received before the Gateway API returns --- .../sdk/api/session/pushers/PushersService.kt | 5 +++-- .../troubleshoot/TestPushFromPushGateway.kt | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/pushers/PushersService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/pushers/PushersService.kt index 9ea820f5b3..a5ec100f64 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/pushers/PushersService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/pushers/PushersService.kt @@ -66,12 +66,13 @@ interface PushersService { /** * Directly ask the push gateway to send a push to this device + * If successful, the push gateway has accepted the request. In this case, the app should receive a Push with the provided eventId. + * In case of error, PusherRejected will be thrown. In this case it means that the pushkey is not valid. + * * @param url the push gateway url (full path) * @param appId the application id * @param pushkey the FCM token * @param eventId the eventId which will be sent in the Push message. Use a fake eventId. - * @param callback callback to know if the push gateway has accepted the request. In this case, the app should receive a Push with the provided eventId. - * In case of error, PusherRejected failure can happen. In this case it means that the pushkey is not valid. */ suspend fun testPush(url: String, appId: String, diff --git a/vector/src/gplay/java/im/vector/app/gplay/features/settings/troubleshoot/TestPushFromPushGateway.kt b/vector/src/gplay/java/im/vector/app/gplay/features/settings/troubleshoot/TestPushFromPushGateway.kt index ad4b9ecb01..15c7e88bac 100644 --- a/vector/src/gplay/java/im/vector/app/gplay/features/settings/troubleshoot/TestPushFromPushGateway.kt +++ b/vector/src/gplay/java/im/vector/app/gplay/features/settings/troubleshoot/TestPushFromPushGateway.kt @@ -42,8 +42,10 @@ class TestPushFromPushGateway @Inject constructor(private val context: AppCompat : TroubleshootTest(R.string.settings_troubleshoot_test_push_loop_title) { private var action: Job? = null + private var pushReceived: Boolean = false override fun perform(activityResultLauncher: ActivityResultLauncher) { + pushReceived = false val fcmToken = FcmHelper.getFcmToken(context) ?: run { status = TestStatus.FAILED return @@ -55,9 +57,15 @@ class TestPushFromPushGateway @Inject constructor(private val context: AppCompat status = result .fold( { - // Wait for the push to be received - description = stringProvider.getString(R.string.settings_troubleshoot_test_push_loop_waiting_for_push) - TestStatus.RUNNING + if (pushReceived) { + // Push already received (race condition) + description = stringProvider.getString(R.string.settings_troubleshoot_test_push_loop_success) + TestStatus.SUCCESS + } else { + // Wait for the push to be received + description = stringProvider.getString(R.string.settings_troubleshoot_test_push_loop_waiting_for_push) + TestStatus.RUNNING + } }, { description = if (it is PushGatewayFailure.PusherRejected) { @@ -73,6 +81,7 @@ class TestPushFromPushGateway @Inject constructor(private val context: AppCompat } override fun onPushReceived() { + pushReceived = true description = stringProvider.getString(R.string.settings_troubleshoot_test_push_loop_success) status = TestStatus.SUCCESS }