0

I'm trying to make sure that after some actions, no new records have appeared in the database, for this I use the following construction:

messages = new String[] {"message1", "message2"};
await("wait")
        .during(Duration.ofSeconds(15))
        .atMost(Duration.ofSeconds(19))
        .pollInterval(Duration.ofSeconds(1))
        .until(() -> databaseService.getJournalRecords().stream()
                .map(JournalRecord::getMessage)
                .filter(Arrays.asList(messages)::contains)
                .findFirst()
                .orElse(null), Matchers.nullValue()
        );

But I am getting an error:

org.awaitility.core.ConditionTimeoutException: Condition with alias 'wait' didn't complete within 19 seconds because lambda expression in my.path.MyClass: expected null but was null.

when using the construction

.orElse("some value"), Matchers.equalTo("some value")

I get a similar error

org.awaitility.core.ConditionTimeoutException: Condition with alias 'wait' didn't complete within 19 seconds because lambda expression in my.path.MyClass: expected "some value" but was "some value".

How to build awaitility correctly, what is my mistake?

1 Answers1

0

decided not to use awaitility. And use future.get() with a timeout

private List<JournalRecord> waitResult(Callable<List<JournalRecord>> findRecords, String message) {
        ExecutorService executor = Executors.newFixedThreadPool(1);
        AtomicReference<List<JournalRecord >> result = new AtomicReference<>(Collections.emptyList());
        Future<?> future = executor.submit(() -> searchJob(findRecords, message, result));
        try {
            future.get(40, TimeUnit.SECONDS);
            executor.shutdown();
        } catch (TimeoutException ignore) {

        }
        executor.shutdown();
        return result.get();
    }

private void searchJob(Callable<List<JournalRecord>> findRecords,
                           String message,
                           AtomicReference<List<JournalRecord>> result) {
        for (int i = 0; i < 40; i++) {
            //do search in database by message and fill AtomicReference<List<JournalRecord>> result
            if(result was found) {
              return;
            }
            Thread.sleep(1000);
        }
    }

I don't know if it's correct, but it works for me. In fact, the number of attempts depends on the database load, on average I get 7-12. The main task was to limit the execution time of searching for messages in the database.

I would be grateful if someone points out errors or tells me how to improve the solution.