-
Notifications
You must be signed in to change notification settings - Fork 122
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
Error Handling for urlConnection #2902
Open
anandwana001
wants to merge
19
commits into
master
Choose a base branch
from
anandwana001/2683/prevent-socket-timeout-exception
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
494f766
added try catch
anandwana001 e00b968
Merge branch 'master' into anandwana001/2683/prevent-socket-timeout-e…
anandwana001 d41ab05
not throwing
anandwana001 ff4c85f
Merge branch 'master' into anandwana001/2683/prevent-socket-timeout-e…
anandwana001 96871f2
added failure handling
anandwana001 60d4787
nit fix
anandwana001 b70eb4a
Merge branch 'master' into anandwana001/2683/prevent-socket-timeout-e…
anandwana001 a16a172
nit fixes
anandwana001 e8555fe
Merge remote-tracking branch 'origin/anandwana001/2683/prevent-socket…
anandwana001 6b8fa5e
Merge branch 'master' into anandwana001/2683/prevent-socket-timeout-e…
anandwana001 a0fe855
Merge branch 'master' into anandwana001/2683/prevent-socket-timeout-e…
anandwana001 9530810
Merge branch 'master' into anandwana001/2683/prevent-socket-timeout-e…
shobhitagarwal1612 48890ae
Merge branch 'master' into anandwana001/2683/prevent-socket-timeout-e…
anandwana001 7bcb1a0
partial test added
anandwana001 4d1c6ad
Merge branch 'master' into anandwana001/2683/prevent-socket-timeout-e…
anandwana001 011d10f
Merge branch 'master' into anandwana001/2683/prevent-socket-timeout-e…
anandwana001 2d7e8eb
import fix
anandwana001 f88c972
Merge branch 'master' into anandwana001/2683/prevent-socket-timeout-e…
anandwana001 ea08f37
merge latest
anandwana001 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ import kotlinx.coroutines.CoroutineDispatcher | |
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.launch | ||
|
||
private const val MIN_DOWNLOAD_ZOOM_LEVEL = 9 | ||
|
@@ -78,6 +79,7 @@ internal constructor( | |
val downloadProgress = MutableLiveData(0f) | ||
val bottomText = MutableLiveData<String?>(null) | ||
val downloadButtonEnabled = MutableLiveData(false) | ||
val isFailure = MutableLiveData(false) | ||
|
||
private val _navigate = MutableSharedFlow<UiState>(replay = 0) | ||
val navigate = _navigate.asSharedFlow() | ||
|
@@ -100,20 +102,29 @@ internal constructor( | |
|
||
isDownloadProgressVisible.value = true | ||
downloadProgress.value = 0f | ||
downloadJob = | ||
viewModelScope.launch(ioDispatcher) { | ||
offlineAreaRepository.downloadTiles(viewport!!).collect { (bytesDownloaded, totalBytes) -> | ||
val progressValue = | ||
if (totalBytes > 0) { | ||
(bytesDownloaded.toFloat() / totalBytes.toFloat()).coerceIn(0f, 1f) | ||
} else { | ||
0f | ||
} | ||
downloadProgress.postValue(progressValue) | ||
downloadJob = viewModelScope.launch(ioDispatcher) { | ||
offlineAreaRepository | ||
.downloadTiles(viewport!!) | ||
.catch { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we log the exception to |
||
isFailure.postValue(true) | ||
isDownloadProgressVisible.postValue(false) | ||
} | ||
isDownloadProgressVisible.postValue(false) | ||
_navigate.emit(UiState.OfflineAreaBackToHomeScreen) | ||
.collect { (bytesDownloaded, totalBytes) -> | ||
updateDownloadProgress(bytesDownloaded, totalBytes) | ||
} | ||
isDownloadProgressVisible.postValue(false) | ||
_navigate.emit(UiState.OfflineAreaBackToHomeScreen) | ||
} | ||
} | ||
|
||
private fun updateDownloadProgress(bytesDownloaded: Int, totalBytes: Int) { | ||
val progressValue = | ||
if (totalBytes > 0) { | ||
(bytesDownloaded.toFloat() / totalBytes.toFloat()).coerceIn(0f, 1f) | ||
} else { | ||
0f | ||
} | ||
downloadProgress.postValue(progressValue) | ||
} | ||
|
||
fun onCancelClick() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,8 @@ import androidx.compose.ui.test.junit4.createAndroidComposeRule | |
import androidx.compose.ui.test.onNodeWithText | ||
import androidx.compose.ui.test.performClick | ||
import androidx.lifecycle.Observer | ||
import android.widget.Toast | ||
import androidx.lifecycle.lifecycleScope | ||
import androidx.test.espresso.Espresso.onView | ||
import androidx.test.espresso.assertion.ViewAssertions.matches | ||
import androidx.test.espresso.matcher.ViewMatchers.hasDescendant | ||
|
@@ -33,20 +35,25 @@ import com.google.android.ground.BaseHiltTest | |
import com.google.android.ground.R | ||
import com.google.android.ground.launchFragmentInHiltContainer | ||
import com.google.android.ground.repository.OfflineAreaRepository | ||
import com.google.common.truth.Truth.assertThat | ||
import dagger.hilt.android.testing.HiltAndroidTest | ||
import javax.inject.Inject | ||
import junit.framework.Assert.assertFalse | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.test.advanceUntilIdle | ||
import org.junit.Assert.assertNull | ||
import junit.framework.TestCase.assertEquals | ||
import kotlinx.coroutines.launch | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mockito.mock | ||
import org.mockito.kotlin.any | ||
import org.mockito.kotlin.whenever | ||
import org.mockito.Mockito.verify | ||
import org.robolectric.RobolectricTestRunner | ||
import org.robolectric.shadows.ShadowToast | ||
|
||
@HiltAndroidTest | ||
@RunWith(RobolectricTestRunner::class) | ||
|
@@ -125,4 +132,31 @@ class OfflineAreaSelectorFragmentTest : BaseHiltTest() { | |
|
||
viewModel.downloadProgress.removeObserver(observer) | ||
} | ||
|
||
@Test | ||
fun `test failure case displays toast`() = runWithTestDispatcher { | ||
val isFailureObserver = mock(Observer::class.java) as Observer<Boolean> | ||
fragment.viewLifecycleOwner.lifecycleScope.launch { | ||
viewModel.isFailure.observeForever(isFailureObserver) | ||
viewModel.isFailure.postValue(true) | ||
Comment on lines
+140
to
+141
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to call the download flow and mock the |
||
} | ||
|
||
verify(isFailureObserver).onChanged(true) | ||
|
||
ShadowToast.reset() | ||
|
||
advanceUntilIdle() | ||
|
||
val toast = ShadowToast.getLatestToast() | ||
assertThat(ShadowToast.shownToastCount()).isEqualTo(1) | ||
assertEquals(toast.duration, Toast.LENGTH_LONG) | ||
assertEquals( | ||
ShadowToast.getTextOfLatestToast(), | ||
fragment.getString(R.string.offline_area_download_error), | ||
) | ||
|
||
fragment.viewLifecycleOwner.lifecycleScope.launch { | ||
viewModel.isFailure.removeObserver(isFailureObserver) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this necessary? Can it be removed given that coroutines aren't getting suspended.