Interrupted test cases cause random failures with thread reuse
See original GitHub issueWhenever 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 behaviour: When the thread is reused, 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.
The following code should reproduce the problem, given that test1 is run before test2
import org.junit.Test;
public class ThreadTest {
@Test
public void test1() {
Thread.currentThread().interrupt();
}
@Test
public void test2() throws InterruptedException {
Thread.sleep(1000);
}
}
The problem I encountered is similar to the one described in https://www.thoughtwire.com/junit-interupt/
Issue Analytics
- State:
- Created 7 years ago
- Reactions:3
- Comments:14 (11 by maintainers)
Top Results From Across the Web
Interrupted test cases cause random failures with thread reuse
When the thread is reused, the interrupted flag for the thread is cleaned before starting a new test case. This can be done...
Read more >Is it OK to ignore InterruptedException if nobody calls interrupt()?
On the other hand, InterruptedException is not thrown by the JVM itself in case of a hardware failure, it is a user indicated...
Read more >Java static code analysis: "InterruptedException" should not ...
Reusable resources should be initialized at construction time of Lambda functions ... "Thread.sleep" should not be used in tests ... Java parser failure....
Read more >XPC connection interrupted | Apple Developer Forums
Hi,. I am getting this error, "XPC connection interrupted" and caused my app to crash. Any idea what is this? I had tried...
Read more >How to Handle Exceptions With the ThreadPoolExecutor in ...
In this tutorial, you will discover how to handle exceptions in a Python thread pool. Let's get started. Table of Contents.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I would support the idea to call
Thread.interrupted()
on each thread that is reused for more than one test case just before executing a new test case. But especially if one thread is used for more then one test class. The interrupted flag is state that leaks from one test case to others, making test cases flaky. Especially the test cases that deal with multiple threads that are notoriously hard to get right anyway.@joaodias14 Please upgrade to the latest version, it was fixed in 5.4.0 (see https://github.com/junit-team/junit5/issues/1688).