Skip to content

Commit

Permalink
Missing method and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sdsantos committed Aug 5, 2024
1 parent 1199e6e commit 5625b94
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 1 deletion.
12 changes: 12 additions & 0 deletions composeApp/src/commonMain/kotlin/org/ooni/engine/Engine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.withContext
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.ooni.engine.OonimkallBridge.SubmitMeasurementResults
import org.ooni.engine.models.TaskEvent
import org.ooni.engine.models.TaskEventResult
import org.ooni.engine.models.TaskLogLevel
Expand Down Expand Up @@ -51,6 +52,15 @@ class Engine(
}
}.flowOn(backgroundDispatcher)

suspend fun submitMeasurements(
measurement: String,
taskOrigin: TaskOrigin = TaskOrigin.OoniRun,
): SubmitMeasurementResults =
withContext(backgroundDispatcher) {
val sessionConfig = buildSessionConfig(taskOrigin)
session(sessionConfig).submitMeasurement(measurement)
}

suspend fun checkIn(
categories: List<String>,
taskOrigin: TaskOrigin,
Expand Down Expand Up @@ -113,6 +123,8 @@ class Engine(
noCollector = true,
softwareName = buildSoftwareName(taskOrigin),
softwareVersion = platformInfo.version,
// TODO: fetch from preferences
maxRuntime = -1,
),
annotations =
TaskSettings.Annotations(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ data class TaskSettings(
// built from the flavors + debug or not + -unattended if autorun
@SerialName("software_name") val softwareName: String,
@SerialName("software_version") val softwareVersion: String,
@SerialName("max_runtime") val maxRuntime: Int? = null,
@SerialName("max_runtime") val maxRuntime: Int,
)

@Serializable
Expand Down
60 changes: 60 additions & 0 deletions composeApp/src/commonTest/kotlin/org/ooni/engine/EngineTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.ooni.engine

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.test.runTest
import org.ooni.engine.models.NetworkType
import org.ooni.engine.models.TaskEvent
import org.ooni.engine.models.TaskOrigin
import org.ooni.engine.models.TaskSettings
import org.ooni.probe.di.Dependencies
import org.ooni.probe.shared.Platform
import org.ooni.probe.shared.PlatformInfo
import kotlin.test.Test
import kotlin.test.assertEquals

class EngineTest {
private val json = Dependencies.buildJson()
private val networkTypeFinder = NetworkTypeFinder { NetworkType.NoInternet }

@Test
fun startTaskAndGetEvents() =
runTest {
val bridge = TestOonimkallBridge()
bridge.addNextEvents("""{"key":"status.started","value":{}}""")
val engine = buildEngine(bridge)

val events =
engine.startTask(
name = "web_connectivity",
inputs = listOf("https://ooni.org"),
TaskOrigin.OoniRun,
).toList()

assertEquals(1, events.size)
assertEquals(TaskEvent.Started, events.first())

val settings = json.decodeFromString<TaskSettings>(bridge.lastStartTaskSettingsSerialized!!)
assertEquals("web_connectivity", settings.name)
assertEquals(listOf("https://ooni.org"), settings.inputs)
assertEquals(NetworkType.NoInternet, settings.annotations.networkType)
}

private fun buildEngine(bridge: OonimkallBridge) =
Engine(
bridge = bridge,
json = json,
baseFilePath = "",
cacheDir = "",
taskEventMapper = TaskEventMapper(networkTypeFinder, json),
networkTypeFinder = networkTypeFinder,
platformInfo =
object : PlatformInfo {
override val version = "1"
override val platform = Platform.Ios
override val osVersion = "1"
override val model = "test"
},
backgroundDispatcher = Dispatchers.Default,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.ooni.engine

/*
Bridge implementation to be used in tests
*/
class TestOonimkallBridge : OonimkallBridge {
// Test helpers

private val nextEvents = mutableListOf<String>()

fun addNextEvents(vararg events: String) {
nextEvents.addAll(events)
}

var lastStartTaskSettingsSerialized: String? = null
private set

var lastSessionConfig: OonimkallBridge.SessionConfig? = null
private set

var submitMeasurement: ((String) -> OonimkallBridge.SubmitMeasurementResults)? = null
var checkIn: ((OonimkallBridge.CheckInConfig) -> OonimkallBridge.CheckInResults)? = null
var httpDo: ((OonimkallBridge.HTTPRequest) -> OonimkallBridge.HTTPResponse)? = null

// Base implementation

override fun startTask(settingsSerialized: String): OonimkallBridge.Task {
lastStartTaskSettingsSerialized = settingsSerialized
return object : OonimkallBridge.Task {
override fun interrupt() {}

override fun isDone() = nextEvents.isEmpty()

override fun waitForNextEvent(): String {
return nextEvents.removeAt(0)
}
}
}

override fun newSession(sessionConfig: OonimkallBridge.SessionConfig): OonimkallBridge.Session {
lastSessionConfig = sessionConfig
return Session()
}

class Session : OonimkallBridge.Session {
override fun submitMeasurement(measurement: String): OonimkallBridge.SubmitMeasurementResults {
return submitMeasurement(measurement)
}

override fun checkIn(config: OonimkallBridge.CheckInConfig): OonimkallBridge.CheckInResults {
return checkIn(config)
}

override fun httpDo(request: OonimkallBridge.HTTPRequest): OonimkallBridge.HTTPResponse {
return httpDo(request)
}
}
}

0 comments on commit 5625b94

Please sign in to comment.