Skip to content

Commit a6cd265

Browse files
authored
[ML] Fix streaming test regex (#115589)
Replace regex with string parsing. Fix #114788
1 parent 0aa25a9 commit a6cd265

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

muted-tests.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,6 @@ tests:
197197
- class: org.elasticsearch.xpack.security.operator.OperatorPrivilegesIT
198198
method: testEveryActionIsEitherOperatorOnlyOrNonOperator
199199
issue: https://github.com/elastic/elasticsearch/issues/102992
200-
- class: org.elasticsearch.xpack.inference.rest.ServerSentEventsRestActionListenerTests
201-
method: testNoStream
202-
issue: https://github.com/elastic/elasticsearch/issues/114788
203200
- class: org.elasticsearch.xpack.remotecluster.RemoteClusterSecurityWithApmTracingRestIT
204201
method: testTracingCrossCluster
205202
issue: https://github.com/elastic/elasticsearch/issues/112731

x-pack/plugin/inference/src/internalClusterTest/java/org/elasticsearch/xpack/inference/rest/ServerSentEventsRestActionListenerTests.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
import java.util.concurrent.atomic.AtomicReference;
6565
import java.util.function.Predicate;
6666
import java.util.function.Supplier;
67-
import java.util.regex.Pattern;
6867

6968
import static org.hamcrest.Matchers.equalTo;
7069
import static org.hamcrest.Matchers.is;
@@ -414,17 +413,19 @@ public void testErrorMidStream() {
414413
assertThat(collector.stringsVerified.getLast(), equalTo(expectedExceptionAsServerSentEvent));
415414
}
416415

417-
public void testNoStream() throws IOException {
418-
var pattern = Pattern.compile("^\uFEFFevent: message\ndata: \\{\"result\":\".*\"}\n\n\uFEFFevent: message\ndata: \\[DONE]\n\n$");
416+
public void testNoStream() {
417+
var collector = new RandomStringCollector();
418+
var expectedTestCount = randomIntBetween(2, 30);
419419
var request = new Request(RestRequest.Method.POST.name(), NO_STREAM_ROUTE);
420-
var response = getRestClient().performRequest(request);
421-
assertThat(response.getStatusLine().getStatusCode(), is(HttpStatus.SC_OK));
422-
var responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
423-
424-
assertThat(
425-
"Expected " + responseString + " to match pattern " + pattern.pattern(),
426-
pattern.matcher(responseString).matches(),
427-
is(true)
420+
request.setOptions(
421+
RequestOptions.DEFAULT.toBuilder()
422+
.setHttpAsyncResponseConsumerFactory(() -> new AsyncResponseConsumer(collector))
423+
.addParameter(REQUEST_COUNT, String.valueOf(expectedTestCount))
424+
.build()
428425
);
426+
var response = callAsync(request);
427+
assertThat(response.getStatusLine().getStatusCode(), is(HttpStatus.SC_OK));
428+
assertThat(collector.stringsVerified.size(), equalTo(2)); // single payload count + done byte
429+
assertThat(collector.stringsVerified.peekLast(), equalTo("[DONE]"));
429430
}
430431
}

0 commit comments

Comments
 (0)