Created
March 11, 2019 19:01
-
-
Save Groostav/a5695bdbe2e4df22824d0cad7d32e8fd to your computer and use it in GitHub Desktop.
synchronizing on database events
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
@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) | |
} |
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
@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