misc(send queue) : do not disable send queue when Network is Offline

This commit is contained in:
ganfra
2024-12-20 18:15:45 +01:00
parent d608eeace7
commit b3a0e2f9ea
2 changed files with 18 additions and 44 deletions

View File

@@ -13,9 +13,9 @@ import io.element.android.features.networkmonitor.api.NetworkStatus
import io.element.android.libraries.di.SessionScope
import io.element.android.libraries.di.SingleIn
import io.element.android.libraries.matrix.api.MatrixClient
import io.element.android.libraries.matrix.api.core.RoomId
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
@@ -29,18 +29,19 @@ class SendQueues @Inject constructor(
private val matrixClient: MatrixClient,
private val networkMonitor: NetworkMonitor,
) {
/**
* Launches the send queues retry mechanism in the given [coroutineScope].
* Makes sure to re-enable all send queues when the network status is [NetworkStatus.Online].
*/
@OptIn(FlowPreview::class)
fun launchIn(coroutineScope: CoroutineScope) {
networkMonitor.connectivity
.onEach { networkStatus ->
matrixClient.setAllSendQueuesEnabled(enabled = networkStatus == NetworkStatus.Online)
}
.launchIn(coroutineScope)
@OptIn(FlowPreview::class)
matrixClient.sendQueueDisabledFlow()
combine(
networkMonitor.connectivity,
matrixClient.sendQueueDisabledFlow(),
) { networkStatus, _ -> networkStatus }
.debounce(SEND_QUEUES_RETRY_DELAY_MILLIS)
.onEach { _: RoomId ->
if (networkMonitor.connectivity.value == NetworkStatus.Online) {
.onEach { networkStatus ->
if (networkStatus == NetworkStatus.Online) {
matrixClient.setAllSendQueuesEnabled(enabled = true)
}
}

View File

@@ -22,7 +22,8 @@ import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Test
@OptIn(ExperimentalCoroutinesApi::class) class SendQueuesTest {
@OptIn(ExperimentalCoroutinesApi::class)
class SendQueuesTest {
private val matrixClient = FakeMatrixClient()
private val networkMonitor = FakeNetworkMonitor()
private val sut = SendQueues(matrixClient, networkMonitor)
@@ -45,11 +46,8 @@ import org.junit.Test
runCurrent()
assert(setAllSendQueuesEnabledLambda)
.isCalledExactly(2)
.withSequence(
listOf(value(true)),
listOf(value(true)),
)
.isCalledOnce()
.with(value(true))
assert(setRoomSendQueueEnabledLambda).isNeverCalled()
}
@@ -74,32 +72,7 @@ import org.junit.Test
advanceTimeBy(SEND_QUEUES_RETRY_DELAY_MILLIS)
runCurrent()
assert(setAllSendQueuesEnabledLambda)
.isCalledOnce()
.with(value(false))
assert(setRoomSendQueueEnabledLambda)
.isNeverCalled()
}
@Test
fun `test network status getting offline and online`() = runTest {
val setEnableSendingQueueLambda = lambdaRecorder { _: Boolean -> }
matrixClient.setAllSendQueuesEnabledLambda = setEnableSendingQueueLambda
sut.launchIn(backgroundScope)
advanceTimeBy(SEND_QUEUES_RETRY_DELAY_MILLIS)
networkMonitor.connectivity.value = NetworkStatus.Offline
advanceTimeBy(SEND_QUEUES_RETRY_DELAY_MILLIS)
networkMonitor.connectivity.value = NetworkStatus.Online
advanceTimeBy(SEND_QUEUES_RETRY_DELAY_MILLIS)
assert(setEnableSendingQueueLambda)
.isCalledExactly(3)
.withSequence(
listOf(value(true)),
listOf(value(false)),
listOf(value(true)),
)
assert(setAllSendQueuesEnabledLambda).isNeverCalled()
assert(setRoomSendQueueEnabledLambda).isNeverCalled()
}
}