Skip to content

Commit

Permalink
Close mozilla-mobile#7236: Fix last verification check in AutoPushFea…
Browse files Browse the repository at this point in the history
…ture

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.
  • Loading branch information
jonalmeida committed Jun 4, 2020
1 parent ba7a238 commit fc15596
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ permalink: /changelog/
* **browser-icons**
* Fixed issue [#7142](https://github.com/mozilla-mobile/android-components/issues/7142)

* **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)
Expand Down

0 comments on commit fc15596

Please sign in to comment.