1 20 package org.apache.mina.transport.socket.nio; 21 22 import java.net.InetSocketAddress ; 23 24 import junit.framework.Assert; 25 import junit.framework.TestCase; 26 27 import org.apache.mina.common.ByteBuffer; 28 import org.apache.mina.common.ConnectFuture; 29 import org.apache.mina.common.ExpiringSessionRecycler; 30 import org.apache.mina.common.IdleStatus; 31 import org.apache.mina.common.IoAcceptor; 32 import org.apache.mina.common.IoConnector; 33 import org.apache.mina.common.IoHandlerAdapter; 34 import org.apache.mina.common.IoSession; 35 import org.apache.mina.util.AvailablePortFinder; 36 37 43 public class DatagramRecyclerTest extends TestCase { 44 private final IoAcceptor acceptor = new DatagramAcceptor(); 45 46 private final IoConnector connector = new DatagramConnector(); 47 48 public DatagramRecyclerTest() { 49 } 50 51 public void testDatagramRecycler() throws Exception { 52 int port = AvailablePortFinder.getNextAvailable(1024); 53 DatagramAcceptorConfig config = new DatagramAcceptorConfig(); 54 ExpiringSessionRecycler recycler = new ExpiringSessionRecycler(1, 1); 55 config.setSessionRecycler(recycler); 56 57 MockHandler acceptorHandler = new MockHandler(); 58 MockHandler connectorHandler = new MockHandler(); 59 60 acceptor.bind(new InetSocketAddress (port), acceptorHandler, config); 61 62 try { 63 ConnectFuture future = connector.connect(new InetSocketAddress ( 64 "localhost", port), connectorHandler, config); 65 future.join(); 66 67 future.getSession().write(ByteBuffer.allocate(1)).join(); 69 70 future.getSession().getCloseFuture().join(3000); 72 Assert.assertTrue(future.getSession().getCloseFuture().isClosed()); 73 acceptorHandler.session.getCloseFuture().join(3000); 74 Assert.assertTrue(acceptorHandler.session.getCloseFuture() 75 .isClosed()); 76 77 Thread.sleep(1000); 78 79 Assert.assertEquals("CROPSECL", connectorHandler.result); 80 Assert.assertEquals("CROPRECL", acceptorHandler.result); 81 } finally { 82 acceptor.unbind(new InetSocketAddress (port)); 83 } 84 } 85 86 private class MockHandler extends IoHandlerAdapter { 87 public IoSession session; 88 89 public String result = ""; 90 91 public void exceptionCaught(IoSession session, Throwable cause) 92 throws Exception { 93 this.session = session; 94 result += "CA"; 95 } 96 97 public void messageReceived(IoSession session, Object message) 98 throws Exception { 99 this.session = session; 100 result += "RE"; 101 } 102 103 public void messageSent(IoSession session, Object message) 104 throws Exception { 105 this.session = session; 106 result += "SE"; 107 } 108 109 public void sessionClosed(IoSession session) throws Exception { 110 this.session = session; 111 result += "CL"; 112 } 113 114 public void sessionCreated(IoSession session) throws Exception { 115 this.session = session; 116 result += "CR"; 117 } 118 119 public void sessionIdle(IoSession session, IdleStatus status) 120 throws Exception { 121 this.session = session; 122 result += "ID"; 123 } 124 125 public void sessionOpened(IoSession session) throws Exception { 126 this.session = session; 127 result += "OP"; 128 } 129 130 } 131 } 132 | Popular Tags |