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

Change the Timeout rule to never timeout the test if zero is passed in. Fixes #875 #876

Merged
merged 1 commit into from
Apr 16, 2014

Conversation

madrob
Copy link

@madrob madrob commented Apr 14, 2014

No description provided.

@busbey
Copy link

busbey commented Apr 14, 2014

Looks good, fixes my test case.

It does change a bit of behavior from 4.7-4.11. In those, a negative timeout would cause an error. With this change, negative will be interpreted as "no timeout." Since that's the same thing the Test annotation's timeout does, it seems fine. Worth noting though.

Is it worth adding a note in the documentation for Test and Timeout that a non-positive timeout means to wait forever?

@kcooney
Copy link
Member

kcooney commented Apr 14, 2014

Thanks for the fix.

Note with this change, if timeout is zero, the test will run in the same thread. In 4.7 it still ran in a different thread.

I think we should instead treat zero as if they passed in MAX_INT

@kcooney
Copy link
Member

kcooney commented Apr 14, 2014

Also, could you put in a more useful description? See http://robots.thoughtbot.com/5-useful-tips-for-a-better-commit-message

Thanks!

@madrob
Copy link
Author

madrob commented Apr 14, 2014

Would that be MAX_INT seconds or millis?

@busbey
Copy link

busbey commented Apr 14, 2014

We could instead update FailOnTimeout's getResult:

    private Throwable getResult(FutureTask<Throwable> task, Thread thread) {
        try {
          if (fTimeout > 0) {
            return task.get(fTimeout, fTimeUnit);
          } else {
            return task.get();
          }
        } catch (InterruptedException e) {
            return e; // caller will re-throw; no need to call Thread.interrupt()
        } catch (ExecutionException e) {
            // test failed; have caller re-throw the exception thrown by the test
            return e.getCause();
        } catch (TimeoutException e) {
            return createTimeoutException(thread);
        }
    }

That would get you execution in a different thread and behavior consistent with the Test annotation.

@madrob
Copy link
Author

madrob commented Apr 14, 2014

Updated. I had not considered Sean's suggestion, but can do that instead if it is deemed preferable.

@kcooney
Copy link
Member

kcooney commented Apr 14, 2014

@madrob I think Sean's suggestion was much better than mine; his works regardless of the units passed in. My only concern with fixing this FailOnTimeout is that it might cause a change in behavior in the timeout attribute of @Test compared with 4.7. I haven't looked at the 4.7 behavior; could someone on this thread do that?

@kcooney kcooney changed the title fixes #875 Change the Timeout rule to never timeout the test if zero is passed in.fixes #875 Apr 14, 2014
@kcooney kcooney changed the title Change the Timeout rule to never timeout the test if zero is passed in.fixes #875 Change the Timeout rule to never timeout the test if zero is passed in. Apr 14, 2014
@kcooney kcooney changed the title Change the Timeout rule to never timeout the test if zero is passed in. Change the Timeout rule to never timeout the test if zero is passed in. Fixes #875 Apr 14, 2014
@madrob
Copy link
Author

madrob commented Apr 14, 2014

@kcooney In 4.7 @Test would happily wait forever if the timeout attribute was not set. The default value returned by the timeout() method on the annotation is 0, so every test without a set timeout verifies this behavior. That part is pretty well covered against regressions already :)

Was there a different concern you had?

@kcooney
Copy link
Member

kcooney commented Apr 14, 2014

@madrob no other concerns. Let's make the fix that @busbey suggested.

@madrob
Copy link
Author

madrob commented Apr 14, 2014

Implemented in 14ecb4b and already pushed.

@busbey
Copy link

busbey commented Apr 14, 2014

lgtm

@marcphilipp
Copy link
Member

+1

@@ -55,7 +55,11 @@ public void evaluate() throws Throwable {
*/
private Throwable getResult(FutureTask<Throwable> task, Thread thread) {
try {
return task.get(fTimeout, fTimeUnit);
if (fTimeout > 0) {
return task.get(fTimeout, fTimeUnit);
Copy link
Member

Choose a reason for hiding this comment

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

nit: this should be indented one more space (we use a four-space indent in JUnit)

@kcooney
Copy link
Member

kcooney commented Apr 15, 2014

LGTM with one very minor style comment. Thanks!

Fixes junit-team#875

In versions 4.7 through 4.11 setting up a Timeout @rule with a value of
0 would result in no timeout. In 4.12 FailOnTimeout switched to using
FutureTask.get(timeout). That method treats non-positive timeouts to
mean "timeout right now".

This fix checks that the timeout value is positive before using it,
and otherwise will wait indefinitely.
@madrob
Copy link
Author

madrob commented Apr 15, 2014

Indent fixed in c52397d. Somehow I managed to accidentally sneak a tab in there earlier.

@kcooney
Copy link
Member

kcooney commented Apr 16, 2014

Thanks!

kcooney added a commit that referenced this pull request Apr 16, 2014
Change the Timeout rule to never timeout the test if zero is passed in. Fixes #875
@kcooney kcooney merged commit c399dd6 into junit-team:master Apr 16, 2014
@madrob madrob deleted the #875 branch April 16, 2014 15:00
@stefanbirkner stefanbirkner added this to the 4.12 milestone Apr 17, 2014
@stefanbirkner
Copy link
Contributor

@madrob Could you please make a note at https://github.com/junit-team/junit/wiki/4.12-release-notes

@madrob
Copy link
Author

madrob commented Apr 17, 2014

Done, how's that look?

@Stephan202
Copy link
Contributor

The comments above discuss subtle differences with earlier versions of JUnit. Not sure which of those are applicable to the code that was eventually merged (didn't check), but I think that the current release notes entry is not particularly useful:

In JUnit 4.7 - 4.11 setting up a Timeout rule with value 0 would result in no timeout, consistent with @Test(timeout = 0). The switch in 4.12 to using FutureTask for the implementation would instead cause a timeout immediately, and this change brings the behavior back in line with previous versions.

A regular user upgrading from JUnit 4.11 to 4.12 will conclude that "nothing changed for me" and wonder why such implementation details are mentioned in the release notes. (@madrob effectively just caught a regression and fixed it in time.) Can we instead document the ways in which timeouts are now handled differently, if at all, and otherwise omit this from the release notes?

@kcooney
Copy link
Member

kcooney commented Jul 11, 2014

@Stephan202 good point. I removed the text about this pull from the release notes.

stefanbirkner added a commit to stefanbirkner/junit that referenced this pull request Oct 2, 2018
We missed to add it before we released JUnit 4.12. I think it may be
still helpful when someone reads the old release notes.
stefanbirkner added a commit to stefanbirkner/junit that referenced this pull request Oct 3, 2018
We missed to add it before we released JUnit 4.12. I think it may be
still helpful when someone reads the old release notes.
aristotle0x01 pushed a commit to aristotle0x01/junit4 that referenced this pull request Jun 27, 2022
We missed to add it before we released JUnit 4.12. I think it may be
still helpful when someone reads the old release notes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants