Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
For #12469 - Cancel previous queries before new suggestions requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mugurell authored and mergify[bot] committed Jul 11, 2022
1 parent 64fcbac commit b5de3aa
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
package mozilla.components.compose.browser.awesomebar.internal

import android.os.SystemClock
import androidx.annotation.VisibleForTesting
import androidx.compose.runtime.RememberObserver
import androidx.compose.runtime.mutableStateOf
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import mozilla.components.compose.browser.awesomebar.AwesomeBarFacts
import mozilla.components.compose.browser.awesomebar.AwesomeBarFacts.emitAwesomeBarFact
Expand All @@ -32,6 +34,9 @@ internal class SuggestionFetcher(
NamedThreadFactory("SuggestionFetcher")
).asCoroutineDispatcher()

@VisibleForTesting
internal var fetchJob: Job? = null

/**
* The current list of suggestions as an observable list.
*/
Expand All @@ -45,7 +50,9 @@ internal class SuggestionFetcher(
suspend fun fetch(text: String) {
profiler?.addMarker("SuggestionFetcher.fetch") // DO NOT ADD ANYTHING ABOVE THIS addMarker CALL.

coroutineScope {
fetchJob?.cancel()

fetchJob = CoroutineScope(dispatcher).launch {
groups.forEach { group ->
group.providers.forEach { provider ->
val profilerStartTime = profiler?.getProfilerTime() // DO NOT ADD ANYTHING ABOVE getProfilerTime.
Expand All @@ -58,7 +65,8 @@ internal class SuggestionFetcher(
/**
* Fetches suggestions from [provider].
*/
private suspend fun fetchFrom(
@VisibleForTesting
internal suspend fun fetchFrom(
group: AwesomeBar.SuggestionProviderGroup,
provider: AwesomeBar.SuggestionProvider,
text: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mozilla.components.compose.browser.awesomebar.internal

import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import mozilla.components.concept.awesomebar.AwesomeBar.SuggestionProvider
import mozilla.components.concept.awesomebar.AwesomeBar.SuggestionProviderGroup
import mozilla.components.support.test.any
import mozilla.components.support.test.mock
import mozilla.components.support.test.rule.MainCoroutineRule
import mozilla.components.support.test.rule.runTestOnMain
import org.junit.Rule
import org.junit.Test
import org.mockito.Mockito.doAnswer
import org.mockito.Mockito.inOrder
import org.mockito.Mockito.spy

@ExperimentalCoroutinesApi // for runTestOnMain
class SuggestionFetcherTest {
@get:Rule
val coroutinesTestRule = MainCoroutineRule()

@Test
fun `GIVEN a new fetch request THEN all previous queries are cancelled`() = runTestOnMain {
val provider: SuggestionProvider = mock()
val providerGroup = SuggestionProviderGroup(listOf(provider))
val fetcher = spy(SuggestionFetcher(listOf(providerGroup), null))
val previousFetchJob: Job = mock()
fetcher.fetchJob = previousFetchJob
doAnswer {}.`when`(fetcher).fetchFrom(any(), any(), any(), any())
val orderVerifier = inOrder(previousFetchJob, fetcher)

fetcher.fetch("test")

orderVerifier.verify(previousFetchJob)!!.cancel()
orderVerifier.verify(fetcher).fetchFrom(providerGroup, provider, "test", null)
}
}
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ permalink: /changelog/
* [Gecko](https://github.com/mozilla-mobile/android-components/blob/main/buildSrc/src/main/java/Gecko.kt)
* [Configuration](https://github.com/mozilla-mobile/android-components/blob/main/.config.yml)

* **browser-awesomebar**:
* 🚒 Bug fixed [issue #12469](https://github.com/mozilla-mobile/android-components/issues/12469) Cancel previous queries before new suggestions requests.

* **service-pocket**
* Add an index for sponsored stories foreign key to resolve a compilation warning. [#12406](https://github.com/mozilla-mobile/android-components/issues/12406)

Expand Down
6 changes: 6 additions & 0 deletions samples/browser/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ dependencies {

debugImplementation Dependencies.leakcanary

testImplementation Dependencies.androidx_test_core
testImplementation Dependencies.androidx_test_junit
testImplementation Dependencies.testing_robolectric
testImplementation Dependencies.testing_mockito
testImplementation Dependencies.testing_coroutines

androidTestImplementation project(':support-android-test')
androidTestImplementation Dependencies.androidx_test_core
androidTestImplementation Dependencies.androidx_test_runner
Expand Down

1 comment on commit b5de3aa

@firefoxci-taskcluster
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh oh! Looks like an error! Details

Failed to fetch task artifact public/github/customCheckRunText.md for GitHub integration.
Make sure the artifact exists on the worker or other location.

Please sign in to comment.