~ubuntu-branches/ubuntu/trusty/httpcomponents-core/trusty

« back to all changes in this revision

Viewing changes to httpcore-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultListeningIOReactor.java

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2010-06-12 08:37:34 UTC
  • Revision ID: james.westby@ubuntu.com-20100612083734-1y8kp6qm4sjk60az
Tags: upstream-4.0.1
ImportĀ upstreamĀ versionĀ 4.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0.1/httpcore-nio/src/test/java/org/apache/http/impl/nio/reactor/TestDefaultListeningIOReactor.java $
 
3
 * $Revision: 744515 $
 
4
 * $Date: 2009-02-14 17:36:56 +0100 (Sat, 14 Feb 2009) $
 
5
 * ====================================================================
 
6
 * Licensed to the Apache Software Foundation (ASF) under one
 
7
 * or more contributor license agreements.  See the NOTICE file
 
8
 * distributed with this work for additional information
 
9
 * regarding copyright ownership.  The ASF licenses this file
 
10
 * to you under the Apache License, Version 2.0 (the
 
11
 * "License"); you may not use this file except in compliance
 
12
 * with the License.  You may obtain a copy of the License at
 
13
 *
 
14
 *   http://www.apache.org/licenses/LICENSE-2.0
 
15
 *
 
16
 * Unless required by applicable law or agreed to in writing,
 
17
 * software distributed under the License is distributed on an
 
18
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 
19
 * KIND, either express or implied.  See the License for the
 
20
 * specific language governing permissions and limitations
 
21
 * under the License.
 
22
 * ====================================================================
 
23
 *
 
24
 * This software consists of voluntary contributions made by many
 
25
 * individuals on behalf of the Apache Software Foundation.  For more
 
26
 * information on the Apache Software Foundation, please see
 
27
 * <http://www.apache.org/>.
 
28
 *
 
29
 */
 
30
 
 
31
package org.apache.http.impl.nio.reactor;
 
32
 
 
33
import java.io.IOException;
 
34
import java.net.BindException;
 
35
import java.net.InetSocketAddress;
 
36
import java.util.Set;
 
37
import java.util.concurrent.CountDownLatch;
 
38
import java.util.concurrent.TimeUnit;
 
39
 
 
40
import junit.framework.Test;
 
41
import junit.framework.TestCase;
 
42
import junit.framework.TestSuite;
 
43
 
 
44
import org.apache.http.impl.DefaultConnectionReuseStrategy;
 
45
import org.apache.http.impl.DefaultHttpResponseFactory;
 
46
import org.apache.http.impl.nio.DefaultServerIOEventDispatch;
 
47
import org.apache.http.nio.protocol.BufferingHttpServiceHandler;
 
48
import org.apache.http.nio.reactor.IOEventDispatch;
 
49
import org.apache.http.nio.reactor.IOReactorExceptionHandler;
 
50
import org.apache.http.nio.reactor.IOReactorStatus;
 
51
import org.apache.http.nio.reactor.ListenerEndpoint;
 
52
import org.apache.http.nio.reactor.ListeningIOReactor;
 
53
import org.apache.http.params.BasicHttpParams;
 
54
import org.apache.http.params.HttpParams;
 
55
import org.apache.http.protocol.BasicHttpProcessor;
 
56
import org.apache.http.protocol.ResponseConnControl;
 
57
import org.apache.http.protocol.ResponseContent;
 
58
import org.apache.http.protocol.ResponseDate;
 
59
import org.apache.http.protocol.ResponseServer;
 
60
 
 
61
/**
 
62
 * Basic tests for {@link DefaultListeningIOReactor}.
 
63
 *
 
64
 * 
 
65
 * @version $Id: TestDefaultListeningIOReactor.java 744515 2009-02-14 16:36:56Z sebb $
 
66
 */
 
