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  
28  package org.apache.http.nio.integration;
29  
30  import java.net.InetSocketAddress;
31  import java.net.ServerSocket;
32  import java.net.Socket;
33  import java.util.Arrays;
34  import java.util.Collection;
35  import java.util.concurrent.CountDownLatch;
36  import java.util.concurrent.TimeUnit;
37  
38  import org.apache.http.HttpHost;
39  import org.apache.http.HttpRequest;
40  import org.apache.http.HttpResponse;
41  import org.apache.http.concurrent.FutureCallback;
42  import org.apache.http.message.BasicHttpRequest;
43  import org.apache.http.nio.protocol.BasicAsyncRequestProducer;
44  import org.apache.http.nio.protocol.BasicAsyncResponseConsumer;
45  import org.apache.http.nio.testserver.HttpCoreNIOTestBase;
46  import org.apache.http.protocol.BasicHttpContext;
47  import org.apache.http.protocol.HttpContext;
48  import org.junit.After;
49  import org.junit.Assert;
50  import org.junit.Before;
51  import org.junit.Test;
52  import org.junit.runner.RunWith;
53  import org.junit.runners.Parameterized;
54  
55  @RunWith(Parameterized.class)
56  public class TestHttpAsyncTimeout extends HttpCoreNIOTestBase {
57  
58      @Parameterized.Parameters(name = "{0}")
59      public static Collection<Object[]> protocols() {
60          return Arrays.asList(new Object[][]{
61                  {ProtocolScheme.http},
62                  {ProtocolScheme.https},
63          });
64      }
65  
66      public TestHttpAsyncTimeout(final ProtocolScheme scheme) {
67          super(scheme);
68      }
69  
70      private ServerSocket serverSocket;
71  
72      @Before
73      public void setUp() throws Exception {
74          initClient();
75      }
76  
77      @After
78      public void tearDown() throws Exception {
79          serverSocket.close();
80          shutDownClient();
81      }
82  
83      private InetSocketAddress start() throws Exception {
84  
85          this.client.start();
86          serverSocket = new ServerSocket(0);
87          return new InetSocketAddress(serverSocket.getInetAddress(), serverSocket.getLocalPort());
88      }
89  
90      @Test
91      public void testHandshakeTimeout() throws Exception {
92          // This test creates a server socket and accepts the incoming
93          // socket connection without reading any data.  The client should
94          // connect, be unable to progress through the handshake, and then
95          // time out when SO_TIMEOUT has elapsed.
96  
97          final InetSocketAddress address = start();
98          final HttpHost target = new HttpHost("localhost", address.getPort(), getScheme().name());
99  
100         final CountDownLatch latch = new CountDownLatch(1);
101 
102         final FutureCallback<HttpResponse> callback = new FutureCallback<HttpResponse>() {
103 
104             @Override
105             public void cancelled() {
106                 latch.countDown();
107             }
108 
109             @Override
110             public void failed(final Exception ex) {
111                 latch.countDown();
112             }
113 
114             @Override
115             public void completed(final HttpResponse response) {
116                 Assert.fail();
117             }
118 
119         };
120 
121         final HttpRequest request = new BasicHttpRequest("GET", "/");
122         final HttpContext context = new BasicHttpContext();
123         this.client.setTimeout(1000);
124         this.client.execute(
125                 new BasicAsyncRequestProducer(target, request),
126                 new BasicAsyncResponseConsumer(),
127                 context, callback);
128         final Socket accepted = serverSocket.accept();
129         try {
130             Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
131         } finally {
132             accepted.close();
133         }
134     }
135 
136 }