Yokan / rebuild
Course 1 8624561

JvmLocalMediaInputTest.kt

composeApp/src/jvmCommonTest/kotlin/eu/friedcutlet/yokan/services/files/JvmLocalMediaInputTest.kt

test / fixture50 lines
1package eu.friedcutlet.yokan.services.files23import java.io.ByteArrayInputStream4import java.util.concurrent.atomic.AtomicInteger5import kotlinx.coroutines.CancellationException6import kotlinx.coroutines.CompletableDeferred7import kotlinx.coroutines.async8import kotlinx.coroutines.test.runTest9import kotlin.test.*1011class JvmLocalMediaInputTest {12    @Test fun readerWaitsForDestinationAndNeverReadsMoreThanOneBoundedChunkAhead() = runTest {13        val bytes = ByteArray(LOCAL_IMPORT_CHUNK_BYTES * 3 + 3) { it.toByte() }14        val reads = AtomicInteger()15        val closes = AtomicInteger()16        val stream = object : ByteArrayInputStream(bytes) {17            override fun read(b: ByteArray, off: Int, len: Int): Int { reads.incrementAndGet(); return super.read(b, off, len) }18            override fun close() { closes.incrementAndGet(); super.close() }19        }20        val first = CompletableDeferred<Unit>()21        val resume = CompletableDeferred<Unit>()22        val chunks = mutableListOf<ByteArray>()23        val reader = JvmLocalMediaInput("clip.mp4", bytes.size.toLong()) { stream }24        val work = async {25            reader.readChunks {26                chunks += it27                if (chunks.size == 1) { first.complete(Unit); resume.await() }28            }29        }30        first.await()31        assertEquals(1, reads.get())32        assertEquals(0, closes.get())33        resume.complete(Unit)34        work.await()35        assertEquals(listOf(65536, 65536, 65536, 3), chunks.map { it.size })36        assertContentEquals(bytes, chunks.reduce { a, b -> a + b })37        assertEquals(1, closes.get())38    }3940    @Test fun cancellationClosesTheSourceWithoutReadingTheRemainingFile() = runTest {41        val closes = AtomicInteger()42        val stream = object : ByteArrayInputStream(ByteArray(LOCAL_IMPORT_CHUNK_BYTES * 3)) {43            override fun close() { closes.incrementAndGet(); super.close() }44        }45        val reader = JvmLocalMediaInput("clip.mp4", null) { stream }46        assertFailsWith<CancellationException> { reader.readChunks { throw CancellationException("Cancelled") } }47        assertEquals(1, closes.get())48        assertEquals(LOCAL_IMPORT_CHUNK_BYTES * 2, stream.available())49    }50}51

Search chapters, reference and source filenames.