67
public class TestDefaultListeningIOReactor extends TestCase {
 
68
 
 
69
    // ------------------------------------------------------------ Constructor
 
70
    public TestDefaultListeningIOReactor(String testName) {
 
71
        super(testName);
 
72
    }
 
73
 
 
74
    // ------------------------------------------------------------------- Main
 
75
    public static void main(String args[]) {
 
76
        String[] testCaseName = { TestDefaultListeningIOReactor.class.getName() };
 
77
        junit.textui.TestRunner.main(testCaseName);
 
78
    }
 
79
 
 
80
    // ------------------------------------------------------- TestCase Methods
 
81
 
 
82
    public static Test suite() {
 
83
        return new TestSuite(TestDefaultListeningIOReactor.class);
 
84
    }
 
85
    
 
86
    public void testEndpointUpAndDown() throws Exception {
 
87
        
 
88
        HttpParams params = new BasicHttpParams();
 
89
        
 
90
        BasicHttpProcessor httpproc = new BasicHttpProcessor();
 
91
        httpproc.addInterceptor(new ResponseDate());
 
92
        httpproc.addInterceptor(new ResponseServer());
 
93
        httpproc.addInterceptor(new ResponseContent());
 
94
        httpproc.addInterceptor(new ResponseConnControl());
 
95
 
 
96
        final BufferingHttpServiceHandler serviceHandler = new BufferingHttpServiceHandler(
 
97
                httpproc,
 
98
                new DefaultHttpResponseFactory(),
 
99
                new DefaultConnectionReuseStrategy(),
 
100
                params);
 
101
        
 
102
        final IOEventDispatch eventDispatch = new DefaultServerIOEventDispatch(
 
103
                serviceHandler, 
 
104
                params);
 
105
        
 
106
        final ListeningIOReactor ioreactor = new DefaultListeningIOReactor(1, params);
 
107
        
 
108
        Thread t = new Thread(new Runnable() {
 
109
            
 
110
            public void run() {
 
111
                try {
 
112
                    ioreactor.execute(eventDispatch);
 
113
                } catch (IOException ex) {
 
114
                }
 
115
            }
 
116
            
 
117
        });
 
118
        
 
119
        t.start();
 
120
        
 
121
        Set<ListenerEndpoint> endpoints = ioreactor.getEndpoints();
 
122
        assertNotNull(endpoints);
 
123
        assertEquals(0, endpoints.size());
 
124
        
 
125
        ListenerEndpoint port9998 = ioreactor.listen(new InetSocketAddress(9998));
 
126
        port9998.waitFor();
 
127
 
 
128
        ListenerEndpoint port9999 = ioreactor.listen(new InetSocketAddress(9999));
 
129
        port9999.waitFor();
 
130
 
 
131
        endpoints = ioreactor.getEndpoints();
 
132
        assertNotNull(endpoints);
 
133
        assertEquals(2, endpoints.size());
 
134
        
 
135
        port9998.close();
 
136
 
 
137
        endpoints = ioreactor.getEndpoints();
 
138
        assertNotNull(endpoints);
 
139
        assertEquals(1, endpoints.size());
 
140
        
 
141
        ListenerEndpoint endpoint = endpoints.iterator().next();
 
142
        
 
143
        assertEquals(9999, ((InetSocketAddress) endpoint.getAddress()).getPort());
 
144
        
 
145
        ioreactor.shutdown(1000);
 
146
        t.join(1000);
 
147
        
 
148
        assertEquals(IOReactorStatus.SHUT_DOWN, ioreactor.getStatus());
 
149
    }
 
150
 
 
151
    public void testEndpointAlreadyBoundFatal() throws Exception {
 
152
        
 
153
        HttpParams params = new BasicHttpParams();
 
154
        
 
155
        BasicHttpProcessor httpproc = new BasicHttpProcessor();
 
156
        httpproc.addInterceptor(new ResponseDate());
 
157
        httpproc.addInterceptor(new ResponseServer());
 
158
        httpproc.addInterceptor(new ResponseContent());
 
159
        httpproc.addInterceptor(new ResponseConnControl());
 
160
 
 
161
        final BufferingHttpServiceHandler serviceHandler = new BufferingHttpServiceHandler(
 
162
                httpproc,
 
163
                new DefaultHttpResponseFactory(),
 
164
                new DefaultConnectionReuseStrategy(),
 
165
                params);
 
166
        
 
167
        final IOEventDispatch eventDispatch = new DefaultServerIOEventDispatch(
 
168
                serviceHandler, 
 
169
                params);
 
170
        
 
171
        final ListeningIOReactor ioreactor = new DefaultListeningIOReactor(1, params);
 
172
        
 
173
        final CountDownLatch latch = new CountDownLatch(1);
 
174
        
 
175
        Thread t = new Thread(new Runnable() {
 
176
            
 
177
            public void run() {
 
178
                try {
 
179
                    ioreactor.execute(eventDispatch);
 
180
                    fail("IOException should have been thrown");
 
181
                } catch (IOException ex) {
 
182
                    latch.countDown();
 
183
                }
 
184
            }
 
185
            
 
186
        });
 
187
        
 
188
        t.start();
 
189
        
 
190
        ListenerEndpoint endpoint1 = ioreactor.listen(new InetSocketAddress(9999));
 
191
        endpoint1.waitFor();
 
192
 
 
193
        ListenerEndpoint endpoint2 = ioreactor.listen(new InetSocketAddress(9999));
 
194
        endpoint2.waitFor();
 
195
        assertNotNull(endpoint2.getException());
 
196
 
 
197
        // I/O reactor is now expected to be shutting down
 
198
        latch.await(2000, TimeUnit.MILLISECONDS);
 
199
        assertEquals(IOReactorStatus.SHUT_DOWN, ioreactor.getStatus());
 
200
        
 
201
        Set<ListenerEndpoint> endpoints = ioreactor.getEndpoints();
 
202
        assertNotNull(endpoints);
 
203
        assertEquals(0, endpoints.size());
 
204
        
 
205
        ioreactor.shutdown(1000);
 
206
        t.join(1000);
 
207
        
 
208
        assertEquals(IOReactorStatus.SHUT_DOWN, ioreactor.getStatus());
 
209
    }
 
210
    
 
211
    public void testEndpointAlreadyBoundNonFatal() throws Exception {
 
212
        
 
213
        HttpParams params = new BasicHttpParams();
 
214
        
 
215
        BasicHttpProcessor httpproc = new BasicHttpProcessor();
 
216
        httpproc.addInterceptor(new ResponseDate());
 
217
        httpproc.addInterceptor(new ResponseServer());
 
218
        httpproc.addInterceptor(new ResponseContent());
 
219
        httpproc.addInterceptor(new ResponseConnControl());
 
220
 
 
221
        final BufferingHttpServiceHandler serviceHandler = new BufferingHttpServiceHandler(
 
222
                httpproc,
 
223
                new DefaultHttpResponseFactory(),
 
224
                new DefaultConnectionReuseStrategy(),
 
225
                params);
 
226
        
 
227
        final IOEventDispatch eventDispatch = new DefaultServerIOEventDispatch(
 
228
                serviceHandler, 
 
229
                params);
 
230
        
 
231
        final DefaultListeningIOReactor ioreactor = new DefaultListeningIOReactor(1, params);
 
232
        
 
233
        ioreactor.setExceptionHandler(new IOReactorExceptionHandler() {
 
234
 
 
235
            public boolean handle(final IOException ex) {
 
236
                return (ex instanceof BindException);
 
237
            }
 
238
 
 
239
            public boolean handle(final RuntimeException ex) {
 
240
                return false;
 
241
            }
 
242
            
 
243
        });
 
244
        
 
245
        Thread t = new Thread(new Runnable() {
 
246
            
 
247
            public void run() {
 
248
                try {
 
249
                    ioreactor.execute(eventDispatch);
 
250
                } catch (IOException ex) {
 
251
                }
 
252
            }
 
253
            
 
254
        });
 
255
        
 
256
        t.start();
 
257
        
 
258
        ListenerEndpoint endpoint1 = ioreactor.listen(new InetSocketAddress(9999));
 
259
        endpoint1.waitFor();
 
260
 
 
261
        ListenerEndpoint endpoint2 = ioreactor.listen(new InetSocketAddress(9999));
 
262
        endpoint2.waitFor();
 
263
        assertNotNull(endpoint2.getException());
 
264
 
 
265
        // Sleep a little to make sure the I/O reactor is not shutting down
 
266
        Thread.sleep(500);
 
267
        
 
268
        assertEquals(IOReactorStatus.ACTIVE, ioreactor.getStatus());
 
269
        
 
270
        ioreactor.shutdown(1000);
 
271
        t.join(1000);
 
272
        
 
273
        assertEquals(IOReactorStatus.SHUT_DOWN, ioreactor.getStatus());
 
274
    }
 
275
 
 
276
}