Skip to content

Commit

Permalink
Tweak run stopping
Browse files Browse the repository at this point in the history
  • Loading branch information
sdsantos committed Nov 12, 2024
1 parent 88c02fe commit 16ac9a6
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 24 deletions.
17 changes: 9 additions & 8 deletions composeApp/src/commonMain/kotlin/org/ooni/engine/Engine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.ooni.engine

import androidx.annotation.VisibleForTesting
import co.touchlab.kermit.Logger
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.channelFlow
Expand Down Expand Up @@ -37,14 +36,14 @@ class Engine(
private val isBatteryCharging: suspend () -> Boolean,
private val platformInfo: PlatformInfo,
private val getEnginePreferences: suspend () -> EnginePreferences,
private val observeCancelTestRun: () -> Flow<Unit>,
private val backgroundContext: CoroutineContext,
) {
fun startTask(
name: String,
inputs: List<String>?,
taskOrigin: TaskOrigin,
descriptorId: InstalledTestDescriptorModel.Id?,
observeCancelTestRun: Flow<Unit>,
): Flow<TaskEvent> =
channelFlow {
val preferences = getEnginePreferences()
Expand All @@ -56,7 +55,7 @@ class Engine(
task = bridge.startTask(settingsSerialized)

val cancelJob = async {
observeCancelTestRun
observeCancelTestRun()
.take(1)
.collect {
task.interrupt()
Expand All @@ -68,15 +67,17 @@ class Engine(
val taskEventResult = json.decodeFromString<TaskEventResult>(eventJson)
taskEventMapper(taskEventResult)?.let { send(it) }
}
cancelJob.cancel()
} catch (e: CancellationException) {
Logger.d("Test cancelled")
throw e

if (cancelJob.isActive) {
cancelJob.cancel()
}
} catch (e: Exception) {
Logger.d("Error while running task", e)
throw MkException(e)
} finally {
task?.interrupt()
if (task?.isDone() == false) {
task.interrupt()
}
}
}.flowOn(backgroundContext)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ class Dependencies(
isBatteryCharging = isBatteryCharging,
platformInfo = platformInfo,
getEnginePreferences = getEnginePreferences::invoke,
observeCancelTestRun = testStateManager::observeCancels,
backgroundContext = backgroundContext,
)
}
Expand Down Expand Up @@ -355,7 +356,6 @@ class Dependencies(
deleteFiles = deleteFiles,
json = json,
spec = spec,
observeCancelTestRun = testStateManager.observeCancels(),
)

// Background
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,6 @@ class RunDescriptors(
spec: RunSpecification,
) {
coroutineScope {
val runJob = async {
// Actually running the descriptors
descriptors.forEachIndexed { index, descriptor ->
// check if cancel has been requested before running descriptor
if (getCurrentTestRunState.first() is TestRunState.Stopping) {
return@forEachIndexed
}
runDescriptor(descriptor, index, spec.taskOrigin, spec.isRerun)
}
}
// Observe if a cancel request has been made
val cancelJob = async {
observeCancelTestRun
Expand All @@ -84,7 +74,11 @@ class RunDescriptors(
}
}

runJob.await()
// Actually running the descriptors
descriptors.forEachIndexed { index, descriptor ->
if (isRunStopped()) return@forEachIndexed
runDescriptor(descriptor, index, spec.taskOrigin, spec.isRerun)
}

if (cancelJob.isActive) {
cancelJob.cancel()
Expand Down Expand Up @@ -142,6 +136,7 @@ class RunDescriptors(
val resultId = storeResult(result)

descriptor.allTests.forEachIndexed { testIndex, netTest ->
if (isRunStopped()) return@forEachIndexed
runNetTest(
RunNetTest.Specification(
descriptor = descriptor,
Expand All @@ -158,4 +153,6 @@ class RunDescriptors(

markResultAsDone(resultId)
}

private suspend fun isRunStopped() = getCurrentTestRunState.first() is TestRunState.Stopping
}
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<Unit>) -> Flow<TaskEvent>,
private val startTest: (String, List<String>?, 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 All @@ -31,7 +31,6 @@ class RunNetTest(
private val deleteFiles: DeleteFiles,
private val json: Json,
private val spec: Specification,
private val observeCancelTestRun: Flow<Unit>,
) {
data class Specification(
val descriptor: Descriptor,
Expand Down Expand Up @@ -69,7 +68,6 @@ class RunNetTest(
spec.netTest.inputs,
spec.taskOrigin,
installedDescriptorId,
observeCancelTestRun,
)
.collect(::onEvent)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class EngineTest {
inputs = listOf("https://ooni.org"),
taskOrigin = TaskOrigin.OoniRun,
descriptorId = null,
observeCancelTestRun = emptyFlow(),
).toList()

assertEquals(1, events.size)
Expand Down Expand Up @@ -87,6 +86,7 @@ class EngineTest {
maxRuntime = null,
)
},
observeCancelTestRun = { emptyFlow() },
backgroundContext = Dispatchers.Unconfined,
)
}

0 comments on commit 16ac9a6

Please sign in to comment.