BackgroundTaskCompletionTest.kt
composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/services/scheduling/BackgroundTaskCompletionTest.kt
1@file:OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)23package eu.friedcutlet.yokan.services.scheduling45import kotlinx.coroutines.CompletableDeferred6import kotlinx.coroutines.CoroutineScope7import kotlinx.coroutines.NonCancellable8import kotlinx.coroutines.SupervisorJob9import kotlinx.coroutines.awaitCancellation10import kotlinx.coroutines.cancelAndJoin11import kotlinx.coroutines.test.StandardTestDispatcher12import kotlinx.coroutines.test.runTest13import kotlinx.coroutines.withContext14import kotlin.test.Test15import kotlin.test.assertEquals16import kotlin.test.assertFalse1718class BackgroundTaskCompletionTest {19 @Test fun expiryBeforeDispatchStillCompletesOnceWithoutRunningWork() = runTest {20 var executed = false21 val completions = mutableListOf<Boolean>()22 val job = launchCompletingBackgroundTask(this, {23 executed = true; ProfileWorkResult.Skipped24 }, completions::add)25 job.cancelAndJoin()26 testScheduler.runCurrent()27 assertFalse(executed); assertEquals(listOf(false), completions)28 }2930 @Test fun expiryWaitsForFinalizersBeforeReportingFailure() = runTest {31 val entered = CompletableDeferred<Unit>(); val release = CompletableDeferred<Unit>()32 val completions = mutableListOf<Boolean>()33 var finalized = false34 val job = launchCompletingBackgroundTask(this, {35 try { entered.complete(Unit); awaitCancellation() }36 finally { withContext(NonCancellable) { release.await(); finalized = true } }37 }, { completions += it; assertEquals(true, finalized) })38 entered.await(); job.cancel(); testScheduler.runCurrent()39 assertEquals(emptyList(), completions)40 release.complete(Unit); job.join()41 assertEquals(listOf(false), completions)42 }4344 @Test fun completedAndStaleTasksSucceedWhileRecoverableFailuresDoNot() = runTest {45 val outcomes = listOf(46 ProfileWorkResult.Completed(PeriodicTaskOutcome(PeriodicTaskStatus.Completed, "Done")),47 ProfileWorkResult.Completed(PeriodicTaskOutcome(PeriodicTaskStatus.Skipped, "Disabled")),48 ProfileWorkResult.Skipped,49 ProfileWorkResult.Completed(PeriodicTaskOutcome(PeriodicTaskStatus.Failed, "Failed")),50 ProfileWorkResult.Retry,51 )52 val completions = mutableListOf<Boolean>()53 for (outcome in outcomes) launchCompletingBackgroundTask(this, { outcome }, completions::add).join()54 assertEquals(listOf(true, true, true, false, false), completions)55 }5657 @Test fun taskExceptionsCompleteWithFailureAndLeaveTheHostAlive() = runTest {58 val parent = SupervisorJob()59 val scope = CoroutineScope(parent + StandardTestDispatcher(testScheduler))60 val completions = mutableListOf<Boolean>()61 launchCompletingBackgroundTask(scope, { error("Task failure") }, completions::add).join()62 assertEquals(listOf(false), completions)63 assertEquals(true, parent.isActive)64 launchCompletingBackgroundTask(scope, { ProfileWorkResult.Skipped }, completions::add).join()65 assertEquals(listOf(false, true), completions)66 parent.cancelAndJoin()67 }68}69