1 | /* |
2 | * @(#) $Id: AbstractBindTest.java 357871 2005-12-20 01:56:40Z trustin $ |
3 | * |
4 | * Copyright 2004 The Apache Software Foundation |
5 | * |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
17 | * |
18 | */ |
19 | package org.apache.mina.io; |
20 | |
21 | import java.io.IOException; |
22 | import java.net.InetSocketAddress; |
23 | import java.util.Date; |
24 | |
25 | import junit.framework.Assert; |
26 | import junit.framework.TestCase; |
27 | |
28 | import org.apache.mina.examples.echoserver.EchoProtocolHandler; |
29 | |
30 | /** |
31 | * Tests {@link IoAcceptor} resource leakage by repeating bind and unbind. |
32 | * |
33 | * @author The Apache Directory Project (dev@directory.apache.org) |
34 | * @version $Rev: 357871 $, $Date: 2005-12-20 10:56:40 +0900 (Tue, 20 Dec 2005) $ |
35 | */ |
36 | public class AbstractBindTest extends TestCase |
37 | { |
38 | protected final IoAcceptor acceptor; |
39 | protected int port; |
40 | |
41 | public AbstractBindTest( IoAcceptor acceptor ) |
42 | { |
43 | this.acceptor = acceptor; |
44 | } |
45 | |
46 | public void setUp() throws IOException |
47 | { |
48 | // Find an availble test port and bind to it. |
49 | boolean socketBound = false; |
50 | |
51 | // Let's start from port #1 to detect possible resource leak |
52 | // because test will fail in port 1-1023 if user run this test |
53 | // as a normal user. |
54 | for( port = 1; port <= 65535; port ++ ) |
55 | { |
56 | socketBound = false; |
57 | try |
58 | { |
59 | acceptor.bind( new InetSocketAddress( port ), |
60 | new EchoProtocolHandler() ); |
61 | socketBound = true; |
62 | break; |
63 | } |
64 | catch( IOException e ) |
65 | { |
66 | } |
67 | } |
68 | |
69 | // If there is no port available, test fails. |
70 | if( !socketBound ) |
71 | { |
72 | throw new IOException( "Cannot bind any test port." ); |
73 | } |
74 | |
75 | //System.out.println( "Using port " + port + " for testing." ); |
76 | } |
77 | |
78 | public void tearDown() |
79 | { |
80 | try |
81 | { |
82 | acceptor.unbind( new InetSocketAddress( port ) ); |
83 | } |
84 | catch( Exception e ) |
85 | { |
86 | // ignore |
87 | } |
88 | } |
89 | |
90 | public void testDuplicateBind() |
91 | { |
92 | try |
93 | { |
94 | acceptor.bind( new InetSocketAddress( port ), new EchoProtocolHandler() ); |
95 | Assert.fail( "IOException is not thrown" ); |
96 | } |
97 | catch( IOException e ) |
98 | { |
99 | } |
100 | } |
101 | |
102 | public void testDuplicateUnbind() |
103 | { |
104 | // this should succeed |
105 | acceptor.unbind( new InetSocketAddress( port ) ); |
106 | |
107 | try |
108 | { |
109 | // this should fail |
110 | acceptor.unbind( new InetSocketAddress( port ) ); |
111 | Assert.fail( "Exception is not thrown" ); |
112 | } |
113 | catch( Exception e ) |
114 | { |
115 | } |
116 | } |
117 | |
118 | public void testManyTimes() throws IOException |
119 | { |
120 | InetSocketAddress addr = new InetSocketAddress( port ); |
121 | EchoProtocolHandler handler = new EchoProtocolHandler(); |
122 | for( int i = 0; i < 1024; i++ ) |
123 | { |
124 | acceptor.unbind( addr ); |
125 | acceptor.bind( addr, handler ); |
126 | } |
127 | } |
128 | |
129 | public void _testRegressively() throws IOException |
130 | { |
131 | tearDown(); |
132 | |
133 | InetSocketAddress addr = new InetSocketAddress( port ); |
134 | EchoProtocolHandler handler = new EchoProtocolHandler(); |
135 | for( int i = 0; i < 1048576; i++ ) |
136 | { |
137 | acceptor.bind( addr, handler ); |
138 | testDuplicateBind(); |
139 | testDuplicateUnbind(); |
140 | if( i % 100 == 0 ) |
141 | { |
142 | System.out.println( i + " (" + new Date() + ")" ); |
143 | } |
144 | } |
145 | setUp(); |
146 | } |
147 | |
148 | } |