mirror of
https://github.com/bitwarden/android.git
synced 2026-04-28 11:58:40 -05:00
[PM-19905] Migrate DispatcherManager to data module (#4999)
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package com.bitwarden.data.manager
|
||||
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.MainCoroutineDispatcher
|
||||
|
||||
/**
|
||||
* An interface for accessing the [CoroutineDispatcher]s.
|
||||
*/
|
||||
interface DispatcherManager {
|
||||
/**
|
||||
* The default [CoroutineDispatcher] for the app.
|
||||
*/
|
||||
val default: CoroutineDispatcher
|
||||
|
||||
/**
|
||||
* The [MainCoroutineDispatcher] for the app.
|
||||
*/
|
||||
val main: MainCoroutineDispatcher
|
||||
|
||||
/**
|
||||
* The IO [CoroutineDispatcher] for the app.
|
||||
*/
|
||||
val io: CoroutineDispatcher
|
||||
|
||||
/**
|
||||
* The unconfined [CoroutineDispatcher] for the app.
|
||||
*/
|
||||
val unconfined: CoroutineDispatcher
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.bitwarden.data.manager
|
||||
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.MainCoroutineDispatcher
|
||||
|
||||
/**
|
||||
* Primary implementation of [DispatcherManager].
|
||||
*/
|
||||
class DispatcherManagerImpl : DispatcherManager {
|
||||
override val default: CoroutineDispatcher = Dispatchers.Default
|
||||
|
||||
override val main: MainCoroutineDispatcher = Dispatchers.Main
|
||||
|
||||
override val io: CoroutineDispatcher = Dispatchers.IO
|
||||
|
||||
override val unconfined: CoroutineDispatcher = Dispatchers.Unconfined
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.bitwarden.data.manager
|
||||
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class DispatcherManagerTest {
|
||||
|
||||
private val dispatcherManager: DispatcherManager = DispatcherManagerImpl()
|
||||
|
||||
@Test
|
||||
fun `io should return Dispatchers IO`() = runTest {
|
||||
val expected = Dispatchers.IO
|
||||
|
||||
val actual = dispatcherManager.io
|
||||
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `main should return Dispatchers Main`() = runTest {
|
||||
val expected = Dispatchers.Main
|
||||
|
||||
val actual = dispatcherManager.main
|
||||
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `default should return Dispatchers Default`() = runTest {
|
||||
val expected = Dispatchers.Default
|
||||
|
||||
val actual = dispatcherManager.default
|
||||
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.bitwarden.data.datasource.disk.base
|
||||
|
||||
import com.bitwarden.data.manager.DispatcherManager
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.MainCoroutineDispatcher
|
||||
import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
||||
import kotlinx.coroutines.test.resetMain
|
||||
import kotlinx.coroutines.test.setMain
|
||||
|
||||
/**
|
||||
* A faked implementation of [DispatcherManager] that uses [UnconfinedTestDispatcher].
|
||||
*/
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class FakeDispatcherManager(
|
||||
override val default: CoroutineDispatcher = UnconfinedTestDispatcher(),
|
||||
override val io: CoroutineDispatcher = UnconfinedTestDispatcher(),
|
||||
override val unconfined: CoroutineDispatcher = UnconfinedTestDispatcher(),
|
||||
) : DispatcherManager {
|
||||
override val main: MainCoroutineDispatcher = Dispatchers.Main
|
||||
|
||||
/**
|
||||
* Updates the main dispatcher to use the provided [dispatcher]. Used in conjunction with
|
||||
* [resetMain].
|
||||
*/
|
||||
fun setMain(dispatcher: CoroutineDispatcher) {
|
||||
Dispatchers.setMain(dispatcher)
|
||||
}
|
||||
|
||||
/**
|
||||
* Restores the main dispatcher to it's default state. Used in conjunction with [setMain].
|
||||
*/
|
||||
fun resetMain() {
|
||||
Dispatchers.resetMain()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user