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

Interrupted test cases cause random failures with thread reuse #1688

Closed
2 tasks done
bacer-esteco opened this issue Nov 28, 2018 · 6 comments
Closed
2 tasks done

Interrupted test cases cause random failures with thread reuse #1688

bacer-esteco opened this issue Nov 28, 2018 · 6 comments

Comments

@bacer-esteco
Copy link

bacer-esteco commented Nov 28, 2018

Status Quo

Whenever a test case is interrupted (Thread.interrupt()), the test runner thread seems to stay in interrupted state. This causes random failures later when the thread is reused for other test cases.

Expected Behavior

When the thread is reused, the interrupted flag for the thread is cleaned before starting a new test case. This can be done by calling Thread.interrupted(), which returns the interrupted state and clears the interrupted flag.

Steps to Reproduce

The following code reproduces the problem (when run against JUnit Jupiter 5.4 snapshots), given that test1() is run before test2().

package example;

import org.junit.jupiter.api.MethodOrderer.Alphanumeric;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

@TestMethodOrder(Alphanumeric.class)
class ThreadTests {

  @Test
  void test1() {
    Thread.currentThread().interrupt();
  }

  @Test
  void test2() throws InterruptedException {
    Thread.sleep(1000);
  }

}

Analysis

This problem is reproducible using both Jupiter and Vintage (tried on version 5.3.1), launching the test both from Gradle 4.10.2 and IntelliJ IDEA 2018.3 (and previous).

I guess it is the same exact bug reported in JUnit 4 (junit-team/junit4#1365)

Deliverables

  • Reset the interrupted flag before/after executing a Node.
  • Document in Release Notes.
@sbrannen
Copy link
Member

Tentatively slated for 5.4 M1 for team discussion.

@sbrannen
Copy link
Member

I added @TestMethodOrder(Alphanumeric.class) to the example and verified that test2() always fails on master.

@sbrannen
Copy link
Member

Inserting Thread.interrupted(); as the first line in org.junit.platform.engine.support.hierarchical.NodeTestTask.execute() allows the example test class to execute correctly.

@junit-team/junit-lambda, As for whether or not that is the correct location to invoke Thread.interrupted(); is open for discussion.

@sbrannen
Copy link
Member

I have removed the Vintage label since the invocation of JUnit 4 tests is handled by the corresponding JUnit 4 Runner which is not under the control of the VintageTestEngine.

@marcphilipp
Copy link
Member

Team Decision: Reset the flag before/after executing a Node.

@sbrannen
Copy link
Member

in progress

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

No branches or pull requests

3 participants