Yokan / rebuild
Course 1 8624561

ProfilePlayerServicesLifecycleTest.kt

composeApp/src/desktopTest/kotlin/eu/friedcutlet/yokan/services/media/player/ProfilePlayerServicesLifecycleTest.kt

test / fixture202 lines
1@file:OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class, androidx.compose.ui.ExperimentalComposeUiApi::class)23package eu.friedcutlet.yokan.services.media.player45import androidx.compose.runtime.SideEffect6import androidx.compose.runtime.mutableStateOf7import androidx.compose.ui.ImageComposeScene8import eu.friedcutlet.yokan.database.DbSettings9import eu.friedcutlet.yokan.ui.screens.player.rememberPlayerServices10import java.util.concurrent.CountDownLatch11import java.util.concurrent.FutureTask12import java.util.concurrent.TimeUnit13import javax.swing.SwingUtilities14import kotlin.test.Test15import kotlin.test.assertEquals16import kotlin.test.assertFailsWith17import kotlin.test.assertFalse18import kotlin.test.assertNotSame19import kotlin.test.assertTrue20import kotlinx.coroutines.CancellationException21import kotlinx.coroutines.Dispatchers22import kotlinx.coroutines.async23import kotlinx.coroutines.cancelAndJoin24import kotlinx.coroutines.test.TestScope25import kotlinx.coroutines.test.UnconfinedTestDispatcher26import kotlinx.coroutines.test.runCurrent27import kotlinx.coroutines.test.runTest28import kotlinx.coroutines.withContext2930class ProfilePlayerServicesLifecycleTest {31    @Test fun canceledFactoryCallerJoinsBlockingAllocationAndLateNativeCleanup() = runTest {32        val entered = CountDownLatch(1); val release = CountDownLatch(1)33        val controller = ProfilePlayerControllerTest.Controller()34        val f = ProfilePlayerServicesTest.Fixture(this, dispatcher = Dispatchers.IO, factory = { _, _ ->35            entered.countDown(); check(release.await(10, TimeUnit.SECONDS)); controller36        })37        var published = false38        val configuration = f.reader.configuration(DbSettings())39        val call = async { f.reader.withController(configuration) { published = true } }40        try {41            runCurrent(); withContext(Dispatchers.IO) { assertTrue(entered.await(5, TimeUnit.SECONDS)) }42            call.cancel(); runCurrent(); assertFalse(call.isCompleted)43            release.countDown(); call.join()44            assertFalse(published); assertEquals(1, controller.closes); assertTrue(controller.released)45            assertTrue(f.service.isAvailable)46        } finally { release.countDown(); call.cancelAndJoin(); f.close() }47    }4849    @Test fun canceledConfigurationCallerJoinsBlockingWriteAndSkipsSecondFile() = runTest {50        val f = ProfilePlayerServicesTest.Fixture(this, dispatcher = Dispatchers.IO)51        val entered = CountDownLatch(1)52        val release = CountDownLatch(1)53        f.files.onWrite = { entered.countDown(); check(release.await(10, TimeUnit.SECONDS)) }54        val configuration = f.reader.configuration(DbSettings())55        val request = async { f.reader.prepareConfiguration(configuration) }56        try {57            runCurrent()58            withContext(Dispatchers.IO) { assertTrue(entered.await(5, TimeUnit.SECONDS)) }59            request.cancel(); runCurrent()60            assertFalse(request.isCompleted)61            release.countDown()62            request.join()63            assertEquals(listOf("/A/cache/mpv/mpv.conf"), f.files.writes)64            assertTrue(f.service.isAvailable)65        } finally { release.countDown(); request.cancelAndJoin(); f.close() }66    }6768    @Test fun retirementJoinsBlockingScreenshotSeedAndDeletesItWithoutDispatch() = runTest {69        val f = ProfilePlayerServicesTest.Fixture(this, dispatcher = Dispatchers.IO)70        val controller = ProfilePlayerServicesTest.Controller()71        val entered = CountDownLatch(1)72        val release = CountDownLatch(1)73        f.files.onWrite = { entered.countDown(); check(release.await(10, TimeUnit.SECONDS)) }74        val request = async { f.reader.requestScreenshot(controller, "Book", "Episode") }75        try {76            runCurrent()77            withContext(Dispatchers.IO) { assertTrue(entered.await(5, TimeUnit.SECONDS)) }78            val close = async { f.service.closeAndJoin() }79            runCurrent()80            assertFalse(close.isCompleted)81            assertFalse(request.isCompleted)82            release.countDown()83            close.await()84            assertFailsWith<CancellationException> { request.await() }85            assertTrue(controller.commands.isEmpty())86            assertEquals(f.files.writes, f.files.deletions)87            assertTrue(f.owner.isActive)88        } finally { release.countDown(); request.cancelAndJoin(); f.close() }89    }9091    @Test fun closeDrainsNativeThreadProxyRegistrationOffUiThread() = runTest {92        val f = ProfilePlayerServicesTest.Fixture(this, dispatcher = Dispatchers.IO)93        val entered = CountDownLatch(1)94        val release = CountDownLatch(1)95        f.proxy.onRegister = { entered.countDown(); check(release.await(10, TimeUnit.SECONDS)) }96        val request = async(Dispatchers.IO) { f.reader.streamProxy.proxyUrl("https://stream.test", emptyMap()) }97        try {98            withContext(Dispatchers.IO) { assertTrue(entered.await(5, TimeUnit.SECONDS)) }99            val close = async { f.service.closeAndJoin() }100            runCurrent()101            assertFalse(close.isCompleted)102            assertFalse(f.service.isAvailable)103            release.countDown()104            close.await()105            assertFailsWith<CancellationException> { request.await() }106            assertEquals(0, f.proxy.stops)107        } finally { release.countDown(); request.cancelAndJoin(); f.close() }108    }109110    @Test fun compositionDisposalExpiresCallsAndRemountGetsFreshHandle() = runTest {111        val f = UiFixture(this)112        try {113            val old = f.reader114            f.show(false)115            assertFalse(old.isAvailable)116            assertFailsWith<CancellationException> { old.markers(42, 1, 0) }117            assertFailsWith<CancellationException> { old.streamProxy.proxyUrl("Old", emptyMap()) }118            assertTrue(f.a.http.requests.isEmpty())119            assertTrue(f.a.proxy.urls.isEmpty())120            f.show(true)121            assertNotSame(old, f.reader)122            assertEquals("proxy:New", f.reader.streamProxy.proxyUrl("New", emptyMap()))123        } finally { f.close() }124    }125126    @Test fun chapterReplacementRejectsOldConfigurationAndProxyWithoutStoppingSession() = runTest {127        val f = UiFixture(this)128        try {129            val old = f.reader130            val configuration = old.configuration(DbSettings())131            f.ui { f.chapter.value = 20L }; f.pump()132            assertNotSame(old, f.reader)133            assertFailsWith<CancellationException> { old.prepareConfiguration(configuration) }134            old.streamProxy.stop()135            assertEquals("proxy:New chapter", f.reader.streamProxy.proxyUrl("New chapter", emptyMap()))136            assertEquals(0, f.a.proxy.stops)137            assertTrue(f.a.files.writes.isEmpty())138        } finally { f.close() }139    }140141    @Test fun serviceReplacementWithSameChapterUsesNewFilesAndRejectsOldScreenshot() = runTest {142        val f = UiFixture(this)143        try {144            val old = f.reader145            f.ui { f.current.value = f.b.service }; f.pump()146            assertNotSame(old, f.reader)147            assertFailsWith<CancellationException> {148                old.requestScreenshot(ProfilePlayerServicesTest.Controller(), "Book", "Episode")149            }150            f.reader.prepareConfiguration(f.reader.configuration(DbSettings()))151            assertTrue(f.a.files.writes.isEmpty())152            assertEquals(listOf("/B/cache/mpv/mpv.conf", "/B/cache/mpv/input.conf"), f.b.files.writes)153        } finally { f.close() }154    }155156    @Test fun retiringStatusRejectsVisibleHandleBeforeOwnerScopeCancellation() = runTest {157        val f = UiFixture(this)158        try {159            f.a.open = false160            assertTrue(f.a.owner.isActive)161            assertFailsWith<CancellationException> { f.reader.markers(42, 1, 0) }162            assertFailsWith<CancellationException> { f.reader.streamProxy.proxyUrl("Late", emptyMap()) }163            assertTrue(f.a.http.requests.isEmpty())164            assertTrue(f.a.proxy.urls.isEmpty())165        } finally { f.close() }166    }167168    private class UiFixture(val test: TestScope) {169        val a = ProfilePlayerServicesTest.Fixture(test, "A")170        val b = ProfilePlayerServicesTest.Fixture(test, "B")171        val current = mutableStateOf(a.service)172        val chapter = mutableStateOf(10L)173        val visible = mutableStateOf(true)174        lateinit var reader: ProfilePlayerServices.Reader175        val scene = ui {176            ImageComposeScene(100, 100, coroutineContext = UnconfinedTestDispatcher(test.testScheduler)) {177                if (visible.value) {178                    val binding = rememberPlayerServices(current.value, chapter.value)179                    SideEffect { reader = binding }180                }181            }182        }183        init { pump() }184        fun show(value: Boolean) { ui { visible.value = value }; pump() }185        fun pump() = ui { repeat(3) {186            test.testScheduler.runCurrent()187            scene.render(test.testScheduler.currentTime * 1_000_000).close()188            test.testScheduler.runCurrent()189        } }190        fun <T> ui(block: () -> T): T = onUi(block)191        suspend fun close() { ui { scene.close() }; a.close(); b.close() }192    }193194    private companion object {195        fun <T> onUi(block: () -> T): T {196            if (SwingUtilities.isEventDispatchThread()) return block()197            val task = FutureTask(block)198            SwingUtilities.invokeAndWait(task)199            return task.get()200        }201    }202}203

Search chapters, reference and source filenames.