diff --git a/feature/firmware/src/main/kotlin/org/meshtastic/feature/firmware/ota/BleOtaTransport.kt b/feature/firmware/src/main/kotlin/org/meshtastic/feature/firmware/ota/BleOtaTransport.kt index 2776e42cd..db48737b6 100644 --- a/feature/firmware/src/main/kotlin/org/meshtastic/feature/firmware/ota/BleOtaTransport.kt +++ b/feature/firmware/src/main/kotlin/org/meshtastic/feature/firmware/ota/BleOtaTransport.kt @@ -17,7 +17,9 @@ package org.meshtastic.feature.firmware.ota import co.touchlab.kermit.Logger +import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancel import kotlinx.coroutines.flow.filterNotNull @@ -47,9 +49,13 @@ import kotlin.uuid.toKotlinUuid * - OTA Characteristic (Write): 62ec0272-3ec5-11eb-b378-0242ac130005 * - TX Characteristic (Notify): 62ec0272-3ec5-11eb-b378-0242ac130003 */ -class BleOtaTransport(private val centralManager: CentralManager, private val address: String) : UnifiedOtaProtocol { +class BleOtaTransport( + private val centralManager: CentralManager, + private val address: String, + dispatcher: CoroutineDispatcher = Dispatchers.Default, +) : UnifiedOtaProtocol { - private val transportScope = CoroutineScope(SupervisorJob()) + private val transportScope = CoroutineScope(SupervisorJob() + dispatcher) private var peripheral: Peripheral? = null private var otaCharacteristic: RemoteCharacteristic? = null diff --git a/feature/firmware/src/test/kotlin/org/meshtastic/feature/firmware/ota/BleOtaTransportMtuTest.kt b/feature/firmware/src/test/kotlin/org/meshtastic/feature/firmware/ota/BleOtaTransportMtuTest.kt index 02702d398..5a6b508ae 100644 --- a/feature/firmware/src/test/kotlin/org/meshtastic/feature/firmware/ota/BleOtaTransportMtuTest.kt +++ b/feature/firmware/src/test/kotlin/org/meshtastic/feature/firmware/ota/BleOtaTransportMtuTest.kt @@ -24,6 +24,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.test.UnconfinedTestDispatcher import kotlinx.coroutines.test.runTest import no.nordicsemi.kotlin.ble.client.RemoteCharacteristic import no.nordicsemi.kotlin.ble.client.RemoteService @@ -45,10 +46,11 @@ class BleOtaTransportMtuTest { private val centralManager: CentralManager = mockk(relaxed = true) private val address = "00:11:22:33:44:55" - private val transport = BleOtaTransport(centralManager, address) + private val testDispatcher = UnconfinedTestDispatcher() + private val transport = BleOtaTransport(centralManager, address, testDispatcher) @Test - fun `connect requests MTU`() = runTest { + fun `connect requests MTU`() = runTest(testDispatcher) { val peripheral: Peripheral = mockk(relaxed = true) val otaChar: RemoteCharacteristic = mockk(relaxed = true) val txChar: RemoteCharacteristic = mockk(relaxed = true) diff --git a/feature/firmware/src/test/kotlin/org/meshtastic/feature/firmware/ota/BleOtaTransportTest.kt b/feature/firmware/src/test/kotlin/org/meshtastic/feature/firmware/ota/BleOtaTransportTest.kt index eeda694c9..c5edf8b39 100644 --- a/feature/firmware/src/test/kotlin/org/meshtastic/feature/firmware/ota/BleOtaTransportTest.kt +++ b/feature/firmware/src/test/kotlin/org/meshtastic/feature/firmware/ota/BleOtaTransportTest.kt @@ -23,6 +23,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.test.UnconfinedTestDispatcher import kotlinx.coroutines.test.runTest import no.nordicsemi.kotlin.ble.client.RemoteCharacteristic import no.nordicsemi.kotlin.ble.client.RemoteService @@ -44,10 +45,11 @@ class BleOtaTransportTest { private val centralManager: CentralManager = mockk() private val address = "00:11:22:33:44:55" - private val transport = BleOtaTransport(centralManager, address) + private val testDispatcher = UnconfinedTestDispatcher() + private val transport = BleOtaTransport(centralManager, address, testDispatcher) @Test - fun `race condition check - response before waitForResponse`() = runTest { + fun `race condition check - response before waitForResponse`() = runTest(testDispatcher) { val peripheral: Peripheral = mockk(relaxed = true) val otaChar: RemoteCharacteristic = mockk(relaxed = true) val txChar: RemoteCharacteristic = mockk(relaxed = true) @@ -70,7 +72,7 @@ class BleOtaTransportTest { coEvery { centralManager.connect(any(), any()) } returns Unit - val notificationFlow = MutableSharedFlow() + val notificationFlow = MutableSharedFlow(extraBufferCapacity = 1) every { txChar.subscribe() } returns notificationFlow // Connect