Test: wait for netty threads in a JUnit ClassRule (#30763)

This commit changes the wait for a few netty threads to wait for these
threads to complete after the cluster has stopped. Previously, we were
waiting for these threads before the cluster was actually stopped; the
cluster is stopped in an AfterClass method of ESIntegTestCase, while
the wait was performed in the AfterClass of a class that extended
ESIntegTestCase, which is always executed before the AfterClass of
ESIntegTestCase.

Now, the wait is contained in an ExternalResource ClassRule that
implements the waiting for the threads to terminate in the after
method. This rule is executed after the AfterClass method in
ESIntegTestCase. The same fix has also been applied in
SecuritySingleNodeTestCase.

Closes #30563
This commit is contained in:
Jay Modi 2018-05-22 07:35:16 -06:00 committed by GitHub
parent 54740cc551
commit 0a3b9e2138
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 37 deletions

View File

@ -47,6 +47,7 @@ import org.elasticsearch.xpack.security.support.SecurityIndexManager;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.rules.ExternalResource;
@ -163,24 +164,6 @@ public abstract class SecurityIntegTestCase extends ESIntegTestCase {
public static void destroyDefaultSettings() {
SECURITY_DEFAULT_SETTINGS = null;
customSecuritySettingsSource = null;
// Wait for the network threads to finish otherwise there is the possibility that one of
// the threads lingers and trips the thread leak detector
try {
GlobalEventExecutor.INSTANCE.awaitInactivity(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (IllegalStateException e) {
if (e.getMessage().equals("thread was not started") == false) {
throw e;
}
// ignore since the thread was never started
}
try {
ThreadDeathWatcher.awaitInactivity(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
@Rule
@ -204,6 +187,35 @@ public abstract class SecurityIntegTestCase extends ESIntegTestCase {
}
};
/**
* A JUnit class level rule that runs after the AfterClass method in {@link ESIntegTestCase},
* which stops the cluster. After the cluster is stopped, there are a few netty threads that
* can linger, so we wait for them to finish otherwise these lingering threads can intermittently
* trigger the thread leak detector
*/
@ClassRule
public static final ExternalResource STOP_NETTY_RESOURCE = new ExternalResource() {
@Override
protected void after() {
try {
GlobalEventExecutor.INSTANCE.awaitInactivity(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (IllegalStateException e) {
if (e.getMessage().equals("thread was not started") == false) {
throw e;
}
// ignore since the thread was never started
}
try {
ThreadDeathWatcher.awaitInactivity(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
};
@Before
//before methods from the superclass are run before this, which means that the current cluster is ready to go
public void assertXPackIsInstalled() {

View File

@ -26,6 +26,7 @@ import org.elasticsearch.xpack.security.LocalStateSecurity;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.rules.ExternalResource;
@ -97,9 +98,30 @@ public abstract class SecuritySingleNodeTestCase extends ESSingleNodeTestCase {
IOUtils.closeWhileHandlingException(restClient);
restClient = null;
}
}
// Wait for the network threads to finish otherwise there is the possibility that one of
// the threads lingers and trips the thread leak detector
@Rule
//Rules are the only way to have something run before the before (final) method inherited from ESSingleNodeTestCase
public ExternalResource externalResource = new ExternalResource() {
@Override
protected void before() {
if (customSecuritySettingsSource == null) {
customSecuritySettingsSource =
new CustomSecuritySettingsSource(transportSSLEnabled(), createTempDir(), ESIntegTestCase.Scope.SUITE);
}
}
};
/**
* A JUnit class level rule that runs after the AfterClass method in {@link ESIntegTestCase},
* which stops the cluster. After the cluster is stopped, there are a few netty threads that
* can linger, so we wait for them to finish otherwise these lingering threads can intermittently
* trigger the thread leak detector
*/
@ClassRule
public static final ExternalResource STOP_NETTY_RESOURCE = new ExternalResource() {
@Override
protected void after() {
try {
GlobalEventExecutor.INSTANCE.awaitInactivity(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
@ -117,17 +139,6 @@ public abstract class SecuritySingleNodeTestCase extends ESSingleNodeTestCase {
Thread.currentThread().interrupt();
}
}
@Rule
//Rules are the only way to have something run before the before (final) method inherited from ESSingleNodeTestCase
public ExternalResource externalResource = new ExternalResource() {
@Override
protected void before() {
if (customSecuritySettingsSource == null) {
customSecuritySettingsSource =
new CustomSecuritySettingsSource(transportSSLEnabled(), createTempDir(), ESIntegTestCase.Scope.SUITE);
}
}
};
@Before