Skip to content

Commit

Permalink
Merge pull request #125 from ooni/improve-run-tests
Browse files Browse the repository at this point in the history
Various Run Test improvements
  • Loading branch information
sdsantos authored Sep 23, 2024
2 parents 7a58cd0 + 2c42424 commit d2d9531
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class RunWorker(
NotificationChannel(
NOTIFICATION_CHANNEL_ID,
getString(Res.string.notification_channel_name),
NotificationManager.IMPORTANCE_DEFAULT,
NotificationManager.IMPORTANCE_LOW,
),
)
}
Expand All @@ -150,6 +150,10 @@ class RunWorker(
.setColor(state.descriptor?.color?.toArgb() ?: primaryLight.toArgb())
.setProgress(1000, (state.progress * 1000).roundToInt(), false)
.setAutoCancel(false)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setSound(null)
.setVibrate(null)
.setLights(0, 0, 0)
.setContentIntent(openAppIntent)
.addAction(
NotificationCompat.Action.Builder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import app.cash.sqldelight.coroutines.asFlow
import app.cash.sqldelight.coroutines.mapToList
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext
import org.ooni.engine.models.WebConnectivityCategory
Expand Down Expand Up @@ -65,6 +66,19 @@ class UrlRepository(
}
}

suspend fun getOrCreateByUrl(url: String): UrlModel =
listByUrls(listOf(url))
.first()
.firstOrNull()
?: run {
val newModel = UrlModel(
url = url,
category = WebConnectivityCategory.MISC,
countryCode = null,
)
newModel.copy(id = createOrUpdate(newModel))
}

fun list(): Flow<List<UrlModel>> =
database.urlQueries
.selectAll()
Expand All @@ -78,10 +92,6 @@ class UrlRepository(
.asFlow()
.mapToList(backgroundDispatcher)
.map { list -> list.mapNotNull { it.toModel() } }

fun getByUrl(url: String): Flow<UrlModel?> =
listByUrls(listOf(url))
.map { it.firstOrNull() }
}

fun Url.toModel(): UrlModel? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ class Dependencies(
observeSettings = preferenceRepository::allSettings,
)
}

