1414import org .junit .jupiter .api .AfterAll ;
1515import org .junit .jupiter .api .Assertions ;
1616import org .junit .jupiter .api .BeforeAll ;
17- import org .junit .jupiter .api .Disabled ;
1817import org .junit .jupiter .api .Test ;
1918import reactor .core .publisher .Flux ;
2019import reactor .core .publisher .Mono ;
4039import static com .github .tomakehurst .wiremock .client .WireMock .aResponse ;
4140import static com .github .tomakehurst .wiremock .client .WireMock .get ;
4241import static com .github .tomakehurst .wiremock .client .WireMock .post ;
42+ import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
4343import static org .junit .jupiter .api .Assertions .assertEquals ;
4444import static org .junit .jupiter .api .Assertions .assertLinesMatch ;
4545import static org .junit .jupiter .api .Assertions .assertNotNull ;
@@ -66,6 +66,7 @@ public static void beforeClass() {
6666 server .stubFor (post ("/shortPost" ).willReturn (aResponse ().withBody (SHORT_BODY )));
6767 server .stubFor (get (RETURN_HEADERS_AS_IS_PATH ).willReturn (aResponse ()
6868 .withTransformers (OkHttpAsyncHttpClientResponseTransformer .NAME )));
69+
6970 server .start ();
7071 }
7172
@@ -87,25 +88,33 @@ public void testFlowableResponseLongBodyAsByteArrayAsync() {
8788 }
8889
8990 @ Test
90- @ Disabled ("This tests behaviour of reactor netty's ByteBufFlux, not applicable for OkHttp" )
9191 public void testMultipleSubscriptionsEmitsError () {
9292 HttpResponse response = getResponse ("/short" );
93+
9394 // Subscription:1
94- response .getBodyAsByteArray ().block ();
95+ StepVerifier .create (response .getBodyAsByteArray ())
96+ .assertNext (Assertions ::assertNotNull )
97+ .expectComplete ()
98+ .verify (Duration .ofSeconds (20 ));
99+
95100 // Subscription:2
101+ // Getting the bytes of an OkHttp response closes the stream on first read.
102+ // Subsequent reads will return an IllegalStateException due to the stream being closed.
96103 StepVerifier .create (response .getBodyAsByteArray ())
97- .expectNextCount (0 ) // TODO: Check with smaldini, what is the verifier operator equivalent to .awaitDone(20, TimeUnit.SECONDS)
98- .verifyError (IllegalStateException .class );
104+ .expectNextCount (0 )
105+ .expectError (IllegalStateException .class )
106+ .verify (Duration .ofSeconds (20 ));
99107
100108 }
101109
102110 @ Test
103111 public void testFlowableWhenServerReturnsBodyAndNoErrorsWhenHttp500Returned () {
104112 HttpResponse response = getResponse ("/error" );
105- StepVerifier .create (response .getBodyAsString ())
106- .expectNext ("error" ) // TODO: .awaitDone(20, TimeUnit.SECONDS) [See previous todo]
107- .verifyComplete ();
108113 assertEquals (500 , response .getStatusCode ());
114+ StepVerifier .create (response .getBodyAsString ())
115+ .expectNext ("error" )
116+ .expectComplete ()
117+ .verify (Duration .ofSeconds (20 ));
109118 }
110119
111120 @ Test
@@ -128,7 +137,7 @@ public void testFlowableBackpressure() {
128137
129138 @ Test
130139 public void testRequestBodyIsErrorShouldPropagateToResponse () {
131- HttpClient client = HttpClient . createDefault ();
140+ HttpClient client = new OkHttpAsyncClientProvider (). createInstance ();
132141 HttpRequest request = new HttpRequest (HttpMethod .POST , url (server , "/shortPost" ))
133142 .setHeader ("Content-Length" , "123" )
134143 .setBody (Flux .error (new RuntimeException ("boo" )));
@@ -140,7 +149,7 @@ public void testRequestBodyIsErrorShouldPropagateToResponse() {
140149
141150 @ Test
142151 public void testRequestBodyEndsInErrorShouldPropagateToResponse () {
143- HttpClient client = HttpClient . createDefault ();
152+ HttpClient client = new OkHttpAsyncClientProvider (). createInstance ();
144153 String contentChunk = "abcdefgh" ;
145154 int repetitions = 1000 ;
146155 HttpRequest request = new HttpRequest (HttpMethod .POST , url (server , "/shortPost" ))
@@ -149,10 +158,14 @@ public void testRequestBodyEndsInErrorShouldPropagateToResponse() {
149158 .repeat (repetitions )
150159 .map (s -> ByteBuffer .wrap (s .getBytes (StandardCharsets .UTF_8 )))
151160 .concatWith (Flux .error (new RuntimeException ("boo" ))));
152- StepVerifier .create (client .send (request ))
153- // .awaitDone(10, TimeUnit.SECONDS)
154- .expectErrorMessage ("boo" )
155- .verify ();
161+
162+ try {
163+ StepVerifier .create (client .send (request ))
164+ .expectErrorMessage ("boo" )
165+ .verify (Duration .ofSeconds (10 ));
166+ } catch (Exception ex ) {
167+ assertEquals ("boo" , ex .getMessage ());
168+ }
156169 }
157170
158171 @ Test
@@ -200,42 +213,30 @@ public void testServerShutsDownSocketShouldPushErrorToContentFlowable() {
200213 });
201214 }
202215
203- @ Disabled ("This flakey test fails often on MacOS. https://github.com/Azure/azure-sdk-for-java/issues/4357." )
204216 @ Test
205217 public void testConcurrentRequests () throws NoSuchAlgorithmException {
206218 int numRequests = 100 ; // 100 = 1GB of data read
207- HttpClient client = HttpClient . createDefault ();
219+ HttpClient client = new OkHttpAsyncClientProvider (). createInstance ();
208220 byte [] expectedDigest = digest (LONG_BODY );
221+ long expectedByteCount = (long ) numRequests * LONG_BODY .getBytes (StandardCharsets .UTF_8 ).length ;
209222
210223 Mono <Long > numBytesMono = Flux .range (1 , numRequests )
211224 .parallel (10 )
212225 .runOn (Schedulers .boundedElastic ())
213226 .flatMap (n -> Mono .fromCallable (() -> getResponse (client , "/long" )).flatMapMany (response -> {
214227 MessageDigest md = md5Digest ();
215228 return response .getBody ()
216- .doOnNext (md ::update )
217- .map (bb -> new NumberedByteBuffer (n , bb ))
218- // .doOnComplete(() -> System.out.println("completed " + n))
219- .doOnComplete (() -> Assertions .assertArrayEquals (expectedDigest ,
220- md .digest (), "wrong digest!" ));
229+ .doOnNext (buffer -> md .update (buffer .duplicate ()))
230+ .doOnComplete (() -> assertArrayEquals (expectedDigest , md .digest (), "wrong digest!" ));
221231 }))
222232 .sequential ()
223- // enable the doOnNext call to see request numbers and thread names
224- // .doOnNext(g -> System.out.println(g.n + " " +
225- // Thread.currentThread().getName()))
226- .map (nbb -> (long ) nbb .bb .limit ())
227- .reduce (Long ::sum )
228- .subscribeOn (Schedulers .boundedElastic ());
233+ .map (buffer -> (long ) buffer .remaining ())
234+ .reduce (Long ::sum );
229235
230236 StepVerifier .create (numBytesMono )
231- // .awaitDone(timeoutSeconds, TimeUnit.SECONDS)
232- .expectNext ((long ) (numRequests * LONG_BODY .getBytes (StandardCharsets .UTF_8 ).length ))
233- .verifyComplete ();
234- //
235- // long numBytes = numBytesMono.block();
236- // t = System.currentTimeMillis() - t;
237- // System.out.println("totalBytesRead=" + numBytes / 1024 / 1024 + "MB in " + t / 1000.0 + "s");
238- // assertEquals(numRequests * LONG_BODY.getBytes(StandardCharsets.UTF_8).length, numBytes);
237+ .expectNext (expectedByteCount )
238+ .expectComplete ()
239+ .verify (Duration .ofSeconds (60 ));
239240 }
240241
241242 @ Test
@@ -284,16 +285,6 @@ private static byte[] digest(String s) throws NoSuchAlgorithmException {
284285 return md .digest ();
285286 }
286287
287- private static final class NumberedByteBuffer {
288- final long n ;
289- final ByteBuffer bb ;
290-
291- NumberedByteBuffer (long n , ByteBuffer bb ) {
292- this .n = n ;
293- this .bb = bb ;
294- }
295- }
296-
297288 private static HttpResponse getResponse (String path ) {
298289 HttpClient client = new OkHttpAsyncHttpClientBuilder ().build ();
299290 return getResponse (client , path );
0 commit comments