Skip to content

Commit

Permalink
Don't statically swap Dispatchers.Main
Browse files Browse the repository at this point in the history
  • Loading branch information
veyndan committed Aug 10, 2022
1 parent e111772 commit 103b478
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 12 deletions.
Expand Up @@ -29,13 +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
Expand All @@ -51,17 +47,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

0 comments on commit 103b478

Please sign in to comment.