1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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: 332218 $, $Date: 2005-11-10 12:52:42 +0900 $
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
49 boolean socketBound = false;
50
51
52
53
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
70 if( !socketBound )
71 {
72 throw new IOException( "Cannot bind any test port." );
73 }
74
75
76 }
77
78 public void tearDown()
79 {
80 try
81 {
82 acceptor.unbind( new InetSocketAddress( port ) );
83 }
84 catch( Exception e )
85 {
86
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
105 acceptor.unbind( new InetSocketAddress( port ) );
106
107 try
108 {
109
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 }