Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add delay at least wrapper and apply to test api method #6387

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions android/app/src/main/kotlin/net/mullvad/mullvadvpn/util/Delay.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package net.mullvad.mullvadvpn.util

import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay

suspend fun <T> delayAtLeast(duration: Long, f: suspend () -> T): T = coroutineScope {
val result = async { f() }
delay(timeMillis = duration)
result.await()
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import net.mullvad.mullvadvpn.compose.state.ApiAccessMethodDetailsUiState
import net.mullvad.mullvadvpn.constant.MINIMUM_LOADING_TIME_MILLIS
import net.mullvad.mullvadvpn.lib.model.ApiAccessMethod
import net.mullvad.mullvadvpn.lib.model.ApiAccessMethodId
import net.mullvad.mullvadvpn.lib.model.TestApiAccessMethodError
import net.mullvad.mullvadvpn.repository.ApiAccessRepository
import net.mullvad.mullvadvpn.util.delayAtLeast

class ApiAccessMethodDetailsViewModel(
private val apiAccessMethodId: ApiAccessMethodId,
Expand Down Expand Up @@ -107,8 +109,9 @@ class ApiAccessMethodDetailsViewModel(

private suspend fun testMethodById(): Either<TestApiAccessMethodError, Unit> {
isTestingApiAccessMethodState.value = true
return apiAccessRepository
.testApiAccessMethodById(apiAccessMethodId)
return delayAtLeast(MINIMUM_LOADING_TIME_MILLIS) {
apiAccessRepository.testApiAccessMethodById(apiAccessMethodId)
}
.onLeft { isTestingApiAccessMethodState.value = false }
.onRight { isTestingApiAccessMethodState.value = false }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import kotlinx.coroutines.launch
import net.mullvad.mullvadvpn.compose.state.ApiAccessMethodTypes
import net.mullvad.mullvadvpn.compose.state.EditApiAccessFormData
import net.mullvad.mullvadvpn.compose.state.EditApiAccessMethodUiState
import net.mullvad.mullvadvpn.constant.MINIMUM_LOADING_TIME_MILLIS
import net.mullvad.mullvadvpn.lib.model.ApiAccessMethod
import net.mullvad.mullvadvpn.lib.model.ApiAccessMethodId
import net.mullvad.mullvadvpn.lib.model.ApiAccessMethodName
Expand All @@ -32,6 +33,7 @@ import net.mullvad.mullvadvpn.lib.model.ParsePortError
import net.mullvad.mullvadvpn.lib.model.Port
import net.mullvad.mullvadvpn.lib.model.SocksAuth
import net.mullvad.mullvadvpn.repository.ApiAccessRepository
import net.mullvad.mullvadvpn.util.delayAtLeast
import org.apache.commons.validator.routines.InetAddressValidator

class EditApiAccessMethodViewModel(
Expand Down Expand Up @@ -104,7 +106,10 @@ class EditApiAccessMethodViewModel(
{ errors -> formData.update { it.updateWithErrors(errors) } },
{ customProxy ->
isTestingApiAccessMethod.value = true
val result = apiAccessRepository.testCustomApiAccessMethod(customProxy)
val result =
delayAtLeast(MINIMUM_LOADING_TIME_MILLIS) {
apiAccessRepository.testCustomApiAccessMethod(customProxy)
}
_uiSideEffect.send(
EditApiAccessSideEffect.TestApiAccessMethodResult(result.isRight())
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import net.mullvad.mullvadvpn.lib.model.LoginAccountError
import net.mullvad.mullvadvpn.lib.shared.AccountRepository
import net.mullvad.mullvadvpn.repository.NewDeviceRepository
import net.mullvad.mullvadvpn.usecase.InternetAvailableUseCase
import net.mullvad.mullvadvpn.util.delayAtLeast
import net.mullvad.mullvadvpn.util.getOrDefault

private const val MINIMUM_LOADING_SPINNER_TIME_MILLIS = 500L
Expand Down Expand Up @@ -101,15 +102,12 @@ class LoginViewModel(
}
_loginState.value = Loading.LoggingIn
viewModelScope.launch(dispatcher) {
// Ensure we always take at least MINIMUM_LOADING_SPINNER_TIME_MILLIS to show the
// loading indicator
val result = async { accountRepository.login(AccountNumber(accountNumber)) }

delay(MINIMUM_LOADING_SPINNER_TIME_MILLIS)

val uiState =
result
.await()
// Ensure we always take at least MINIMUM_LOADING_SPINNER_TIME_MILLIS to show the
// loading indicator
delayAtLeast(MINIMUM_LOADING_SPINNER_TIME_MILLIS) {
accountRepository.login(AccountNumber(accountNumber))
}
.fold(
{ it.toUiState() },
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package net.mullvad.mullvadvpn.viewmodel

import app.cash.turbine.test
import arrow.core.Either
import arrow.core.left
import arrow.core.right
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import java.time.Duration
import kotlin.test.assertIs
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -20,6 +22,7 @@ import net.mullvad.mullvadvpn.lib.model.ApiAccessMethodSetting
import net.mullvad.mullvadvpn.lib.model.TestApiAccessMethodError
import net.mullvad.mullvadvpn.lib.model.UnknownApiAccessMethodError
import net.mullvad.mullvadvpn.repository.ApiAccessRepository
import net.mullvad.mullvadvpn.util.delayAtLeast
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
Expand All @@ -38,6 +41,7 @@ class ApiAccessMethodDetailsViewModelTest {

@BeforeEach
fun setUp() {
mockkStatic(DELAY_UTIL)
every { mockApiAccessRepository.apiAccessMethodSettingById(apiAccessMethodId) } returns
accessMethodFlow
every { mockApiAccessRepository.enabledApiAccessMethods() } returns enabledMethodsFlow
Expand All @@ -58,6 +62,8 @@ class ApiAccessMethodDetailsViewModelTest {
Unit.right()
coEvery { mockApiAccessRepository.setCurrentApiAccessMethod(any()) } returns
Unit.right()
coEvery { delayAtLeast<Either<TestApiAccessMethodError, Unit>>(any(), any()) } returns
Unit.right()

// Act
apiAccessMethodDetailsViewModel.setCurrentMethod()
Expand Down Expand Up @@ -177,4 +183,8 @@ class ApiAccessMethodDetailsViewModelTest {
)
}
}

companion object {
private const val DELAY_UTIL = "net.mullvad.mullvadvpn.util.DelayKt"
}
}
Loading