[PM-19905] Migrate DispatcherManager to data module (#4999)

This commit is contained in:
Patrick Honkonen
2025-04-07 14:16:04 -04:00
committed by GitHub
parent 4a63a709b8
commit 2176b61cd3
89 changed files with 99 additions and 183 deletions

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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)
}
}

View File

@@ -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()
}
}