Skip to content
This repository has been archived by the owner on Dec 14, 2021. It is now read-only.

Commit

Permalink
Add telemetry probes to syncIfRequired trigger syncs (#887)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhugman authored Aug 28, 2019
1 parent 49b230f commit 3fe3294
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
13 changes: 9 additions & 4 deletions app/src/main/java/mozilla/lockbox/store/DataStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,11 @@ open class DataStore(
if (item != null) {
backend.delete(item.id)
.asSingle(coroutineContext)
.subscribe()
.subscribe { _ ->
dispatcher.dispatch(DataStoreAction.Sync)
}
.addTo(compositeDisposable)
sync()

deletedItemSubject.accept(Consumable(item))
}
} catch (loginsStorageException: LoginsStorageException) {
Expand Down Expand Up @@ -230,9 +232,12 @@ open class DataStore(
}
}

private fun syncIfRequired() {
@VisibleForTesting(
otherwise = VisibleForTesting.PRIVATE
)
fun syncIfRequired() {
if (timingSupport.shouldSync) {
this.sync()
dispatcher.dispatch(DataStoreAction.Sync)
timingSupport.storeNextSyncTime()
}
}
Expand Down
18 changes: 18 additions & 0 deletions app/src/test/java/mozilla/lockbox/store/DataStoreTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import mozilla.components.concept.sync.AccessTokenInfo
import mozilla.lockbox.DisposingTest
import mozilla.lockbox.action.DataStoreAction
import mozilla.lockbox.action.LifecycleAction
import mozilla.lockbox.extensions.filterByType
import mozilla.lockbox.flux.Dispatcher
import mozilla.lockbox.mocks.MockDataStoreSupport
import mozilla.lockbox.model.FixedSyncCredentials
Expand All @@ -33,6 +34,7 @@ import org.mockito.Mockito.never
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.powermock.api.mockito.PowerMockito
import java.util.concurrent.atomic.AtomicBoolean
import org.powermock.api.mockito.PowerMockito.`when` as whenCalled

@ExperimentalCoroutinesApi
Expand Down Expand Up @@ -87,6 +89,22 @@ class DataStoreTest : DisposingTest() {
Assert.assertEquals(0, listIterator.next().size)
}

@Test
fun testSyncIfRequired_dispatchesSyncAction() {
whenCalled(timingSupport.shouldSync).thenReturn(true)
val isSyncing = AtomicBoolean(false)
val sub = dispatcher.register
.filterByType(DataStoreAction::class.java)
.subscribe {
isSyncing.set(true)
}

subject.syncIfRequired()

Assert.assertTrue(isSyncing.get())
sub.dispose()
}

@Test
fun testLockUnlock_shouldNotSync() {
val stateIterator = this.subject.state.blockingIterable().iterator()
Expand Down

0 comments on commit 3fe3294

Please sign in to comment.