Skip to content

Instantly share code, notes, and snippets.

@Groostav
Created March 11, 2019 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Groostav/a5695bdbe2e4df22824d0cad7d32e8fd to your computer and use it in GitHub Desktop.
Save Groostav/a5695bdbe2e4df22824d0cad7d32e8fd to your computer and use it in GitHub Desktop.
synchronizing on database events
@Test fun `should receive updated data when querying all challenges`() = runBlocking<Unit> {
//setup
val initChallenges = list(Challenge(...),Challenge(...))
val newSurveyChallenge = SurveyChallenge(...)
val expectedChallenges = initChallenges + newSurveyChallenge
//act
initChallenges.forEach(challengeDao::addOrUpdate)
challengeDao.addOrUpdate(newSurveyChallenge)
val actualChallenges = withTimeoutOrNull(300 * 10) {
repository.availableChallenges(coroutineContext).take(expectedChallenges.size).toList()
//note: if you're not certain you've got cancellation on the `availableChallenges` implemented correctly,
// you might wrap the above line in async(IO) { ...availableChallenges() }.await().take(expected...
// that will give the `withTimeoutOrNull` something to 'bite' into, but it could leave you with a lingering open connection.
// Some @Before and @After/Teardown logic is probably best here.
}
//assert
assertThat(actualChallengesLists).isEqualTo(expectedChallenges)
}
@Test
fun `should receive updated data when querying all challenges`() {
val initChallenges = mutableListOf(Challenge(...),Challenge(...))
val newSurveyChallenge = SurveyChallenge(...)
val expectedChallenges = listOf(
initChallenges,
initChallenges += newSurveyChallenge
)
initChallenges.forEach(challengeDao::addOrUpdate)
runBlocking {
val channel = repository.availableChallenges(coroutineContext)
val actualChallengesLists = async(IO) {
channel.fold(mutableListOf<List<Challenge>>()) { list, challenges -> list.apply { add(challenges) } }
}
delay(300)
challengeDao.addOrUpdate(newSurveyChallenge)
delay(300)
channel.cancel()
assertThat(actualChallengesLists.await()).containsExactlyElementsIn(expectedChallenges).inOrder()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment