From de6f2220936693e82edac575d390c883e79eb260 Mon Sep 17 00:00:00 2001 From: Jonathan Almeida Date: Thu, 4 Jun 2020 01:04:51 -0400 Subject: [PATCH] Close #7236: Fix last verification check in AutoPushFeature When we first access the `lastVerified` pref we do not have an value stored so we get the value from the current time. The first usage is where we compare the delta of the current time with the `lastVerified` so this lead us to have a negative value which fails. --- .../feature/push/AutoPushFeature.kt | 2 +- .../feature/push/AutoPushFeatureTest.kt | 25 +++++++++++++++++-- docs/changelog.md | 3 +++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/components/feature/push/src/main/java/mozilla/components/feature/push/AutoPushFeature.kt b/components/feature/push/src/main/java/mozilla/components/feature/push/AutoPushFeature.kt index 48e59dbe115..610922d7623 100644 --- a/components/feature/push/src/main/java/mozilla/components/feature/push/AutoPushFeature.kt +++ b/components/feature/push/src/main/java/mozilla/components/feature/push/AutoPushFeature.kt @@ -85,7 +85,7 @@ class AutoPushFeature( private val prefToken: String? get() = preferences(context).getString(PREF_TOKEN, null) private var prefLastVerified: Long - get() = preferences(context).getLong(LAST_VERIFIED, System.currentTimeMillis()) + get() = preferences(context).getLong(LAST_VERIFIED, 0) set(value) = preferences(context).edit().putLong(LAST_VERIFIED, value).apply() private val coroutineScope = CoroutineScope(coroutineContext) + SupervisorJob() + exceptionHandler { onError(it) } diff --git a/components/feature/push/src/test/java/mozilla/components/feature/push/AutoPushFeatureTest.kt b/components/feature/push/src/test/java/mozilla/components/feature/push/AutoPushFeatureTest.kt index 894e8cb32a1..df5bb339646 100644 --- a/components/feature/push/src/test/java/mozilla/components/feature/push/AutoPushFeatureTest.kt +++ b/components/feature/push/src/test/java/mozilla/components/feature/push/AutoPushFeatureTest.kt @@ -26,6 +26,7 @@ import mozilla.components.support.test.mock import mozilla.components.support.test.nullable import mozilla.components.support.test.robolectric.testContext import mozilla.components.support.test.whenever +import org.junit.After import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNotNull @@ -54,11 +55,14 @@ class AutoPushFeatureTest { @Before fun setup() { - lastVerified = 0L - whenever(connection.isInitialized()).thenReturn(true) } + @After + fun shutdown() { + preference(testContext).edit().remove(LAST_VERIFIED).apply() + } + @Test fun `initialize starts push service`() { val service: PushService = mock() @@ -407,6 +411,23 @@ class AutoPushFeatureTest { verify(feature, never()).verifyActiveSubscriptions() } + @Test + fun `verification always happens on first attempt`() = runBlockingTest { + val feature = spy( + AutoPushFeature( + context = testContext, + service = mock(), + config = mock(), + coroutineContext = coroutineContext, + connection = mock() + ) + ) + + feature.initialize() + + verify(feature).verifyActiveSubscriptions() + } + @Test fun `crash reporter is notified of errors`() = runBlockingTest { val native: PushConnection = TestPushConnection(true) diff --git a/docs/changelog.md b/docs/changelog.md index f40d9c73de7..5c41827e4b5 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -31,6 +31,9 @@ permalink: /changelog/ * **support-rustlog** * `RustLog.enable` now takes an optional [CrashReporting] instance which is used to submit error-level log messages as `RustErrorException`s. +* **feature-push** + * Fixed a bug where we do not verify subscriptions on first attempt. + # 44.0.0 * [Commits](https://github.com/mozilla-mobile/android-components/compare/v43.0.0...v44.0.0)