Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Treat no push subscription as though it had expired, and poll for mis…
Browse files Browse the repository at this point in the history
…sed commands in both cases
  • Loading branch information
mhammond authored and mergify[bot] committed Dec 10, 2020
1 parent c6d2a41 commit 30e3ea9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ internal class ConstellationObserver(

override fun onDevicesUpdate(constellation: ConstellationState) {
logger.info("onDevicesUpdate triggered.")
val updateSubscription = constellation.currentDevice?.subscriptionExpired ?: false
val updateSubscription = constellation.currentDevice?.let {
it.subscription == null || it.subscriptionExpired
} ?: false

// If our subscription has not expired, we do nothing.
// If our last check was recent (see: PERIODIC_INTERVAL_MILLISECONDS), we do nothing.
Expand All @@ -204,7 +206,7 @@ internal class ConstellationObserver(
logger.info("Proceeding to renew registration")
}

logger.info("We have been notified that our push subscription has expired; re-subscribing.")
logger.info("Our push subscription either doesn't exist or is expired; re-subscribing.")
push.unsubscribe(
scope = scope,
onUnsubscribeError = ::onUnsubscribeError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,9 @@ class FxaDeviceConstellation(
// Find the current device.
val currentDevice = allDevices.find { it.isCurrentDevice }?.also {
// Check if our current device's push subscription needs to be renewed.
if (it.subscriptionExpired) {
logger.info("Current device needs push endpoint registration")
if (it.subscription == null || it.subscriptionExpired) {
logger.info("Current device needs push endpoint registration, so checking for missed commands")
pollForCommands()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,31 @@ class FxaDeviceConstellationTest {
`when`(account.getDevices()).thenReturn(arrayOf(
currentDeviceExpired, testDevice2
))

`when`(account.pollDeviceCommands()).thenReturn(emptyArray())
`when`(account.gatherTelemetry()).thenReturn("{}")

constellation.refreshDevices()

verify(account, times(1)).pollDeviceCommands()

assertEquals(ConstellationState(currentDeviceExpired.into(), listOf(testDevice2.into())), observer.state)
assertEquals(ConstellationState(currentDeviceExpired.into(), listOf(testDevice2.into())), constellation.state())

// Current device with no subscription.
val currentDeviceNoSub = testDevice("currentNoSub", true, expired = false, subscribed = false)

`when`(account.getDevices()).thenReturn(arrayOf(
currentDeviceNoSub, testDevice2
))

`when`(account.pollDeviceCommands()).thenReturn(emptyArray())
`when`(account.gatherTelemetry()).thenReturn("{}")

constellation.refreshDevices()

verify(account, times(2)).pollDeviceCommands()
assertEquals(ConstellationState(currentDeviceNoSub.into(), listOf(testDevice2.into())), constellation.state())
}

@Test
Expand Down Expand Up @@ -405,7 +426,7 @@ class FxaDeviceConstellationTest {
// assertEquals(authErrorObserver.latestException!!.cause, authException)
}

private fun testDevice(id: String, current: Boolean, expired: Boolean = false): NativeDevice {
private fun testDevice(id: String, current: Boolean, expired: Boolean = false, subscribed: Boolean = true): NativeDevice {
return NativeDevice(
id = id,
displayName = "testName",
Expand All @@ -414,7 +435,7 @@ class FxaDeviceConstellationTest {
lastAccessTime = 123L,
capabilities = listOf(),
pushEndpointExpired = expired,
pushSubscription = null
pushSubscription = if (subscribed) NativeDevice.PushSubscription("http://endpoint.com", "pk", "auth key") else null
)
}

Expand Down

0 comments on commit 30e3ea9

Please sign in to comment.