-
Notifications
You must be signed in to change notification settings - Fork 207
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 #1010 from bugsnag/PLAT-5401/synchronized-store
Create synchronized store for user information
- Loading branch information
Showing
7 changed files
with
225 additions
and
5 deletions.
There are no files selected for viewing
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
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
141 changes: 141 additions & 0 deletions
141
...-android-core/src/androidTest/java/com/bugsnag/android/SynchronizedStreamableStoreTest.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,141 @@ | ||
package com.bugsnag.android | ||
|
||
import android.content.Context | ||
import android.util.JsonReader | ||
import androidx.test.core.app.ApplicationProvider | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNull | ||
import org.junit.Test | ||
import java.io.EOFException | ||
import java.io.File | ||
import java.io.FileNotFoundException | ||
import java.util.concurrent.CountDownLatch | ||
import java.util.concurrent.Executors | ||
|
||
internal class SynchronizedStreamableStoreTest { | ||
|
||
private val user = User("123", "[email protected]", "Tess Tng") | ||
|
||
@Test | ||
fun testPersistNonExistingFile() { | ||
val ctx = ApplicationProvider.getApplicationContext<Context>() | ||
val file = File(ctx.cacheDir, "no-such-file.json") | ||
val store = SynchronizedStreamableStore<User>(file) | ||
store.persist(user) | ||
assertEquals(user, store.load(User.Companion::fromReader)) | ||
} | ||
|
||
@Test | ||
fun testPersistWritableFile() { | ||
val file = File.createTempFile("test", "json") | ||
val store = SynchronizedStreamableStore<User>(file) | ||
store.persist(user) | ||
assertEquals(user, store.load(User.Companion::fromReader)) | ||
} | ||
|
||
@Test(expected = FileNotFoundException::class) | ||
fun testPersistNonWritableFile() { | ||
val file = File.createTempFile("test", "json").apply { | ||
setWritable(false) | ||
} | ||
val store = SynchronizedStreamableStore<User>(file) | ||
store.persist(user) | ||
assertNull(store.load(User.Companion::fromReader)) | ||
} | ||
|
||
@Test(expected = NotImplementedError::class) | ||
fun testPersistExceptionInStreamable() { | ||
val file = File.createTempFile("test", "json") | ||
val store = SynchronizedStreamableStore<CrashyStreamable>(file) | ||
store.persist(CrashyStreamable()) | ||
assertNull(store.load(CrashyStreamable.Companion::fromReader)) | ||
} | ||
|
||
@Test(expected = FileNotFoundException::class) | ||
fun testReadNonExistingFile() { | ||
val file = File("no-such-file.bmp") | ||
val store = SynchronizedStreamableStore<User>(file) | ||
assertNull(store.load(User.Companion::fromReader)) | ||
} | ||
|
||
@Test(expected = EOFException::class) | ||
fun testReadNonWritableFile() { | ||
val file = File.createTempFile("test", "json").apply { | ||
setWritable(false) | ||
} | ||
val store = SynchronizedStreamableStore<User>(file) | ||
assertNull(store.load(User.Companion::fromReader)) | ||
} | ||
|
||
/** | ||
* Reads the same file concurrently to assert that a [ReadWriteLock] is used | ||
*/ | ||
@Test(timeout = 2000) | ||
fun testConcurrentReadsPossible() { | ||
// persist some initial data | ||
val file = File.createTempFile("test", "json") | ||
val store = SynchronizedStreamableStore<ThreadTestStreamable>(file) | ||
store.persist(ThreadTestStreamable("some_val")) | ||
|
||
// read file on bg thread, triggered halfway through reading file on main thread | ||
var alreadyReadingBgThread = false | ||
ThreadTestStreamable.readCallback = { | ||
if (!alreadyReadingBgThread) { | ||
alreadyReadingBgThread = true | ||
val reader = JsonReader(file.reader()) | ||
val latch = CountDownLatch(1) | ||
|
||
Executors.newSingleThreadExecutor().execute { | ||
val bgThreadObj = ThreadTestStreamable.fromReader(reader) | ||
assertEquals("some_val", bgThreadObj.id) | ||
latch.countDown() | ||
} | ||
latch.await() | ||
} | ||
} | ||
|
||
// read the file on the main thread | ||
val reader = JsonReader(file.reader()) | ||
val mainThreadObj = ThreadTestStreamable.fromReader(reader) | ||
assertEquals("some_val", mainThreadObj.id) | ||
} | ||
} | ||
|
||
internal class ThreadTestStreamable( | ||
val id: String, | ||
val writeCallback: () -> Unit = {} | ||
) : JsonStream.Streamable { | ||
|
||
override fun toStream(stream: JsonStream) { | ||
with(stream) { | ||
beginObject() | ||
name("test") | ||
writeCallback() | ||
value(id) | ||
endObject() | ||
} | ||
} | ||
|
||
companion object : JsonReadable<ThreadTestStreamable> { | ||
var readCallback: () -> Unit = {} | ||
|
||
override fun fromReader(reader: JsonReader): ThreadTestStreamable { | ||
with(reader) { | ||
beginObject() | ||
nextName() | ||
readCallback() | ||
val obj = ThreadTestStreamable(nextString()) | ||
endObject() | ||
return obj | ||
} | ||
} | ||
} | ||
} | ||
|
||
internal class CrashyStreamable : JsonStream.Streamable { | ||
override fun toStream(stream: JsonStream) = TODO("I'll handle this later...") | ||
|
||
companion object: JsonReadable<CrashyStreamable> { | ||
override fun fromReader(reader: JsonReader) = TODO("coffee break...") | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
bugsnag-android-core/src/main/java/com/bugsnag/android/JsonReadable.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,14 @@ | ||
package com.bugsnag.android | ||
|
||
import android.util.JsonReader | ||
|
||
/** | ||
* Classes which implement this interface are capable of deserializing a JSON input. | ||
*/ | ||
internal interface JsonReadable<T : JsonStream.Streamable> { | ||
|
||
/** | ||
* Constructs an object from a JSON input. | ||
*/ | ||
fun fromReader(reader: JsonReader): T | ||
} |
33 changes: 33 additions & 0 deletions
33
bugsnag-android-core/src/main/java/com/bugsnag/android/SynchronizedStreamableStore.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,33 @@ | ||
package com.bugsnag.android | ||
|
||
import android.util.JsonReader | ||
import java.io.File | ||
import java.io.IOException | ||
import java.util.concurrent.locks.ReentrantReadWriteLock | ||
import kotlin.concurrent.withLock | ||
|
||
internal class SynchronizedStreamableStore<T : JsonStream.Streamable>( | ||
private val file: File | ||
) { | ||
|
||
private val lock = ReentrantReadWriteLock() | ||
|
||
@Throws(IOException::class) | ||
fun persist(streamable: T) { | ||
lock.writeLock().withLock { | ||
file.writer().use { | ||
streamable.toStream(JsonStream(it)) | ||
true | ||
} | ||
} | ||
} | ||
|
||
@Throws(IOException::class) | ||
fun load(loadCallback: (JsonReader) -> T): T { | ||
lock.readLock().withLock { | ||
return file.reader().use { | ||
loadCallback(JsonReader(it)) | ||
} | ||
} | ||
} | ||
} |
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
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