Skip to content

Instantly share code, notes, and snippets.

@micHar
Created June 20, 2022 12:10
Show Gist options
  • Save micHar/8262eafe78df54b02ca86f634fc950b5 to your computer and use it in GitHub Desktop.
Save micHar/8262eafe78df54b02ca86f634fc950b5 to your computer and use it in GitHub Desktop.
interface IssueReporter {
fun reportIssue(e: Exception)
}
interface SoundPlayer {
suspend fun playSound()
}
class MediaPlayer(
private val scope: CoroutineScope,
private val soundPlayer: SoundPlayer,
private val issueReporter: IssueReporter
) {
val playerErrors = MutableSharedFlow<Exception>()
init {
scope.launch {
playerErrors.collect {
issueReporter.reportIssue(it)
}
}
}
fun play() {
scope.launch {
try {
soundPlayer.playSound()
} catch (e: Exception) {
playerErrors.emit(e)
}
}
}
}
@OptIn(ExperimentalCoroutinesApi::class)
class NeverEndingCoroutineTest {
@Test
fun `should emit error when playSound throws`() = runTest {
val exception = Exception("Oopsie")
val soundPlayer = mockk<SoundPlayer>()
val issueReporter = mockk<IssueReporter>()
justRun { issueReporter.reportIssue(any()) }
coEvery { soundPlayer.playSound() } throws exception
val sut = MediaPlayer(this, soundPlayer, issueReporter)
sut.playerErrors.test {
sut.play()
awaitItem() shouldBe exception
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment