Skip to content

Support timeout assertions for lambdas #3637

Description

@alkismavridis

Feature summary

I would find it very helpful if there was a way to assert that a block of code runs (succeeds / fails) within a certain time duration (without having to add extra dependencies such as awaitility).
AssertJ already supports such assertions for java.util.concurrent.Future arguments:

assertThat(myFuture).succeedsWithin(Duration.ofMillis(2000));

One can of course convert a lambda into a Future and then use these assertions.
What this feature request is about is avoiding the boilerplate necessary to do that. It is not nice and often not trivial, especially if the code block throws checked exceptions. Basically, I ask for an analogue to jupiters org.junit.jupiter.api.Assertions.assertTimeout.

I think this could work nicely with assertThatCode, but the exact semantics are of course up to you.

Example

assertThatCode(() -> {
	CloseableHttpClient client = HttpClientFactory.getInstance().create();
	HttpRequestUtils rqUtils = new HttpRequestUtils(client);

	assertThat(rqUtils.isAvailable(new URI("http://www.orf.at"))).isTrue();
	assertThat(rqUtils.isAvailable(new URI("www.____NOTEXISTING___.com"))).isFalse();
}).succeedsWithin(Duration.ofMillis(2000));

Motivation:

I often convert projects from hamcrest/jupiter assertions to exclusively use assertJ. Jupiters assertTimeout was the only case I could find so far where convertion to assertJ was not possible without adding significant boilerplate.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions