Skip to content

Commit 999274c

Browse files
Cleanup HotThreadsIT (example of test cleanup) (#115601)
Just a quick example of how to save quite a few lines of code and make a test easier to reason about.
1 parent 13e67bd commit 999274c

File tree

1 file changed

+15
-39
lines changed

1 file changed

+15
-39
lines changed

server/src/internalClusterTest/java/org/elasticsearch/action/admin/HotThreadsIT.java

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import org.apache.logging.log4j.Level;
1212
import org.apache.lucene.util.Constants;
13-
import org.elasticsearch.action.ActionListener;
13+
import org.elasticsearch.action.ActionFuture;
1414
import org.elasticsearch.action.admin.cluster.node.hotthreads.NodeHotThreads;
1515
import org.elasticsearch.action.admin.cluster.node.hotthreads.NodesHotThreadsRequest;
1616
import org.elasticsearch.action.admin.cluster.node.hotthreads.NodesHotThreadsResponse;
@@ -26,15 +26,14 @@
2626
import org.hamcrest.Matcher;
2727

2828
import java.util.Map;
29-
import java.util.concurrent.CountDownLatch;
30-
import java.util.concurrent.atomic.AtomicBoolean;
29+
import java.util.concurrent.ExecutionException;
3130

3231
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
3332
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
3433
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
3534
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
35+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponse;
3636
import static org.hamcrest.CoreMatchers.equalTo;
37-
import static org.hamcrest.CoreMatchers.is;
3837
import static org.hamcrest.CoreMatchers.notNullValue;
3938
import static org.hamcrest.Matchers.allOf;
4039
import static org.hamcrest.Matchers.containsString;
@@ -44,11 +43,10 @@
4443

4544
public class HotThreadsIT extends ESIntegTestCase {
4645

47-
public void testHotThreadsDontFail() throws InterruptedException {
46+
public void testHotThreadsDontFail() throws InterruptedException, ExecutionException {
4847
// This test just checks if nothing crashes or gets stuck etc.
4948
createIndex("test");
5049
final int iters = scaledRandomIntBetween(2, 20);
51-
final AtomicBoolean hasErrors = new AtomicBoolean(false);
5250
for (int i = 0; i < iters; i++) {
5351
final NodesHotThreadsRequest request = new NodesHotThreadsRequest(
5452
Strings.EMPTY_ARRAY,
@@ -67,36 +65,7 @@ public void testHotThreadsDontFail() throws InterruptedException {
6765
randomBoolean()
6866
)
6967
);
70-
final CountDownLatch latch = new CountDownLatch(1);
71-
client().execute(TransportNodesHotThreadsAction.TYPE, request, new ActionListener<>() {
72-
@Override
73-
public void onResponse(NodesHotThreadsResponse nodeHotThreads) {
74-
boolean success = false;
75-
try {
76-
assertThat(nodeHotThreads, notNullValue());
77-
Map<String, NodeHotThreads> nodesMap = nodeHotThreads.getNodesMap();
78-
assertThat(nodeHotThreads.failures(), empty());
79-
assertThat(nodesMap.size(), equalTo(cluster().size()));
80-
for (NodeHotThreads ht : nodeHotThreads.getNodes()) {
81-
assertNotNull(ht.getHotThreads());
82-
}
83-
success = true;
84-
} finally {
85-
if (success == false) {
86-
hasErrors.set(true);
87-
}
88-
latch.countDown();
89-
}
90-
}
91-
92-
@Override
93-
public void onFailure(Exception e) {
94-
logger.error("FAILED", e);
95-
hasErrors.set(true);
96-
latch.countDown();
97-
fail();
98-
}
99-
});
68+
final ActionFuture<NodesHotThreadsResponse> hotThreadsFuture = client().execute(TransportNodesHotThreadsAction.TYPE, request);
10069

10170
indexRandom(
10271
true,
@@ -105,7 +74,7 @@ public void onFailure(Exception e) {
10574
prepareIndex("test").setId("3").setSource("field1", "value3")
10675
);
10776
ensureSearchable();
108-
while (latch.getCount() > 0) {
77+
while (hotThreadsFuture.isDone() == false) {
10978
assertHitCount(
11079
prepareSearch().setQuery(matchAllQuery())
11180
.setPostFilter(
@@ -115,8 +84,15 @@ public void onFailure(Exception e) {
11584
3L
11685
);
11786
}
118-
safeAwait(latch);
119-
assertThat(hasErrors.get(), is(false));
87+
assertResponse(hotThreadsFuture, nodeHotThreads -> {
88+
assertThat(nodeHotThreads, notNullValue());
89+
Map<String, NodeHotThreads> nodesMap = nodeHotThreads.getNodesMap();
90+
assertThat(nodeHotThreads.failures(), empty());
91+
assertThat(nodesMap.size(), equalTo(cluster().size()));
92+
for (NodeHotThreads ht : nodeHotThreads.getNodes()) {
93+
assertNotNull(ht.getHotThreads());
94+
}
95+
});
12096
}
12197
}
12298

0 commit comments

Comments
 (0)