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 org.hamcrest.MatcherAssert.assertThat;
30  
31  import java.util.LinkedList;
32  import java.util.Queue;
33  import java.util.Random;
34  import java.util.concurrent.Future;
35  import java.util.concurrent.TimeUnit;
36  
37  import org.apache.hc.client5.http.impl.async.MinimalHttpAsyncClient;
38  import org.apache.hc.client5.http.protocol.HttpClientContext;
39  import org.apache.hc.client5.testing.extension.async.ClientProtocolLevel;
40  import org.apache.hc.client5.testing.extension.async.ServerProtocolLevel;
41  import org.apache.hc.core5.http.ContentType;
42  import org.apache.hc.core5.http.HttpHost;
43  import org.apache.hc.core5.http.HttpResponse;
44  import org.apache.hc.core5.http.Message;
45  import org.apache.hc.core5.http.Method;
46  import org.apache.hc.core5.http.URIScheme;
47  import org.apache.hc.core5.http.nio.AsyncClientEndpoint;
48  import org.apache.hc.core5.http.nio.entity.AsyncEntityProducers;
49  import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityConsumer;
50  import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
51  import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
52  import org.apache.hc.core5.reactive.ReactiveServerExchangeHandler;
53  import org.apache.hc.core5.testing.reactive.ReactiveEchoProcessor;
54  import org.hamcrest.CoreMatchers;
55  import org.junit.jupiter.api.Test;
56  
57  public abstract class TestHttp1ReactiveMinimal extends AbstractHttpReactiveFundamentalsTest {
58  
59      public TestHttp1ReactiveMinimal(final URIScheme scheme) {
60          super(scheme, ClientProtocolLevel.MINIMAL, ServerProtocolLevel.STANDARD);
61      }
62  
63      @Test
64      public void testConcurrentPostRequestsSameEndpoint() throws Exception {
65          configureServer(bootstrap -> bootstrap.register("/echo/*", () ->
66                  new ReactiveServerExchangeHandler(new ReactiveEchoProcessor())));
67          final HttpHost target = startServer();
68  
69          final MinimalHttpAsyncClient client = startClient().getImplementation();
70  
71          final byte[] b1 = new byte[1024];
72          final Random rnd = new Random(System.currentTimeMillis());
73          rnd.nextBytes(b1);
74  
75          final int reqCount = 20;
76  
77          final Future<AsyncClientEndpoint> endpointLease = client.lease(target, null);
78          final AsyncClientEndpoint endpoint = endpointLease.get(5, TimeUnit.SECONDS);
79          try {
80              final Queue<Future<Message<HttpResponse, byte[]>>> queue = new LinkedList<>();
81              for (int i = 0; i < reqCount; i++) {
82                  final Future<Message<HttpResponse, byte[]>> future = endpoint.execute(
83                          new BasicRequestProducer(Method.GET, target, "/echo/",
84                                  AsyncEntityProducers.create(b1, ContentType.APPLICATION_OCTET_STREAM)),
85                          new BasicResponseConsumer<>(new BasicAsyncEntityConsumer()), HttpClientContext.create(), null);
86                  queue.add(future);
87              }
88              while (!queue.isEmpty()) {
89                  final Future<Message<HttpResponse, byte[]>> future = queue.remove();
90                  final Message<HttpResponse, byte[]> responseMessage = future.get();
91                  assertThat(responseMessage, CoreMatchers.notNullValue());
92                  final HttpResponse response = responseMessage.getHead();
93                  assertThat(response.getCode(), CoreMatchers.equalTo(200));
94                  final byte[] b2 = responseMessage.getBody();
95                  assertThat(b1, CoreMatchers.equalTo(b2));
96                  endpoint.releaseAndReuse();
97              }
98          } finally {
99              endpoint.releaseAndDiscard();
100         }
101 
102     }
103 
104 }