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.hamcrest.CoreMatchers;
53  import org.junit.jupiter.api.Test;
54  
55  public abstract class TestHttp1AsyncMinimal extends AbstractHttpAsyncFundamentalsTest {
56  
57      public TestHttp1AsyncMinimal(final URIScheme scheme) {
58          super(scheme, ClientProtocolLevel.MINIMAL, ServerProtocolLevel.STANDARD);
59      }
60  
61      @Test
62      public void testConcurrentPostRequestsSameEndpoint() throws Exception {
63          configureServer(bootstrap -> bootstrap.register("/echo/*", AsyncEchoHandler::new));
64          final HttpHost target = startServer();
65  
66          final MinimalHttpAsyncClient client = startClient().getImplementation();
67  
68          final byte[] b1 = new byte[1024];
69          final Random rnd = new Random(System.currentTimeMillis());
70          rnd.nextBytes(b1);
71  
72          final int reqCount = 20;
73  
74          final Future<AsyncClientEndpoint> endpointLease = client.lease(target, null);
75          final AsyncClientEndpoint endpoint = endpointLease.get(5, TimeUnit.SECONDS);
76          try {
77              final Queue<Future<Message<HttpResponse, byte[]>>> queue = new LinkedList<>();
78              for (int i = 0; i < reqCount; i++) {
79                  final Future<Message<HttpResponse, byte[]>> future = endpoint.execute(
80                          new BasicRequestProducer(Method.GET, target, "/echo/",
81                                  AsyncEntityProducers.create(b1, ContentType.APPLICATION_OCTET_STREAM)),
82                          new BasicResponseConsumer<>(new BasicAsyncEntityConsumer()), HttpClientContext.create(), null);
83                  queue.add(future);
84              }
85              while (!queue.isEmpty()) {
86                  final Future<Message<HttpResponse, byte[]>> future = queue.remove();
87                  final Message<HttpResponse, byte[]> responseMessage = future.get();
88                  assertThat(responseMessage, CoreMatchers.notNullValue());
89                  final HttpResponse response = responseMessage.getHead();
90                  assertThat(response.getCode(), CoreMatchers.equalTo(200));
91                  final byte[] b2 = responseMessage.getBody();
92                  assertThat(b1, CoreMatchers.equalTo(b2));
93                  endpoint.releaseAndReuse();
94              }
95          } finally {
96              endpoint.releaseAndDiscard();
97          }
98  
99      }
100 
101 }