Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't statically swap Dispatchers.Main #3428

Merged
merged 1 commit into from Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -29,14 +29,9 @@ import app.cash.sqldelight.db.SqlCursor
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.junit.After
import org.junit.Before
import org.junit.Test
import kotlin.coroutines.EmptyCoroutineContext
Expand All @@ -51,17 +46,11 @@ class OffsetQueryPagingSourceTest {

@Before
fun init() {
Dispatchers.setMain(StandardTestDispatcher())
driver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY)
driver.execute(null, "CREATE TABLE TestItem(id INTEGER NOT NULL PRIMARY KEY);", 0)
transacter = object : TransacterImpl(driver) {}
}

@After
fun tearDown() {
Dispatchers.resetMain()
}

@Test
fun test_itemCount() = runTest {
insertItems(ITEMS_LIST)
Expand Down
Expand Up @@ -19,10 +19,10 @@ import androidx.paging.AsyncPagingDataDiffer
import androidx.paging.PagingData
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListUpdateCallback
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.advanceUntilIdle

private object NoopListCallback : ListUpdateCallback {
Expand All @@ -38,10 +38,12 @@ fun <T : Any> PagingData<T>.withPagingDataDiffer(
diffCallback: DiffUtil.ItemCallback<T>,
block: AsyncPagingDataDiffer<T>.() -> Unit,
) {
val testDispatcher = UnconfinedTestDispatcher(testScope.testScheduler)
val pagingDataDiffer = AsyncPagingDataDiffer(
diffCallback,
NoopListCallback,
workerDispatcher = Dispatchers.Main,
mainDispatcher = testDispatcher,
workerDispatcher = testDispatcher,
)
val job = testScope.launch {
pagingDataDiffer.submitData(this@withPagingDataDiffer)
Expand Down