问题描述
我想为JDA Discord Bots创建一个(功能)测试系统.
I want to create a (feature) testing system for JDA Discord Bots.
为此,我需要等待直到当前队列中的所有消息都已发送(消息已通过 RestAction#queue
发送)并测试是否有消息.
For this, I need to wait until all messages in the current queue are sent(messages are sent with RestAction#queue
) and test if there is the message.
是否有一种方法可以等待所有排队的 RestAction
,还是应该以指数方式等待(例如,等待0.5秒,等待1秒,等待2秒并测试消息是否存在)
Is there a way to await all queued RestAction
s or should I wait exponentially (e.g. wait 0.5 seconds, wait 1 second, wait 2 seconds and test if the message exists)
[注意]我不想在生产代码中将 queue
更改为 submit
并将其发送到测试.
[Note]I don't want to change the queue
to submit
in my production code and send it to the test.
推荐答案
理想情况下,测试系统根本不应该与Internet交互.但是,要回答您的问题,您可以使用提交,而不使用队列.
Ideally a testing system shouldn't interact with the internet at all. However, to answer your question you can wait for rest actions by using the promise returned by submit instead of using queue.
List<CompletableFuture<?>> list = new ArrayList<>();
list.add(action1.submit());
list.add(action2.submit());
...
CompletableFuture<Void> merged = CompletableFuture.allOf(list.toArray(new CompletableFuture[0]));
merged.whenComplete((v, error) -> {
if (error != null) error.printStackTrace();
else System.out.println("All futures completed successfully");
});
这篇关于等待所有JDA RestActions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!