【发布时间】:2019-06-28 06:32:56
【问题描述】:
我正在使用 Awaitility 工具,我需要从 await 返回一个集合,以便以后能够使用它。
我有一个从 GET 调用返回的集合:
Collection collection = usersService.getAllUsers();
以下代码有效(GET调用最多执行5次以满足条件):
waitForEvent(() -> usersService.getAllUsers()).size());
地点:
private void waitForEvent(Callable<Integer> collectionSize) {
await().atMost(5, TimeUnit.SECONDS)
.pollDelay(1, TimeUnit.SECONDS).until(collectionSize, greaterThan(5));
}
但我需要传递一个集合(而不是它的大小)才能重用它。为什么这段代码不起作用(GET 调用只执行一次并等待 5 秒)?
waitForEvent2(usersService.getAllUsers());
在哪里
private Collection waitForEvent2(Collection collection) {
await().atMost(5, TimeUnit.SECONDS)
.pollDelay(1, TimeUnit.SECONDS).until(collectionSize(collection), greaterThan(5));
return collection;
}
private Callable<Integer> collectionSize(Collection collection) {
return new Callable<Integer>() {
public Integer call() throws Exception {
return collection.size(); // The condition supplier part
}
};
}
我需要做什么才能使 GET 请求被轮询多次,并将集合作为参数传递?
【问题讨论】:
-
为什么需要返回
Collection?对我来说听起来像XY-Problem -
我不明白为什么第一个 sn-p 会起作用而第二个不会。问题是第一个 sn-p 不包括
collectionSize实际上是什么。它是同一个可调用对象吗? -
添加了使用示例。 @Lino 我使用 Collection 以便在列表和地图中重用它
-
@Nataliya 是什么阻止你声明
Collection变量并将userService.getAllUsers()的结果存储在其中? -
@Lino 我想这是主要问题,因为集合被分配给变量,所以轮询不起作用(GET 请求只执行一次)。似乎轮询适用于
size()方法,而不是usersService.getAllUsers().size()。也就是说usersService.getAllUsers()在重试中不会执行
标签: java awaitility