是否有一种方法可以测试Workmanager Google API的周期性工人,而无需每次执行至少15分钟?
我的意思是,这是一个调试应用程序,我正在通过Android Studio运行它,我不想等待这么长时间来测试我的功能。
you 不能。
定期工作的最小间隔为15分钟,并且不能具有初始延迟。您可以在WorkSpec.java
类中找到证明。
/**
* Sets the periodic interval for this unit of work.
*
* @param intervalDuration The interval in milliseconds
*/
public void setPeriodic(long intervalDuration) {
if (intervalDuration < MIN_PERIODIC_INTERVAL_MILLIS) {
Logger.get().warning(TAG, String.format(
"Interval duration lesser than minimum allowed value; Changed to %s",
MIN_PERIODIC_INTERVAL_MILLIS));
intervalDuration = MIN_PERIODIC_INTERVAL_MILLIS;
}
setPeriodic(intervalDuration, intervalDuration);
}
但是还有其他方法可以处理。
- 使用 Work-Testing 库编写单元测试,并确保您的业务逻辑按预期工作。
- 使用依赖注入方法并在调试模式下提供
OneTimeWorkRequest
,例如:
interface Scheduler {
fun schedule()
}
class DebugScheduler {
fun schedule() {
WorkManager.getInstance().enqueue(
OneTimeWorkRequest.Builder(MyWorker::class.java)
.build()
)
}
}
class ProductionScheduler {
fun schedule() {
// your actual scheduling logic
}
}
用于测试目的,您可以使用工作测试库,如下所示:https://developer.android.com/topic/libraries/libraries/architection/workmanager/workmanager/workmanager/how-to-to/testing
具体来说,您想查看如何测试周期性工作:https://developer.android.com/topic/libraries/architecture/workmanager/workmanager/how-to-teesting#periodic-work-work-work
<>