Skip to content

Commit d2e7511

Browse files
committed
Adjust test fixture lifecycle ordering in apm integration tests (elastic#136926)
1 parent e6cd8b8 commit d2e7511

File tree

1 file changed

+31
-5
lines changed
  • test/external-modules/apm-integration/src/javaRestTest/java/org/elasticsearch/test/apmintegration

1 file changed

+31
-5
lines changed

test/external-modules/apm-integration/src/javaRestTest/java/org/elasticsearch/test/apmintegration/TracesApmIT.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
import org.hamcrest.Matcher;
2323
import org.hamcrest.StringDescription;
2424
import org.junit.ClassRule;
25-
import org.junit.Rule;
25+
import org.junit.rules.TestRule;
26+
import org.junit.runners.model.Statement;
2627

2728
import java.io.IOException;
2829
import java.util.Arrays;
@@ -43,20 +44,45 @@ public class TracesApmIT extends ESRestTestCase {
4344
final String traceIdValue = "0af7651916cd43dd8448eb211c80319c";
4445
final String traceParentValue = "00-" + traceIdValue + "-b7ad6b7169203331-01";
4546

46-
@ClassRule
4747
public static RecordingApmServer mockApmServer = new RecordingApmServer();
4848

49-
@Rule
50-
public ElasticsearchCluster cluster = ElasticsearchCluster.local()
49+
public static ElasticsearchCluster cluster = ElasticsearchCluster.local()
5150
.distribution(DistributionType.INTEG_TEST)
5251
.module("test-apm-integration")
5352
.module("apm")
5453
.setting("telemetry.metrics.enabled", "false")
5554
.setting("telemetry.tracing.enabled", "true")
5655
.setting("telemetry.agent.metrics_interval", "1s")
57-
.setting("telemetry.agent.server_url", "http://127.0.0.1:" + mockApmServer.getPort())
56+
.setting("telemetry.agent.server_url", () -> "http://127.0.0.1:" + mockApmServer.getPort())
5857
.build();
5958

59+
@ClassRule
60+
// Custom test rule which manually orders test fixtures. A RuleChain won't work here due to the specific order we require, which is:
61+
// 1. Start the mock APM server
62+
// 2. Start ES cluster
63+
// 3. Run the test
64+
// 4. Stop the mock APM server
65+
// 5. Stop ES cluster
66+
public static TestRule rule = (base, description) -> {
67+
return new Statement() {
68+
@Override
69+
public void evaluate() throws Throwable {
70+
mockApmServer.before();
71+
Statement s = new Statement() {
72+
@Override
73+
public void evaluate() throws Throwable {
74+
try {
75+
base.evaluate();
76+
} finally {
77+
mockApmServer.after();
78+
}
79+
}
80+
};
81+
cluster.apply(s, description).evaluate();
82+
}
83+
};
84+
};
85+
6086
@Override
6187
protected String getTestRestCluster() {
6288
return cluster.getHttpAddresses();

0 commit comments

Comments
 (0)