1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  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,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.mina.transport.socket.nio;
21  
22  import java.net.InetSocketAddress;
23  import java.net.SocketAddress;
24  import java.util.Collection;
25  
26  import junit.framework.Assert;
27  
28  import org.apache.mina.common.ConnectFuture;
29  import org.apache.mina.common.IoConnector;
30  import org.apache.mina.common.IoHandlerAdapter;
31  import org.apache.mina.common.IoSession;
32  import org.apache.mina.transport.AbstractBindTest;
33  
34  /**
35   * Tests {@link SocketAcceptor} resource leakage.
36   * 
37   * @author The Apache Directory Project (mina-dev@directory.apache.org)
38   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (Fri, 13 Jul 2007) $ 
39   */
40  public class SocketBindTest extends AbstractBindTest {
41  
42      public SocketBindTest() {
43          super(new SocketAcceptor());
44      }
45  
46      protected SocketAddress createSocketAddress(int port) {
47          return new InetSocketAddress(port);
48      }
49  
50      protected int getPort(SocketAddress address) {
51          return ((InetSocketAddress) address).getPort();
52      }
53  
54      public void testUnbindDisconnectsClients() throws Exception {
55          // TODO: This test is almost identical to the test with the same name in VmPipeBindTest
56          bind(false);
57  
58          SocketAddress addr = createSocketAddress(port);
59  
60          IoConnector connector = new SocketConnector();
61          IoSession[] sessions = new IoSession[5];
62          for (int i = 0; i < sessions.length; i++) {
63              ConnectFuture future = connector.connect(new InetSocketAddress(
64                      "localhost", port), new IoHandlerAdapter());
65              future.join();
66              sessions[i] = future.getSession();
67              Assert.assertTrue(sessions[i].isConnected());
68          }
69  
70          // Wait for the server side sessions to be created.
71          Thread.sleep(500);
72  
73          Collection managedSessions = acceptor.getManagedSessions(addr);
74          Assert.assertEquals(5, managedSessions.size());
75  
76          acceptor.unbind(addr);
77  
78          // Wait for the client side sessions to close.
79          Thread.sleep(500);
80  
81          for (int i = 0; i < sessions.length; i++) {
82              Assert.assertFalse(sessions[i].isConnected());
83          }
84      }
85  }