From c5ca79daf6e5337b70c4354ec30c26700e461d4e Mon Sep 17 00:00:00 2001 From: LooKeR Date: Wed, 21 Aug 2024 18:41:59 +0530 Subject: [PATCH] Delete Jar file after sync in V1Syncable --- .../main/kotlin/com/looker/sync/fdroid/v1/V1Parser.kt | 2 +- .../kotlin/com/looker/sync/fdroid/v1/V1Syncable.kt | 4 ++-- .../main/kotlin/com/looker/sync/fdroid/v2/V2Parser.kt | 4 +++- .../kotlin/com/looker/sync/fdroid/V1ParserTest.kt | 11 +++++++++++ 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/sync/fdroid/src/main/kotlin/com/looker/sync/fdroid/v1/V1Parser.kt b/sync/fdroid/src/main/kotlin/com/looker/sync/fdroid/v1/V1Parser.kt index c87095dc..7c6f6859 100644 --- a/sync/fdroid/src/main/kotlin/com/looker/sync/fdroid/v1/V1Parser.kt +++ b/sync/fdroid/src/main/kotlin/com/looker/sync/fdroid/v1/V1Parser.kt @@ -1,9 +1,9 @@ package com.looker.sync.fdroid.v1 -import com.looker.sync.fdroid.Parser import com.looker.core.domain.model.Fingerprint import com.looker.core.domain.model.Repo import com.looker.sync.fdroid.IndexValidator +import com.looker.sync.fdroid.Parser import com.looker.sync.fdroid.utils.toJarFile import com.looker.sync.fdroid.v1.model.IndexV1 import kotlinx.coroutines.CoroutineDispatcher diff --git a/sync/fdroid/src/main/kotlin/com/looker/sync/fdroid/v1/V1Syncable.kt b/sync/fdroid/src/main/kotlin/com/looker/sync/fdroid/v1/V1Syncable.kt index 739e1cc3..9607696a 100644 --- a/sync/fdroid/src/main/kotlin/com/looker/sync/fdroid/v1/V1Syncable.kt +++ b/sync/fdroid/src/main/kotlin/com/looker/sync/fdroid/v1/V1Syncable.kt @@ -12,7 +12,6 @@ import com.looker.sync.fdroid.common.toV2 import com.looker.sync.fdroid.v1.model.IndexV1 import com.looker.sync.fdroid.v2.model.IndexV2 import kotlinx.coroutines.CoroutineDispatcher -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext class V1Syncable( @@ -27,13 +26,14 @@ class V1Syncable( ) override suspend fun sync(repo: Repo): Pair = - withContext(Dispatchers.IO) { + withContext(dispatcher) { val jar = downloader.downloadIndex( repo = repo, url = repo.address.removeSuffix("/") + "/index-v1.jar", fileName = "index-v1.jar", ) val (fingerprint, indexV1) = parser.parse(jar, repo) + jar.delete() fingerprint to indexV1.toV2() } } diff --git a/sync/fdroid/src/main/kotlin/com/looker/sync/fdroid/v2/V2Parser.kt b/sync/fdroid/src/main/kotlin/com/looker/sync/fdroid/v2/V2Parser.kt index 53ffe836..6edc94af 100644 --- a/sync/fdroid/src/main/kotlin/com/looker/sync/fdroid/v2/V2Parser.kt +++ b/sync/fdroid/src/main/kotlin/com/looker/sync/fdroid/v2/V2Parser.kt @@ -24,6 +24,8 @@ class V2Parser( val indexV2 = file.inputStream().use { json.decodeFromStream(IndexV2.serializer(), it) } - repo.fingerprint!! to indexV2 + requireNotNull(repo.fingerprint) { + "Fingerprint should not be null if index v2 is being fetched" + } to indexV2 } } diff --git a/sync/fdroid/src/test/kotlin/com/looker/sync/fdroid/V1ParserTest.kt b/sync/fdroid/src/test/kotlin/com/looker/sync/fdroid/V1ParserTest.kt index 93c93c35..fef51b8a 100644 --- a/sync/fdroid/src/test/kotlin/com/looker/sync/fdroid/V1ParserTest.kt +++ b/sync/fdroid/src/test/kotlin/com/looker/sync/fdroid/V1ParserTest.kt @@ -7,12 +7,15 @@ import com.looker.sync.fdroid.common.getResource import com.looker.sync.fdroid.common.toV2 import com.looker.sync.fdroid.v1.V1Parser import com.looker.sync.fdroid.v2.V2Parser +import io.ktor.utils.io.CancellationException +import kotlinx.coroutines.async import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.runTest import kotlinx.serialization.json.Json import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertIterableEquals import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows import java.util.jar.JarEntry import kotlin.time.measureTime @@ -58,6 +61,14 @@ class V1ParserTest { ) } } + + @Test + fun `check cancellation`() = runTest(dispatcher) { + requireNotNull(jarFile) + val job = async { v1Parser.parse(jarFile, repo) } + job.cancel() + assertThrows { job.await() } + } } internal inline fun memory(