Skip to content

Commit 6924a15

Browse files
committed
Test: wait for netty threads in a JUnit ClassRule
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 elastic#30301
1 parent be8c0f2 commit 6924a15

File tree

2 files changed

+60
-37
lines changed

2 files changed

+60
-37
lines changed

x-pack/plugin/security/src/test/java/org/elasticsearch/test/SecurityIntegTestCase.java

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.junit.AfterClass;
4848
import org.junit.Before;
4949
import org.junit.BeforeClass;
50+
import org.junit.ClassRule;
5051
import org.junit.Rule;
5152
import org.junit.rules.ExternalResource;
5253

@@ -163,24 +164,6 @@ public static void initDefaultSettings() {
163164
public static void destroyDefaultSettings() {
164165
SECURITY_DEFAULT_SETTINGS = null;
165166
customSecuritySettingsSource = null;
166-
// Wait for the network threads to finish otherwise there is the possibility that one of
167-
// the threads lingers and trips the thread leak detector
168-
try {
169-
GlobalEventExecutor.INSTANCE.awaitInactivity(5, TimeUnit.SECONDS);
170-
} catch (InterruptedException e) {
171-
Thread.currentThread().interrupt();
172-
} catch (IllegalStateException e) {
173-
if (e.getMessage().equals("thread was not started") == false) {
174-
throw e;
175-
}
176-
// ignore since the thread was never started
177-
}
178-
179-
try {
180-
ThreadDeathWatcher.awaitInactivity(5, TimeUnit.SECONDS);
181-
} catch (InterruptedException e) {
182-
Thread.currentThread().interrupt();
183-
}
184167
}
185168

186169
@Rule
@@ -204,6 +187,35 @@ protected void before() throws Throwable {
204187
}
205188
};
206189

190+
/**
191+
* A JUnit class level rule that runs after the AfterClass method in {@link ESIntegTestCase},
192+
* which stops the cluster. After the cluster is stopped, there are a few netty threads that
193+
* can linger, so we wait for them to finish otherwise these lingering threads can intermittently
194+
* trigger the thread leak detector
195+
*/
196+
@ClassRule
197+
public static final ExternalResource STOP_NETTY_RESOURCE = new ExternalResource() {
198+
@Override
199+
protected void after() {
200+
try {
201+
GlobalEventExecutor.INSTANCE.awaitInactivity(5, TimeUnit.SECONDS);
202+
} catch (InterruptedException e) {
203+
Thread.currentThread().interrupt();
204+
} catch (IllegalStateException e) {
205+
if (e.getMessage().equals("thread was not started") == false) {
206+
throw e;
207+
}
208+
// ignore since the thread was never started
209+
}
210+
211+
try {
212+
ThreadDeathWatcher.awaitInactivity(5, TimeUnit.SECONDS);
213+
} catch (InterruptedException e) {
214+
Thread.currentThread().interrupt();
215+
}
216+
}
217+
};
218+
207219
@Before
208220
//before methods from the superclass are run before this, which means that the current cluster is ready to go
209221
public void assertXPackIsInstalled() {

x-pack/plugin/security/src/test/java/org/elasticsearch/test/SecuritySingleNodeTestCase.java

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.junit.AfterClass;
2727
import org.junit.Before;
2828
import org.junit.BeforeClass;
29+
import org.junit.ClassRule;
2930
import org.junit.Rule;
3031
import org.junit.rules.ExternalResource;
3132

@@ -97,25 +98,6 @@ private static void tearDownRestClient() {
9798
IOUtils.closeWhileHandlingException(restClient);
9899
restClient = null;
99100
}
100-
101-
// Wait for the network threads to finish otherwise there is the possibility that one of
102-
// the threads lingers and trips the thread leak detector
103-
try {
104-
GlobalEventExecutor.INSTANCE.awaitInactivity(5, TimeUnit.SECONDS);
105-
} catch (InterruptedException e) {
106-
Thread.currentThread().interrupt();
107-
} catch (IllegalStateException e) {
108-
if (e.getMessage().equals("thread was not started") == false) {
109-
throw e;
110-
}
111-
// ignore since the thread was never started
112-
}
113-
114-
try {
115-
ThreadDeathWatcher.awaitInactivity(5, TimeUnit.SECONDS);
116-
} catch (InterruptedException e) {
117-
Thread.currentThread().interrupt();
118-
}
119101
}
120102

121103
@Rule
@@ -130,6 +112,35 @@ protected void before() {
130112
}
131113
};
132114

115+
/**
116+
* A JUnit class level rule that runs after the AfterClass method in {@link ESIntegTestCase},
117+
* which stops the cluster. After the cluster is stopped, there are a few netty threads that
118+
* can linger, so we wait for them to finish otherwise these lingering threads can intermittently
119+
* trigger the thread leak detector
120+
*/
121+
@ClassRule
122+
public static final ExternalResource STOP_NETTY_RESOURCE = new ExternalResource() {
123+
@Override
124+
protected void after() {
125+
try {
126+
GlobalEventExecutor.INSTANCE.awaitInactivity(5, TimeUnit.SECONDS);
127+
} catch (InterruptedException e) {
128+
Thread.currentThread().interrupt();
129+
} catch (IllegalStateException e) {
130+
if (e.getMessage().equals("thread was not started") == false) {
131+
throw e;
132+
}
133+
// ignore since the thread was never started
134+
}
135+
136+
try {
137+
ThreadDeathWatcher.awaitInactivity(5, TimeUnit.SECONDS);
138+
} catch (InterruptedException e) {
139+
Thread.currentThread().interrupt();
140+
}
141+
}
142+
};
143+
133144
@Before
134145
//before methods from the superclass are run before this, which means that the current cluster is ready to go
135146
public void assertXPackIsInstalled() {

0 commit comments

Comments
 (0)