Created
September 12, 2020 10:16
-
-
Save aartikov/34f1413c9bb8d3b548730b668dbfbd5d to your computer and use it in GitHub Desktop.
Test for issue https://github.com/dropbox/Store/issues/124
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
package me.aartikov.storesample | |
import com.dropbox.android.external.store4.* | |
import kotlinx.coroutines.delay | |
import kotlinx.coroutines.flow.Flow | |
import kotlinx.coroutines.flow.MutableStateFlow | |
import kotlinx.coroutines.flow.toList | |
import kotlinx.coroutines.launch | |
import kotlinx.coroutines.runBlocking | |
import org.junit.Assert.assertEquals | |
import org.junit.Test | |
class StoreTest { | |
@Test | |
fun refreshWithError() = runBlocking { | |
val fetcher = Fetcher.ofResult<Unit, String> { | |
FetcherResult.Error.Message("Error") | |
} | |
val sourceOfTruth = InMemorySourceOfTruth() | |
val store = StoreBuilder.from(fetcher, sourceOfTruth).build() | |
val result = mutableListOf<StoreResponse<String>>() | |
val job = launch { | |
store.stream(StoreRequest.cached(Unit, refresh = true)) | |
.toList(result) | |
} | |
delay(1000) | |
runCatching { | |
store.fresh(Unit) | |
} | |
delay(1000) | |
job.cancel() | |
val expected = listOf<StoreResponse<String>>( | |
StoreResponse.Loading(origin = ResponseOrigin.Fetcher), | |
StoreResponse.Error.Message(message = "Error", origin = ResponseOrigin.Fetcher), | |
StoreResponse.Error.Message(message = "Error", origin = ResponseOrigin.Fetcher) | |
) | |
assertEquals(expected, result) | |
} | |
} | |
class InMemorySourceOfTruth : SourceOfTruth<Unit, String, String> { | |
private val stateFlow = MutableStateFlow<String?>(null) | |
override suspend fun delete(key: Unit) { | |
stateFlow.value = null | |
} | |
override suspend fun deleteAll() { | |
stateFlow.value = null | |
} | |
override fun reader(key: Unit): Flow<String?> { | |
return stateFlow | |
} | |
override suspend fun write(key: Unit, value: String) { | |
stateFlow.value = value | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment