From 3d80c690cc497b77acd8f3ae703f2ac036402701 Mon Sep 17 00:00:00 2001 From: yamilmedina Date: Thu, 14 Sep 2023 13:52:21 +0200 Subject: [PATCH 1/4] fix: add failure handling while remote be is offline for assests scheduler mechanism --- .../logic/feature/asset/ScheduleNewAssetMessageUseCase.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/asset/ScheduleNewAssetMessageUseCase.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/asset/ScheduleNewAssetMessageUseCase.kt index e9c8b2a31d8..e0598cbfb96 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/asset/ScheduleNewAssetMessageUseCase.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/asset/ScheduleNewAssetMessageUseCase.kt @@ -294,7 +294,9 @@ internal class ScheduleNewAssetMessageUseCaseImpl( expirationData = updatedMessage.expirationData, isSelfMessage = updatedMessage.isSelfMessage ) - messageSender.sendMessage(finalMessage) + messageSender.sendMessage(finalMessage).onFailure { + messageSendFailureHandler.handleFailureAndUpdateMessageStatus(it, conversationId, message.id, TYPE) + } } } From f1d80d0f2d8654a5e9c73ccbe1c6ed946bcd0db1 Mon Sep 17 00:00:00 2001 From: yamilmedina Date: Thu, 14 Sep 2023 14:46:02 +0200 Subject: [PATCH 2/4] fix: coverage --- .../ScheduleNewAssetMessageUseCaseTest.kt | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/asset/ScheduleNewAssetMessageUseCaseTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/asset/ScheduleNewAssetMessageUseCaseTest.kt index 81735b49b61..89602dabdc3 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/asset/ScheduleNewAssetMessageUseCaseTest.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/asset/ScheduleNewAssetMessageUseCaseTest.kt @@ -229,6 +229,57 @@ class ScheduleNewAssetMessageUseCaseTest { .wasInvoked(exactly = once) } + @Test + fun givenASuccessfulSendAssetMessageRequest_whenSendingTheAssetAndMessageFails_thenTheMessageStatusIsUpdatedToFailed() = + runTest(testDispatcher.default) { + // Given + val assetToSend = mockedLongAssetData() + val assetName = "some-asset.txt" + val conversationId = ConversationId("some-convo-id", "some-domain-id") + val dataPath = fakeKaliumFileSystem.providePersistentAssetPath(assetName) + val expectedAssetId = dummyUploadedAssetId + val expectedAssetSha256 = SHA256Key("some-asset-sha-256".toByteArray()) + val (arrangement, sendAssetUseCase) = Arrangement(this) + .withUnSuccessfulSendMessageResponse(expectedAssetId, expectedAssetSha256) + .withSelfDeleteTimer(SelfDeletionTimer.Disabled) + .withObserveMessageVisibility() + .withDeleteAssetLocally() + .arrange() + + // When + sendAssetUseCase.invoke( + conversationId = conversationId, + assetDataPath = dataPath, + assetDataSize = assetToSend.size.toLong(), + assetName = assetName, + assetMimeType = "text/plain", + assetWidth = null, + assetHeight = null, + audioLengthInMs = 0 + ) + + advanceUntilIdle() + + // Then + verify(arrangement.persistMessage) + .suspendFunction(arrangement.persistMessage::invoke) + .with(any()) + .wasInvoked(exactly = twice) + verify(arrangement.assetDataSource) + .suspendFunction(arrangement.assetDataSource::uploadAndPersistPrivateAsset) + .with(any(), any(), any(), any()) + .wasInvoked(exactly = once) + verify(arrangement.messageSender) + .suspendFunction(arrangement.messageSender::sendMessage) + .with(any()) + .wasInvoked(exactly = once) + + verify(arrangement.messageSendFailureHandler) + .suspendFunction(arrangement.messageSendFailureHandler::handleFailureAndUpdateMessageStatus) + .with(any(), any(), any(), any(), any()) + .wasInvoked(exactly = once) + } + @Test fun givenASuccessfulSendAssetMessageRequest_whenCheckingTheMessageRepository_thenTheAssetIsMarkedAsSavedInternally() = runTest(testDispatcher.default) { @@ -592,6 +643,41 @@ class ScheduleNewAssetMessageUseCaseTest { .thenReturn(UpdateUploadStatusResult.Success) } + fun withUnSuccessfulSendMessageResponse( + expectedAssetId: UploadedAssetId, + assetSHA256Key: SHA256Key, + temporaryAssetId: String = "temporary_id" + ): Arrangement = apply { + given(assetDataSource) + .suspendFunction(assetDataSource::persistAsset) + .whenInvokedWith(any(), any(), any(), any(), any()) + .thenReturn(Either.Right(fakeKaliumFileSystem.providePersistentAssetPath(temporaryAssetId))) + given(assetDataSource) + .suspendFunction(assetDataSource::uploadAndPersistPrivateAsset) + .whenInvokedWith(any(), any(), any(), any()) + .thenReturn(Either.Right(expectedAssetId to assetSHA256Key)) + given(currentClientIdProvider) + .suspendFunction(currentClientIdProvider::invoke) + .whenInvoked() + .thenReturn(Either.Right(someClientId)) + given(slowSyncRepository) + .getter(slowSyncRepository::slowSyncStatus) + .whenInvoked() + .thenReturn(completeStateFlow) + given(persistMessage) + .suspendFunction(persistMessage::invoke) + .whenInvokedWith(any()) + .thenReturn(Either.Right(Unit)) + given(messageSender) + .suspendFunction(messageSender::sendMessage) + .whenInvokedWith(any(), any()) + .thenReturn(Either.Left(CoreFailure.Unknown(RuntimeException("some error")))) + given(messageSendFailureHandler) + .suspendFunction(messageSendFailureHandler::handleFailureAndUpdateMessageStatus) + .whenInvokedWith(any(), any(), any(), any(), any()) + .thenReturn(Unit) + } + fun withUploadAssetErrorResponse(exception: KaliumException): Arrangement = apply { given(slowSyncRepository) .getter(slowSyncRepository::slowSyncStatus) From 97fb8ddd49e427cc3b89dd4101abcbf7dab1d3bc Mon Sep 17 00:00:00 2001 From: yamilmedina Date: Thu, 14 Sep 2023 14:47:59 +0200 Subject: [PATCH 3/4] fix: coverage --- .../feature/asset/ScheduleNewAssetMessageUseCaseTest.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/asset/ScheduleNewAssetMessageUseCaseTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/asset/ScheduleNewAssetMessageUseCaseTest.kt index 89602dabdc3..8869c2a8ef8 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/asset/ScheduleNewAssetMessageUseCaseTest.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/asset/ScheduleNewAssetMessageUseCaseTest.kt @@ -227,6 +227,10 @@ class ScheduleNewAssetMessageUseCaseTest { .suspendFunction(arrangement.messageSender::sendMessage) .with(any()) .wasInvoked(exactly = once) + verify(arrangement.messageSendFailureHandler) + .suspendFunction(arrangement.messageSendFailureHandler::handleFailureAndUpdateMessageStatus) + .with(any(), any(), any(), any(), any()) + .wasNotInvoked() } @Test @@ -273,7 +277,6 @@ class ScheduleNewAssetMessageUseCaseTest { .suspendFunction(arrangement.messageSender::sendMessage) .with(any()) .wasInvoked(exactly = once) - verify(arrangement.messageSendFailureHandler) .suspendFunction(arrangement.messageSendFailureHandler::handleFailureAndUpdateMessageStatus) .with(any(), any(), any(), any(), any()) From 9c0d2b9bc9548a0e04a6f299f35bf7cb72dc91a7 Mon Sep 17 00:00:00 2001 From: yamilmedina Date: Thu, 14 Sep 2023 15:43:37 +0200 Subject: [PATCH 4/4] fix: coverage --- .../logic/feature/asset/ScheduleNewAssetMessageUseCaseTest.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/asset/ScheduleNewAssetMessageUseCaseTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/asset/ScheduleNewAssetMessageUseCaseTest.kt index 8869c2a8ef8..aa847d77b3e 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/asset/ScheduleNewAssetMessageUseCaseTest.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/asset/ScheduleNewAssetMessageUseCaseTest.kt @@ -244,7 +244,7 @@ class ScheduleNewAssetMessageUseCaseTest { val expectedAssetId = dummyUploadedAssetId val expectedAssetSha256 = SHA256Key("some-asset-sha-256".toByteArray()) val (arrangement, sendAssetUseCase) = Arrangement(this) - .withUnSuccessfulSendMessageResponse(expectedAssetId, expectedAssetSha256) + .withUnsuccessfulSendMessageResponse(expectedAssetId, expectedAssetSha256) .withSelfDeleteTimer(SelfDeletionTimer.Disabled) .withObserveMessageVisibility() .withDeleteAssetLocally() @@ -646,7 +646,7 @@ class ScheduleNewAssetMessageUseCaseTest { .thenReturn(UpdateUploadStatusResult.Success) } - fun withUnSuccessfulSendMessageResponse( + fun withUnsuccessfulSendMessageResponse( expectedAssetId: UploadedAssetId, assetSHA256Key: SHA256Key, temporaryAssetId: String = "temporary_id"