From d2e751136c577291eeb08c64dc24accd9d45113e Mon Sep 17 00:00:00 2001 From: Mark Vieira Date: Wed, 22 Oct 2025 08:16:31 -0700 Subject: [PATCH] Adjust test fixture lifecycle ordering in apm integration tests (#136926) --- .../test/apmintegration/TracesApmIT.java | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/test/external-modules/apm-integration/src/javaRestTest/java/org/elasticsearch/test/apmintegration/TracesApmIT.java b/test/external-modules/apm-integration/src/javaRestTest/java/org/elasticsearch/test/apmintegration/TracesApmIT.java index afb9243e0f3ef..d401fb1166149 100644 --- a/test/external-modules/apm-integration/src/javaRestTest/java/org/elasticsearch/test/apmintegration/TracesApmIT.java +++ b/test/external-modules/apm-integration/src/javaRestTest/java/org/elasticsearch/test/apmintegration/TracesApmIT.java @@ -22,7 +22,8 @@ import org.hamcrest.Matcher; import org.hamcrest.StringDescription; import org.junit.ClassRule; -import org.junit.Rule; +import org.junit.rules.TestRule; +import org.junit.runners.model.Statement; import java.io.IOException; import java.util.Arrays; @@ -43,20 +44,45 @@ public class TracesApmIT extends ESRestTestCase { final String traceIdValue = "0af7651916cd43dd8448eb211c80319c"; final String traceParentValue = "00-" + traceIdValue + "-b7ad6b7169203331-01"; - @ClassRule public static RecordingApmServer mockApmServer = new RecordingApmServer(); - @Rule - public ElasticsearchCluster cluster = ElasticsearchCluster.local() + public static ElasticsearchCluster cluster = ElasticsearchCluster.local() .distribution(DistributionType.INTEG_TEST) .module("test-apm-integration") .module("apm") .setting("telemetry.metrics.enabled", "false") .setting("telemetry.tracing.enabled", "true") .setting("telemetry.agent.metrics_interval", "1s") - .setting("telemetry.agent.server_url", "http://127.0.0.1:" + mockApmServer.getPort()) + .setting("telemetry.agent.server_url", () -> "http://127.0.0.1:" + mockApmServer.getPort()) .build(); + @ClassRule + // Custom test rule which manually orders test fixtures. A RuleChain won't work here due to the specific order we require, which is: + // 1. Start the mock APM server + // 2. Start ES cluster + // 3. Run the test + // 4. Stop the mock APM server + // 5. Stop ES cluster + public static TestRule rule = (base, description) -> { + return new Statement() { + @Override + public void evaluate() throws Throwable { + mockApmServer.before(); + Statement s = new Statement() { + @Override + public void evaluate() throws Throwable { + try { + base.evaluate(); + } finally { + mockApmServer.after(); + } + } + }; + cluster.apply(s, description).evaluate(); + } + }; + }; + @Override protected String getTestRestCluster() { return cluster.getHttpAddresses();