Yokan / rebuild
Course 1 8624561

MpvCommandBlockingTest.kt

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

test / fixture50 lines
1@file:OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)23package eu.friedcutlet.yokan.services.media.player45import eu.friedcutlet.yokan.database.DbSettings6import java.util.concurrent.CountDownLatch7import java.util.concurrent.TimeUnit8import kotlinx.coroutines.CancellationException9import kotlinx.coroutines.CompletableDeferred10import kotlinx.coroutines.Dispatchers11import kotlinx.coroutines.async12import kotlinx.coroutines.awaitCancellation13import kotlinx.coroutines.cancelAndJoin14import kotlinx.coroutines.test.runCurrent15import kotlinx.coroutines.test.runTest16import kotlinx.coroutines.withContext17import kotlin.test.Test18import kotlin.test.assertEquals19import kotlin.test.assertFailsWith20import kotlin.test.assertFalse21import kotlin.test.assertTrue2223class MpvCommandBlockingTest {24    @Test fun retirementWaitsForBlockingNativeCaptureBeforeDeletingFileAndReleasingPlayer() = runTest {25        val entered = CountDownLatch(1); val finish = CountDownLatch(1)26        val player = MediaPlayerInitializationTest.RecordingMpvPlayer()27        val controller = MpvMediaPlayerController(player, NoopMediaStreamProxy, Dispatchers.IO)28        val f = ProfilePlayerServicesTest.Fixture(this, dispatcher = Dispatchers.IO, factory = { _, _ -> controller })29        val published = CompletableDeferred<Unit>()30        val lifetime = async { f.reader.withController(f.reader.configuration(DbSettings())) {31            published.complete(Unit); awaitCancellation()32        } }33        player.onCommand = {34            entered.countDown(); check(finish.await(10, TimeUnit.SECONDS))35            assertTrue(f.files.deletions.isEmpty()); assertEquals(0, player.releaseCount)36            f.files.bytes[f.files.writes.single()] = byteArrayOf(1)37        }38        val capture = async { published.await(); f.reader.requestScreenshot(controller, "Book", "Episode") }39        try {40            runCurrent(); withContext(Dispatchers.IO) { assertTrue(entered.await(5, TimeUnit.SECONDS)) }41            val close = async { f.service.closeAndJoin() }; runCurrent()42            assertFalse(close.isCompleted); assertFalse(capture.isCompleted)43            assertTrue(f.files.deletions.isEmpty()); assertEquals(0, player.releaseCount)44            finish.countDown(); close.await()45            assertFailsWith<CancellationException> { capture.await() }46            assertTrue(f.files.bytes.isEmpty()); assertEquals(f.files.writes, f.files.deletions)47            assertEquals(1, player.releaseCount); assertTrue(f.owner.isActive)48        } finally { finish.countDown(); capture.cancelAndJoin(); lifetime.cancelAndJoin(); f.close(); controller.releaseAndJoin() }49    }50}51

Search chapters, reference and source filenames.