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

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

Merged
merged 1 commit into from
May 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
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;

Expand Down Expand Up @@ -163,24 +164,6 @@ public static void initDefaultSettings() {
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
Expand All @@ -204,6 +187,35 @@ protected void before() throws Throwable {
}
};

/**
* 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;

Expand Down Expand Up @@ -97,25 +98,6 @@ private static void tearDownRestClient() {
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
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
Expand All @@ -130,6 +112,35 @@ protected void before() {
}
};

/**
* 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() {
Expand Down