-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from kkostov/feature/offline-cache
Add a basic persistent local cache for signals
- Loading branch information
Showing
3 changed files
with
184 additions
and
9 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
lib/src/main/java/com/telemetrydeck/sdk/PersistentSignalCache.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.telemetrydeck.sdk | ||
|
||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.decodeFromString | ||
import kotlinx.serialization.encodeToString | ||
import java.io.File | ||
import java.lang.Exception | ||
|
||
class PersistentSignalCache(private var signalQueue: MutableList<Signal> = mutableListOf()): SignalCache { | ||
val cacheFileName: String = "telemetrydeck.json" | ||
var file: File? = null | ||
|
||
constructor(cacheDir: File, logger: DebugLogger?) : this() { | ||
if (!cacheDir.isDirectory) { | ||
logger?.error("Expected a folder but received a file instead.") | ||
return | ||
} | ||
|
||
val writeFile = File(cacheDir, cacheFileName) | ||
logger?.debug("Using signal cache at ${writeFile.absolutePath}.") | ||
if (writeFile.exists()) { | ||
logger?.debug("Detected existing signal cache, attempting to parse...") | ||
val content = writeFile.readText() | ||
try { | ||
val oldSignals: List<Signal> = Json.decodeFromString(content) | ||
logger?.error("Restoring ${oldSignals.count()} signals from cache.") | ||
signalQueue.addAll(oldSignals) | ||
} catch (e: Exception) { | ||
logger?.error("Failed to parse signal cache.") | ||
} | ||
} | ||
file = writeFile | ||
saveSignals() | ||
} | ||
|
||
override fun add(signal: Signal) { | ||
synchronized(this) { | ||
signalQueue.add(signal) | ||
saveSignals() | ||
} | ||
|
||
} | ||
|
||
override fun empty(): List<Signal> { | ||
synchronized(this) { | ||
val items = signalQueue.toList() | ||
signalQueue = mutableListOf() | ||
saveSignals() | ||
return items | ||
} | ||
} | ||
|
||
override fun count(): Int { | ||
synchronized(this) { | ||
return signalQueue.count() | ||
} | ||
} | ||
|
||
fun saveSignals() { | ||
file?.createNewFile() | ||
val json = Json.encodeToString(signalQueue) | ||
file?.writeText(json) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
lib/src/test/java/com/telemetrydeck/sdk/PersistentSignalCacheTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.telemetrydeck.sdk | ||
|
||
import androidx.arch.core.executor.testing.InstantTaskExecutorRule | ||
import kotlinx.serialization.decodeFromString | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.coroutines.* | ||
import org.junit.Assert | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.rules.TemporaryFolder | ||
import java.io.File | ||
import java.util.* | ||
|
||
class PersistentSignalCacheTest { | ||
@get:Rule | ||
val instantTaskExecutorRule = InstantTaskExecutorRule() | ||
|
||
@get:Rule | ||
var folder = TemporaryFolder() | ||
|
||
@Test | ||
fun persistentSignalCache_starts_with_empty_queue() { | ||
val cacheDir = folder.newFolder() | ||
val sut = PersistentSignalCache(cacheDir, null) | ||
|
||
Assert.assertEquals(0, sut.count()) | ||
} | ||
|
||
@Test | ||
fun persistentSignalCache_creates_cache_file() { | ||
val cacheDir = folder.newFolder() | ||
val sut = PersistentSignalCache(cacheDir, null) | ||
|
||
val cacheFile = File(cacheDir, sut.cacheFileName) | ||
|
||
Assert.assertTrue(cacheFile.exists()) | ||
|
||
val json = cacheFile.readText() | ||
val signals = Json.decodeFromString<List<Signal>>(json) | ||
Assert.assertEquals(0, signals.count()) | ||
} | ||
|
||
@Test | ||
fun persistentSignalCache_new_signals_added_to_cache() { | ||
val cacheDir = folder.newFolder() | ||
val sut = PersistentSignalCache(cacheDir, null) | ||
sut.add(Signal(UUID.randomUUID(), "type", "user", SignalPayload())) | ||
val cacheFile = File(cacheDir, sut.cacheFileName) | ||
|
||
val json = cacheFile.readText() | ||
val signals = Json.decodeFromString<List<Signal>>(json) | ||
Assert.assertEquals(1, signals.count()) | ||
Assert.assertEquals("type", signals[0].type) | ||
Assert.assertEquals("user", signals[0].clientUser) | ||
} | ||
|
||
@Test | ||
fun persistentSignalCache_empty_clears_cache() { | ||
val cacheDir = folder.newFolder() | ||
val sut = PersistentSignalCache(cacheDir, null) | ||
sut.add(Signal(UUID.randomUUID(), "type", "user", SignalPayload())) | ||
sut.empty() | ||
val cacheFile = File(cacheDir, sut.cacheFileName) | ||
|
||
val json = cacheFile.readText() | ||
val signals = Json.decodeFromString<List<Signal>>(json) | ||
Assert.assertEquals(0, signals.count()) | ||
} | ||
|
||
@DelicateCoroutinesApi | ||
@Test | ||
fun persistentSignalCache_allows_adding_signals_concurrently() = runBlocking { | ||
val cacheDir = folder.newFolder() | ||
val sut = PersistentSignalCache(cacheDir, null) | ||
|
||
// the cache should accept signals from concurrent coroutines | ||
val scope = CoroutineScope(newFixedThreadPoolContext(4, "pool")) | ||
scope.launch { | ||
val coroutines = 1.rangeTo(100).map { | ||
// create 100 coroutines (light-weight threads). | ||
launch { | ||
for (i in 1..20) { // and in each of them, add 20 signals | ||
sut.add(Signal(UUID.randomUUID(), "type", "user", SignalPayload())) | ||
} | ||
} | ||
} | ||
|
||
coroutines.forEach { coroutine -> | ||
coroutine.join() // wait for all coroutines to finish their jobs. | ||
} | ||
}.join() | ||
|
||
val cacheFile = File(cacheDir, sut.cacheFileName) | ||
|
||
val json = cacheFile.readText() | ||
val signals = Json.decodeFromString<List<Signal>>(json) | ||
Assert.assertEquals(2000, signals.count()) | ||
} | ||
} |