从返回的await收集

问题描述 投票:0回答:1

我使用Awaitility工具,我需要返回一个集合,从等待到能够后来与它的工作。

我从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请求被调查的几次?

java awaitility
1个回答
0
投票

嗯,显然在第一个片段您使用

usersService.getAllUsers().size()

而被多次调用(呼叫服务 - >获取调用)

并在第二你只使用

collection.size()

而这并不是什么取 - 因为为什么会 - 但仍然会被调用的时间相同。

你会怎么做(我不喜欢)是

private Callable<Integer> collectionSize(Collection collection) {

    return new Callable<Integer>() {
        public Integer call() throws Exception {
            collection.clear();
            collection.addAll(usersService.getAllUsers());
            return collection.size(); // The condition supplier part
        }
    };
}
© www.soinside.com 2019 - 2024. All rights reserved.