From 178227e39f6544174b6e8680eb1370f80680b9ca Mon Sep 17 00:00:00 2001 From: Norbel AMBANUMBEN Date: Mon, 25 Nov 2024 12:12:30 +0100 Subject: [PATCH] feat: limit test duration should only apply to the OONI websites descriptor (#292) * chore: remove man runtime for autorun --- .../kotlin/org/ooni/engine/Engine.kt | 31 ++++++++++++++----- .../kotlin/org/ooni/engine/models/TestType.kt | 3 +- .../org/ooni/probe/data/models/NetTest.kt | 1 + .../org/ooni/probe/domain/RunDescriptors.kt | 5 ++- .../org/ooni/probe/domain/RunNetTest.kt | 5 ++- .../kotlin/org/ooni/engine/EngineTest.kt | 8 +++-- 6 files changed, 38 insertions(+), 15 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/org/ooni/engine/Engine.kt b/composeApp/src/commonMain/kotlin/org/ooni/engine/Engine.kt index 6815eb3d..f06299af 100644 --- a/composeApp/src/commonMain/kotlin/org/ooni/engine/Engine.kt +++ b/composeApp/src/commonMain/kotlin/org/ooni/engine/Engine.kt @@ -19,10 +19,13 @@ import org.ooni.engine.models.TaskSettings import org.ooni.engine.models.resultOf import org.ooni.probe.config.OrganizationConfig import org.ooni.probe.data.models.InstalledTestDescriptorModel +import org.ooni.probe.data.models.NetTest import org.ooni.probe.shared.PlatformInfo import org.ooni.probe.shared.value import kotlin.coroutines.CoroutineContext +const val MAX_RUNTIME_DISABLED = -1 + class Engine( @VisibleForTesting var bridge: OonimkallBridge, @@ -38,15 +41,14 @@ class Engine( private val backgroundContext: CoroutineContext, ) { fun startTask( - name: String, - inputs: List?, + netTest: NetTest, taskOrigin: TaskOrigin, descriptorId: InstalledTestDescriptorModel.Id?, ): Flow = channelFlow { val preferences = getEnginePreferences() val taskSettings = - buildTaskSettings(name, inputs, taskOrigin, preferences, descriptorId) + buildTaskSettings(netTest, taskOrigin, preferences, descriptorId) val settingsSerialized = json.encodeToString(taskSettings) var task: OonimkallBridge.Task? = null @@ -116,14 +118,13 @@ class Engine( private fun session(sessionConfig: OonimkallBridge.SessionConfig): OonimkallBridge.Session = bridge.newSession(sessionConfig) private fun buildTaskSettings( - name: String, - inputs: List?, + netTest: NetTest, taskOrigin: TaskOrigin, preferences: EnginePreferences, descriptorId: InstalledTestDescriptorModel.Id?, ) = TaskSettings( - name = name, - inputs = inputs.orEmpty(), + name = netTest.test.name, + inputs = netTest.inputs.orEmpty(), disabledEvents = listOf( "status.queued", "status.update.websites", @@ -138,7 +139,7 @@ class Engine( noCollector = !preferences.uploadResults, softwareName = buildSoftwareName(taskOrigin), softwareVersion = platformInfo.buildName, - maxRuntime = preferences.maxRuntime?.inWholeSeconds?.toInt() ?: -1, + maxRuntime = maxRuntime(taskOrigin, netTest, preferences), ), annotations = TaskSettings.Annotations( networkType = networkTypeFinder(), @@ -149,6 +150,20 @@ class Engine( proxy = preferences.proxy, ) + private fun maxRuntime( + taskOrigin: TaskOrigin, + netTest: NetTest, + preferences: EnginePreferences, + ) = if (taskOrigin == TaskOrigin.AutoRun) { + MAX_RUNTIME_DISABLED + } else if (netTest.callCheckIn) { + preferences.maxRuntime?.inWholeSeconds?.toInt()?.let { maxRuntimePreference -> + if (maxRuntimePreference > 0) 30 + maxRuntimePreference else MAX_RUNTIME_DISABLED + } ?: MAX_RUNTIME_DISABLED + } else { + MAX_RUNTIME_DISABLED + } + private fun buildSessionConfig( taskOrigin: TaskOrigin, preferences: EnginePreferences, diff --git a/composeApp/src/commonMain/kotlin/org/ooni/engine/models/TestType.kt b/composeApp/src/commonMain/kotlin/org/ooni/engine/models/TestType.kt index 4d9f3bc8..456b6f5e 100644 --- a/composeApp/src/commonMain/kotlin/org/ooni/engine/models/TestType.kt +++ b/composeApp/src/commonMain/kotlin/org/ooni/engine/models/TestType.kt @@ -34,6 +34,7 @@ import org.jetbrains.compose.resources.StringResource import org.jetbrains.compose.resources.stringResource import kotlin.time.Duration import kotlin.time.Duration.Companion.seconds +import kotlin.time.times @Serializable(with = TestTypeSerializer::class) sealed class TestType { @@ -140,7 +141,7 @@ sealed class TestType { override val iconRes: DrawableResource = Res.drawable.test_websites override val url: String = "https://ooni.org/nettest/web-connectivity" - override fun runtime(inputs: List?) = 5.seconds * (inputs?.ifEmpty { null }?.size ?: 30) + override fun runtime(inputs: List?) = 30.seconds + inputs.orEmpty().size.times(5.seconds) } data object Whatsapp : TestType() { diff --git a/composeApp/src/commonMain/kotlin/org/ooni/probe/data/models/NetTest.kt b/composeApp/src/commonMain/kotlin/org/ooni/probe/data/models/NetTest.kt index 7b2aa7fd..1fdd2403 100644 --- a/composeApp/src/commonMain/kotlin/org/ooni/probe/data/models/NetTest.kt +++ b/composeApp/src/commonMain/kotlin/org/ooni/probe/data/models/NetTest.kt @@ -8,6 +8,7 @@ import org.ooni.engine.models.TestType data class NetTest( val test: TestType, val inputs: List? = emptyList(), + val callCheckIn: Boolean = false, ) { fun toOONI() = OONINetTest(test.name, inputs) diff --git a/composeApp/src/commonMain/kotlin/org/ooni/probe/domain/RunDescriptors.kt b/composeApp/src/commonMain/kotlin/org/ooni/probe/domain/RunDescriptors.kt index d8c37cb9..529a47d1 100644 --- a/composeApp/src/commonMain/kotlin/org/ooni/probe/domain/RunDescriptors.kt +++ b/composeApp/src/commonMain/kotlin/org/ooni/probe/domain/RunDescriptors.kt @@ -81,7 +81,10 @@ class RunDescriptors( taskOrigin: TaskOrigin, descriptor: Descriptor, ): List = - map { test -> test.copy(inputs = test.inputsOrDownloadUrls(taskOrigin, descriptor)) } + map { test -> + val urls = test.inputsOrDownloadUrls(taskOrigin, descriptor) + test.copy(inputs = urls, callCheckIn = test.inputs != urls) + } .filterNot { it.test is TestType.WebConnectivity && it.inputs?.any() != true } private suspend fun NetTest.inputsOrDownloadUrls( diff --git a/composeApp/src/commonMain/kotlin/org/ooni/probe/domain/RunNetTest.kt b/composeApp/src/commonMain/kotlin/org/ooni/probe/domain/RunNetTest.kt index a852a318..f6f27216 100644 --- a/composeApp/src/commonMain/kotlin/org/ooni/probe/domain/RunNetTest.kt +++ b/composeApp/src/commonMain/kotlin/org/ooni/probe/domain/RunNetTest.kt @@ -21,7 +21,7 @@ import org.ooni.probe.data.models.UrlModel import org.ooni.probe.shared.toLocalDateTime class RunNetTest( - private val startTest: (String, List?, TaskOrigin, InstalledTestDescriptorModel.Id?) -> Flow, + private val startTest: (NetTest, TaskOrigin, InstalledTestDescriptorModel.Id?) -> Flow, private val getOrCreateUrl: suspend (String) -> UrlModel, private val storeMeasurement: suspend (MeasurementModel) -> MeasurementModel.Id, private val storeNetwork: suspend (NetworkModel) -> NetworkModel.Id, @@ -64,8 +64,7 @@ class RunNetTest( (spec.descriptor.source as? Descriptor.Source.Installed)?.value?.id startTest( - spec.netTest.test.name, - spec.netTest.inputs, + spec.netTest, spec.taskOrigin, installedDescriptorId, ) diff --git a/composeApp/src/commonTest/kotlin/org/ooni/engine/EngineTest.kt b/composeApp/src/commonTest/kotlin/org/ooni/engine/EngineTest.kt index 5128dfe5..d66b7dbc 100644 --- a/composeApp/src/commonTest/kotlin/org/ooni/engine/EngineTest.kt +++ b/composeApp/src/commonTest/kotlin/org/ooni/engine/EngineTest.kt @@ -10,6 +10,8 @@ import org.ooni.engine.models.TaskEvent import org.ooni.engine.models.TaskLogLevel import org.ooni.engine.models.TaskOrigin import org.ooni.engine.models.TaskSettings +import org.ooni.engine.models.TestType +import org.ooni.probe.data.models.NetTest import org.ooni.probe.di.Dependencies import org.ooni.probe.shared.Platform import org.ooni.probe.shared.PlatformInfo @@ -29,8 +31,10 @@ class EngineTest { val engine = buildEngine(bridge) val events = engine.startTask( - name = "web_connectivity", - inputs = listOf("https://ooni.org"), + NetTest( + test = TestType.WebConnectivity, + inputs = listOf("https://ooni.org"), + ), taskOrigin = TaskOrigin.OoniRun, descriptorId = null, ).toList()