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 $
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
14
* http://www.apache.org/licenses/LICENSE-2.0
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
22
* ====================================================================
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/>.
31
package org.apache.http.impl.nio.reactor;
33
import java.io.IOException;
34
import java.net.BindException;
35
import java.net.InetSocketAddress;
37
import java.util.concurrent.CountDownLatch;
38
import java.util.concurrent.TimeUnit;
40
import junit.framework.Test;
41
import junit.framework.TestCase;
42
import junit.framework.TestSuite;
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;
62
* Basic tests for {@link DefaultListeningIOReactor}.
65
* @version $Id: TestDefaultListeningIOReactor.java 744515 2009-02-14 16:36:56Z sebb $
67
public class TestDefaultListeningIOReactor extends TestCase {
69
// ------------------------------------------------------------ Constructor
70
public TestDefaultListeningIOReactor(String testName) {
74
// ------------------------------------------------------------------- Main
75
public static void main(String args[]) {
76
String[] testCaseName = { TestDefaultListeningIOReactor.class.getName() };
77
junit.textui.TestRunner.main(testCaseName);
80
// ------------------------------------------------------- TestCase Methods
82
public static Test suite() {
83
return new TestSuite(TestDefaultListeningIOReactor.class);
86
public void testEndpointUpAndDown() throws Exception {
88
HttpParams params = new BasicHttpParams();
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());
96
final BufferingHttpServiceHandler serviceHandler = new BufferingHttpServiceHandler(
98
new DefaultHttpResponseFactory(),
99
new DefaultConnectionReuseStrategy(),
102
final IOEventDispatch eventDispatch = new DefaultServerIOEventDispatch(
106
final ListeningIOReactor ioreactor = new DefaultListeningIOReactor(1, params);
108
Thread t = new Thread(new Runnable() {
112
ioreactor.execute(eventDispatch);
113
} catch (IOException ex) {
121
Set<ListenerEndpoint> endpoints = ioreactor.getEndpoints();
122
assertNotNull(endpoints);
123
assertEquals(0, endpoints.size());
125
ListenerEndpoint port9998 = ioreactor.listen(new InetSocketAddress(9998));
128
ListenerEndpoint port9999 = ioreactor.listen(new InetSocketAddress(9999));
131
endpoints = ioreactor.getEndpoints();
132
assertNotNull(endpoints);
133
assertEquals(2, endpoints.size());
137
endpoints = ioreactor.getEndpoints();
138
assertNotNull(endpoints);
139
assertEquals(1, endpoints.size());
141
ListenerEndpoint endpoint = endpoints.iterator().next();
143
assertEquals(9999, ((InetSocketAddress) endpoint.getAddress()).getPort());
145
ioreactor.shutdown(1000);
148
assertEquals(IOReactorStatus.SHUT_DOWN, ioreactor.getStatus());
151
public void testEndpointAlreadyBoundFatal() throws Exception {
153
HttpParams params = new BasicHttpParams();
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());
161
final BufferingHttpServiceHandler serviceHandler = new BufferingHttpServiceHandler(
163
new DefaultHttpResponseFactory(),
164
new DefaultConnectionReuseStrategy(),
167
final IOEventDispatch eventDispatch = new DefaultServerIOEventDispatch(
171
final ListeningIOReactor ioreactor = new DefaultListeningIOReactor(1, params);
173
final CountDownLatch latch = new CountDownLatch(1);
175
Thread t = new Thread(new Runnable() {
179
ioreactor.execute(eventDispatch);
180
fail("IOException should have been thrown");
181
} catch (IOException ex) {
190
ListenerEndpoint endpoint1 = ioreactor.listen(new InetSocketAddress(9999));
193
ListenerEndpoint endpoint2 = ioreactor.listen(new InetSocketAddress(9999));
195
assertNotNull(endpoint2.getException());
197
// I/O reactor is now expected to be shutting down
198
latch.await(2000, TimeUnit.MILLISECONDS);
199
assertEquals(IOReactorStatus.SHUT_DOWN, ioreactor.getStatus());
201
Set<ListenerEndpoint> endpoints = ioreactor.getEndpoints();
202
assertNotNull(endpoints);
203
assertEquals(0, endpoints.size());
205
ioreactor.shutdown(1000);
208
assertEquals(IOReactorStatus.SHUT_DOWN, ioreactor.getStatus());
211
public void testEndpointAlreadyBoundNonFatal() throws Exception {
213
HttpParams params = new BasicHttpParams();
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());
221
final BufferingHttpServiceHandler serviceHandler = new BufferingHttpServiceHandler(
223
new DefaultHttpResponseFactory(),
224
new DefaultConnectionReuseStrategy(),
227
final IOEventDispatch eventDispatch = new DefaultServerIOEventDispatch(
231
final DefaultListeningIOReactor ioreactor = new DefaultListeningIOReactor(1, params);
233
ioreactor.setExceptionHandler(new IOReactorExceptionHandler() {
235
public boolean handle(final IOException ex) {
236
return (ex instanceof BindException);
239
public boolean handle(final RuntimeException ex) {
245
Thread t = new Thread(new Runnable() {
249
ioreactor.execute(eventDispatch);
250
} catch (IOException ex) {
258
ListenerEndpoint endpoint1 = ioreactor.listen(new InetSocketAddress(9999));
261
ListenerEndpoint endpoint2 = ioreactor.listen(new InetSocketAddress(9999));
263
assertNotNull(endpoint2.getException());
265
// Sleep a little to make sure the I/O reactor is not shutting down
268
assertEquals(IOReactorStatus.ACTIVE, ioreactor.getStatus());
270
ioreactor.shutdown(1000);
273
assertEquals(IOReactorStatus.SHUT_DOWN, ioreactor.getStatus());