From 12f491737ec2e2d774a816e84170ff352d1b6cd6 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Thu, 7 Mar 2019 14:59:40 -0500 Subject: Add cache test --- .../kotlin/com/pitchedapps/frost/db/CacheDbTest.kt | 32 ++++++++++++++++++ .../kotlin/com/pitchedapps/frost/db/CacheDb.kt | 38 +++++++++++++++++----- .../com/pitchedapps/frost/db/NotificationDb.kt | 2 +- .../1.json | 29 ++++++++++++++--- 4 files changed, 86 insertions(+), 15 deletions(-) create mode 100644 app/src/androidTest/kotlin/com/pitchedapps/frost/db/CacheDbTest.kt (limited to 'app/src') diff --git a/app/src/androidTest/kotlin/com/pitchedapps/frost/db/CacheDbTest.kt b/app/src/androidTest/kotlin/com/pitchedapps/frost/db/CacheDbTest.kt new file mode 100644 index 00000000..780bbd3e --- /dev/null +++ b/app/src/androidTest/kotlin/com/pitchedapps/frost/db/CacheDbTest.kt @@ -0,0 +1,32 @@ +package com.pitchedapps.frost.db + +import kotlinx.coroutines.runBlocking +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue +import kotlin.test.fail + +class CacheDbTest : BaseDbTest() { + + private val dao get() = db.cacheDao() + private val cookieDao get() = db.cookieDao() + + private fun cookie(id: Long) = CookieEntity(id, "name$id", "cookie$id") + + @Test + fun save() { + val cookie = cookie(1L) + val type = "test" + val content = "long test".repeat(10000) + runBlocking { + cookieDao.insertCookie(cookie) + dao.save(cookie.id, type, content) + val cache = dao.select(cookie.id, type) ?: fail("Cache not found") + assertEquals(content, cache.contents, "Content mismatch") + assertTrue( + System.currentTimeMillis() - cache.lastUpdated < 500, + "Cache retrieval took over 500ms (${System.currentTimeMillis() - cache.lastUpdated})" + ) + } + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/db/CacheDb.kt b/app/src/main/kotlin/com/pitchedapps/frost/db/CacheDb.kt index 4d6bc938..8c3c9c6b 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/db/CacheDb.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/db/CacheDb.kt @@ -19,9 +19,11 @@ package com.pitchedapps.frost.db import android.os.Parcelable import androidx.room.Dao import androidx.room.Entity +import androidx.room.ForeignKey import androidx.room.Insert import androidx.room.OnConflictStrategy import androidx.room.Query +import com.pitchedapps.frost.utils.L import kotlinx.android.parcel.Parcelize /** @@ -31,11 +33,20 @@ import kotlinx.android.parcel.Parcelize /** * Generic cache to store serialized content */ -@Entity(tableName = "frost_cache") +@Entity( + tableName = "frost_cache", + primaryKeys = ["id", "type"], + foreignKeys = [ForeignKey( + entity = CookieEntity::class, + parentColumns = ["cookie_id"], + childColumns = ["id"], + onDelete = ForeignKey.CASCADE + )] +) @Parcelize data class CacheEntity( - @androidx.room.PrimaryKey - val id: String, + val id: Long, + val type: String, val lastUpdated: Long, val contents: String ) : Parcelable @@ -43,15 +54,24 @@ data class CacheEntity( @Dao interface CacheDao { - @Query("SELECT * FROM frost_cache WHERE id = :id") - suspend fun selectById(id: Long): CacheEntity? + @Query("SELECT * FROM frost_cache WHERE id = :id AND type = :type") + suspend fun select(id: Long, type: String): CacheEntity? @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun insertCache(cache: CacheEntity) - @Query("DELETE FROM frost_cache WHERE id = :id") - suspend fun deleteById(id: Long) + @Query("DELETE FROM frost_cache WHERE id = :id AND type = :type") + suspend fun delete(id: Long, type: String) } -suspend fun CacheDao.save(id: String, contents: String) = - insertCache(CacheEntity(id, System.currentTimeMillis(), contents)) \ No newline at end of file +/** + * Returns true if successful, given that there are constraints to the insertion + */ +suspend fun CacheDao.save(id: Long, type: String, contents: String): Boolean = + try { + insertCache(CacheEntity(id, type, System.currentTimeMillis(), contents)) + true + } catch (e: Exception) { + L.e(e) { "Cache save failed for $type" } + false + } \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/db/NotificationDb.kt b/app/src/main/kotlin/com/pitchedapps/frost/db/NotificationDb.kt index 7f41fbf8..d2771754 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/db/NotificationDb.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/db/NotificationDb.kt @@ -146,7 +146,7 @@ suspend fun NotificationDao.saveNotifications(type: String, notifs: List