gpt4 book ai didi

java - 单元测试 WorkManager 时周期性工作未达到运行状态

转载 作者:行者123 更新时间:2023-12-02 01:24:46 25 4
gpt4 key购买 nike

我创建了一个 PeriodicWorkRequest来 self 的SyncDatabaseWorker ,如下所示:

class SyncDatabaseWorker(ctx: Context, params: WorkerParameters) : RxWorker(ctx, params) {

private val dataManager: DataManager = App.getDataManager()

override fun createWork(): Single<Result> {
return Single.create { emitter ->

dataManager.loadStoresFromServer()
.subscribe(object : SingleObserver<List<Store>> {
override fun onSubscribe(d: Disposable) {
}

override fun onSuccess(storeList: List<Store>) {
if (!storeList.isEmpty()) {
emitter.onSuccess(Result.success())
} else {
emitter.onSuccess(Result.retry())
}
}

override fun onError(e: Throwable) {
emitter.onSuccess(Result.failure())
}
})
}
}

companion object {
fun prepareSyncDBWorker(): PeriodicWorkRequest {
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()

val myWorkBuilder = PeriodicWorkRequest.Builder(SyncDatabaseWorker::class.java, 7, TimeUnit.DAYS)
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 1, TimeUnit.DAYS) // Backoff retry after 1 day
.setConstraints(constraints)

return myWorkBuilder.build()
}
}
}

然后我根据 Google's guide 编写单元测试像这样:

@RunWith(AndroidJUnit4::class)
class SyncDatabaseWorkerTest {
private lateinit var context: Context

@Before
fun setup() {
context = InstrumentationRegistry.getInstrumentation().targetContext

val config = Configuration.Builder()
.setMinimumLoggingLevel(Log.DEBUG)
.setExecutor(SynchronousExecutor())
.build()

// Initialize WorkManager for instrumentation tests.
WorkManagerTestInitHelper.initializeTestWorkManager(context, config)
}

@Test
@Throws(Exception::class)
fun testPeriodicWork_WithConstrains() {
// Create request
val request = SyncDatabaseWorker.prepareSyncDBWorker()

val workManager = WorkManager.getInstance(context)
val testDriver = WorkManagerTestInitHelper.getTestDriver(context)

// Enqueue and wait for result.
workManager.enqueue(request).result.get()

// Check work request is enqueued
var workInfo = workManager.getWorkInfoById(request.id).get()
assertThat(workInfo.state, `is`(WorkInfo.State.ENQUEUED))

// Tells the testing framework the period delay & all constrains is met
testDriver!!.setPeriodDelayMet(request.id)
testDriver.setAllConstraintsMet(request.id)

// Check work request is running
workInfo = workManager.getWorkInfoById(request.id).get()
assertThat(workInfo.state, `is`(WorkInfo.State.RUNNING))
}
}

始终处于状态 ENQUEUED ,即使经期延迟和所有限制均得到满足

Expected: is <RUNNING>
but: was <ENQUEUED>

当我调试测试并发现 createWork(): Single<Result>方法也被触发了,但是为什么状态不是RUNNING

可能我的方法是错误的,但是现在关于WorkManager单元测试的文档很少,而且我不知道正确的方法。

最佳答案

由于您使用的是同步执行器,因此您实际上永远不会看到处于 RUNNING 状态的工作 - 它应该已经执行。我怀疑您的工作实际上被标记为重试,因此再次进入 ENQUEUED 状态。您应该能够通过设置断点或查看日志来验证这一点。

关于java - 单元测试 WorkManager 时周期性工作未达到运行状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57099887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com