Yokan / rebuild
Course 1 8624561

ProfileDownloadBindingsTest.kt

composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/services/download/ProfileDownloadBindingsTest.kt

test / fixture172 lines
1@file:OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)23package eu.friedcutlet.yokan.services.download45import eu.friedcutlet.yokan.domain.ProfileId6import eu.friedcutlet.yokan.services.scheduling.ProfileWorkNamespace7import eu.friedcutlet.yokan.services.scheduling.ProfileWorkResult8import kotlinx.coroutines.CompletableDeferred9import kotlinx.coroutines.CancellationException10import kotlinx.coroutines.CoroutineScope11import kotlinx.coroutines.NonCancellable12import kotlinx.coroutines.SupervisorJob13import kotlinx.coroutines.awaitCancellation14import kotlinx.coroutines.cancelAndJoin15import kotlinx.coroutines.cancel16import kotlinx.coroutines.currentCoroutineContext17import kotlinx.coroutines.launch18import kotlinx.coroutines.test.StandardTestDispatcher19import kotlinx.coroutines.test.UnconfinedTestDispatcher20import kotlinx.coroutines.test.runTest21import kotlinx.coroutines.withContext22import kotlin.test.Test23import kotlin.test.assertEquals24import kotlin.test.assertFalse25import kotlin.test.assertFailsWith26import kotlin.test.assertNotEquals27import kotlin.test.assertNull28import kotlin.test.assertSame29import kotlin.test.assertTrue3031class ProfileDownloadBindingsTest {32    @Test fun alreadyExpiredCallerCannotDispatchIntoALiveDownloadOwner() = runTest {33        val registry = ProfileDownloadBindings(); val parent = SupervisorJob()34        val binding = registry.install(A, FakeDownloads(), CoroutineScope(parent + UnconfinedTestDispatcher(testScheduler)))35        var effects = 036        val caller = launch {37            currentCoroutineContext().cancel()38            assertFailsWith<CancellationException> { binding.run { effects++ } }39        }40        caller.join()41        assertEquals(0, effects); assertTrue(binding.isActive)42        assertSame(binding, registry.resolve(A.value, binding.token))43        parent.cancelAndJoin()44    }4546    @Test fun cancelActionsReachOnlyTheirRecordedLiveOwner() = runTest {47        val registry = ProfileDownloadBindings()48        val a = FakeDownloads(); val b = FakeDownloads()49        val parentA = SupervisorJob(); val parentB = SupervisorJob()50        val scopeA = CoroutineScope(parentA + StandardTestDispatcher(testScheduler))51        val scopeB = CoroutineScope(parentB + StandardTestDispatcher(testScheduler))52        val left = registry.install(A, a, scopeA); val right = registry.install(B, b, scopeB)53        registry.resolve(A.value, left.token)?.cancelAll(); testScheduler.runCurrent()54        assertEquals(1, a.cancels); assertEquals(0, b.cancels)55        assertNull(registry.resolve(B.value, left.token)); assertNull(registry.resolve(A.value, right.token))56        parentA.cancelAndJoin()57        assertNull(registry.resolve(A.value, left.token)); assertSame(right, registry.resolve(B.value, right.token))58        right.cancelAll(); testScheduler.runCurrent(); assertEquals(1, b.cancels)59        parentB.cancelAndJoin()60    }6162    @Test fun oldActionsAndCleanupCannotAffectAReplacementSessionForTheSameProfile() = runTest {63        val registry = ProfileDownloadBindings()64        val parentOld = SupervisorJob(); val parentNew = SupervisorJob()65        val oldDownloads = FakeDownloads(); val newDownloads = FakeDownloads()66        val old = registry.install(A, oldDownloads, CoroutineScope(parentOld + StandardTestDispatcher(testScheduler)))67        val entered = CompletableDeferred<Unit>(); val release = CompletableDeferred<Unit>()68        val mirror = launch {69            old.run { try { entered.complete(Unit); awaitCancellation() } finally { withContext(NonCancellable) { release.await() } } }70        }71        entered.await()72        val next = registry.install(A, newDownloads, CoroutineScope(parentNew + StandardTestDispatcher(testScheduler)))73        assertNotEquals(old.token, next.token); assertNotEquals(old.actionIdentity, next.actionIdentity)74        assertNull(registry.resolve(A.value, old.token)); assertSame(next, registry.resolve(A.value, next.token))75        old.cancelAll(); testScheduler.runCurrent()76        assertEquals(0, oldDownloads.cancels); assertEquals(0, newDownloads.cancels)77        release.complete(Unit); mirror.join(); parentOld.cancelAndJoin()78        assertSame(next, registry.resolve(A.value, next.token))79        next.cancelAll(); testScheduler.runCurrent(); assertEquals(1, newDownloads.cancels)80        parentNew.cancelAndJoin()81    }8283    @Test fun expiredServiceMirrorJoinsCleanupWithoutCancelingTheDownloadOwner() = runTest {84        val registry = ProfileDownloadBindings(); val parent = SupervisorJob()85        val scope = CoroutineScope(parent + StandardTestDispatcher(testScheduler))86        val binding = registry.install(A, FakeDownloads(), scope)87        val downloading = scope.launch { awaitCancellation() }88        val entered = CompletableDeferred<Unit>(); val release = CompletableDeferred<Unit>()89        var finalized = false90        val mirror = launch {91            binding.run { try { entered.complete(Unit); awaitCancellation() } finally { withContext(NonCancellable) { release.await(); finalized = true } } }92        }93        entered.await(); mirror.cancel(); testScheduler.runCurrent()94        assertFalse(mirror.isCompleted); assertFalse(finalized); assertTrue(downloading.isActive)95        release.complete(Unit); mirror.join()96        assertTrue(finalized); assertTrue(downloading.isActive); assertTrue(binding.isActive)97        assertSame(binding, registry.resolve(A.value, binding.token))98        parent.cancelAndJoin()99    }100101    @Test fun ownerRetirementRejectsActionsImmediatelyAndJoinsMirrorFinalizers() = runTest {102        val registry = ProfileDownloadBindings(); val parent = SupervisorJob()103        val downloads = FakeDownloads()104        val binding = registry.install(A, downloads, CoroutineScope(parent + StandardTestDispatcher(testScheduler)))105        val entered = CompletableDeferred<Unit>(); val release = CompletableDeferred<Unit>()106        var finalized = false107        val mirror = launch {108            binding.run { try { entered.complete(Unit); awaitCancellation() } finally { withContext(NonCancellable) { release.await(); finalized = true } } }109        }110        entered.await(); parent.cancel(); testScheduler.runCurrent()111        assertNull(registry.resolve(A.value, binding.token)); assertFalse(parent.isCompleted)112        binding.cancelAll(); testScheduler.runCurrent(); assertEquals(0, downloads.cancels)113        release.complete(Unit); parent.join(); mirror.join()114        assertTrue(finalized); assertTrue(parent.isCompleted)115        assertEquals(ProfileWorkResult.Skipped, binding.run { error("Retired work") })116    }117118    @Test fun retirementBeforeDispatchNeverRunsAQueuedCancelAction() = runTest {119        val registry = ProfileDownloadBindings(); val parent = SupervisorJob(); val downloads = FakeDownloads()120        val binding = registry.install(A, downloads, CoroutineScope(parent + StandardTestDispatcher(testScheduler)))121        binding.cancelAll(); parent.cancelAndJoin()122        assertEquals(0, downloads.cancels); assertNull(registry.resolve(A.value, binding.token))123    }124125    @Test fun missingMalformedAndUnknownActionsNeverBootstrapOrFallBack() = runTest {126        val registry = ProfileDownloadBindings(); val parent = SupervisorJob()127        val binding = registry.install(ProfileId.Default, FakeDownloads(), CoroutineScope(parent + StandardTestDispatcher(testScheduler)))128        for (owner in listOf(null, "", "../default", A.value)) assertNull(registry.resolve(owner, binding.token))129        assertNull(registry.resolve("default", null)); assertNull(registry.resolve("default", "old"))130        assertSame(binding, registry.resolve("default", binding.token))131        parent.cancelAndJoin()132    }133134    @Test fun retiredInstallationCannotDisplaceALiveCapability() = runTest {135        val registry = ProfileDownloadBindings(); val parent = SupervisorJob(); val retired = SupervisorJob()136        val binding = registry.install(A, FakeDownloads(), CoroutineScope(parent + StandardTestDispatcher(testScheduler)))137        retired.cancelAndJoin()138        assertFailsWith<IllegalArgumentException> { registry.install(A, FakeDownloads(), CoroutineScope(retired + StandardTestDispatcher(testScheduler))) }139        assertSame(binding, registry.resolve(A.value, binding.token))140        parent.cancelAndJoin()141    }142143    @Test fun resumeNamesAndActionIdentitiesPartitionIdenticalChapterQueues() = runTest {144        val registry = ProfileDownloadBindings(); val parent = SupervisorJob()145        val scope = CoroutineScope(parent + StandardTestDispatcher(testScheduler))146        val ids = listOf(ProfileId.Default, A, B)147        val names = ids.map { ProfileWorkNamespace(it).uniqueName("yokan.download_resume") }148        val bindings = ids.map { registry.install(it, FakeDownloads(), scope) }149        assertEquals("yokan.download_resume", names.first()); assertEquals(3, names.distinct().size)150        assertEquals(3, bindings.map { it.actionIdentity }.distinct().size)151        parent.cancelAndJoin()152    }153154    private class FakeDownloads : DownloadService {155        var cancels = 0156        override suspend fun initialize(poolSize: Int) = Unit157        override fun enqueue(request: DownloadRequest, onProgress: (DownloadProgress) -> Unit) = Unit158        override fun cancel(taskId: String) = Unit159        override fun cancelAll() { cancels++ }160        override fun pause(taskId: String) = Unit161        override fun resume(taskId: String) = Unit162        override fun retry(taskId: String) = false163        override fun storageLocation(taskId: String): String? = null164        override fun deleteArtifacts(taskId: String) = Unit165        override fun activeDownloads(): List<DownloadProgress> = emptyList()166    }167168    private companion object {169        val A = ProfileId("p_" + "a".repeat(32))170        val B = ProfileId("p_" + "b".repeat(32))171    }172}173

Search chapters, reference and source filenames.