Skip to content

Commit ec7f125

Browse files
committed
Refactor test
1 parent 9f2e297 commit ec7f125

File tree

1 file changed

+21
-34
lines changed

1 file changed

+21
-34
lines changed

x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/EsqlListQueriesActionIT.java

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.xpack.core.async.GetAsyncResultRequest;
1616
import org.elasticsearch.xpack.esql.EsqlTestUtils;
1717

18+
import java.io.IOException;
1819
import java.util.Map;
1920
import java.util.concurrent.TimeUnit;
2021

@@ -41,26 +42,10 @@ public void testRunningQueries() throws Exception {
4142
try (var initialResponse = sendAsyncQuery()) {
4243
id = initialResponse.asyncExecutionId().get();
4344

45+
assertRunningQueries();
4446
var getResultsRequest = new GetAsyncResultRequest(id);
4547
getResultsRequest.setWaitForCompletionTimeout(timeValueSeconds(1));
4648
client().execute(EsqlAsyncGetResultAction.INSTANCE, getResultsRequest).get().close();
47-
Response listResponse = getRestClient().performRequest(new Request("GET", "/_query/queries"));
48-
@SuppressWarnings("unchecked")
49-
var listResult = (Map<String, Map<String, Object>>) EsqlTestUtils.singleValue(
50-
jsonEntityToMap(listResponse.getEntity()).values()
51-
);
52-
String queryId = EsqlTestUtils.singleValue(listResult.keySet());
53-
MapMatcher basicMatcher = MapMatcher.matchesMap()
54-
.entry("query", is(QUERY))
55-
.entry("start_time_millis", IntOrLongMatcher.isIntOrLong())
56-
.entry("running_time_nanos", IntOrLongMatcher.isIntOrLong());
57-
MapMatcher.assertMap(EsqlTestUtils.singleValue(listResult.values()), basicMatcher);
58-
59-
Response getQueryResponse = getRestClient().performRequest(new Request("GET", "/_query/queries/" + queryId));
60-
MapMatcher.assertMap(
61-
jsonEntityToMap(getQueryResponse.getEntity()),
62-
basicMatcher.entry("documents_found", IntOrLongMatcher.isIntOrLong()).entry("values_loaded", IntOrLongMatcher.isIntOrLong())
63-
);
6449
} finally {
6550
if (id != null) {
6651
// Finish the query.
@@ -77,29 +62,31 @@ public void testRunningQueriesSync() throws Exception {
7762
var future = sendSyncQueryAsyncly();
7863
try {
7964
scriptWaits.acquire();
80-
Response listResponse = getRestClient().performRequest(new Request("GET", "/_query/queries"));
81-
@SuppressWarnings("unchecked")
82-
var listResult = (Map<String, Map<String, Object>>) EsqlTestUtils.singleValue(
83-
jsonEntityToMap(listResponse.getEntity()).values()
84-
);
85-
String queryId = EsqlTestUtils.singleValue(listResult.keySet());
86-
MapMatcher basicMatcher = MapMatcher.matchesMap()
87-
.entry("query", is(QUERY))
88-
.entry("start_time_millis", IntOrLongMatcher.isIntOrLong())
89-
.entry("running_time_nanos", IntOrLongMatcher.isIntOrLong());
90-
MapMatcher.assertMap(EsqlTestUtils.singleValue(listResult.values()), basicMatcher);
91-
92-
Response getQueryResponse = getRestClient().performRequest(new Request("GET", "/_query/queries/" + queryId));
93-
MapMatcher.assertMap(
94-
jsonEntityToMap(getQueryResponse.getEntity()),
95-
basicMatcher.entry("documents_found", IntOrLongMatcher.isIntOrLong()).entry("values_loaded", IntOrLongMatcher.isIntOrLong())
96-
);
65+
assertRunningQueries();
9766
} finally {
9867
scriptPermits.release(numberOfDocs());
9968
future.actionGet(timeValueSeconds(60)).close();
10069
}
10170
}
10271

72+
private static void assertRunningQueries() throws IOException {
73+
Response listResponse = getRestClient().performRequest(new Request("GET", "/_query/queries"));
74+
@SuppressWarnings("unchecked")
75+
var listResult = (Map<String, Map<String, Object>>) EsqlTestUtils.singleValue(jsonEntityToMap(listResponse.getEntity()).values());
76+
String queryId = EsqlTestUtils.singleValue(listResult.keySet());
77+
MapMatcher basicMatcher = MapMatcher.matchesMap()
78+
.entry("query", is(QUERY))
79+
.entry("start_time_millis", IntOrLongMatcher.isIntOrLong())
80+
.entry("running_time_nanos", IntOrLongMatcher.isIntOrLong());
81+
MapMatcher.assertMap(EsqlTestUtils.singleValue(listResult.values()), basicMatcher);
82+
83+
Response getQueryResponse = getRestClient().performRequest(new Request("GET", "/_query/queries/" + queryId));
84+
MapMatcher.assertMap(
85+
jsonEntityToMap(getQueryResponse.getEntity()),
86+
basicMatcher.entry("documents_found", IntOrLongMatcher.isIntOrLong()).entry("values_loaded", IntOrLongMatcher.isIntOrLong())
87+
);
88+
}
89+
10390
private EsqlQueryResponse sendAsyncQuery() {
10491
scriptPermits.drainPermits();
10592
scriptPermits.release(between(1, 5));

0 commit comments

Comments
 (0)