1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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.core5.http.ContentType;
40 import org.apache.hc.core5.http.HttpHost;
41 import org.apache.hc.core5.http.HttpResponse;
42 import org.apache.hc.core5.http.Message;
43 import org.apache.hc.core5.http.Method;
44 import org.apache.hc.core5.http.URIScheme;
45 import org.apache.hc.core5.http.config.Http1Config;
46 import org.apache.hc.core5.http.nio.AsyncClientEndpoint;
47 import org.apache.hc.core5.http.nio.entity.AsyncEntityProducers;
48 import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityConsumer;
49 import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
50 import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
51 import org.apache.hc.core5.http2.config.H2Config;
52 import org.apache.hc.core5.testing.nio.H2TestServer;
53 import org.hamcrest.CoreMatchers;
54 import org.junit.jupiter.api.Test;
55
56 public abstract class TestHttp1AsyncMinimal extends AbstractHttpAsyncFundamentalsTest<MinimalHttpAsyncClient> {
57
58 public TestHttp1AsyncMinimal(final URIScheme scheme) {
59 super(scheme);
60 }
61
62 @Override
63 protected H2TestServer startServer() throws Exception {
64 return startServer(Http1Config.DEFAULT, null, null);
65 }
66
67 @Override
68 protected MinimalHttpAsyncClient startClient() throws Exception {
69 return startMinimalClient(
70 Http1Config.DEFAULT,
71 H2Config.DEFAULT,
72 b -> {});
73 }
74
75 @Test
76 public void testConcurrentPostRequestsSameEndpoint() throws Exception {
77 final H2TestServer server = startServer();
78 server.register("/echo/*", AsyncEchoHandler::new);
79 final HttpHost target = targetHost();
80
81 final MinimalHttpAsyncClient client = startClient();
82
83 final byte[] b1 = new byte[1024];
84 final Random rnd = new Random(System.currentTimeMillis());
85 rnd.nextBytes(b1);
86
87 final int reqCount = 20;
88
89 final Future<AsyncClientEndpoint> endpointLease = client.lease(target, null);
90 final AsyncClientEndpoint endpoint = endpointLease.get(5, TimeUnit.SECONDS);
91 try {
92 final Queue<Future<Message<HttpResponse, byte[]>>> queue = new LinkedList<>();
93 for (int i = 0; i < reqCount; i++) {
94 final Future<Message<HttpResponse, byte[]>> future = endpoint.execute(
95 new BasicRequestProducer(Method.GET, target, "/echo/",
96 AsyncEntityProducers.create(b1, ContentType.APPLICATION_OCTET_STREAM)),
97 new BasicResponseConsumer<>(new BasicAsyncEntityConsumer()), HttpClientContext.create(), null);
98 queue.add(future);
99 }
100 while (!queue.isEmpty()) {
101 final Future<Message<HttpResponse, byte[]>> future = queue.remove();
102 final Message<HttpResponse, byte[]> responseMessage = future.get();
103 assertThat(responseMessage, CoreMatchers.notNullValue());
104 final HttpResponse response = responseMessage.getHead();
105 assertThat(response.getCode(), CoreMatchers.equalTo(200));
106 final byte[] b2 = responseMessage.getBody();
107 assertThat(b1, CoreMatchers.equalTo(b2));
108 endpoint.releaseAndReuse();
109 }
110 } finally {
111 endpoint.releaseAndDiscard();
112 }
113
114 }
115
116 }