Fix ConcurrentModificationException in RecipePagingSourceFactory

It seems that it is possible to launch several coroutines on same
main thread of application. That's why it is possible to launch both
invoke and invalidate at the same time even though they are marked as
synchronized. To fix the issue this commit uses a concurrent collection
instead of synchronization.
This commit is contained in:
Kirill Kamakin
2021-11-20 21:13:26 +03:00
parent 965b488eb4
commit 70c0df1cf7
2 changed files with 49 additions and 3 deletions

View File

@@ -0,0 +1,35 @@
package gq.kirmanak.mealient.data.recipes.impl
import dagger.hilt.android.testing.HiltAndroidTest
import gq.kirmanak.mealient.data.recipes.db.RecipeStorage
import gq.kirmanak.mealient.test.HiltRobolectricTest
import kotlinx.coroutines.*
import org.junit.Before
import org.junit.Test
import javax.inject.Inject
@ExperimentalCoroutinesApi
@HiltAndroidTest
class RecipePagingSourceFactoryTest : HiltRobolectricTest() {
@Inject
lateinit var storage: RecipeStorage
lateinit var subject: RecipePagingSourceFactory
@Before
fun setUp() {
subject = RecipePagingSourceFactory(storage)
}
@Test
fun `when modifying concurrently then doesn't throw`(): Unit = runBlocking {
(0..100).map {
async(Dispatchers.Default) {
for (i in 0..100) {
subject.invalidate()
subject.invoke()
}
}
}.awaitAll()
}
}