This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For #12469 - Cancel in progress storage requests before new awesomeba…
…r suggestions
- Loading branch information
1 parent
f3b2428
commit b9d28c5
Showing
17 changed files
with
306 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
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() | ||
} |
31 changes: 31 additions & 0 deletions
31
components/concept/storage/src/main/java/mozilla/components/concept/storage/Cancellable.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,31 @@ | ||
/* 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 Cancellable { | ||
/** | ||
* Cleans up all background work and operations queue. | ||
*/ | ||
fun cleanup() { | ||
// no-op | ||
} | ||
|
||
/** | ||
* Cleans up all pending write operations. | ||
*/ | ||
fun cancelWrites() { | ||
// no-op | ||
} | ||
|
||
/** | ||
* Cleans up all pending read operations. | ||
*/ | ||
fun cancelReads() { | ||
// no-op | ||
} | ||
} |
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.