Skip to content

Commit b3ead2a

Browse files
committed
Revert stop, more lenient error-handling in handle, comment to explain why
1 parent 519364b commit b3ead2a

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ public void testApmIntegration() throws Exception {
129129
var remainingAssertions = Stream.concat(valueAssertions.keySet().stream(), histogramAssertions.keySet().stream())
130130
.collect(Collectors.joining(","));
131131
assertTrue("Timeout when waiting for assertions to complete. Remaining assertions to match: " + remainingAssertions, completed);
132-
mockApmServer.stop();
133132
}
134133

135134
private <T> Map.Entry<String, Predicate<Map<String, Object>>> assertion(

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.apache.logging.log4j.LogManager;
1616
import org.apache.logging.log4j.Logger;
1717
import org.elasticsearch.core.SuppressForbidden;
18-
import org.elasticsearch.xcontent.spi.XContentProvider;
1918
import org.junit.rules.ExternalResource;
2019

2120
import java.io.BufferedReader;
@@ -34,8 +33,6 @@
3433
public class RecordingApmServer extends ExternalResource {
3534
private static final Logger logger = LogManager.getLogger(RecordingApmServer.class);
3635

37-
private static final XContentProvider.FormatProvider XCONTENT = XContentProvider.provider().getJsonXContent();
38-
3936
final ArrayBlockingQueue<String> received = new ArrayBlockingQueue<>(1000);
4037

4138
private static HttpServer server;
@@ -73,14 +70,11 @@ private Thread consumerThread() {
7370

7471
@Override
7572
protected void after() {
73+
running = false;
7674
server.stop(1);
7775
consumer = null;
7876
}
7977

80-
void stop() {
81-
running = false;
82-
}
83-
8478
private void handle(HttpExchange exchange) throws IOException {
8579
try (exchange) {
8680
if (running) {
@@ -92,15 +86,22 @@ private void handle(HttpExchange exchange) throws IOException {
9286
}
9387
}
9488

95-
} catch (RuntimeException e) {
96-
logger.warn("failed to parse request", e);
89+
} catch (Throwable t) {
90+
// The lifetime of HttpServer makes message handling "brittle": we need to start handling and recording received
91+
// messages before the test starts running. We should also stop handling them before the test ends (and the test
92+
// cluster is torn down), or we may run into IOException as the communication channel is interrupted.
93+
// Coordinating the lifecycle of the mock HttpServer and of the test ES cluster is difficult and error-prone, so
94+
// we just handle Throwable and don't care (log, but don't care): if we have an error in communicating to/from
95+
// the mock server while the test is running, the test would fail anyway as the expected messages will not arrive, and
96+
// if we have an error outside the test scope (before or after) that is OK.
97+
logger.warn("failed to parse request", t);
9798
}
9899
}
99100
exchange.sendResponseHeaders(201, 0);
100101
}
101102
}
102103

103-
private List<String> readJsonMessages(InputStream input) throws IOException {
104+
private List<String> readJsonMessages(InputStream input) {
104105
// parse NDJSON
105106
return new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)).lines().toList();
106107
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ public void testApmIntegration() throws Exception {
9494
var completed = finished.await(30, TimeUnit.SECONDS);
9595
assertTrue("Timeout when waiting for assertions to complete", completed);
9696
assertThat(assertions, equalTo(Collections.emptySet()));
97-
98-
mockApmServer.stop();
9997
}
10098

10199
private boolean isTransactionTraceMessage(Map<String, Object> apmMessage) {

0 commit comments

Comments
 (0)