-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simple test to check if Diff is patched successfully
Touches #287
- Loading branch information
Showing
18 changed files
with
211 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
48 changes: 48 additions & 0 deletions
48
sync/fdroid/src/androidTest/kotlin/com/looker/sync/fdroid/Downloader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.looker.sync.fdroid | ||
|
||
import com.looker.core.common.extension.writeTo | ||
import com.looker.network.Downloader | ||
import com.looker.network.NetworkResponse | ||
import com.looker.network.ProgressListener | ||
import com.looker.network.header.HeadersBuilder | ||
import com.looker.network.validation.FileValidator | ||
import com.looker.sync.fdroid.common.assets | ||
import java.io.File | ||
import java.net.Proxy | ||
|
||
val FakeDownloader = object : Downloader { | ||
override fun setProxy(proxy: Proxy) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override suspend fun headCall( | ||
url: String, | ||
headers: HeadersBuilder.() -> Unit | ||
): NetworkResponse { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override suspend fun downloadToFile( | ||
url: String, | ||
target: File, | ||
validator: FileValidator?, | ||
headers: HeadersBuilder.() -> Unit, | ||
block: ProgressListener? | ||
): NetworkResponse { | ||
return if (url.endsWith("fail")) NetworkResponse.Error.Unknown(Exception("You asked for it")) | ||
else { | ||
val index = when { | ||
url.endsWith("index-v1.jar") -> assets("izzy_index_v1.jar") | ||
url.endsWith("index-v2.json") -> assets("izzy_index_v2.json") | ||
url.endsWith("entry.jar") -> assets("izzy_entry.jar") | ||
url.endsWith("/diff/1725731263000.json") -> assets("izzy_diff.json") | ||
// Just in case we try these in future | ||
url.endsWith("index-v1.json") -> assets("izzy_index_v1.json") | ||
url.endsWith("entry.json") -> assets("izzy_entry.json") | ||
else -> error("Unknown URL: $url") | ||
} | ||
index.writeTo(target) | ||
NetworkResponse.Success(200, null, null) | ||
} | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
sync/fdroid/src/androidTest/kotlin/com/looker/sync/fdroid/EntrySyncableTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package com.looker.sync.fdroid | ||
|
||
import android.content.Context | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import com.looker.core.domain.model.Repo | ||
import com.looker.sync.fdroid.common.Izzy | ||
import com.looker.sync.fdroid.common.JsonParser | ||
import com.looker.sync.fdroid.common.assets | ||
import com.looker.sync.fdroid.common.memory | ||
import com.looker.sync.fdroid.v2.EntrySyncable | ||
import com.looker.sync.fdroid.v2.model.Entry | ||
import com.looker.sync.fdroid.v2.model.IndexV2 | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.test.StandardTestDispatcher | ||
import kotlinx.coroutines.test.runTest | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.json.decodeFromStream | ||
import org.junit.Before | ||
import org.junit.runner.RunWith | ||
import kotlin.test.Test | ||
import kotlin.test.assertContentEquals | ||
import kotlin.test.assertEquals | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class EntrySyncableTest { | ||
|
||
private lateinit var dispatcher: CoroutineDispatcher | ||
private lateinit var context: Context | ||
private lateinit var syncable: Syncable<Entry> | ||
private lateinit var repo: Repo | ||
private lateinit var newIndex: IndexV2 | ||
|
||
/** | ||
* In this particular test 1 package is removed and 36 packages are updated | ||
*/ | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
@Before | ||
fun before() { | ||
context = InstrumentationRegistry.getInstrumentation().context | ||
dispatcher = StandardTestDispatcher() | ||
syncable = EntrySyncable(context, FakeDownloader, dispatcher) | ||
newIndex = JsonParser.parser.decodeFromStream<IndexV2>(assets("izzy_index_v2_updated.json")) | ||
repo = Izzy | ||
} | ||
|
||
// Not very trustworthy | ||
@Test | ||
fun benchmark_sync_full() = runTest(dispatcher) { | ||
memory("Full Benchmark") { | ||
syncable.sync(repo) | ||
} | ||
memory("Diff Benchmark") { | ||
syncable.sync(repo) | ||
} | ||
} | ||
|
||
@Test | ||
fun check_if_patch_applies() = runTest(dispatcher) { | ||
// Downloads old index file as the index file does not exist | ||
val (fingerprint1, index1) = syncable.sync(repo) | ||
assert(index1 != null) | ||
// Downloads the diff as the index file exists and is older than entry version | ||
val (fingerprint2, index2) = syncable.sync( | ||
repo.copy( | ||
versionInfo = repo.versionInfo.copy( | ||
timestamp = index1!!.repo.timestamp | ||
) | ||
) | ||
) | ||
assert(index2 != null) | ||
// Does not download anything | ||
val (fingerprint3, index3) = syncable.sync( | ||
repo.copy( | ||
versionInfo = repo.versionInfo.copy( | ||
timestamp = index2!!.repo.timestamp | ||
) | ||
) | ||
) | ||
assert(index3 == null) | ||
|
||
// Check if all the packages are same | ||
assertContentEquals(newIndex.packages.keys.sorted(), index2.packages.keys.sorted()) | ||
// Check if all the version hashes are same | ||
assertContentEquals( | ||
newIndex.packages.values.flatMap { it.versions.keys }.sorted(), | ||
index2.packages.values.flatMap { it.versions.keys }.sorted(), | ||
) | ||
|
||
// Check if repo antifeatures are same | ||
assertContentEquals( | ||
newIndex.repo.antiFeatures.keys.sorted(), | ||
index2.repo.antiFeatures.keys.sorted() | ||
) | ||
|
||
// Check if repo categories are same | ||
assertContentEquals( | ||
newIndex.repo.categories.keys.sorted(), | ||
index2.repo.categories.keys.sorted() | ||
) | ||
|
||
assertEquals(fingerprint1, fingerprint2) | ||
assertEquals(fingerprint2, fingerprint3) | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
sync/fdroid/src/androidTest/kotlin/com/looker/sync/fdroid/IndexValidator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.looker.sync.fdroid | ||
|
||
import com.looker.core.domain.model.Fingerprint | ||
import java.util.jar.JarEntry | ||
|
||
val FakeIndexValidator = object : IndexValidator { | ||
override suspend fun validate( | ||
jarEntry: JarEntry, | ||
expectedFingerprint: Fingerprint? | ||
): Fingerprint { | ||
return expectedFingerprint ?: Fingerprint("0".repeat(64)) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
sync/fdroid/src/androidTest/kotlin/com/looker/sync/fdroid/common/Benchmark.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.looker.sync.fdroid.common | ||
|
||
import com.looker.network.DataSize | ||
import kotlin.time.measureTime | ||
|
||
internal inline fun memory( | ||
extraMessage: String? = null, | ||
block: () -> Unit, | ||
) { | ||
val runtime = Runtime.getRuntime() | ||
if (extraMessage != null) { | ||
println("=".repeat(50)) | ||
println(extraMessage) | ||
} | ||
println("=".repeat(50)) | ||
val initial = runtime.freeMemory() | ||
val time = measureTime { | ||
block() | ||
} | ||
val final = runtime.freeMemory() | ||
println("Time Taken: ${time}, Usage: ${DataSize(initial - final)} / ${DataSize(runtime.maxMemory())}") | ||
println("=".repeat(50)) | ||
println() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
sync/fdroid/src/androidTest/kotlin/com/looker/sync/fdroid/common/Resource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.looker.sync.fdroid.common | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry | ||
import java.io.File | ||
import java.io.InputStream | ||
|
||
fun getResource(name: String): File? { | ||
val url = Thread.currentThread().contextClassLoader?.getResource(name) ?: return null | ||
return File(url.file) | ||
} | ||
|
||
fun assets(name: String): InputStream { | ||
return InstrumentationRegistry.getInstrumentation().context.assets.open(name) | ||
} |
87 changes: 0 additions & 87 deletions
87
sync/fdroid/src/test/kotlin/com/looker/sync/fdroid/V1ParserTest.kt
This file was deleted.
Oops, something went wrong.
8 changes: 0 additions & 8 deletions
8
sync/fdroid/src/test/kotlin/com/looker/sync/fdroid/common/Resource.kt
This file was deleted.
Oops, something went wrong.