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.http.nio.client.integration;
28  
29  import org.apache.http.HttpEntityEnclosingRequest;
30  import org.apache.http.HttpHost;
31  import org.apache.http.HttpRequest;
32  import org.apache.http.HttpResponse;
33  import org.apache.http.client.methods.HttpGet;
34  import org.apache.http.client.methods.HttpPost;
35  import org.apache.http.entity.StringEntity;
36  import org.apache.http.impl.nio.client.CloseableHttpPipeliningClient;
37  import org.apache.http.impl.nio.client.HttpAsyncClients;
38  import org.apache.http.localserver.AbstractAsyncTest;
39  import org.apache.http.localserver.EchoHandler;
40  import org.apache.http.localserver.RandomHandler;
41  import org.apache.http.nio.protocol.BasicAsyncRequestHandler;
42  import org.apache.http.util.EntityUtils;
43  import org.junit.After;
44  import org.junit.Assert;
45  import org.junit.Before;
46  import org.junit.Test;
47  import org.junit.runner.RunWith;
48  import org.junit.runners.Parameterized;
49  
50  import java.util.Arrays;
51  import java.util.Collection;
52  import java.util.List;
53  import java.util.Queue;
54  import java.util.concurrent.ConcurrentLinkedQueue;
55  import java.util.concurrent.Future;
56  
57  @RunWith(Parameterized.class)
58  public class TestHttpAsyncPipelining extends AbstractAsyncTest {
59  
60      @Parameterized.Parameters(name = "{0}")
61      public static Collection<Object[]> protocols() {
62          return Arrays.asList(new Object[][]{
63                  {ProtocolScheme.http},
64                  {ProtocolScheme.https},
65          });
66      }
67  
68      protected CloseableHttpPipeliningClient httpclient;
69  
70      public TestHttpAsyncPipelining(final ProtocolScheme scheme) {
71          super(scheme);
72      }
73  
74      @Before @Override
75      public void setUp() throws Exception {
76          super.setUp();
77          this.serverBootstrap.registerHandler("/echo/*", new BasicAsyncRequestHandler(new EchoHandler()));
78          this.serverBootstrap.registerHandler("/random/*", new BasicAsyncRequestHandler(new RandomHandler()));
79  
80          this.httpclient = HttpAsyncClients.createPipelining(this.connMgr);
81      }
82  
83      @After @Override
84      public void shutDown() throws Exception {
85          if (this.httpclient != null) {
86              this.httpclient.close();
87          }
88          super.shutDown();
89      }
90  
91      public HttpHost start() throws Exception {
92          final HttpHost serverEndpoint = startServer();
93  
94          this.connMgr.setDefaultMaxPerRoute(1);
95          this.httpclient.start();
96  
97          return serverEndpoint;
98      }
99  
100     @Test
101     public void testPipelinedGets() throws Exception {
102         final HttpHost target = start();
103 
104         final Queue<Future<List<HttpResponse>>> queue = new ConcurrentLinkedQueue<Future<List<HttpResponse>>>();
105         for (int i = 0; i < 10; i++) {
106             final HttpRequest httpget1 = new HttpGet("/random/512");
107             final HttpRequest httpget2 = new HttpGet("/random/1024");
108             final HttpRequest httpget3 = new HttpGet("/random/2048");
109             queue.add(this.httpclient.execute(target, Arrays.asList(httpget1, httpget2, httpget3), null));
110         }
111 
112         while (!queue.isEmpty()) {
113             final Future<List<HttpResponse>> future = queue.remove();
114             final List<HttpResponse> responses = future.get();
115             Assert.assertNotNull(responses);
116             Assert.assertEquals(3, responses.size());
117             final HttpResponse response1 = responses.get(0);
118             Assert.assertEquals(200, response1.getStatusLine().getStatusCode());
119             final byte[] bytes1 = EntityUtils.toByteArray(response1.getEntity());
120             Assert.assertNotNull(bytes1);
121             Assert.assertEquals(512, bytes1.length);
122             final HttpResponse response2 = responses.get(1);
123             Assert.assertEquals(200, response2.getStatusLine().getStatusCode());
124             final byte[] bytes2 = EntityUtils.toByteArray(response2.getEntity());
125             Assert.assertNotNull(bytes2);
126             Assert.assertEquals(1024, bytes2.length);
127             final HttpResponse response3 = responses.get(2);
128             Assert.assertEquals(200, response3.getStatusLine().getStatusCode());
129             final byte[] bytes3 = EntityUtils.toByteArray(response3.getEntity());
130             Assert.assertNotNull(bytes3);
131             Assert.assertEquals(2048, bytes3.length);
132         }
133 
134     }
135 
136     @Test
137     public void testPipelinedPostsAndGets() throws Exception {
138         final HttpHost target = start();
139 
140         final Queue<Future<List<HttpResponse>>> queue = new ConcurrentLinkedQueue<Future<List<HttpResponse>>>();
141         for (int i = 0; i < 10; i++) {
142             final HttpEntityEnclosingRequest httppost1 = new HttpPost("/echo/");
143             httppost1.setEntity(new StringEntity("this and that"));
144             final HttpRequest httpget2 = new HttpGet("/echo/");
145             final HttpEntityEnclosingRequest httppost3 = new HttpPost("/echo/");
146             httppost3.setEntity(new StringEntity("all sorts of things"));
147             queue.add(this.httpclient.execute(target, Arrays.asList(httppost1, httpget2, httppost3), null));
148         }
149 
150         while (!queue.isEmpty()) {
151             final Future<List<HttpResponse>> future = queue.remove();
152             final List<HttpResponse> responses = future.get();
153             Assert.assertNotNull(responses);
154             Assert.assertEquals(3, responses.size());
155             final HttpResponse response1 = responses.get(0);
156             Assert.assertEquals(200, response1.getStatusLine().getStatusCode());
157             final String s1 = EntityUtils.toString(response1.getEntity());
158             Assert.assertNotNull(s1);
159             Assert.assertEquals("this and that", s1);
160             final HttpResponse response2 = responses.get(1);
161             Assert.assertEquals(200, response2.getStatusLine().getStatusCode());
162             final String s2 = EntityUtils.toString(response2.getEntity());
163             Assert.assertNotNull(s2);
164             Assert.assertEquals("", s2);
165             final HttpResponse response3 = responses.get(2);
166             Assert.assertEquals(200, response3.getStatusLine().getStatusCode());
167             final String s3 = EntityUtils.toString(response3.getEntity());
168             Assert.assertNotNull(s3);
169             Assert.assertEquals("all sorts of things", s3);
170         }
171 
172     }
173 
174 }