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

Mark ThreadGroups created by FailOnTimeout as daemon groups #1687

Merged
merged 6 commits into from
Jan 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,28 +121,21 @@ public void evaluate() throws Throwable {
CallableStatement callable = new CallableStatement();
FutureTask<Throwable> task = new FutureTask<Throwable>(callable);
ThreadGroup threadGroup = new ThreadGroup("FailOnTimeoutGroup");
Thread thread = new Thread(threadGroup, task, "Time-limited test");
try {
thread.setDaemon(true);
thread.start();
callable.awaitStarted();
Throwable throwable = getResult(task, thread);
if (throwable != null) {
throw throwable;
}
} finally {
if (!threadGroup.isDaemon()) {
try {
thread.join(1);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
try {
threadGroup.destroy();
} catch (IllegalThreadStateException e) {
// If a thread from the group is still alive, the ThreadGroup cannot be destroyed.
threadGroup.setDaemon(true);
Copy link

Choose a reason for hiding this comment

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

There should be a comment here pointing to this pull request and explaining why they should be daemon.

Copy link
Contributor

Choose a reason for hiding this comment

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

#1691 adds an explanation

Copy link

Choose a reason for hiding this comment

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

Certainly does. Thanks.

} catch (SecurityException e) {
// Swallow the exception to keep the same behavior prior to this change.
kcooney marked this conversation as resolved.
Show resolved Hide resolved
}
}
Thread thread = new Thread(threadGroup, task, "Time-limited test");
thread.setDaemon(true);
thread.start();
callable.awaitStarted();
Throwable throwable = getResult(task, thread);
if (throwable != null) {
throw throwable;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@
import static org.junit.Assert.fail;
import static org.junit.internal.runners.statements.FailOnTimeout.builder;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.junit.Test;
import org.junit.function.ThrowingRunnable;
import org.junit.internal.runners.statements.Fail;
import org.junit.runners.model.Statement;
import org.junit.runners.model.TestTimedOutException;


/**
* @author Asaf Ary, Stefan Birkner
*/
Expand Down Expand Up @@ -91,20 +89,24 @@ public void throwsExceptionWithTimeoutValueAndTimeUnitSet() {
assertEquals(TimeUnit.MILLISECONDS, e.getTimeUnit());
}

private ThrowingRunnable evaluateWithException(final Exception exception) {
private ThrowingRunnable evaluateWithDelegate(final Statement delegate) {
return new ThrowingRunnable() {
public void run() throws Throwable {
statement.nextException = exception;
statement.nextStatement = delegate;
statement.waitDuration = 0;
failOnTimeout.evaluate();
}
};
}

private ThrowingRunnable evaluateWithException(Exception exception) {
return evaluateWithDelegate(new Fail(exception));
}

private ThrowingRunnable evaluateWithWaitDuration(final long waitDuration) {
return new ThrowingRunnable() {
public void run() throws Throwable {
statement.nextException = null;
statement.nextStatement = null;
statement.waitDuration = waitDuration;
failOnTimeout.evaluate();
}
Expand All @@ -114,13 +116,13 @@ public void run() throws Throwable {
private static final class TestStatement extends Statement {
long waitDuration;

Exception nextException;
Statement nextStatement;

@Override
public void evaluate() throws Throwable {
sleep(waitDuration);
if (nextException != null) {
throw nextException;
if (nextStatement != null) {
nextStatement.evaluate();
}
}
}
Expand Down Expand Up @@ -209,21 +211,16 @@ private void notTheRealCauseOfTheTimeout() {
}

@Test
public void threadGroupNotLeaked() throws Throwable {
Collection<ThreadGroup> groupsBeforeSet = subGroupsOfCurrentThread();

evaluateWithWaitDuration(0);

for (ThreadGroup group: subGroupsOfCurrentThread()) {
if (!groupsBeforeSet.contains(group) && "FailOnTimeoutGroup".equals(group.getName())) {
fail("A 'FailOnTimeoutGroup' thread group remains referenced after the test execution.");
public void threadGroupIsDaemon() throws Throwable {
ThrowingRunnable runnable = evaluateWithDelegate(new Statement() {
@Override
public void evaluate() {
ThreadGroup group = currentThread().getThreadGroup();
assertEquals("FailOnTimeoutGroup", group.getName());
assertTrue("The 'FailOnTimeoutGroup' thread group should be a daemon thread group.", group.isDaemon());
}
}
}

private Collection<ThreadGroup> subGroupsOfCurrentThread() {
ThreadGroup[] subGroups = new ThreadGroup[256];
int numGroups = currentThread().getThreadGroup().enumerate(subGroups);
return Arrays.asList(subGroups).subList(0, numGroups);
});

runnable.run();
}
}