|
3 | 3 | */ |
4 | 4 | package com.marklogic.client.impl; |
5 | 5 |
|
| 6 | +import com.fasterxml.jackson.databind.ObjectMapper; |
6 | 7 | import com.marklogic.client.impl.okhttp.RetryIOExceptionInterceptor; |
7 | | -import okhttp3.MediaType; |
| 8 | +import com.marklogic.client.io.BytesHandle; |
| 9 | +import com.marklogic.client.io.FileHandle; |
| 10 | +import com.marklogic.client.io.InputStreamHandle; |
| 11 | +import com.marklogic.client.io.JacksonHandle; |
| 12 | +import com.marklogic.client.io.ReaderHandle; |
| 13 | +import com.marklogic.client.io.StringHandle; |
| 14 | +import com.marklogic.client.io.marker.AbstractWriteHandle; |
8 | 15 | import okhttp3.MultipartBody; |
9 | 16 | import okhttp3.OkHttpClient; |
| 17 | +import okhttp3.Protocol; |
10 | 18 | import okhttp3.Request; |
| 19 | +import okhttp3.RequestBody; |
| 20 | +import okhttp3.Response; |
| 21 | +import okhttp3.ResponseBody; |
11 | 22 | import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.params.ParameterizedTest; |
| 24 | +import org.junit.jupiter.params.provider.MethodSource; |
12 | 25 |
|
13 | 26 | import java.io.ByteArrayInputStream; |
| 27 | +import java.io.File; |
14 | 28 | import java.io.IOException; |
15 | | -import java.io.InputStream; |
| 29 | +import java.io.StringReader; |
16 | 30 | import java.nio.charset.StandardCharsets; |
17 | | -import java.util.concurrent.atomic.AtomicInteger; |
| 31 | +import java.util.stream.Stream; |
18 | 32 |
|
19 | 33 | import static org.junit.jupiter.api.Assertions.assertEquals; |
20 | 34 | import static org.junit.jupiter.api.Assertions.assertFalse; |
21 | 35 | import static org.junit.jupiter.api.Assertions.assertThrows; |
22 | 36 | import static org.junit.jupiter.api.Assertions.assertTrue; |
23 | 37 |
|
| 38 | +/** |
| 39 | + * Verifies that the request body built for a handle reports {@code isOneShot()} according to whether the handle's |
| 40 | + * content can be written more than once, and that OkHttp-based retry honours that. |
| 41 | + */ |
24 | 42 | class StreamingOutputImplTest { |
25 | 43 |
|
26 | | - @Test |
27 | | - void multipartBodyWithStreamingPartIsNotRetried() { |
28 | | - StreamingOutputImpl streamingBody = new StreamingOutputImpl( |
29 | | - outputStream -> outputStream.write("content".getBytes(StandardCharsets.UTF_8)), |
30 | | - null, |
31 | | - MediaType.get("application/xml") |
| 44 | + static Stream<Object[]> handlesAndOneShotFlags() { |
| 45 | + return Stream.of( |
| 46 | + new Object[]{"StringHandle", new StringHandle("<a/>"), false}, |
| 47 | + new Object[]{"JacksonHandle", new JacksonHandle(new ObjectMapper().createObjectNode()), false}, |
| 48 | + new Object[]{"BytesHandle", new BytesHandle("<a/>".getBytes(StandardCharsets.UTF_8)), false}, |
| 49 | + new Object[]{"FileHandle", new FileHandle(new File("pom.xml")), false}, |
| 50 | + new Object[]{"ReaderHandle", new ReaderHandle(new StringReader("<a/>")), true}, |
| 51 | + new Object[]{"InputStreamHandle", |
| 52 | + new InputStreamHandle(new ByteArrayInputStream("<a/>".getBytes(StandardCharsets.UTF_8))), true} |
32 | 53 | ); |
33 | | - MultipartBody multipartBody = new MultipartBody.Builder() |
34 | | - .setType(MultipartBody.FORM) |
35 | | - .addFormDataPart("body", null, streamingBody) |
36 | | - .build(); |
37 | | - assertTrue(multipartBody.isOneShot()); |
| 54 | + } |
38 | 55 |
|
39 | | - AtomicInteger attempts = new AtomicInteger(); |
40 | | - OkHttpClient client = new OkHttpClient.Builder() |
41 | | - .addInterceptor(new RetryIOExceptionInterceptor(3, 0, 1, 0)) |
42 | | - .addInterceptor(chain -> { |
43 | | - attempts.incrementAndGet(); |
44 | | - throw new IOException("unexpected end of stream"); |
45 | | - }) |
46 | | - .build(); |
47 | | - Request request = new Request.Builder() |
48 | | - .url("http://localhost/") |
49 | | - .post(multipartBody) |
50 | | - .build(); |
| 56 | + @ParameterizedTest(name = "{0}") |
| 57 | + @MethodSource("handlesAndOneShotFlags") |
| 58 | + void oneShotMatchesWhetherTheHandleCanBeResent(String name, AbstractWriteHandle handle, boolean expectedOneShot) { |
| 59 | + assertEquals(expectedOneShot, OkHttpServices.makeRequestBodyForContent(handle).isOneShot()); |
| 60 | + } |
51 | 61 |
|
52 | | - assertThrows(IOException.class, () -> client.newCall(request).execute()); |
53 | | - assertEquals(1, attempts.get()); |
| 62 | + @Test |
| 63 | + void multipartWithAnyOneShotPartIsOneShot() { |
| 64 | + MultipartBody body = multipart( |
| 65 | + OkHttpServices.makeRequestBodyForContent(new StringHandle("<a/>")), |
| 66 | + OkHttpServices.makeRequestBodyForContent(new ReaderHandle(new StringReader("<b/>"))) |
| 67 | + ); |
| 68 | + assertTrue(body.isOneShot()); |
54 | 69 | } |
55 | 70 |
|
56 | 71 | @Test |
57 | | - void multipartBodyWithInputStreamPartIsNotRetried() { |
58 | | - InputStream inputStream = new ByteArrayInputStream("content".getBytes(StandardCharsets.UTF_8)); |
59 | | - OkHttpServices.ObjectRequestBody inputStreamBody = |
60 | | - new OkHttpServices.ObjectRequestBody(inputStream, MediaType.get("application/xml")); |
61 | | - MultipartBody multipartBody = new MultipartBody.Builder() |
62 | | - .setType(MultipartBody.FORM) |
63 | | - .addFormDataPart("body", null, inputStreamBody) |
64 | | - .build(); |
65 | | - assertTrue(multipartBody.isOneShot(), |
66 | | - "A multipart body containing an InputStream-backed part must be one-shot so the retry interceptor does " + |
67 | | - "not replay an already-consumed stream."); |
| 72 | + void multipartWithOnlyResendablePartsIsNotOneShot() { |
| 73 | + MultipartBody body = multipart( |
| 74 | + OkHttpServices.makeRequestBodyForContent(new StringHandle("<a/>")), |
| 75 | + OkHttpServices.makeRequestBodyForContent(new JacksonHandle(new ObjectMapper().createObjectNode())) |
| 76 | + ); |
| 77 | + assertFalse(body.isOneShot()); |
| 78 | + } |
68 | 79 |
|
69 | | - AtomicInteger attempts = new AtomicInteger(); |
70 | | - OkHttpClient client = new OkHttpClient.Builder() |
71 | | - .addInterceptor(new RetryIOExceptionInterceptor(3, 0, 1, 0)) |
72 | | - .addInterceptor(chain -> { |
73 | | - attempts.incrementAndGet(); |
74 | | - throw new IOException("unexpected end of stream"); |
75 | | - }) |
76 | | - .build(); |
77 | | - Request request = new Request.Builder() |
78 | | - .url("http://localhost/") |
79 | | - .post(multipartBody) |
80 | | - .build(); |
| 80 | + @Test |
| 81 | + void oneShotMultipartIsNotRetried() { |
| 82 | + MultipartBody body = multipart( |
| 83 | + OkHttpServices.makeRequestBodyForContent(new ReaderHandle(new StringReader("<a/>"))) |
| 84 | + ); |
| 85 | + CountingFailure failure = new CountingFailure(Integer.MAX_VALUE); |
81 | 86 |
|
82 | | - assertThrows(IOException.class, () -> client.newCall(request).execute()); |
83 | | - assertEquals(1, attempts.get()); |
| 87 | + assertThrows(IOException.class, () -> client(failure).newCall(post(body)).execute()); |
| 88 | + assertEquals(1, failure.attempts); |
84 | 89 | } |
85 | 90 |
|
86 | 91 | @Test |
87 | | - void resendableStreamingBodyIsRetried() { |
88 | | - StreamingOutputImpl resendableBody = new StreamingOutputImpl( |
89 | | - outputStream -> outputStream.write("content".getBytes(StandardCharsets.UTF_8)), |
90 | | - null, |
91 | | - MediaType.get("application/xml"), |
92 | | - true |
| 92 | + void resendableMultipartIsRetried() throws IOException { |
| 93 | + MultipartBody body = multipart( |
| 94 | + OkHttpServices.makeRequestBodyForContent(new StringHandle("<a/>")) |
93 | 95 | ); |
94 | | - assertTrue(resendableBody.isRetryable(), |
95 | | - "A handle that reports isResendable() == true should allow its StreamingOutputImpl wrapper to be retried."); |
96 | | - assertFalse(resendableBody.isOneShot(), |
97 | | - "A resendable StreamingOutputImpl should not be marked one-shot, so OkHttp permits rewriting its body."); |
| 96 | + CountingFailure failure = new CountingFailure(1); |
98 | 97 |
|
99 | | - AtomicInteger attempts = new AtomicInteger(); |
100 | | - OkHttpClient client = new OkHttpClient.Builder() |
101 | | - .addInterceptor(new RetryIOExceptionInterceptor(3, 0, 1, 0)) |
102 | | - .addInterceptor(chain -> { |
103 | | - if (attempts.incrementAndGet() < 2) { |
104 | | - throw new IOException("unexpected end of stream"); |
105 | | - } |
106 | | - return new okhttp3.Response.Builder() |
107 | | - .request(chain.request()) |
108 | | - .protocol(okhttp3.Protocol.HTTP_1_1) |
109 | | - .code(200) |
110 | | - .message("OK") |
111 | | - .body(okhttp3.ResponseBody.create("", null)) |
112 | | - .build(); |
113 | | - }) |
114 | | - .build(); |
115 | | - Request request = new Request.Builder() |
116 | | - .url("http://localhost/") |
117 | | - .post(resendableBody) |
118 | | - .build(); |
119 | | - |
120 | | - try (okhttp3.Response response = client.newCall(request).execute()) { |
| 98 | + try (Response response = client(failure).newCall(post(body)).execute()) { |
121 | 99 | assertEquals(200, response.code()); |
122 | | - } catch (IOException e) { |
123 | | - throw new RuntimeException(e); |
124 | 100 | } |
125 | | - assertEquals(2, attempts.get(), |
126 | | - "The request should have been retried once after the first simulated connection failure."); |
| 101 | + assertEquals(2, failure.attempts); |
127 | 102 | } |
128 | 103 |
|
129 | | - @Test |
130 | | - void nonResendableHandleInMultipartIsNotRetried() { |
131 | | - // Mirrors how OkHttpServices wraps a non-resendable handle (e.g. ReaderHandle) in a Data Services multipart |
132 | | - // request: the per-part resendable flag should propagate up through MultipartBody.isOneShot(). |
133 | | - StreamingOutputImpl nonResendablePart = new StreamingOutputImpl( |
134 | | - outputStream -> outputStream.write("content".getBytes(StandardCharsets.UTF_8)), |
135 | | - null, |
136 | | - MediaType.get("application/xml"), |
137 | | - false |
138 | | - ); |
139 | | - MultipartBody multipartBody = new MultipartBody.Builder() |
140 | | - .setType(MultipartBody.FORM) |
141 | | - .addFormDataPart("body", null, nonResendablePart) |
| 104 | + private static MultipartBody multipart(RequestBody... parts) { |
| 105 | + MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM); |
| 106 | + for (int i = 0; i < parts.length; i++) { |
| 107 | + builder.addFormDataPart("part" + i, null, parts[i]); |
| 108 | + } |
| 109 | + return builder.build(); |
| 110 | + } |
| 111 | + |
| 112 | + private static Request post(RequestBody body) { |
| 113 | + return new Request.Builder().url("http://localhost/").post(body).build(); |
| 114 | + } |
| 115 | + |
| 116 | + private static OkHttpClient client(CountingFailure failure) { |
| 117 | + return new OkHttpClient.Builder() |
| 118 | + .addInterceptor(new RetryIOExceptionInterceptor(3, 0, 1, 0)) |
| 119 | + .addInterceptor(failure) |
142 | 120 | .build(); |
143 | | - assertTrue(multipartBody.isOneShot(), |
144 | | - "A multipart request containing even one non-resendable part must remain one-shot overall."); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Fails the first {@code failuresBeforeSuccess} attempts with an IOException, then returns a 200. |
| 125 | + */ |
| 126 | + private static class CountingFailure implements okhttp3.Interceptor { |
| 127 | + private final int failuresBeforeSuccess; |
| 128 | + int attempts; |
| 129 | + |
| 130 | + CountingFailure(int failuresBeforeSuccess) { |
| 131 | + this.failuresBeforeSuccess = failuresBeforeSuccess; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public Response intercept(Chain chain) throws IOException { |
| 136 | + if (++attempts <= failuresBeforeSuccess) { |
| 137 | + throw new IOException("unexpected end of stream"); |
| 138 | + } |
| 139 | + return new Response.Builder() |
| 140 | + .request(chain.request()) |
| 141 | + .protocol(Protocol.HTTP_1_1) |
| 142 | + .code(200) |
| 143 | + .message("OK") |
| 144 | + .body(ResponseBody.create("", null)) |
| 145 | + .build(); |
| 146 | + } |
145 | 147 | } |
146 | 148 | } |
0 commit comments