misc(send queue) : do not disable send queue when Network is Offline
This commit is contained in:
parent
d608eeace7
commit
b3a0e2f9ea
2 changed files with 18 additions and 44 deletions
|
|
@ -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.SessionScope
|
||||||
import io.element.android.libraries.di.SingleIn
|
import io.element.android.libraries.di.SingleIn
|
||||||
import io.element.android.libraries.matrix.api.MatrixClient
|
import io.element.android.libraries.matrix.api.MatrixClient
|
||||||
import io.element.android.libraries.matrix.api.core.RoomId
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.FlowPreview
|
import kotlinx.coroutines.FlowPreview
|
||||||
|
import kotlinx.coroutines.flow.combine
|
||||||
import kotlinx.coroutines.flow.debounce
|
import kotlinx.coroutines.flow.debounce
|
||||||
import kotlinx.coroutines.flow.launchIn
|
import kotlinx.coroutines.flow.launchIn
|
||||||
import kotlinx.coroutines.flow.onEach
|
import kotlinx.coroutines.flow.onEach
|
||||||
|
|
@ -29,18 +29,19 @@ class SendQueues @Inject constructor(
|
||||||
private val matrixClient: MatrixClient,
|
private val matrixClient: MatrixClient,
|
||||||
private val networkMonitor: NetworkMonitor,
|
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) {
|
fun launchIn(coroutineScope: CoroutineScope) {
|
||||||
networkMonitor.connectivity
|
combine(
|
||||||
.onEach { networkStatus ->
|
networkMonitor.connectivity,
|
||||||
matrixClient.setAllSendQueuesEnabled(enabled = networkStatus == NetworkStatus.Online)
|
matrixClient.sendQueueDisabledFlow(),
|
||||||
}
|
) { networkStatus, _ -> networkStatus }
|
||||||
.launchIn(coroutineScope)
|
|
||||||
|
|
||||||
@OptIn(FlowPreview::class)
|
|
||||||
matrixClient.sendQueueDisabledFlow()
|
|
||||||
.debounce(SEND_QUEUES_RETRY_DELAY_MILLIS)
|
.debounce(SEND_QUEUES_RETRY_DELAY_MILLIS)
|
||||||
.onEach { _: RoomId ->
|
.onEach { networkStatus ->
|
||||||
if (networkMonitor.connectivity.value == NetworkStatus.Online) {
|
if (networkStatus == NetworkStatus.Online) {
|
||||||
matrixClient.setAllSendQueuesEnabled(enabled = true)
|
matrixClient.setAllSendQueuesEnabled(enabled = true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,8 @@ import kotlinx.coroutines.test.runCurrent
|
||||||
import kotlinx.coroutines.test.runTest
|
import kotlinx.coroutines.test.runTest
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
|
||||||
@OptIn(ExperimentalCoroutinesApi::class) class SendQueuesTest {
|
@OptIn(ExperimentalCoroutinesApi::class)
|
||||||
|
class SendQueuesTest {
|
||||||
private val matrixClient = FakeMatrixClient()
|
private val matrixClient = FakeMatrixClient()
|
||||||
private val networkMonitor = FakeNetworkMonitor()
|
private val networkMonitor = FakeNetworkMonitor()
|
||||||
private val sut = SendQueues(matrixClient, networkMonitor)
|
private val sut = SendQueues(matrixClient, networkMonitor)
|
||||||
|
|
@ -45,11 +46,8 @@ import org.junit.Test
|
||||||
runCurrent()
|
runCurrent()
|
||||||
|
|
||||||
assert(setAllSendQueuesEnabledLambda)
|
assert(setAllSendQueuesEnabledLambda)
|
||||||
.isCalledExactly(2)
|
.isCalledOnce()
|
||||||
.withSequence(
|
.with(value(true))
|
||||||
listOf(value(true)),
|
|
||||||
listOf(value(true)),
|
|
||||||
)
|
|
||||||
|
|
||||||
assert(setRoomSendQueueEnabledLambda).isNeverCalled()
|
assert(setRoomSendQueueEnabledLambda).isNeverCalled()
|
||||||
}
|
}
|
||||||
|
|
@ -74,32 +72,7 @@ import org.junit.Test
|
||||||
advanceTimeBy(SEND_QUEUES_RETRY_DELAY_MILLIS)
|
advanceTimeBy(SEND_QUEUES_RETRY_DELAY_MILLIS)
|
||||||
runCurrent()
|
runCurrent()
|
||||||
|
|
||||||
assert(setAllSendQueuesEnabledLambda)
|
assert(setAllSendQueuesEnabledLambda).isNeverCalled()
|
||||||
.isCalledOnce()
|
assert(setRoomSendQueueEnabledLambda).isNeverCalled()
|
||||||
.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)),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue