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
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
93
94
95
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 }