Тест инструментирования с использованием androidx.datastore версии 1.1.1 теперь завершается с ошибкой UncompletedCoroutiAndroid

Форум для тех, кто программирует под Android
Ответить
Anonymous
 Тест инструментирования с использованием androidx.datastore версии 1.1.1 теперь завершается с ошибкой UncompletedCorouti

Сообщение Anonymous »

Я обновил эти реализации в app/build.gradle с 1.0.0 до 1.1.1:

Код: Выделить всё

implementation "androidx.datastore:datastore-preferences:$androidXDatastore"
implementation "androidx.datastore:datastore-core:$androidXDatastore"
implementation "androidx.datastore:datastore-preferences-core:$androidXDatastore"
После этого некоторые инструментальные тесты завершились неудачей, хотя рабочий код по-прежнему работает нормально:

Код: Выделить всё

kotlinx.coroutines.test.UncompletedCoroutinesError: After waiting for 10s, the test coroutine is not completing, there were active child jobs: ["coroutine#1":StandaloneCoroutine{Active}@284ce4db]
at kotlinx.coroutines.test.TestBuildersKt__TestBuildersKt$runTest$2$1$2$1.invoke(TestBuilders.kt:349)
at kotlinx.coroutines.test.TestBuildersKt__TestBuildersKt$runTest$2$1$2$1.invoke(TestBuilders.kt:333)
at kotlinx.coroutines.InvokeOnCancelling.invoke(JobSupport.kt:1431)
at kotlinx.coroutines.JobSupport.notifyCancelling(JobSupport.kt:1477)
at kotlinx.coroutines.JobSupport.tryMakeCancelling(JobSupport.kt:799)
at kotlinx.coroutines.JobSupport.makeCancelling(JobSupport.kt:759)
at kotlinx.coroutines.JobSupport.cancelImpl$kotlinx_coroutines_core(JobSupport.kt:675)
at kotlinx.coroutines.JobSupport.cancelCoroutine(JobSupport.kt:662)
at kotlinx.coroutines.TimeoutCoroutine.run(Timeout.kt:159)
at kotlinx.coroutines.EventLoopImplBase$DelayedRunnableTask.run(EventLoop.common.kt:501)
at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:280)
at kotlinx.coroutines.DefaultExecutor.run(DefaultExecutor.kt:109)
at java.base@18.0.1/java.lang.Thread.run(Thread.java:833)
Целевой класс

Код: Выделить всё

class ScrollPositionDataStore(private val dataStore: DataStore
) {
private val _displayedPositionOffsetKey = stringPreferencesKey("displayed_position_offset")

suspend fun saveScrollPositionStore(topRowIndex: Int, positionOffset: Int) {
val position = "$topRowIndex,$positionOffset"
dataStore.edit { preferences ->
preferences[_displayedPositionOffsetKey] = position
}
}

suspend fun getScrollPosition(): Pair? = scrollPositionFlow.firstOrNull()

private val scrollPositionFlow: Flow  = dataStore.data
.map { preferences ->
// On the first run of the app, we will use LinearLayoutManager by default
val i = preferences[_displayedPositionOffsetKey]
val split = i?.split(",")
if (split == null) Pair(4, 0)
else Pair(split[0].toInt(), split[1].toInt())
}
}
Тестовый класс

Код: Выделить всё

private const val POSITION_PREFERENCES_NAME = "position_preferences"

@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class ScrollPositionDataStoreTest : JunitDataStoreTest(POSITION_PREFERENCES_NAME) {
private var inputDataStore: ScrollPositionDataStore = ScrollPositionDataStore(dataStore)

@Test
fun `ScrollPositionDataStore 入力した値をそのまま帰す`() {
scope.runTest {
dataStore.edit { it.clear() }
inputDataStore.saveScrollPositionStore(10, 15)
assertThat(inputDataStore.getScrollPosition())
.isEqualTo(Pair(10, 15))
}
}

@Test
fun `ScrollPositionDataStore 未入力時に400を返す`() {
scope.runTest {
dataStore.edit { it.clear() }
assertThat(inputDataStore.getScrollPosition())
.isEqualTo(Pair(4, 0))
}
}

@Test
fun `ScrollPositionDataStore マイナス値をそのまま還す`() {
scope.runTest {
dataStore.edit { it.clear() }
inputDataStore.saveScrollPositionStore(-1, -111)
assertThat(inputDataStore.getScrollPosition())
.isEqualTo(Pair(-1, -111))
}
}
}

@ExperimentalCoroutinesApi
abstract class JunitDataStoreTest(dataStoreName :String) {

private val dispatcher = UnconfinedTestDispatcher()
private val context: Context = InstrumentationRegistry.getInstrumentation().targetContext
val scope = TestScope(dispatcher + Job())
val dataStore = PreferenceDataStoreFactory.create(
scope = scope,
produceFile = { context.preferencesDataStoreFile(dataStoreName) }
)

@CallSuper
@Before
open fun setUp() {
Dispatchers.setMain(dispatcher)
}

@CallSuper
@After
open fun tearDown() {
Dispatchers.resetMain()
}
}
После возврата к версии 1.0.0 тесты работают нормально, поэтому проблема, вероятно, связана с новой версией хранилища данных.
Как можно Я снова пройду тесты с новой версией хранилища данных?

Подробнее здесь: https://stackoverflow.com/questions/784 ... ith-uncomp
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Android»