fix(media): an uploader checks the relay's upload status before trusting it
A chunk of 0 looped forever, a huge chunk held the film in memory and a huge window opened a request (a thread on Kotlin) per piece. statusProblem checks chunk, window, received and landed; the client window is capped at 16. Same in MediaUploader, with tests on both sides. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package no.zyon.shade
|
||||
|
||||
import com.sun.net.httpserver.HttpServer
|
||||
import no.zyon.shade.media.ByteArraySource
|
||||
import no.zyon.shade.media.DEFAULT_CHUNK_BYTES
|
||||
import no.zyon.shade.media.MediaOwner
|
||||
import no.zyon.shade.media.MediaSealer
|
||||
import no.zyon.shade.media.MediaUploadException
|
||||
import no.zyon.shade.media.MediaUploader
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertThrows
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import java.net.InetSocketAddress
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
|
||||
/** What the uploader does with a relay that answers nonsense; the real conversation is `MediaLiveTest`. */
|
||||
class MediaUploaderTest {
|
||||
|
||||
private fun relay(answer: String, calls: AtomicInteger): HttpServer =
|
||||
HttpServer.create(InetSocketAddress("127.0.0.1", 0), 0).apply {
|
||||
createContext("/") { ex ->
|
||||
calls.incrementAndGet()
|
||||
ex.requestBody.readBytes()
|
||||
val b = answer.toByteArray()
|
||||
ex.responseHeaders.add("content-type", "application/json")
|
||||
ex.sendResponseHeaders(201, b.size.toLong())
|
||||
ex.responseBody.use { it.write(b) }
|
||||
}
|
||||
start()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun anUnusableUploadStatusIsRefusedAtOnceNotLoopedOn() {
|
||||
val (sealed, result) = MediaSealer.sealBytes(ByteArray(DEFAULT_CHUNK_BYTES.toInt() + 4096))
|
||||
for (bad in listOf(
|
||||
"""{"chunk":0,"received":0}""",
|
||||
"""{"chunk":${64L * 1024 * 1024 * 4},"received":0}""",
|
||||
"""{"chunk":${DEFAULT_CHUNK_BYTES},"received":-1}""",
|
||||
"""{"chunk":${DEFAULT_CHUNK_BYTES},"received":0,"window":0}""",
|
||||
"""{"chunk":${DEFAULT_CHUNK_BYTES},"received":0,"landed":["x"]}""",
|
||||
)) {
|
||||
val calls = AtomicInteger()
|
||||
val server = relay(bad, calls)
|
||||
try {
|
||||
val up = MediaUploader("http://127.0.0.1:${server.address.port}", MediaOwner.generate())
|
||||
val e = assertThrows(MediaUploadException::class.java) { up.putSealed(result, ByteArraySource(sealed)) }
|
||||
assertTrue(bad, e.message!!.contains("unusable"))
|
||||
assertEquals(bad, 1, calls.get())
|
||||
} finally {
|
||||
server.stop(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user