1 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 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 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 Thread.sleep(500); 72 73 Collection managedSessions = acceptor.getManagedSessions(addr); 74 Assert.assertEquals(5, managedSessions.size()); 75 76 acceptor.unbind(addr); 77 78 Thread.sleep(500); 80 81 for (int i = 0; i < sessions.length; i++) { 82 Assert.assertFalse(sessions[i].isConnected()); 83 } 84 } 85 } 86 | Popular Tags |