From beeb77543b8135338e4774ce7bdf6c86d7913d1a Mon Sep 17 00:00:00 2001 From: Tangem Date: Tue, 21 Jul 2026 11:38:46 +0300 Subject: [PATCH] Updated on 2026-08-14 --- ...etPushNotificationPreferencesRepository.kt | 14 +++++++++- ...shNotificationPreferencesRepositoryTest.kt | 28 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/data/push-notification-preferences/src/main/kotlin/com/tangem/data/pushnotificationpreferences/DefaultWalletPushNotificationPreferencesRepository.kt b/data/push-notification-preferences/src/main/kotlin/com/tangem/data/pushnotificationpreferences/DefaultWalletPushNotificationPreferencesRepository.kt index e2b1ee4cc5..ba04307423 100644 --- a/data/push-notification-preferences/src/main/kotlin/com/tangem/data/pushnotificationpreferences/DefaultWalletPushNotificationPreferencesRepository.kt +++ b/data/push-notification-preferences/src/main/kotlin/com/tangem/data/pushnotificationpreferences/DefaultWalletPushNotificationPreferencesRepository.kt @@ -14,6 +14,8 @@ import com.tangem.domain.pushnotificationpreferences.models.PushNotificationCate import com.tangem.domain.pushnotificationpreferences.models.WalletPushNotificationPreferences import com.tangem.domain.pushnotificationpreferences.repository.WalletPushNotificationPreferencesRepository import com.tangem.utils.coroutines.CoroutineDispatcherProvider +import com.tangem.utils.coroutines.runSuspendCatching +import com.tangem.utils.logging.TangemLogger import java.util.concurrent.ConcurrentHashMap import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.distinctUntilChanged @@ -49,6 +51,16 @@ internal class DefaultWalletPushNotificationPreferencesRepository( } override suspend fun preload(userWalletId: UserWalletId) { + runSuspendCatching { preloadOrThrow(userWalletId) } + .onFailure { error -> + TangemLogger.e( + "Failed to preload push notification preferences for wallet ${userWalletId.stringValue}", + error, + ) + } + } + + private suspend fun preloadOrThrow(userWalletId: UserWalletId) { if (isCached(userWalletId)) return mutexFor(userWalletId).withLock { if (isCached(userWalletId)) return @@ -64,7 +76,7 @@ internal class DefaultWalletPushNotificationPreferencesRepository( } override fun observePreferences(userWalletId: UserWalletId): Flow = cache.get() - .onStart { preload(userWalletId) } + .onStart { preloadOrThrow(userWalletId) } .map { it[userWalletId.stringValue] } .filterNotNull() .distinctUntilChanged() diff --git a/data/push-notification-preferences/src/test/kotlin/com/tangem/data/pushnotificationpreferences/DefaultWalletPushNotificationPreferencesRepositoryTest.kt b/data/push-notification-preferences/src/test/kotlin/com/tangem/data/pushnotificationpreferences/DefaultWalletPushNotificationPreferencesRepositoryTest.kt index 60382fb811..4aee180627 100644 --- a/data/push-notification-preferences/src/test/kotlin/com/tangem/data/pushnotificationpreferences/DefaultWalletPushNotificationPreferencesRepositoryTest.kt +++ b/data/push-notification-preferences/src/test/kotlin/com/tangem/data/pushnotificationpreferences/DefaultWalletPushNotificationPreferencesRepositoryTest.kt @@ -76,6 +76,34 @@ class DefaultWalletPushNotificationPreferencesRepositoryTest { coVerify(exactly = 1) { tangemTechApi.getPushNotificationPreferences(userWalletId.stringValue) } } + @Test + fun `GIVEN server fails WHEN preload THEN does not throw AND does not cache`() = runTest { + // Arrange + coEvery { tangemTechApi.getPushNotificationPreferences(userWalletId.stringValue) } throws + IllegalStateException("boom") + + // Act + val thrown = runCatching { repository.preload(userWalletId) }.exceptionOrNull() + + // Assert + assertThat(thrown).isNull() + stubGet(userWalletId, transaction = true, offers = true, price = false) + repository.preload(userWalletId) + coVerify(exactly = 2) { tangemTechApi.getPushNotificationPreferences(userWalletId.stringValue) } + } + + @Test + fun `GIVEN server fails WHEN observePreferences THEN flow errors for collectors`() = runTest { + // Arrange + coEvery { tangemTechApi.getPushNotificationPreferences(userWalletId.stringValue) } throws + IllegalStateException("boom") + + // Act & Assert + repository.observePreferences(userWalletId).test { + assertThat(awaitError()).isInstanceOf(IllegalStateException::class.java) + } + } + @Test fun `GIVEN cache miss WHEN updatePreference THEN fetches baseline AND sends full-replace PUT AND caches echo`() = runTest {