0

I'm using Awaitility tool and I need to return a collection from await to be able to work with it later.

I have a collection returned from a GET call:

Collection collection = usersService.getAllUsers();

The following code works (GET call is executed up to 5 times in order to meet the conditions):

    waitForEvent(() -> usersService.getAllUsers()).size());

Where:

private void waitForEvent(Callable<Integer> collectionSize) {
    await().atMost(5, TimeUnit.SECONDS)
            .pollDelay(1, TimeUnit.SECONDS).until(collectionSize, greaterThan(5));
}

But I need to pass a Collection (not its size) to be able to reuse it. Why this code is not working (GET call is executed just once and it waits for 5 sec)?

waitForEvent2(usersService.getAllUsers());

Where

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
        }
    };
}

What do I need to do so that GET request was polled several times with collection passed as a parameter?

Nataliya
  • 111
  • 1
  • 11
  • Why exactly do you need to return the `Collection`? Sounds like an [XY-Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) to me – Lino Feb 04 '19 at 13:49
  • 1
    I dont see why first snippet would work and second one would not. The problem is that first snippet does not include what `collectionSize` actually is. Is it the same callable ? – Antoniossss Feb 04 '19 at 13:51
  • Added usage examples. @Lino I used Collection in order to reuse it with both Lists and Maps – Nataliya Feb 04 '19 at 13:55
  • @Nataliya what stops you from declaring a `Collection` variable and store the result of `userService.getAllUsers()` inside that? – Lino Feb 04 '19 at 13:56
  • @Lino I guess this is the main problem, as the collection is assigned to variable, so polling is not working (GET request is executed just once). Seems like polling works on `size()` method, not on `usersService.getAllUsers().size()`. In other words, `usersService.getAllUsers()` is not executed in retry – Nataliya Feb 04 '19 at 14:20

1 Answers1

0

Well obviously in first snippet you are using

usersService.getAllUsers().size()

and that gets called multiple times (calling service -> get call)

and in second you are using only

collection.size()

and that won't fetch anything - because why would it - but still will be called the same amount of time.

What you could do (which I dont like) is

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
        }
    };
}
Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • Thank you, this snippet works. But is there any possibility not to hardcode `usersService.getAllUsers()`, so that I was able to reuse this method? Also, this does not solve the main problem: GET request is performed twice – Nataliya Feb 04 '19 at 14:15
  • Maybe it gets correct size after 2 calls. You can pass service call as a producer as well. – Antoniossss Feb 04 '19 at 14:25
  • what exectly do you mean regarding passing service call as a producer? – Nataliya Feb 04 '19 at 14:35