Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce lambda based timeout assertions #439

Merged
merged 1 commit into from
Jul 27, 2016

Conversation

sbrannen
Copy link
Member

@sbrannen sbrannen commented Jul 26, 2016

Overview

This commit introduces assertTimeout() methods in Assertions that support both lazy and preemptive timeouts for blocks of code supplied as lambda expressions.

For example, the following assertion would pass:

assertTimeout(ofMillis(500), () -> Thread.sleep(10));

Whereas, the following assertion would fail:

assertTimeout(ofMillis(10), () -> Thread.sleep(500));

The above examples execute the code block in the same thread as the caller. To perform the same assertion as the last example and have the code executed in a separate thread and then preemptively aborted, the above can be rewritten as follows.

assertTimeoutPreemptively(ofMillis(10), () -> Thread.sleep(500));

Issue: #438


I hereby agree to the terms of the JUnit Contributor License Agreement.

@sbrannen
Copy link
Member Author

@junit-team/junit-lambda, please let me know your thoughts on the PR.

* <em>Asserts</em> that execution of the supplied {@code executable}
* completes before the given {@code timeout} is exceeded.
*
* <p>Note: the executable will be executed in the same thread as that
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: space between "as" and "that". same for other timeout methods

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch! (y)

I'll fix it.

@sbrannen sbrannen force-pushed the issues/438-assert-timeout branch 6 times, most recently from 2309eda to a7e71d8 Compare July 27, 2016 12:59
This commit introduces assertTimeout() methods in Assertions that
support both lazy and preemptive timeouts for blocks of code supplied
as lambda expressions.

For example, the following assertion would pass:

assertTimeout(ofMillis(500), () -> Thread.sleep(10));

Whereas, the following assertion would fail:

assertTimeout(ofMillis(10), () -> Thread.sleep(500));

The above examples execute the code block in the same thread as the
caller. To perform the same assertions and have the code executed in a
separate thread and then preemptively aborted, the above can be
rewritten as follows.

assertTimeoutPreemptively(ofMillis(10), () -> Thread.sleep(500));

Issue: #438
@sbrannen sbrannen force-pushed the issues/438-assert-timeout branch from a7e71d8 to 0759a68 Compare July 27, 2016 15:05
@marcphilipp
Copy link
Member

Looks good to me! Great to finally have support for timeouts! 👍

@sbrannen
Copy link
Member Author

Thanks for the review, and... I couldn't agree more: it definitely is great to finally have official support for timeouts in JUnit 5!

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

Successfully merging this pull request may close these issues.

3 participants