1 20 package org.apache.mina.transport.vmpipe; 21 22 import junit.framework.Assert; 23 import junit.framework.TestCase; 24 25 import org.apache.mina.common.ConnectFuture; 26 import org.apache.mina.common.IoAcceptor; 27 import org.apache.mina.common.IoConnector; 28 import org.apache.mina.common.IoHandlerAdapter; 29 import org.apache.mina.common.IoSession; 30 import org.apache.mina.common.ThreadModel; 31 32 38 public class VmPipeEventOrderTest extends TestCase { 39 public void testServerToClient() throws Exception { 40 IoAcceptor acceptor = new VmPipeAcceptor(); 41 acceptor.getDefaultConfig().setThreadModel(ThreadModel.MANUAL); 42 44 IoConnector connector = new VmPipeConnector(); 45 connector.getDefaultConfig().setThreadModel(ThreadModel.MANUAL); 46 48 acceptor.bind(new VmPipeAddress(1), new IoHandlerAdapter() { 49 public void sessionOpened(IoSession session) throws Exception { 50 session.write("B"); 51 } 52 53 public void messageSent(IoSession session, Object message) 54 throws Exception { 55 session.close(); 56 } 57 }); 58 59 final StringBuffer actual = new StringBuffer (); 60 61 ConnectFuture future = connector.connect(new VmPipeAddress(1), 62 new IoHandlerAdapter() { 63 64 public void messageReceived(IoSession session, 65 Object message) throws Exception { 66 actual.append(message); 67 } 68 69 public void sessionClosed(IoSession session) 70 throws Exception { 71 actual.append("C"); 72 } 73 74 public void sessionOpened(IoSession session) 75 throws Exception { 76 actual.append("A"); 77 } 78 79 }); 80 81 future.join(); 82 future.getSession().getCloseFuture().join(); 83 acceptor.unbindAll(); 84 85 while (actual.indexOf("C") < 0) { 88 Thread.yield(); 89 } 90 91 Assert.assertEquals("ABC", actual.toString()); 92 } 93 94 public void testClientToServer() throws Exception { 95 IoAcceptor acceptor = new VmPipeAcceptor(); 96 acceptor.getDefaultConfig().setThreadModel(ThreadModel.MANUAL); 97 99 IoConnector connector = new VmPipeConnector(); 100 connector.getDefaultConfig().setThreadModel(ThreadModel.MANUAL); 101 103 final StringBuffer actual = new StringBuffer (); 104 105 acceptor.bind(new VmPipeAddress(1), new IoHandlerAdapter() { 106 107 public void messageReceived(IoSession session, Object message) 108 throws Exception { 109 actual.append(message); 110 } 111 112 public void sessionClosed(IoSession session) throws Exception { 113 actual.append("C"); 114 } 115 116 public void sessionOpened(IoSession session) throws Exception { 117 actual.append("A"); 118 } 119 120 }); 121 122 ConnectFuture future = connector.connect(new VmPipeAddress(1), 123 new IoHandlerAdapter() { 124 public void sessionOpened(IoSession session) 125 throws Exception { 126 session.write("B"); 127 } 128 129 public void messageSent(IoSession session, Object message) 130 throws Exception { 131 session.close(); 132 } 133 }); 134 135 future.join(); 136 future.getSession().getCloseFuture().join(); 137 acceptor.unbindAll(); 138 139 while (actual.indexOf("C") < 0) { 142 Thread.yield(); 143 } 144 145 Assert.assertEquals("ABC", actual.toString()); 146 } 147 } 148 | Popular Tags |