fix: disconnect desktop download on session cancel

This commit is contained in:
xXJsonDeruloXx committed 2026-10-22 09:56:05 -04:00
1 parent 4e4a731221
commit c02832ac59
6 files changed
+84 -7

No files matched your search

@@ -6,6 +6,9 @@ import java.net.Socket
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import org.junit.After
import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
@@ -29,6 +32,9 @@ class DownloaderResumeTest {
@Volatile private var misbehave = false
@Volatile private var etag: String? = "\"v1\""
@Volatile private var corrupt = false
@Volatile private var holdAfterFirstChunk = false
private var bodyStarted = CountDownLatch(1)
private var continueBody = CountDownLatch(1)
@Before fun setUp() {
dir = Files.createTempDirectory("downloader-resume").toFile()
@@ -70,8 +76,15 @@ class DownloaderResumeTest {
val start = if (from == null) "200 OK" else "206 Partial Content"
val extra = if (from == null) "" else "Content-Range: bytes 0-${data.size - 1}/${data.size}\r\n"
out.write("HTTP/1.1 $start\r\n${extra}${validator}Content-Length: ${data.size}\r\nConnection: close\r\n\r\n".toByteArray())
out.write(content)
out.flush()
if (holdAfterFirstChunk) {
out.write(content, 0, 1024)
out.flush()
bodyStarted.countDown()
continueBody.await(5, TimeUnit.SECONDS)
} else {
out.write(content)
out.flush()
}
} else if (from == null) {
out.write("HTTP/1.1 200 OK\r\n${validator}Content-Length: ${data.size}\r\nConnection: close\r\n\r\n".toByteArray())
out.write(content, 0, cutAt)
@@ -181,6 +194,25 @@ class DownloaderResumeTest {
assertTrue("the transfer stopped early (kept=${target.length()})", target.length() in 1 until data.size)
}
@Test fun cancellationDisconnectsAStalledTransfer() {
val url = "http://127.0.0.1:${server.localPort}/first"
val target = File(dir, "runtime.tar.zst")
misbehave = true
holdAfterFirstChunk = true
val cancellation = Downloader.Cancellation()
val executor = Executors.newSingleThreadExecutor()
try {
val result = executor.submit<Boolean> { Downloader.downloadFile(url, target, true, null, cancellation, null) }
assertTrue("server sent the first chunk", bodyStarted.await(2, TimeUnit.SECONDS))
cancellation.cancel()
assertFalse("cancelled download", result.get(2, TimeUnit.SECONDS))
assertEquals(listOf<String?>(null), requests.toList())
} finally {
continueBody.countDown()
executor.shutdownNow()
}
}
@Test fun aCancelBeforeTheFirstRequestSendsNothing() {
val url = "http://127.0.0.1:${server.localPort}/runtime.tar.zst"
val target = File(dir, "runtime.tar.zst")