Skip to content

Commit

Permalink
feat: limit test duration should only apply to the OONI websites desc…
Browse files Browse the repository at this point in the history
…riptor (#292)

* chore: remove man runtime for autorun
  • Loading branch information
aanorbel authored Nov 25, 2024
1 parent 62736eb commit 178227e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 15 deletions.
31 changes: 23 additions & 8 deletions composeApp/src/commonMain/kotlin/org/ooni/engine/Engine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -38,15 +41,14 @@ class Engine(
private val backgroundContext: CoroutineContext,
) {
fun startTask(
name: String,
inputs: List<String>?,
netTest: NetTest,
taskOrigin: TaskOrigin,
descriptorId: InstalledTestDescriptorModel.Id?,
): Flow<TaskEvent> =
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
Expand Down Expand Up @@ -116,14 +118,13 @@ class Engine(
private fun session(sessionConfig: OonimkallBridge.SessionConfig): OonimkallBridge.Session = bridge.newSession(sessionConfig)

private fun buildTaskSettings(
name: String,
inputs: List<String>?,
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",
Expand All @@ -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(),
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<String>?) = 5.seconds * (inputs?.ifEmpty { null }?.size ?: 30)
override fun runtime(inputs: List<String>?) = 30.seconds + inputs.orEmpty().size.times(5.seconds)
}

data object Whatsapp : TestType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.ooni.engine.models.TestType
data class NetTest(
val test: TestType,
val inputs: List<String>? = emptyList(),
val callCheckIn: Boolean = false,
) {
fun toOONI() = OONINetTest(test.name, inputs)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ class RunDescriptors(
taskOrigin: TaskOrigin,
descriptor: Descriptor,
): List<NetTest> =
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.ooni.probe.data.models.UrlModel
import org.ooni.probe.shared.toLocalDateTime

class RunNetTest(
private val startTest: (String, List<String>?, TaskOrigin, InstalledTestDescriptorModel.Id?) -> Flow<TaskEvent>,
private val startTest: (NetTest, TaskOrigin, InstalledTestDescriptorModel.Id?) -> Flow<TaskEvent>,
private val getOrCreateUrl: suspend (String) -> UrlModel,
private val storeMeasurement: suspend (MeasurementModel) -> MeasurementModel.Id,
private val storeNetwork: suspend (NetworkModel) -> NetworkModel.Id,
Expand Down Expand Up @@ -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,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit 178227e

Please sign in to comment.