Skip to content

Commit

Permalink
New fields for SearchRequestTask (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
DzmitryFomchyn authored Apr 18, 2022
1 parent 939dced commit bf64864
Show file tree
Hide file tree
Showing 34 changed files with 776 additions and 437 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@

### New features
- [CORE] Now customers can create several independent from each other `SearchEngine`s. See `MapboxSearchSdk.createSearchEngine()`.
- [CORE] Now `SearchRequestTask` provides `isDone`, and `isCancelled` properties.

### Mapbox dependencies
- Search Native SDK `0.51.0`
- Search Native SDK `0.52.0`
- Common SDK `21.2.0`
- Telemetry SDK `8.1.1`
- Kotlin `1.5.31`
Expand Down
2 changes: 1 addition & 1 deletion MapboxSearch/gradle/versions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ ext {
mapbox_base_version = '0.6.0'
mapbox_android_core_version = '5.0.1'

search_native_version = '0.51.0'
search_native_version = '0.52.0'

detekt_version = "1.19.0"

Expand Down
7 changes: 7 additions & 0 deletions MapboxSearch/sdk/api/api-metalava.txt
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,10 @@ package com.mapbox.search {

public interface SearchRequestTask {
method public void cancel();
method public boolean isCancelled();
method public boolean isDone();
property public abstract boolean isCancelled;
property public abstract boolean isDone;
}

public final class SearchRequestTaskKt {
Expand Down Expand Up @@ -1204,6 +1208,9 @@ package com.mapbox.search.result {
public final class OriginalResultTypeKt {
}

public final class OriginalSearchResponseKt {
}

public final class OriginalSearchResultKt {
}

Expand Down
2 changes: 2 additions & 0 deletions MapboxSearch/sdk/api/sdk.api
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,8 @@ public final class com/mapbox/search/SearchRequestException : java/lang/RuntimeE

public abstract interface class com/mapbox/search/SearchRequestTask {
public abstract fun cancel ()V
public abstract fun isCancelled ()Z
public abstract fun isDone ()Z
}

public final class com/mapbox/search/SearchResultMetadata : android/os/Parcelable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.mapbox.search.result.SearchResultType
import com.mapbox.search.result.ServerSearchResultImpl
import com.mapbox.search.tests_support.BlockingCompletionCallback
import com.mapbox.search.tests_support.BlockingSearchCallback
import com.mapbox.search.tests_support.EmptySearchCallback
import com.mapbox.search.tests_support.createHistoryRecord
import com.mapbox.search.tests_support.createTestHistoryRecord
import com.mapbox.search.tests_support.createTestOriginalSearchResult
Expand All @@ -32,6 +33,7 @@ import com.mapbox.search.utils.TimeProvider
import com.mapbox.search.utils.UUIDProvider
import com.mapbox.search.utils.assertEqualsIgnoreCase
import com.mapbox.search.utils.concurrent.SearchSdkMainThreadWorker
import com.mapbox.search.utils.enqueueMultiple
import com.mapbox.search.utils.orientation.ScreenOrientation
import com.mapbox.search.utils.orientation.ScreenOrientationProvider
import okhttp3.mockwebserver.MockResponse
Expand Down Expand Up @@ -493,6 +495,20 @@ internal class CategorySearchIntegrationTest : BaseTest() {
}
}

@Test
fun testConsecutiveRequests() {
mockServer.enqueueMultiple(createSuccessfulResponse("sbs_responses/category/successful_incorrect_response.json"), 2)

val task1 = searchEngine.search(TEST_CATEGORY, CategorySearchOptions(requestDebounce = 1000), EmptySearchCallback)

val callback = BlockingSearchCallback()
val task2 = searchEngine.search(TEST_CATEGORY, CategorySearchOptions(), callback)
callback.getResultBlocking()

assertTrue(task1.isCancelled)
assertTrue(task2.isDone)
}

@Test
fun testErrorResponse() {
mockServer.enqueue(MockResponse().setResponseCode(404))
Expand Down Expand Up @@ -550,7 +566,7 @@ internal class CategorySearchIntegrationTest : BaseTest() {

task = searchEngine.search(TEST_CATEGORY, CategorySearchOptions(), object : SearchCallback {
override fun onResults(results: List<SearchResult>, responseInfo: ResponseInfo) {
assertTrue((task as? SearchRequestTaskImpl<*>)?.isExecuted == true)
assertTrue((task as? SearchRequestTaskImpl<*>)?.isDone == true)
countDownLatch.countDown()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.mapbox.search.tests_support.BlockingOnIndexChangeListener
import com.mapbox.search.tests_support.BlockingSearchCallback
import com.mapbox.search.tests_support.BlockingSearchCallback.SearchEngineResult
import com.mapbox.search.tests_support.BlockingSearchSelectionCallback
import com.mapbox.search.tests_support.EmptySearchSuggestionsCallback
import com.mapbox.search.tests_support.createTestOriginalSearchResult
import com.mapbox.search.tests_support.createTestSuggestion
import com.mapbox.search.tests_support.getAllTileRegionsBlocking
Expand Down Expand Up @@ -242,7 +243,7 @@ internal class OfflineSearchEngineIntegrationTest : BaseTest() {

assertTrue(searchResult is BlockingSearchSelectionCallback.SearchEngineResult.Error)
val error = searchResult as BlockingSearchSelectionCallback.SearchEngineResult.Error
assertTrue(error.e.message?.contains("message: Offline regions not added") == true)
assertTrue(error.e.message?.contains("Offline regions not added") == true)
}

@Test
Expand Down Expand Up @@ -520,6 +521,18 @@ internal class OfflineSearchEngineIntegrationTest : BaseTest() {
assertTrue(results.isEmpty())
}

@Test
fun testConsecutiveRequests() {
val task1 = searchEngine.search("Baker street", OfflineSearchOptions(), EmptySearchSuggestionsCallback)

val callback = BlockingSearchSelectionCallback()
val task2 = searchEngine.search("Baker street", OfflineSearchOptions(), callback)
callback.getResultBlocking()

assertTrue(task1.isCancelled)
assertTrue(task2.isDone)
}

companion object {

private val tileStore: TileStore = TileStore.create()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.mapbox.search.result.SearchResult
import com.mapbox.search.result.SearchResultType
import com.mapbox.search.result.ServerSearchResultImpl
import com.mapbox.search.tests_support.BlockingSearchCallback
import com.mapbox.search.tests_support.EmptySearchCallback
import com.mapbox.search.tests_support.createHistoryRecord
import com.mapbox.search.tests_support.createTestOriginalSearchResult
import com.mapbox.search.tests_support.record.addBlocking
Expand All @@ -29,6 +30,7 @@ import com.mapbox.search.utils.TimeProvider
import com.mapbox.search.utils.UUIDProvider
import com.mapbox.search.utils.assertEqualsIgnoreCase
import com.mapbox.search.utils.concurrent.SearchSdkMainThreadWorker
import com.mapbox.search.utils.enqueueMultiple
import com.mapbox.search.utils.orientation.ScreenOrientation
import com.mapbox.search.utils.orientation.ScreenOrientationProvider
import okhttp3.mockwebserver.MockResponse
Expand Down Expand Up @@ -355,7 +357,7 @@ internal class ReverseGeocodingSearchIntegrationTest : BaseTest() {

task = searchEngine.search(ReverseGeoOptions(center = TEST_POINT), object : SearchCallback {
override fun onResults(results: List<SearchResult>, responseInfo: ResponseInfo) {
assertTrue((task as? SearchRequestTaskImpl<*>)?.isExecuted == true)
assertTrue((task as? SearchRequestTaskImpl<*>)?.isDone == true)
countDownLatch.countDown()
}

Expand All @@ -367,6 +369,21 @@ internal class ReverseGeocodingSearchIntegrationTest : BaseTest() {
countDownLatch.await()
}

@Test
fun testConsecutiveRequests() {
mockServer.enqueueMultiple(createSuccessfulResponse("sbs_responses/reverse_geocoding/successful_response.json"), 2)

val task1 = searchEngine.search(ReverseGeoOptions(center = TEST_POINT), EmptySearchCallback)

val callback = BlockingSearchCallback()
val task2 = searchEngine.search(ReverseGeoOptions(center = TEST_POINT), callback)
callback.getResultBlocking()

// Unlike other searches, reverse geocoding doesn't cancel previous requests, so task1 shouldn't be cancelled by this time
assertFalse(task1.isCancelled)
assertTrue(task2.isDone)
}

@Test
fun testErrorBackendResponseSimpleFormat() {
val errorResponse = MockResponse()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.mapbox.search.result.ServerSearchSuggestion
import com.mapbox.search.tests_support.BlockingCompletionCallback
import com.mapbox.search.tests_support.BlockingSearchSelectionCallback
import com.mapbox.search.tests_support.BlockingSearchSelectionCallback.SearchEngineResult
import com.mapbox.search.tests_support.EmptySearchSuggestionsCallback
import com.mapbox.search.tests_support.createHistoryRecord
import com.mapbox.search.tests_support.createTestHistoryRecord
import com.mapbox.search.tests_support.createTestOriginalSearchResult
Expand All @@ -37,6 +38,7 @@ import com.mapbox.search.utils.TimeProvider
import com.mapbox.search.utils.UUIDProvider
import com.mapbox.search.utils.assertEqualsIgnoreCase
import com.mapbox.search.utils.concurrent.SearchSdkMainThreadWorker
import com.mapbox.search.utils.enqueueMultiple
import com.mapbox.search.utils.orientation.ScreenOrientation
import com.mapbox.search.utils.orientation.ScreenOrientationProvider
import okhttp3.mockwebserver.MockResponse
Expand Down Expand Up @@ -897,7 +899,7 @@ internal class SearchEngineIntegrationTest : BaseTest() {
task = searchEngine.search(TEST_QUERY, SearchOptions(), object : SearchSuggestionsCallback {
override fun onSuggestions(suggestions: List<SearchSuggestion>, responseInfo: ResponseInfo) {
searchSuggestions = suggestions
assertTrue((task as? SearchRequestTaskImpl<*>)?.isExecuted == true)
assertTrue((task as? SearchRequestTaskImpl<*>)?.isDone == true)
countDownLatch.countDown()
}

Expand All @@ -913,7 +915,7 @@ internal class SearchEngineIntegrationTest : BaseTest() {
var selectionTask: SearchRequestTask? = null
selectionTask = searchEngine.select(searchSuggestions.first(), object : SearchSelectionCallback {
override fun onSuggestions(suggestions: List<SearchSuggestion>, responseInfo: ResponseInfo) {
assertTrue((selectionTask as? SearchRequestTaskImpl<*>)?.isExecuted == true)
assertTrue((selectionTask as? SearchRequestTaskImpl<*>)?.isDone == true)
countDownLatch.countDown()
}

Expand All @@ -922,7 +924,7 @@ internal class SearchEngineIntegrationTest : BaseTest() {
}

override fun onResult(suggestion: SearchSuggestion, result: SearchResult, responseInfo: ResponseInfo) {
assertTrue((selectionTask as? SearchRequestTaskImpl<*>)?.isExecuted == true)
assertTrue((selectionTask as? SearchRequestTaskImpl<*>)?.isDone == true)
countDownLatch.countDown()
}

Expand All @@ -931,14 +933,28 @@ internal class SearchEngineIntegrationTest : BaseTest() {
results: List<SearchResult>,
responseInfo: ResponseInfo
) {
assertTrue((selectionTask as? SearchRequestTaskImpl<*>)?.isExecuted == true)
assertTrue((selectionTask as? SearchRequestTaskImpl<*>)?.isDone == true)
countDownLatch.countDown()
}
})

countDownLatch.await()
}

@Test
fun testConsecutiveRequests() {
mockServer.enqueueMultiple(createSuccessfulResponse("sbs_responses/suggestions-successful-for-minsk.json"), 2)

val task1 = searchEngine.search(TEST_QUERY, SearchOptions(requestDebounce = 1000), EmptySearchSuggestionsCallback)

val callback = BlockingSearchSelectionCallback()
val task2 = searchEngine.search(TEST_QUERY, SearchOptions(), callback)
callback.getResultBlocking()

assertTrue(task1.isCancelled)
assertTrue(task2.isDone)
}

@Test
fun testErrorBackendResponseSimpleFormat() {
val errorResponse = MockResponse()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal class OfflineSearchEngineImpl(
private val searchResultFactory: SearchResultFactory,
private val engineExecutorService: ExecutorService,
override val tileStore: TileStore,
) : BaseSearchEngine(), OfflineSearchEngine {
) : BaseSearchEngine(autoCancelPreviousRequest = true), OfflineSearchEngine {

private val initializationLock = Any()
private val engineReadyCallbacks = mutableMapOf<EngineReadyCallback, Executor>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.mapbox.search

import android.os.Parcelable
import com.mapbox.search.core.CoreSearchResponse
import com.mapbox.search.result.OriginalSearchResponse
import kotlinx.parcelize.Parcelize

/**
Expand Down Expand Up @@ -31,7 +32,7 @@ import kotlinx.parcelize.Parcelize
public class ResponseInfo internal constructor(
public val requestOptions: RequestOptions,
@get:JvmSynthetic
internal val coreSearchResponse: CoreSearchResponse?,
internal val coreSearchResponse: OriginalSearchResponse?,
@get:JvmSynthetic
internal val isReproducible: Boolean,
) : Parcelable {
Expand Down
Loading

0 comments on commit bf64864

Please sign in to comment.