Skip to content

Commit

Permalink
Delete Jar file after sync in V1Syncable
Browse files Browse the repository at this point in the history
  • Loading branch information
Iamlooker committed Aug 21, 2024
1 parent 09f7d20 commit c5ca79d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -27,13 +26,14 @@ class V1Syncable(
)

override suspend fun sync(repo: Repo): Pair<Fingerprint, IndexV2> =
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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
11 changes: 11 additions & 0 deletions sync/fdroid/src/test/kotlin/com/looker/sync/fdroid/V1ParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -58,6 +61,14 @@ class V1ParserTest {
)
}
}

@Test
fun `check cancellation`() = runTest(dispatcher) {
requireNotNull(jarFile)
val job = async { v1Parser.parse(jarFile, repo) }
job.cancel()
assertThrows<CancellationException> { job.await() }
}
}

internal inline fun memory(
Expand Down

0 comments on commit c5ca79d

Please sign in to comment.