View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.client5.testing.async;
28  
29  import static java.util.concurrent.TimeUnit.MILLISECONDS;
30  import static org.hamcrest.MatcherAssert.assertThat;
31  
32  import java.io.ByteArrayOutputStream;
33  import java.nio.ByteBuffer;
34  import java.nio.channels.Channels;
35  import java.nio.channels.WritableByteChannel;
36  import java.nio.charset.StandardCharsets;
37  import java.util.LinkedHashMap;
38  import java.util.List;
39  import java.util.Map;
40  import java.util.Queue;
41  import java.util.Random;
42  import java.util.concurrent.ArrayBlockingQueue;
43  import java.util.concurrent.BlockingQueue;
44  import java.util.concurrent.ConcurrentLinkedQueue;
45  import java.util.concurrent.CountDownLatch;
46  import java.util.concurrent.ExecutorService;
47  import java.util.concurrent.Executors;
48  import java.util.concurrent.Future;
49  import java.util.concurrent.atomic.AtomicInteger;
50  import java.util.concurrent.atomic.AtomicReference;
51  
52  import io.reactivex.rxjava3.core.Flowable;
53  import io.reactivex.rxjava3.schedulers.Schedulers;
54  import org.apache.hc.client5.http.protocol.HttpClientContext;
55  import org.apache.hc.client5.testing.extension.async.ClientProtocolLevel;
56  import org.apache.hc.client5.testing.extension.async.ServerProtocolLevel;
57  import org.apache.hc.client5.testing.extension.async.TestAsyncClient;
58  import org.apache.hc.core5.concurrent.FutureCallback;
59  import org.apache.hc.core5.http.ContentType;
60  import org.apache.hc.core5.http.HttpHost;
61  import org.apache.hc.core5.http.HttpResponse;
62  import org.apache.hc.core5.http.Message;
63  import org.apache.hc.core5.http.URIScheme;
64  import org.apache.hc.core5.http.nio.AsyncRequestProducer;
65  import org.apache.hc.core5.http.nio.support.AsyncRequestBuilder;
66  import org.apache.hc.core5.reactive.ReactiveEntityProducer;
67  import org.apache.hc.core5.reactive.ReactiveResponseConsumer;
68  import org.apache.hc.core5.reactive.ReactiveServerExchangeHandler;
69  import org.apache.hc.core5.testing.reactive.Reactive3TestUtils;
70  import org.apache.hc.core5.testing.reactive.Reactive3TestUtils.StreamDescription;
71  import org.apache.hc.core5.testing.reactive.ReactiveEchoProcessor;
72  import org.apache.hc.core5.testing.reactive.ReactiveRandomProcessor;
73  import org.apache.hc.core5.util.TextUtils;
74  import org.hamcrest.CoreMatchers;
75  import org.junit.jupiter.api.Assertions;
76  import org.junit.jupiter.api.Test;
77  import org.junit.jupiter.api.Timeout;
78  import org.reactivestreams.Publisher;
79  
80  public abstract class AbstractHttpReactiveFundamentalsTest extends AbstractIntegrationTestBase {
81  
82      public AbstractHttpReactiveFundamentalsTest(final URIScheme scheme, final ClientProtocolLevel clientProtocolLevel, final ServerProtocolLevel serverProtocolLevel) {
83          super(scheme, clientProtocolLevel, serverProtocolLevel);
84      }
85  
86      @Test
87      @Timeout(value = 60_000, unit = MILLISECONDS)
88      public void testSequentialGetRequests() throws Exception {
89          configureServer(bootstrap -> bootstrap
90                  .register("/random/*", () -> new ReactiveServerExchangeHandler(new ReactiveRandomProcessor())));
91          final HttpHost target = startServer();
92  
93          final TestAsyncClient client = startClient();
94          for (int i = 0; i < 3; i++) {
95              final ReactiveResponseConsumer consumer = new ReactiveResponseConsumer();
96  
97              client.execute(AsyncRequestBuilder.get(target + "/random/2048").build(), consumer, null);
98  
99              final Message<HttpResponse, Publisher<ByteBuffer>> response = consumer.getResponseFuture().get();
100             assertThat(response, CoreMatchers.notNullValue());
101             assertThat(response.getHead().getCode(), CoreMatchers.equalTo(200));
102 
103             final String body = publisherToString(response.getBody());
104             assertThat(body, CoreMatchers.notNullValue());
105             assertThat(body.length(), CoreMatchers.equalTo(2048));
106         }
107     }
108 
109     @Test
110     @Timeout(value = 2000, unit = MILLISECONDS)
111     public void testSequentialHeadRequests() throws Exception {
112         configureServer(bootstrap -> bootstrap.register("/random/*", () ->
113                 new ReactiveServerExchangeHandler(new ReactiveRandomProcessor())));
114         final HttpHost target = startServer();
115 
116         final TestAsyncClient client = startClient();
117         for (int i = 0; i < 3; i++) {
118             final ReactiveResponseConsumer consumer = new ReactiveResponseConsumer();
119 
120             client.execute(AsyncRequestBuilder.head(target + "/random/2048").build(), consumer, null);
121 
122             final Message<HttpResponse, Publisher<ByteBuffer>> response = consumer.getResponseFuture().get();
123             assertThat(response, CoreMatchers.notNullValue());
124             assertThat(response.getHead().getCode(), CoreMatchers.equalTo(200));
125 
126             final String body = publisherToString(response.getBody());
127             assertThat(body, CoreMatchers.nullValue());
128         }
129     }
130 
131     @Test
132     @Timeout(value = 60_000, unit = MILLISECONDS)
133     public void testSequentialPostRequests() throws Exception {
134         configureServer(bootstrap -> bootstrap.register("/echo/*", () ->
135                 new ReactiveServerExchangeHandler(new ReactiveEchoProcessor())));
136         final HttpHost target = startServer();
137 
138         final TestAsyncClient client = startClient();
139         for (int i = 0; i < 3; i++) {
140             final byte[] b1 = new byte[1024];
141             final Random rnd = new Random(System.currentTimeMillis());
142             rnd.nextBytes(b1);
143             final Flowable<ByteBuffer> publisher = Flowable.just(ByteBuffer.wrap(b1));
144             final ReactiveResponseConsumer consumer = new ReactiveResponseConsumer();
145             final AsyncRequestProducer request = AsyncRequestBuilder.post(target + "/echo/")
146                 .setEntity(new ReactiveEntityProducer(publisher, -1, ContentType.APPLICATION_OCTET_STREAM, null))
147                 .build();
148 
149             client.execute(request, consumer, HttpClientContext.create(), null);
150 
151             final Future<Message<HttpResponse, Publisher<ByteBuffer>>> responseFuture = consumer.getResponseFuture();
152             final Message<HttpResponse, Publisher<ByteBuffer>> responseMessage = responseFuture.get();
153             assertThat(responseMessage, CoreMatchers.notNullValue());
154             final HttpResponse response = responseMessage.getHead();
155             assertThat(response.getCode(), CoreMatchers.equalTo(200));
156             final byte[] b2 = publisherToByteArray(responseMessage.getBody());
157             assertThat(b1, CoreMatchers.equalTo(b2));
158         }
159     }
160 
161     @Test
162     @Timeout(value = 60_000, unit = MILLISECONDS)
163     public void testConcurrentPostRequests() throws Exception {
164         configureServer(bootstrap -> bootstrap.register("/echo/*", () ->
165                 new ReactiveServerExchangeHandler(new ReactiveEchoProcessor())));
166         final HttpHost target = startServer();
167 
168         final TestAsyncClient client = startClient();
169 
170         final int reqCount = 500;
171         final int maxSize = 128 * 1024;
172         final Map<Long, StreamingTestCase> testCases = StreamingTestCase.generate(reqCount, maxSize);
173         final BlockingQueue<StreamDescription> responses = new ArrayBlockingQueue<>(reqCount);
174 
175         for (final StreamingTestCase testCase : testCases.values()) {
176             final ReactiveEntityProducer producer = new ReactiveEntityProducer(testCase.stream, testCase.length,
177                     ContentType.APPLICATION_OCTET_STREAM, null);
178             final AsyncRequestProducer request = AsyncRequestBuilder.post(target + "/echo/")
179                     .setEntity(producer)
180                     .build();
181 
182             final ReactiveResponseConsumer consumer = new ReactiveResponseConsumer(new FutureCallback<Message<HttpResponse, Publisher<ByteBuffer>>>() {
183                 @Override
184                 public void completed(final Message<HttpResponse, Publisher<ByteBuffer>> result) {
185                     final Flowable<ByteBuffer> flowable = Flowable.fromPublisher(result.getBody())
186                             .observeOn(Schedulers.io()); // Stream the data on an RxJava scheduler, not a client thread
187                     Reactive3TestUtils.consumeStream(flowable).subscribe(responses::add);
188                 }
189                 @Override
190                 public void failed(final Exception ex) { }
191                 @Override
192                 public void cancelled() { }
193             });
194             client.execute(request, consumer, HttpClientContext.create(), null);
195         }
196 
197         for (int i = 0; i < reqCount; i++) {
198             final StreamDescription streamDescription = responses.take();
199             final StreamingTestCase streamingTestCase = testCases.get(streamDescription.length);
200             final long expectedLength = streamingTestCase.length;
201             final long actualLength = streamDescription.length;
202             Assertions.assertEquals(expectedLength, actualLength);
203 
204             final String expectedHash = streamingTestCase.expectedHash.get();
205             final String actualHash = TextUtils.toHexString(streamDescription.md.digest());
206             Assertions.assertEquals(expectedHash, actualHash);
207         }
208     }
209 
210     @Test
211     @Timeout(value = 60_000, unit = MILLISECONDS)
212     public void testRequestExecutionFromCallback() throws Exception {
213         configureServer(bootstrap -> bootstrap.register("/random/*", () ->
214                 new ReactiveServerExchangeHandler(new ReactiveRandomProcessor())));
215         final HttpHost target = startServer();
216 
217         final TestAsyncClient client = startClient();
218         final int requestNum = 50;
219         final AtomicInteger count = new AtomicInteger(requestNum);
220         final Queue<Message<HttpResponse, Publisher<ByteBuffer>>> resultQueue = new ConcurrentLinkedQueue<>();
221         final CountDownLatch countDownLatch = new CountDownLatch(requestNum);
222 
223         final FutureCallback<Message<HttpResponse, Publisher<ByteBuffer>>> callback = new FutureCallback<Message<HttpResponse, Publisher<ByteBuffer>>>() {
224             @Override
225             public void completed(final Message<HttpResponse, Publisher<ByteBuffer>> result) {
226                 try {
227                     resultQueue.add(result);
228                     if (count.decrementAndGet() > 0) {
229                         final ReactiveResponseConsumer consumer = new ReactiveResponseConsumer(this);
230                         client.execute(AsyncRequestBuilder.get(target + "/random/2048").build(), consumer, null);
231                     }
232                 } finally {
233                     countDownLatch.countDown();
234                 }
235             }
236 
237             @Override
238             public void failed(final Exception ex) {
239                 countDownLatch.countDown();
240             }
241 
242             @Override
243             public void cancelled() {
244                 countDownLatch.countDown();
245             }
246         };
247 
248         final int threadNum = 5;
249         final ExecutorService executorService = Executors.newFixedThreadPool(threadNum);
250         for (int i = 0; i < threadNum; i++) {
251             executorService.execute(() -> {
252                 if (!Thread.currentThread().isInterrupted()) {
253                     final ReactiveResponseConsumer consumer = new ReactiveResponseConsumer(callback);
254                     client.execute(AsyncRequestBuilder.get(target + "/random/2048").build(), consumer, null);
255                 }
256             });
257         }
258 
259         assertThat(countDownLatch.await(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()), CoreMatchers.equalTo(true));
260 
261         executorService.shutdownNow();
262         executorService.awaitTermination(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
263 
264         for (;;) {
265             final Message<HttpResponse, Publisher<ByteBuffer>> response = resultQueue.poll();
266             if (response == null) {
267                 break;
268             }
269             assertThat(response.getHead().getCode(), CoreMatchers.equalTo(200));
270         }
271     }
272 
273     @Test
274     public void testBadRequest() throws Exception {
275         configureServer(bootstrap -> bootstrap.register("/random/*", () ->
276                 new ReactiveServerExchangeHandler(new ReactiveRandomProcessor())));
277         final HttpHost target = startServer();
278 
279         final TestAsyncClient client = startClient();
280         final AsyncRequestProducer request = AsyncRequestBuilder.get(target + "/random/boom").build();
281         final ReactiveResponseConsumer consumer = new ReactiveResponseConsumer();
282 
283         client.execute(request, consumer, null);
284 
285         final Future<Message<HttpResponse, Publisher<ByteBuffer>>> future = consumer.getResponseFuture();
286         final HttpResponse response = future.get().getHead();
287         assertThat(response, CoreMatchers.notNullValue());
288         assertThat(response.getCode(), CoreMatchers.equalTo(400));
289     }
290 
291     static String publisherToString(final Publisher<ByteBuffer> publisher) throws Exception {
292         final byte[] bytes = publisherToByteArray(publisher);
293         if (bytes == null) {
294             return null;
295         }
296         return new String(bytes, StandardCharsets.UTF_8);
297     }
298 
299     static byte[] publisherToByteArray(final Publisher<ByteBuffer> publisher) throws Exception {
300         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
301         try (WritableByteChannel channel = Channels.newChannel(baos)) {
302             final List<ByteBuffer> bufs = Flowable.fromPublisher(publisher)
303                 .toList()
304                 .blockingGet();
305             if (bufs.isEmpty()) {
306                 return null;
307             }
308             for (final ByteBuffer buf : bufs) {
309                 channel.write(buf);
310             }
311         }
312         return baos.toByteArray();
313     }
314 
315     private static final class StreamingTestCase {
316         final long length;
317         final AtomicReference<String> expectedHash;
318         final Flowable<ByteBuffer> stream;
319 
320         StreamingTestCase(final long length, final AtomicReference<String> expectedHash, final Flowable<ByteBuffer> stream) {
321             this.length = length;
322             this.expectedHash = expectedHash;
323             this.stream = stream;
324         }
325 
326         static Map<Long, StreamingTestCase> generate(final int numTestCases, final int maxSize) {
327             final Map<Long, StreamingTestCase> testCases = new LinkedHashMap<>();
328             int testCaseNum = 0;
329             while (testCases.size() < numTestCases) {
330                 final long seed = 198723L * testCaseNum++;
331                 final int length = 1 + new Random(seed).nextInt(maxSize);
332                 final AtomicReference<String> expectedHash = new AtomicReference<>();
333                 final Flowable<ByteBuffer> stream = Reactive3TestUtils.produceStream(length, expectedHash);
334                 final StreamingTestCase streamingTestCase = new StreamingTestCase(length, expectedHash, stream);
335                 testCases.put((long) length, streamingTestCase);
336             }
337             return testCases;
338         }
339     }
340 }