testng-team / testng

TestNG testing framework
https://testng.org
Apache License 2.0
1.99k stars 1.02k forks source link

Thread interrupt flag persists between test methods #2075

Closed kcorman closed 5 years ago

kcorman commented 5 years ago

TestNG Version

v6.14.3

Expected behavior

When a test method returns with the current thread interrupt flag set, I would expect one of the following things to happen

Either of the above approaches would be reasonably easy to deal with and understand what's going on

Actual behavior

The interrupt flag stays on the thread running the test, which means if that particular thread executes another test which makes an interruptible call, it will throw an interrupted exception, typically leading to unexpected failures that have nothing to do with the current test.

Is the issue reproductible on runner?

Test case sample

public class InterruptTest {
    @Test(priority=1)
    public void interruptsTheCurrentThread() {
        Thread.currentThread().interrupt();
    }
    @Test(priority=2)
    public void shouldntGetInterruptedButDoes() throws InterruptedException {
        Thread.sleep(0); // this will throw an exception
    }
}
juherr commented 5 years ago

I'm not sure it is an issue: by design, TestNG keeps state between tests and you have to clean the context by yourself. But agree, it could be difficult to understand why it is failing.

The test fails due to finishing with the interrupt flag set to true

is definitively my favorite solution.

Fyi, you should be able to have the expected behavior by using a listener that will check the thread flag and mark the test as failed.

krmahadevan commented 5 years ago

@kcorman - As @juherr mentioned, TestNG is not supposed to be aware of the interrupted states because it doesn't set them. You should be able to get this sorted out at your test project level itself by building an IInvokedMethodListener implementation wherein you do something like this

if (Thread.currentThread().isInterrupted()) {
   Thread.interrupted();
}

Closing this issue.