|
7 | 7 |
|
8 | 8 | package org.elasticsearch.xpack.inference.services.elastic;
|
9 | 9 |
|
| 10 | +import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; |
| 11 | + |
10 | 12 | import org.apache.http.Header;
|
11 | 13 | import org.apache.http.HeaderElement;
|
12 | 14 | import org.apache.http.HttpResponse;
|
|
17 | 19 | import org.elasticsearch.rest.RestStatus;
|
18 | 20 | import org.elasticsearch.test.ESTestCase;
|
19 | 21 | import org.elasticsearch.xpack.inference.external.http.HttpResult;
|
20 |
| -import org.elasticsearch.xpack.inference.external.http.retry.ContentTooLargeException; |
21 | 22 | import org.elasticsearch.xpack.inference.external.http.retry.RetryException;
|
22 | 23 | import org.elasticsearch.xpack.inference.external.request.Request;
|
23 | 24 | import org.hamcrest.MatcherAssert;
|
|
32 | 33 |
|
33 | 34 | public class ElasticInferenceServiceResponseHandlerTests extends ESTestCase {
|
34 | 35 |
|
35 |
| - public void testCheckForFailureStatusCode_DoesNotThrowFor200() { |
36 |
| - callCheckForFailureStatusCode(200, "id"); |
37 |
| - } |
38 |
| - |
39 |
| - public void testCheckForFailureStatusCode_ThrowsFor400() { |
40 |
| - var exception = expectThrows(RetryException.class, () -> callCheckForFailureStatusCode(400, "id")); |
41 |
| - assertFalse(exception.shouldRetry()); |
42 |
| - MatcherAssert.assertThat( |
43 |
| - exception.getCause().getMessage(), |
44 |
| - containsString("Received a bad request status code for request from inference entity id [id] status [400]") |
45 |
| - ); |
46 |
| - MatcherAssert.assertThat(((ElasticsearchStatusException) exception.getCause()).status(), is(RestStatus.BAD_REQUEST)); |
47 |
| - } |
| 36 | + public record FailureTestCase(int inputStatusCode, RestStatus expectedStatus, String errorMessage, boolean shouldRetry) {} |
48 | 37 |
|
49 |
| - public void testCheckForFailureStatusCode_ThrowsFor405() { |
50 |
| - var exception = expectThrows(RetryException.class, () -> callCheckForFailureStatusCode(405, "id")); |
51 |
| - assertFalse(exception.shouldRetry()); |
52 |
| - MatcherAssert.assertThat( |
53 |
| - exception.getCause().getMessage(), |
54 |
| - containsString("Received a method not allowed status code for request from inference entity id [id] status [405]") |
55 |
| - ); |
56 |
| - MatcherAssert.assertThat(((ElasticsearchStatusException) exception.getCause()).status(), is(RestStatus.METHOD_NOT_ALLOWED)); |
57 |
| - } |
| 38 | + private final FailureTestCase failureTestCase; |
58 | 39 |
|
59 |
| - public void testCheckForFailureStatusCode_ThrowsFor413() { |
60 |
| - var exception = expectThrows(ContentTooLargeException.class, () -> callCheckForFailureStatusCode(413, "id")); |
61 |
| - assertTrue(exception.shouldRetry()); |
62 |
| - MatcherAssert.assertThat( |
63 |
| - exception.getCause().getMessage(), |
64 |
| - containsString("Received a content too large status code for request from inference entity id [id] status [413]") |
65 |
| - ); |
66 |
| - MatcherAssert.assertThat(((ElasticsearchStatusException) exception.getCause()).status(), is(RestStatus.REQUEST_ENTITY_TOO_LARGE)); |
| 40 | + public ElasticInferenceServiceResponseHandlerTests(FailureTestCase failureTestCase) { |
| 41 | + this.failureTestCase = failureTestCase; |
67 | 42 | }
|
68 | 43 |
|
69 |
| - public void testCheckForFailureStatusCode_ThrowsFor500_WithShouldRetryTrue() { |
70 |
| - var exception = expectThrows(RetryException.class, () -> callCheckForFailureStatusCode(500, "id")); |
71 |
| - assertTrue(exception.shouldRetry()); |
72 |
| - MatcherAssert.assertThat( |
73 |
| - exception.getCause().getMessage(), |
74 |
| - containsString("Received a server error status code for request from inference entity id [id] status [500]") |
| 44 | + @ParametersFactory |
| 45 | + public static Iterable<FailureTestCase[]> parameters() throws Exception { |
| 46 | + return java.util.Arrays.asList( |
| 47 | + new FailureTestCase[][] { |
| 48 | + { |
| 49 | + new FailureTestCase( |
| 50 | + 400, |
| 51 | + RestStatus.BAD_REQUEST, |
| 52 | + "Received a bad request status code for request from inference entity id [id] status [400]", |
| 53 | + false |
| 54 | + ) }, |
| 55 | + { |
| 56 | + new FailureTestCase( |
| 57 | + 402, |
| 58 | + RestStatus.PAYMENT_REQUIRED, |
| 59 | + "Received an unsuccessful status code for request from inference entity id [id] status [402]", |
| 60 | + false |
| 61 | + ) }, |
| 62 | + { |
| 63 | + new FailureTestCase( |
| 64 | + 405, |
| 65 | + RestStatus.METHOD_NOT_ALLOWED, |
| 66 | + "Received a method not allowed status code for request from inference entity id [id] status [405]", |
| 67 | + false |
| 68 | + ) }, |
| 69 | + { |
| 70 | + new FailureTestCase( |
| 71 | + 413, |
| 72 | + RestStatus.REQUEST_ENTITY_TOO_LARGE, |
| 73 | + "Received a content too large status code for request from inference entity id [id] status [413]", |
| 74 | + true |
| 75 | + ) }, |
| 76 | + { |
| 77 | + new FailureTestCase( |
| 78 | + 500, |
| 79 | + RestStatus.BAD_REQUEST, |
| 80 | + "Received a server error status code for request from inference entity id [id] status [500]", |
| 81 | + true |
| 82 | + ) }, |
| 83 | + { |
| 84 | + new FailureTestCase( |
| 85 | + 503, |
| 86 | + RestStatus.BAD_REQUEST, |
| 87 | + "Received a server error status code for request from inference entity id [id] status [503]", |
| 88 | + true |
| 89 | + ) }, |
| 90 | + |
| 91 | + } |
75 | 92 | );
|
76 |
| - MatcherAssert.assertThat(((ElasticsearchStatusException) exception.getCause()).status(), is(RestStatus.BAD_REQUEST)); |
77 | 93 | }
|
78 | 94 |
|
79 |
| - public void testCheckForFailureStatusCode_ThrowsFor402() { |
80 |
| - var exception = expectThrows(RetryException.class, () -> callCheckForFailureStatusCode(402, "id")); |
81 |
| - assertFalse(exception.shouldRetry()); |
82 |
| - MatcherAssert.assertThat( |
83 |
| - exception.getCause().getMessage(), |
84 |
| - containsString("Received an unsuccessful status code for request from inference entity id [id] status [402]") |
| 95 | + public void testCheckForFailureStatusCode_Throws_WithErrorMessage() { |
| 96 | + var exception = expectThrows( |
| 97 | + RetryException.class, |
| 98 | + () -> callCheckForFailureStatusCode(failureTestCase.inputStatusCode, failureTestCase.errorMessage, "id") |
85 | 99 | );
|
86 |
| - MatcherAssert.assertThat(((ElasticsearchStatusException) exception.getCause()).status(), is(RestStatus.PAYMENT_REQUIRED)); |
| 100 | + assertThat(exception.shouldRetry(), is(failureTestCase.shouldRetry)); |
| 101 | + MatcherAssert.assertThat(exception.getCause().getMessage(), containsString(failureTestCase.errorMessage)); |
| 102 | + MatcherAssert.assertThat(((ElasticsearchStatusException) exception.getCause()).status(), is(failureTestCase.expectedStatus)); |
87 | 103 | }
|
88 | 104 |
|
89 |
| - private static void callCheckForFailureStatusCode(int statusCode, String modelId) { |
90 |
| - callCheckForFailureStatusCode(statusCode, null, modelId); |
| 105 | + public void testCheckForFailureStatusCode_DoesNotThrowFor200() { |
| 106 | + callCheckForFailureStatusCode(200, null, "id"); |
91 | 107 | }
|
92 | 108 |
|
93 | 109 | private static void callCheckForFailureStatusCode(int statusCode, @Nullable String errorMessage, String modelId) {
|
|
0 commit comments