Skip to content

Commit fe5d29c

Browse files
committed
Fix tests to match the new stream error handling
1 parent 9c31d35 commit fe5d29c

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/services/deepseek/DeepSeekServiceTests.java

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,7 @@ public void testDoUnifiedInfer() throws Exception {
240240
data: [DONE]
241241
242242
"""));
243-
var result = doUnifiedCompletionInfer();
244-
InferenceEventsAssertion.assertThat(result).hasFinishedStream().hasNoErrors().hasEvent("""
243+
doUnifiedCompletionInfer().hasNoErrors().hasEvent("""
245244
{"id":"12345","choices":[{"delta":{"content":"hello, world","role":"assistant"},"index":0}],""" + """
246245
"model":"deepseek-chat","object":"chat.completion.chunk"}""");
247246
}
@@ -251,13 +250,18 @@ public void testDoInfer() throws Exception {
251250
{"choices": [{"message": {"content": "hello, world", "role": "assistant"}, "finish_reason": "stop", "index": 0, \
252251
"logprobs": null}], "created": 1718345013, "id": "12345", "model": "deepseek-chat", \
253252
"object": "chat.completion", "system_fingerprint": "fp_1234"}"""));
254-
var result = doInfer(false);
255-
assertThat(result, isA(ChatCompletionResults.class));
256-
var completionResults = (ChatCompletionResults) result;
257-
assertThat(
258-
completionResults.results().stream().map(ChatCompletionResults.Result::predictedValue).toList(),
259-
equalTo(List.of("hello, world"))
260-
);
253+
try (var service = createService()) {
254+
var model = createModel(service, TaskType.COMPLETION);
255+
PlainActionFuture<InferenceServiceResults> listener = new PlainActionFuture<>();
256+
service.infer(model, null, List.of("hello"), false, Map.of(), InputType.UNSPECIFIED, TIMEOUT, listener);
257+
var result = listener.actionGet(TIMEOUT);
258+
assertThat(result, isA(ChatCompletionResults.class));
259+
var completionResults = (ChatCompletionResults) result;
260+
assertThat(
261+
completionResults.results().stream().map(ChatCompletionResults.Result::predictedValue).toList(),
262+
equalTo(List.of("hello, world"))
263+
);
264+
}
261265
}
262266

263267
public void testDoInferStream() throws Exception {
@@ -269,12 +273,16 @@ public void testDoInferStream() throws Exception {
269273
data: [DONE]
270274
271275
"""));
272-
var result = doInfer(true);
273-
InferenceEventsAssertion.assertThat(result).hasFinishedStream().hasNoErrors().hasEvent("""
274-
{"completion":[{"delta":"hello, world"}]}""");
276+
try (var service = createService()) {
277+
var model = createModel(service, TaskType.COMPLETION);
278+
PlainActionFuture<InferenceServiceResults> listener = new PlainActionFuture<>();
279+
service.infer(model, null, List.of("hello"), true, Map.of(), InputType.UNSPECIFIED, TIMEOUT, listener);
280+
InferenceEventsAssertion.assertThat(listener.actionGet(TIMEOUT)).hasFinishedStream().hasNoErrors().hasEvent("""
281+
{"completion":[{"delta":"hello, world"}]}""");
282+
}
275283
}
276284

277-
public void testUnifiedCompletionError() throws Exception {
285+
public void testUnifiedCompletionError() {
278286
String responseJson = """
279287
{
280288
"error": {
@@ -285,14 +293,14 @@ public void testUnifiedCompletionError() throws Exception {
285293
}
286294
}""";
287295
webServer.enqueue(new MockResponse().setResponseCode(404).setBody(responseJson));
288-
testStreamError("""
289-
{\
290-
"error":{\
291-
"code":"model_not_found",\
292-
"message":"Received an unsuccessful status code for request from inference entity id [inference-id] status \
293-
[404]. Error message: [The model `deepseek-not-chat` does not exist or you do not have access to it.]",\
294-
"type":"invalid_request_error"\
295-
}}""");
296+
var e = assertThrows(UnifiedChatCompletionException.class, this::doUnifiedCompletionInfer);
297+
assertThat(
298+
e.getMessage(),
299+
equalTo(
300+
"Received an unsuccessful status code for request from inference entity id [inference-id] status"
301+
+ " [404]. Error message: [The model `deepseek-not-chat` does not exist or you do not have access to it.]"
302+
)
303+
);
296304
}
297305

298306
private void testStreamError(String expectedResponse) throws Exception {
@@ -399,7 +407,7 @@ private DeepSeekChatCompletionModel parsePersistedConfig(String json) throws IOE
399407
}
400408
}
401409

402-
private InferenceServiceResults doUnifiedCompletionInfer() throws Exception {
410+
private InferenceEventsAssertion doUnifiedCompletionInfer() throws Exception {
403411
try (var service = createService()) {
404412
var model = createModel(service, TaskType.CHAT_COMPLETION);
405413
PlainActionFuture<InferenceServiceResults> listener = new PlainActionFuture<>();
@@ -411,16 +419,7 @@ private InferenceServiceResults doUnifiedCompletionInfer() throws Exception {
411419
TIMEOUT,
412420
listener
413421
);
414-
return listener.get(30, TimeUnit.SECONDS);
415-
}
416-
}
417-
418-
private InferenceServiceResults doInfer(boolean stream) throws Exception {
419-
try (var service = createService()) {
420-
var model = createModel(service, TaskType.COMPLETION);
421-
PlainActionFuture<InferenceServiceResults> listener = new PlainActionFuture<>();
422-
service.infer(model, null, List.of("hello"), stream, Map.of(), InputType.UNSPECIFIED, TIMEOUT, listener);
423-
return listener.get(30, TimeUnit.SECONDS);
422+
return InferenceEventsAssertion.assertThat(listener.actionGet(TIMEOUT)).hasFinishedStream();
424423
}
425424
}
426425

0 commit comments

Comments
 (0)