-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cacheOnly option to StoreReadRequest (#586)
* Add cacheOnly option to StoreReadRequest Signed-off-by: William Brawner <me@wbrawner.com> * Fix doc on StoreReadRequest.cacheOnly Signed-off-by: William Brawner <me@wbrawner.com> * Hit disk caches for cacheOnly requests Signed-off-by: William Brawner <me@wbrawner.com> * Rename cacheOnly to localOnly Signed-off-by: William Brawner <william.p.brawner@aexp.com> * Send NoNewData and log warning for localOnly requests with no local data sources configured Signed-off-by: William Brawner <william.p.brawner@aexp.com> --------- Signed-off-by: William Brawner <me@wbrawner.com> Signed-off-by: William Brawner <william.p.brawner@aexp.com>
- Loading branch information
Showing
3 changed files
with
194 additions
and
4 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
149 changes: 149 additions & 0 deletions
149
store/src/commonTest/kotlin/org/mobilenativefoundation/store/store5/LocalOnlyTests.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,149 @@ | ||
package org.mobilenativefoundation.store.store5 | ||
|
||
import co.touchlab.kermit.Logger | ||
import kotlinx.atomicfu.atomic | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.test.TestScope | ||
import kotlinx.coroutines.test.runTest | ||
import org.mobilenativefoundation.store.store5.impl.extensions.get | ||
import org.mobilenativefoundation.store.store5.util.InMemoryPersister | ||
import org.mobilenativefoundation.store.store5.util.asSourceOfTruth | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertTrue | ||
import kotlin.time.Duration | ||
|
||
class LocalOnlyTests { | ||
private val testScope = TestScope() | ||
|
||
@Test | ||
fun givenEmptyMemoryCacheThenCacheOnlyRequestReturnsNoNewData() = testScope.runTest { | ||
val store = StoreBuilder | ||
.from(Fetcher.of { _: Int -> throw RuntimeException("Fetcher shouldn't be hit") }) | ||
.cachePolicy( | ||
MemoryPolicy | ||
.builder<Any, Any>() | ||
.build() | ||
) | ||
.build() | ||
val response = store.stream(StoreReadRequest.localOnly(0)).first() | ||
assertEquals(StoreReadResponse.NoNewData(StoreReadResponseOrigin.Cache), response) | ||
} | ||
|
||
@Test | ||
fun givenPrimedMemoryCacheThenCacheOnlyRequestReturnsData() = testScope.runTest { | ||
val fetcherHitCounter = atomic(0) | ||
val store = StoreBuilder | ||
.from(Fetcher.of { _: Int -> | ||
fetcherHitCounter += 1 | ||
"result" | ||
}) | ||
.cachePolicy( | ||
MemoryPolicy | ||
.builder<Any, Any>() | ||
.build() | ||
) | ||
.build() | ||
val a = store.get(0) | ||
assertEquals("result", a) | ||
assertEquals(1, fetcherHitCounter.value) | ||
val response = store.stream(StoreReadRequest.localOnly(0)).first() | ||
assertEquals("result", response.requireData()) | ||
assertEquals(1, fetcherHitCounter.value) | ||
} | ||
|
||
@Test | ||
fun givenInvalidMemoryCacheThenCacheOnlyRequestReturnsNoNewData() = testScope.runTest { | ||
val fetcherHitCounter = atomic(0) | ||
val store = StoreBuilder | ||
.from(Fetcher.of { _: Int -> | ||
fetcherHitCounter += 1 | ||
"result" | ||
}) | ||
.cachePolicy( | ||
MemoryPolicy | ||
.builder<Any, Any>() | ||
.setExpireAfterWrite(Duration.ZERO) | ||
.build() | ||
) | ||
.build() | ||
val a = store.get(0) | ||
assertEquals("result", a) | ||
assertEquals(1, fetcherHitCounter.value) | ||
val response = store.stream(StoreReadRequest.localOnly(0)).first() | ||
assertEquals(StoreReadResponse.NoNewData(StoreReadResponseOrigin.Cache), response) | ||
assertEquals(1, fetcherHitCounter.value) | ||
} | ||
|
||
@Test | ||
fun givenEmptyDiskCacheThenCacheOnlyRequestReturnsNoNewData() = testScope.runTest { | ||
val persister = InMemoryPersister<Int, String>() | ||
val store = StoreBuilder | ||
.from( | ||
fetcher = Fetcher.of { _: Int -> throw RuntimeException("Fetcher shouldn't be hit") }, | ||
sourceOfTruth = persister.asSourceOfTruth() | ||
) | ||
.disableCache() | ||
.build() | ||
val response = store.stream(StoreReadRequest.localOnly(0)).first() | ||
assertEquals(StoreReadResponse.NoNewData(StoreReadResponseOrigin.SourceOfTruth), response) | ||
} | ||
|
||
@Test | ||
fun givenPrimedDiskCacheThenCacheOnlyRequestReturnsData() = testScope.runTest { | ||
val fetcherHitCounter = atomic(0) | ||
val persister = InMemoryPersister<Int, String>() | ||
val store = StoreBuilder | ||
.from( | ||
fetcher = Fetcher.of { _: Int -> | ||
fetcherHitCounter += 1 | ||
"result" | ||
}, | ||
sourceOfTruth = persister.asSourceOfTruth() | ||
) | ||
.disableCache() | ||
.build() | ||
val a = store.get(0) | ||
assertEquals("result", a) | ||
assertEquals(1, fetcherHitCounter.value) | ||
val response = store.stream(StoreReadRequest.localOnly(0)).first() | ||
assertEquals("result", response.requireData()) | ||
assertEquals(StoreReadResponseOrigin.SourceOfTruth, response.origin) | ||
assertEquals(1, fetcherHitCounter.value) | ||
} | ||
|
||
@Test | ||
fun givenInvalidDiskCacheThenCacheOnlyRequestReturnsNoNewData() = testScope.runTest { | ||
val fetcherHitCounter = atomic(0) | ||
val persister = InMemoryPersister<Int, String>() | ||
persister.write(0, "result") | ||
val store = StoreBuilder | ||
.from( | ||
fetcher = Fetcher.of { _: Int -> | ||
fetcherHitCounter += 1 | ||
"result" | ||
}, | ||
sourceOfTruth = persister.asSourceOfTruth() | ||
) | ||
.disableCache() | ||
.validator(Validator.by { false }) | ||
.build() | ||
val a = store.get(0) | ||
assertEquals("result", a) | ||
assertEquals(1, fetcherHitCounter.value) | ||
val response = store.stream(StoreReadRequest.localOnly(0)).first() | ||
assertEquals(StoreReadResponse.NoNewData(StoreReadResponseOrigin.SourceOfTruth), response) | ||
assertEquals(1, fetcherHitCounter.value) | ||
} | ||
|
||
@Test | ||
fun givenNoCacheThenCacheOnlyRequestReturnsNoNewData() = testScope.runTest { | ||
val store = StoreBuilder | ||
.from(Fetcher.of { _: Int -> throw RuntimeException("Fetcher shouldn't be hit") }) | ||
.disableCache() | ||
.build() | ||
val response = store.stream(StoreReadRequest.localOnly(0)).first() | ||
assertTrue(response is StoreReadResponse.NoNewData) | ||
assertEquals(StoreReadResponseOrigin.Cache, response.origin) | ||
} | ||
} |