Skip to content
Permalink
Browse files

Make loadExperiments and updateExperiments synchronized and guard aga…

…inst storage file not found
  • Loading branch information...
fercarcedo committed May 30, 2018
1 parent 63c9792 commit cd812f3fb63b853d842c7af87c69587bec5f45d6
@@ -53,4 +53,11 @@ class AtomicFileExperimentStorageTest {
assertEquals(0, experiments[0].bucket?.min)
assertEquals(1526991669, experiments[0].lastModified)
}

@Test
fun testRetrieveFileNotFound() {
val file = File(InstrumentationRegistry.getContext().filesDir, "missingFile.json")
val experiments = AtomicFileExperimentStorage(AtomicFile(file)).retrieve()
assertEquals(0, experiments.size)
}
}
@@ -7,11 +7,16 @@ package mozilla.components.service.fretboard.storage.atomicfile
import android.util.AtomicFile
import mozilla.components.service.fretboard.Experiment
import mozilla.components.service.fretboard.ExperimentStorage
import java.io.FileNotFoundException

class AtomicFileExperimentStorage(private val atomicFile: AtomicFile) : ExperimentStorage {
override fun retrieve(): List<Experiment> {
val experimentsJson = String(atomicFile.readFully())
return ExperimentsSerializer().fromJson(experimentsJson)
try {
val experimentsJson = String(atomicFile.readFully())
return ExperimentsSerializer().fromJson(experimentsJson)
} catch (e: FileNotFoundException) {
return listOf()
}
}

override fun save(experiments: List<Experiment>) {
@@ -20,6 +20,7 @@ class Fretboard(
/**
* Loads experiments from local storage
*/
@Synchronized
fun loadExperiments() {
experiments = storage.retrieve()
experimentsLoaded = true
@@ -29,9 +30,11 @@ class Fretboard(
* Requests new experiments from the server and
* saves them to local storage
*/
@Synchronized
fun updateExperiments() {
if (!experimentsLoaded)
if (!experimentsLoaded) {
loadExperiments()
}
try {
val serverExperiments = source.getExperiments(experiments)
experiments = serverExperiments

0 comments on commit cd812f3

Please sign in to comment.
You can’t perform that action at this time.