val runDescriptors by lazy {
RunDescriptors(
getTestDescriptorsBySpec = getTestDescriptorsBySpec::invoke,
Expand Down Expand Up @@ -257,7 +258,7 @@ class Dependencies(
startTest = engine::startTask,
storeResult = resultRepository::createOrUpdate,
setCurrentTestState = testStateManager::updateState,
getUrlByUrl = urlRepository::getByUrl,
getOrCreateUrl = urlRepository::getOrCreateByUrl,
storeMeasurement = measurementRepository::createOrUpdate,
storeNetwork = networkRepository::createIfNew,
writeFile = writeFile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ class RunDescriptors(
longRunningTests = descriptor.longRunningTests.downloadUrlsIfNeeded(taskOrigin),
)
}
.filterNot { it.allTests.isEmpty() }

private suspend fun List<NetTest>.downloadUrlsIfNeeded(taskOrigin: TaskOrigin): List<NetTest> =
map { test -> test.copy(inputs = test.inputsOrDownloadUrls(taskOrigin)) }
.filterNot { it.test is TestType.WebConnectivity && it.inputs?.any() != true }

private suspend fun NetTest.inputsOrDownloadUrls(taskOrigin: TaskOrigin): List<String>? {
if (!inputs.isNullOrEmpty() || test !is TestType.WebConnectivity) return inputs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.ooni.probe.domain
import co.touchlab.kermit.Logger
import co.touchlab.kermit.Severity
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.ooni.engine.models.TaskEvent
Expand All @@ -23,7 +22,7 @@ import org.ooni.probe.shared.toLocalDateTime

class RunNetTest(
private val startTest: (String, List<String>?, TaskOrigin, InstalledTestDescriptorModel.Id?) -> Flow<TaskEvent>,
private val getUrlByUrl: suspend (String) -> Flow<UrlModel?>,
private val getOrCreateUrl: suspend (String) -> UrlModel,
private val storeMeasurement: suspend (MeasurementModel) -> MeasurementModel.Id,
private val storeNetwork: suspend (NetworkModel) -> NetworkModel.Id,
private val storeResult: suspend (ResultModel) -> ResultModel.Id,
Expand Down Expand Up @@ -101,7 +100,7 @@ class RunNetTest(
urlId = if (event.url.isNullOrEmpty()) {
null
} else {
getUrlByUrl(event.url).first()?.id
getOrCreateUrl(event.url).id
},
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,20 @@ class UrlRepositoryTest {

@BeforeTest
fun before() {
subject =
UrlRepository(
database = Dependencies.buildDatabase(::createTestDatabaseDriver),
backgroundDispatcher = Dispatchers.Default,
)
subject = UrlRepository(
database = Dependencies.buildDatabase(::createTestDatabaseDriver),
backgroundDispatcher = Dispatchers.Default,
)
}

@Test
fun createWithoutIdAndGet() =
runTest {
val model =
UrlModelFactory.build(
id = null,
countryCode = "IT",
category = WebConnectivityCategory.ENV,
)
val model = UrlModelFactory.build(
id = null,
countryCode = "IT",
category = WebConnectivityCategory.ENV,
)

subject.createOrUpdate(model)
val result = subject.list().first().first()
Expand All @@ -61,29 +59,26 @@ class UrlRepositoryTest {
@Test
fun createOrUpdateByUrls() =
runTest {
val existingModel =
UrlModelFactory.build(
id = UrlModel.Id(Random.nextLong().absoluteValue),
url = "https://example.org",
countryCode = null,
category = WebConnectivityCategory.MISC,
)
val existingModel = UrlModelFactory.build(
id = UrlModel.Id(Random.nextLong().absoluteValue),
url = "https://example.org",
countryCode = null,
category = WebConnectivityCategory.MISC,
)
subject.createOrUpdate(existingModel)

val modelWithSameUrl =
UrlModelFactory.build(
id = null,
url = existingModel.url,
countryCode = "US",
category = WebConnectivityCategory.ENV,
)
val modelWithNewUrl =
UrlModelFactory.build(
id = null,
url = "https://ooni.org",
countryCode = "IT",
category = WebConnectivityCategory.NEWS,
)
val modelWithSameUrl = UrlModelFactory.build(
id = null,
url = existingModel.url,
countryCode = "US",
category = WebConnectivityCategory.ENV,
)
val modelWithNewUrl = UrlModelFactory.build(
id = null,
url = "https://ooni.org",
countryCode = "IT",
category = WebConnectivityCategory.NEWS,
)

val results = subject.createOrUpdateByUrl(listOf(modelWithSameUrl, modelWithNewUrl))
val allUrls = subject.list().first()
Expand All @@ -105,15 +100,16 @@ class UrlRepositoryTest {
}

@Test
fun getByUrl() =
fun getOrCreateByUrl() =
runTest {
val url = "htts://example.org"
val model = UrlModelFactory.build(url = url)

subject.createOrUpdate(model)
val result = subject.getByUrl(url).first()
val id = subject.createOrUpdate(model)
val result = subject.getOrCreateByUrl(url)

assertNotNull(result)
assertEquals(url, result.url)
assertEquals(id, result.id)
}
}
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[versions]

agp = "8.3.2" # Max compatible version https://kotlinlang.org/docs/multiplatform-compatibility-guide.html#version-compatibility
agp = "8.5.2" # Check compatibility https://kotlinlang.org/docs/multiplatform-compatibility-guide.html#version-compatibility

android-compileSdk = "34"
android-minSdk = "24"
android-targetSdk = "34"

compose-plugin = "1.6.11"
kotlin = "2.0.0"
kotlin = "2.0.20"
sqldelight = "2.0.2"
dataStoreVersion = "1.1.1"

Expand Down

0 comments on commit d2d9531

Please sign in to comment.