Skip to content

Commit f910b92

Browse files
[ML] Fix streaming test regex (#115589) (#116102)
Replace regex with string parsing. Fix #114788 Co-authored-by: Elastic Machine <[email protected]>
1 parent 7b39d3d commit f910b92

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

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)