-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Post List: Fix excessive media requests #20939
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1a062ae
Add an ongoingRequest hashset to track fetch requests
zwarm dc601e9
Invoke featureMediaChanged on error and success
zwarm bc7d8c5
Mark the mapSet and hashMap as internal and set visible for testing
zwarm 0637eec
Add unit tests for PostListFeaturedImageTracker
zwarm 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
111 changes: 111 additions & 0 deletions
111
WordPress/src/test/java/org/wordpress/android/ui/posts/PostListFeaturedImageTrackerTest.kt
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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package org.wordpress.android.ui.posts | ||
|
||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNull | ||
import org.junit.Before | ||
import org.mockito.kotlin.any | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.never | ||
import org.mockito.kotlin.verify | ||
import org.mockito.kotlin.whenever | ||
import org.wordpress.android.BaseUnitTest | ||
import org.wordpress.android.fluxc.Dispatcher | ||
import org.wordpress.android.fluxc.model.MediaModel | ||
import org.wordpress.android.fluxc.model.SiteModel | ||
import org.wordpress.android.fluxc.store.MediaStore | ||
import kotlin.test.Test | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
@ExperimentalCoroutinesApi | ||
class PostListFeaturedImageTrackerTest : BaseUnitTest() { | ||
private val dispatcher: Dispatcher = mock() | ||
private val mediaStore: MediaStore = mock() | ||
|
||
private lateinit var tracker: PostListFeaturedImageTracker | ||
|
||
private val site = SiteModel().apply { id = 123 } | ||
|
||
@Before | ||
fun setup() { | ||
tracker = PostListFeaturedImageTracker(dispatcher, mediaStore) | ||
} | ||
|
||
@Test | ||
fun `given id exists in map, when getFeaturedImageUrl invoked, then return url`() { | ||
val imageId = 123L | ||
val imageUrl = "https://example.com/image.jpg" | ||
tracker.featuredImageMap[imageId] = imageUrl | ||
|
||
val result = tracker.getFeaturedImageUrl(site, imageId) | ||
|
||
assertEquals(imageUrl, result) | ||
} | ||
|
||
@Test | ||
fun `given id is 0, when getFeaturedImageUrl invoked, then return null`() { | ||
val result = tracker.getFeaturedImageUrl(site, 0L) | ||
|
||
assertNull(result) | ||
} | ||
|
||
@Test | ||
fun `given id not in map and exists in store, when invoked, then return url from media store`() { | ||
val imageId = 456L | ||
val imageUrl = "https://example.com/image.jpg" | ||
val mediaModel = MediaModel(site.id, imageId).apply { | ||
url = imageUrl | ||
} | ||
|
||
whenever(mediaStore.getSiteMediaWithId(site, imageId)).thenReturn(mediaModel) | ||
|
||
val result = tracker.getFeaturedImageUrl(site, imageId) | ||
|
||
assertEquals(imageUrl, result) | ||
assertEquals(imageUrl, tracker.featuredImageMap[imageId]) | ||
} | ||
|
||
@Test | ||
fun `given id not in map or store, when invoked, then return null and dispatch fetch request`() { | ||
val imageId = 123L | ||
|
||
whenever(mediaStore.getSiteMediaWithId(site, imageId)).thenReturn(null) | ||
|
||
val result = tracker.getFeaturedImageUrl(site, imageId) | ||
|
||
assertNull(result) | ||
verify(dispatcher).dispatch(any()) | ||
assert(tracker.ongoingRequests.contains(imageId)) | ||
} | ||
|
||
@Test | ||
fun `given request ongoing for id, when invoked, should return null`() { | ||
val imageId = 123L | ||
|
||
tracker.ongoingRequests.add(imageId) | ||
|
||
val result = tracker.getFeaturedImageUrl(site, imageId) | ||
|
||
assertNull(result) | ||
verify(mediaStore, never()).getSiteMediaWithId(site, imageId) | ||
verify(dispatcher, never()).dispatch(any()) | ||
} | ||
|
||
@Test | ||
fun `given id in map and ongoingRequests, when invalidate, then remove id from map and ongoingRequests`() { | ||
val imageId1 = 123L | ||
val imageId2 = 456L | ||
|
||
tracker.featuredImageMap[imageId1] = "https://example.com/image1.jpg" | ||
tracker.featuredImageMap[imageId2] = "https://example.com/image2.jpg" | ||
tracker.ongoingRequests.add(imageId1) | ||
tracker.ongoingRequests.add(imageId2) | ||
|
||
tracker.invalidateFeaturedMedia(listOf(imageId1, imageId2)) | ||
|
||
assertNull(tracker.featuredImageMap[imageId1]) | ||
assertNull(tracker.featuredImageMap[imageId2]) | ||
assert(!tracker.ongoingRequests.contains(imageId1)) | ||
assert(!tracker.ongoingRequests.contains(imageId2)) | ||
} | ||
} |
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.
Will this be called when the featured image request is completed? I mention this because I haven't found any other place where the ongoing request is removed.
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.
Hey @fluiddot
The places where this funcation gets called is
PostListEventListener
→featuredMediaChanged
.The
featuredMediaChanged
is called when the media is changed or uploaded. Correct me if I am wrong. @zwarmThere 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.
Thanks for the question, which @AjeshRPai answered before me down below #20939 (comment)
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.
This is correct, previously only success calls were removed from the featureImageMap and in the updated version, both success and errors are removed. We need to make sure we don't have requests hanging around when they shouldn't be.