View Javadoc

1   package com.atlassian.httpclient.apache.httpcomponents;
2   
3   import com.atlassian.httpclient.api.HttpClient;
4   import com.atlassian.httpclient.api.Response;
5   import com.atlassian.httpclient.api.ResponsePromise;
6   import com.sun.net.httpserver.HttpServer;
7   import org.junit.After;
8   import org.junit.Before;
9   import org.junit.Test;
10  
11  import java.io.IOException;
12  import java.net.InetSocketAddress;
13  import java.util.List;
14  import java.util.concurrent.ExecutionException;
15  import java.util.concurrent.Executors;
16  import java.util.concurrent.TimeUnit;
17  import java.util.concurrent.TimeoutException;
18  import java.util.stream.Collectors;
19  import java.util.stream.IntStream;
20  
21  import static org.junit.Assert.assertEquals;
22  
23  public class ManyConcurrentRequestsTest {
24  
25      private HttpServer server;
26  
27      private HttpClient client;
28  
29      private int port;
30  
31      @Before
32      public void setUp() {
33          server = createOnFreePort();
34          port = server.getAddress().getPort();
35          server.setExecutor(Executors.newFixedThreadPool(40));
36          server.createContext("/", x -> {
37              try {
38                  Thread.sleep(100);
39              } catch (InterruptedException e) {
40                  throw new RuntimeException(e);
41              }
42              String resp = "OK";
43              x.sendResponseHeaders(200, resp.length());
44              x.getResponseBody().write(resp.getBytes());
45              x.getResponseBody().close();
46          });
47          server.start();
48          client = new ApacheAsyncHttpClient<Void>("test");
49      }
50  
51      @After
52      public void tearDown() {
53          server.stop(0);
54      }
55  
56      @Test
57      public void testTooManyRequests() throws InterruptedException, ExecutionException, TimeoutException {
58          final String url = serverUrl(port);
59          List<ResponsePromise> requests = IntStream.range(0, 1000)
60                  .mapToObj(this::request)
61                  .collect(Collectors.toList());
62  
63          requests.stream()
64                  .map(r -> waitForResponse(r).getStatusCode())
65                  .forEach(c -> assertEquals(Integer.valueOf(200), c));
66      }
67  
68      public ResponsePromise request(int i) {
69          ResponsePromise p = client.newRequest(serverUrl(port)).get();
70          p.fail(e -> System.out.println("Request no " + i + " failed with " + e));
71          return p;
72      }
73  
74      private static Response waitForResponse(ResponsePromise promise) {
75          try {
76              return promise.get(1, TimeUnit.MINUTES);
77          } catch (InterruptedException | ExecutionException | TimeoutException e) {
78              throw new RuntimeException(e);
79          }
80      }
81  
82      private static HttpServer createOnFreePort() {
83          try {
84              return HttpServer.create(new InetSocketAddress(0), 0);
85          } catch (IOException e) {
86              throw new RuntimeException(e);
87          }
88      }
89  
90      public static String serverUrl(int port) {
91          return String.format("http://localhost:%d/", port);
92      }
93  }