gpt4 book ai didi

java - 使用 addAsync 对 App Engine 中的延迟任务进行单元测试

转载 作者:太空宇宙 更新时间:2023-11-04 12:18:40 25 4
gpt4 key购买 nike

有没有办法可以强制完成对 addAsync 的所有调用?

@Test
public void aTest() throws Exception {
SomeDeferredTask nextTask = new SomeDeferredTask();

TaskOptions taskOptions = TaskOptions.Builder.withPayload(nextTask).
etaMillis(1000).
taskName("a-task-name");

QueueFactory.getDefaultQueue().addAsync(taskOptions);

LocalTaskQueue ltq = LocalTaskQueueTestConfig.getLocalTaskQueue();
QueueStateInfo qsi = ltq.getQueueStateInfo().get(QueueFactory.getDefaultQueue().getQueueName());

Assert.assertEquals(1, qsi.getTaskInfo().size());
}

我无法轻松使用 addAsync 返回的 Future,因为我还需要检查创建任务的任务。否则我可以这样做:

    Future<TaskHandle> future = QueueFactory.getDefaultQueue().addAsync(taskOptions);
future.get(1, TimeUnit.SECONDS);

我真正需要的是一种刷新 addAsync 任务队列并在完成时返回的方法。

有什么办法可以做到这一点吗?

我能看到的唯一选择是将所有返回的 future 添加到静态队列中,并编写一个方法以确保在继续测试之前所有这些都已完成。

最佳答案

What I really need is a method to flush the queue of addAsync tasks and return when complete. Is there anything to do this?

我什么都不知道。我为自己写了一个类似的方法。它循环遍历所有排队任务,在每个已过截止日期的任务上调用 deserialize(taskInfo.getBodyAsBytes()).run()。对于似乎常见的情况,我希望有更多的代码,但当时我没有找到更好的解决方案。如果您发现其他更好的东西,请告诉我!

关于java - 使用 addAsync 对 App Engine 中的延迟任务进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39099585/

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