question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Interrupted test cases cause random failures with thread reuse

See original GitHub issue

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 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:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:14 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
ArneDeutschcommented, May 25, 2018

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.

1reaction
marcphilippcommented, May 18, 2019

@joaodias14 Please upgrade to the latest version, it was fixed in 5.4.0 (see https://github.com/junit-team/junit5/issues/1688).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found