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.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 abstract class TestHttp1AsyncMinimal extends AbstractHttpAsyncFundamentalsTest {
56
57 public TestHttp1AsyncMinimal(final URIScheme scheme) {
58 super(scheme, ClientProtocolLevel.MINIMAL, ServerProtocolLevel.STANDARD);
59 }
60
61 @Test
62 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 }