forked from mozilla-mobile/android-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For mozilla-mobile#12469 - Cancel in progress storage requests before…
… new awesomebar suggestions
- Loading branch information
Showing
18 changed files
with
308 additions
and
21 deletions.
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
78 changes: 78 additions & 0 deletions
78
...r/storage-sync/src/test/java/mozilla/components/browser/storage/sync/PlacesStorageTest.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,78 @@ | ||
/* 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.browser.storage.sync | ||
|
||
import android.content.Context | ||
import kotlinx.coroutines.cancelChildren | ||
import mozilla.appservices.places.PlacesReaderConnection | ||
import mozilla.appservices.places.PlacesWriterConnection | ||
import mozilla.appservices.places.uniffi.PlacesException | ||
import mozilla.components.support.base.log.logger.Logger | ||
import mozilla.components.support.test.mock | ||
import org.junit.Test | ||
import org.mockito.Mockito.doAnswer | ||
import org.mockito.Mockito.doReturn | ||
import org.mockito.Mockito.verify | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
class PlacesStorageTest { | ||
private val storage = FakePlacesStorage() | ||
|
||
@Test | ||
fun `WHEN all reads are interrupted THEN no exception is thrown`() { | ||
doAnswer { | ||
throw PlacesException.OperationInterrupted("This should be caught") | ||
}.`when`(storage.reader).interrupt() | ||
|
||
storage.interruptCurrentReads() | ||
|
||
verify(storage.reader).interrupt() | ||
} | ||
|
||
@Test | ||
fun `WHEN all writes are interrupted THEN no exception is thrown`() { | ||
doAnswer { | ||
throw PlacesException.OperationInterrupted("This should be caught") | ||
}.`when`(storage.writer).interrupt() | ||
|
||
storage.interruptCurrentWrites() | ||
|
||
verify(storage.writer).interrupt() | ||
} | ||
|
||
@Test | ||
fun `WHEN a call is made to clean all reads THEN they are cancelled`() { | ||
storage.readScope = mock { | ||
doReturn(mock<CoroutineContext>()).`when`(this).coroutineContext | ||
} | ||
|
||
storage.cancelReads() | ||
|
||
verify(storage.reader).interrupt() | ||
verify(storage.readScope.coroutineContext).cancelChildren() | ||
} | ||
|
||
@Test | ||
fun `WHEN a call is made to clean all writes THEN they are cancelled`() { | ||
storage.writeScope = mock { | ||
doReturn(mock<CoroutineContext>()).`when`(this).coroutineContext | ||
} | ||
|
||
storage.cancelWrites() | ||
|
||
verify(storage.writer).interrupt() | ||
verify(storage.writeScope.coroutineContext).cancelChildren() | ||
} | ||
} | ||
|
||
class FakePlacesStorage( | ||
context: Context = mock() | ||
) : PlacesStorage(context) { | ||
override val logger = Logger("FakePlacesStorage") | ||
override fun registerWithSyncManager() {} | ||
|
||
override val writer: PlacesWriterConnection = mock() | ||
override val reader: PlacesReaderConnection = mock() | ||
} |
25 changes: 25 additions & 0 deletions
25
...nts/concept/storage/src/main/java/mozilla/components/concept/storage/CancelableStorage.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,25 @@ | ||
/* 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.concept.storage | ||
|
||
/** | ||
* Storage that allows to stop and clean in progress operations. | ||
*/ | ||
interface CancelableStorage { | ||
/** | ||
* Cleans up all background work and operations queue. | ||
*/ | ||
fun cleanup() | ||
|
||
/** | ||
* Cleans up all pending write operations. | ||
*/ | ||
fun cancelWrites() | ||
|
||
/** | ||
* Cleans up all pending read operations. | ||
*/ | ||
fun cancelReads() | ||
} |
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
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
Oops, something went wrong.