Skip to content

Commit

Permalink
Improve test assertions for debugging.
Browse files Browse the repository at this point in the history
  • Loading branch information
przemekwitek committed May 14, 2024
1 parent 491bc1d commit dcbc83f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.xpack.core.transform.TransformField;
import org.elasticsearch.xpack.core.transform.action.GetTransformNodeStatsAction;
import org.elasticsearch.xpack.core.transform.transforms.persistence.TransformInternalIndexConstants;

import java.io.IOException;
Expand Down Expand Up @@ -77,8 +78,12 @@ protected List<String> getTransformTasksFromClusterState(String transformId) thr
return tasks.stream().map(t -> (String) t.get("id")).filter(transformId::equals).toList();
}

protected Response getNodeStats() throws IOException {
return adminClient().performRequest(new Request("GET", "/_transform/_node_stats"));
}

protected int getTotalRegisteredTransformCount() throws IOException {
Response response = adminClient().performRequest(new Request("GET", "/_transform/_node_stats"));
Response response = getNodeStats();
return (int) XContentMapValues.extractValue(entityAsMap(response), "total", "registered_transform_count");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void setClusterSettings() throws IOException {
{
"persistent": {
"logger.org.elasticsearch.xpack.core.indexing.AsyncTwoPhaseIndexer": "debug",
"logger.org.elasticsearch.xpack.transform": "debug"
"logger.org.elasticsearch.xpack.transform": "trace"
}
}""");
client().performRequest(settingsRequest);
Expand Down Expand Up @@ -245,19 +245,19 @@ public void testTransformLifecycleInALoop() throws Exception {
putTransform(transformId, config, RequestOptions.DEFAULT);
assertThat(getTransformTasks(), is(empty()));
assertThat(getTransformTasksFromClusterState(transformId), is(empty()));
assertThat(getTotalRegisteredTransformCount(), is(equalTo(0)));
assertThat("Node stats were: " + entityAsMap(getNodeStats()), getTotalRegisteredTransformCount(), is(equalTo(0)));

startTransform(transformId, RequestOptions.DEFAULT);
// There is 1 transform task after start.
assertThat(getTransformTasks(), hasSize(1));
assertThat(getTransformTasksFromClusterState(transformId), hasSize(1));
assertThat(getTotalRegisteredTransformCount(), is(equalTo(1)));
assertThat("Node stats were: " + entityAsMap(getNodeStats()), getTotalRegisteredTransformCount(), is(equalTo(1)));

Thread.sleep(sleepAfterStartMillis);
// There should still be 1 transform task as the transform is continuous.
assertThat(getTransformTasks(), hasSize(1));
assertThat(getTransformTasksFromClusterState(transformId), hasSize(1));
assertThat(getTotalRegisteredTransformCount(), is(equalTo(1)));
assertThat("Node stats were: " + entityAsMap(getNodeStats()), getTotalRegisteredTransformCount(), is(equalTo(1)));

// Stop the transform with force set randomly.
stopTransform(transformId, true, null, false, force);
Expand All @@ -271,7 +271,7 @@ public void testTransformLifecycleInALoop() throws Exception {
}
// After the transform is stopped, there should be no transform task left in the cluster state.
assertThat(getTransformTasksFromClusterState(transformId), is(empty()));
assertThat(getTotalRegisteredTransformCount(), is(equalTo(0)));
assertThat("Node stats were: " + entityAsMap(getNodeStats()), getTotalRegisteredTransformCount(), is(equalTo(0)));

// Delete the transform
deleteTransform(transformId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

package org.elasticsearch.xpack.transform.integration;

import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
Expand Down Expand Up @@ -53,12 +52,20 @@ public void testTransformNodeStats() throws Exception {
var nodesInfo = getNodesInfo(adminClient());
assertThat("Nodes were: " + nodesInfo, nodesInfo.size(), is(equalTo(3)));

var response = entityAsMap(adminClient().performRequest(new Request("GET", "/_transform/_node_stats")));
var response = entityAsMap(getNodeStats());
assertThat(response, hasKey("total"));
assertThat(XContentMapValues.extractValue(response, "total", "registered_transform_count"), is(equalTo(0)));
assertThat(
"Response was: " + response,
XContentMapValues.extractValue(response, "total", "registered_transform_count"),
is(equalTo(0))
);
for (String nodeId : nodesInfo.keySet()) {
assertThat(response, hasKey(nodeId));
assertThat(XContentMapValues.extractValue(response, nodeId, "registered_transform_count"), is(equalTo(0)));
assertThat(
"Response was: " + response,
XContentMapValues.extractValue(response, nodeId, "registered_transform_count"),
is(equalTo(0))
);
}
}

Expand Down

0 comments on commit dcbc83f

Please sign in to comment.