Code clarity: use withTimeout with combination of runCatching instead of withTimeoutOrNull
This commit is contained in:
parent
52c039d676
commit
4f320fd4f8
2 changed files with 41 additions and 33 deletions
|
|
@ -29,7 +29,7 @@ import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import kotlinx.coroutines.flow.first
|
import kotlinx.coroutines.flow.first
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withTimeoutOrNull
|
import kotlinx.coroutines.withTimeout
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import kotlin.time.Duration.Companion.seconds
|
import kotlin.time.Duration.Companion.seconds
|
||||||
|
|
@ -72,22 +72,26 @@ class NotificationTest @Inject constructor(
|
||||||
notificationClickHandler.state.first()
|
notificationClickHandler.state.first()
|
||||||
Timber.d("Notification clicked!")
|
Timber.d("Notification clicked!")
|
||||||
}
|
}
|
||||||
val s = withTimeoutOrNull(30.seconds) {
|
runCatching {
|
||||||
job.join()
|
withTimeout(30.seconds) {
|
||||||
}
|
job.join()
|
||||||
job.cancel()
|
}
|
||||||
if (s == null) {
|
}.fold(
|
||||||
notificationDisplayer.dismissDiagnosticNotification()
|
onSuccess = {
|
||||||
delegate.updateState(
|
delegate.updateState(
|
||||||
description = stringProvider.getString(R.string.troubleshoot_notifications_test_display_notification_failure),
|
description = stringProvider.getString(R.string.troubleshoot_notifications_test_display_notification_success),
|
||||||
status = NotificationTroubleshootTestState.Status.Failure(false)
|
status = NotificationTroubleshootTestState.Status.Success
|
||||||
)
|
)
|
||||||
} else {
|
},
|
||||||
delegate.updateState(
|
onFailure = {
|
||||||
description = stringProvider.getString(R.string.troubleshoot_notifications_test_display_notification_success),
|
job.cancel()
|
||||||
status = NotificationTroubleshootTestState.Status.Success
|
notificationDisplayer.dismissDiagnosticNotification()
|
||||||
)
|
delegate.updateState(
|
||||||
}
|
description = stringProvider.getString(R.string.troubleshoot_notifications_test_display_notification_failure),
|
||||||
|
status = NotificationTroubleshootTestState.Status.Failure(false)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
}.invokeOnCompletion {
|
}.invokeOnCompletion {
|
||||||
// Ensure that the notification is cancelled when the screen is left
|
// Ensure that the notification is cancelled when the screen is left
|
||||||
notificationDisplayer.dismissDiagnosticNotification()
|
notificationDisplayer.dismissDiagnosticNotification()
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import kotlinx.coroutines.flow.first
|
import kotlinx.coroutines.flow.first
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withTimeoutOrNull
|
import kotlinx.coroutines.withTimeout
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import kotlin.time.Duration.Companion.seconds
|
import kotlin.time.Duration.Companion.seconds
|
||||||
|
|
@ -84,21 +84,25 @@ class PushLoopbackTest @Inject constructor(
|
||||||
job.cancel()
|
job.cancel()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
val result = withTimeoutOrNull(10.seconds) {
|
runCatching {
|
||||||
completable.await()
|
withTimeout(10.seconds) {
|
||||||
}
|
completable.await()
|
||||||
job.cancel()
|
}
|
||||||
if (result == null) {
|
}.fold(
|
||||||
delegate.updateState(
|
onSuccess = { duration ->
|
||||||
description = stringProvider.getString(R.string.troubleshoot_notifications_test_push_loop_back_failure_4),
|
delegate.updateState(
|
||||||
status = NotificationTroubleshootTestState.Status.Failure(false)
|
description = stringProvider.getString(R.string.troubleshoot_notifications_test_push_loop_back_success, duration),
|
||||||
)
|
status = NotificationTroubleshootTestState.Status.Success
|
||||||
} else {
|
)
|
||||||
delegate.updateState(
|
},
|
||||||
description = stringProvider.getString(R.string.troubleshoot_notifications_test_push_loop_back_success, result),
|
onFailure = {
|
||||||
status = NotificationTroubleshootTestState.Status.Success
|
job.cancel()
|
||||||
)
|
delegate.updateState(
|
||||||
}
|
description = stringProvider.getString(R.string.troubleshoot_notifications_test_push_loop_back_failure_4),
|
||||||
|
status = NotificationTroubleshootTestState.Status.Failure(false)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun reset() = delegate.reset()
|
override suspend fun reset() = delegate.reset()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue