1 5 package com.tc.net.protocol.tcm; 6 7 import com.tc.bytes.TCByteBuffer; 8 import com.tc.config.schema.dynamic.FixedValueConfigItem; 9 import com.tc.net.TCSocketAddress; 10 import com.tc.net.core.ConnectionInfo; 11 import com.tc.net.protocol.NetworkLayer; 12 import com.tc.net.protocol.NetworkStackID; 13 import com.tc.net.protocol.PlainNetworkStackHarnessFactory; 14 import com.tc.net.protocol.TCNetworkMessage; 15 import com.tc.net.protocol.transport.DefaultConnectionIdFactory; 16 import com.tc.net.protocol.transport.NullConnectionPolicy; 17 import com.tc.object.session.NullSessionManager; 18 19 import java.util.HashSet ; 20 import java.util.Set ; 21 22 import junit.framework.TestCase; 23 24 public class ChannelManagerTest extends TestCase { 25 26 MessageMonitor monitor = new NullMessageMonitor(); 27 final NullSessionManager sessionManager = new NullSessionManager(); 28 final TCMessageFactory msgFactory = new TCMessageFactoryImpl(sessionManager, monitor); 29 final TCMessageRouter msgRouter = new TCMessageRouterImpl(); 30 31 final ServerMessageChannelFactory channelFactory = new ServerMessageChannelFactory() { 32 public MessageChannelInternal createNewChannel(ChannelID id) { 33 return new ServerMessageChannelImpl(id, msgRouter, msgFactory); 34 } 35 }; 36 37 public void testEvents() { 38 Events events = new Events(); 39 40 ChannelManagerImpl channelManager = new ChannelManagerImpl(false, channelFactory); 41 42 channelManager.createNewChannel(new ChannelID(1)); 44 45 channelManager.addEventListener(events); 46 assertEquals(0, events.channels.size()); 47 MessageChannelInternal c1 = channelManager.createNewChannel(new ChannelID(2)); 48 channelManager.notifyChannelEvent(new ChannelEventImpl(ChannelEventType.TRANSPORT_CONNECTED_EVENT, c1)); 49 c1.setSendLayer(new NullNetworkLayer()); 50 assertEquals(1, events.channels.size()); 51 assertTrue(events.channels.contains(c1)); 52 c1.close(); 53 assertEquals(0, events.channels.size()); 54 55 try { 56 channelManager.addEventListener(null); 57 fail(); 58 } catch (IllegalArgumentException iae) { 59 } 61 } 62 63 public void testCreateChannel() throws Exception { 64 ChannelManagerImpl channelManager = new ChannelManagerImpl(false, channelFactory); 65 66 int channelCount = 0; 67 long sequence = 1; 68 69 MessageChannelInternal channel1 = channelManager.createNewChannel(new ChannelID(sequence++)); 70 channel1.setSendLayer(new NullNetworkLayer()); 71 assertEquals(++channelCount, channelManager.getChannels().length); 72 assertTrue(channel1.isOpen()); 73 74 MessageChannelInternal channel2 = channelManager.createNewChannel(new ChannelID(sequence++)); 75 channel2.setSendLayer(new NullNetworkLayer()); 76 assertEquals(++channelCount, channelManager.getChannels().length); 77 assertTrue(channel2.isOpen()); 78 79 MessageChannelInternal channel3 = channelManager.createNewChannel(new ChannelID(sequence++)); 80 channel3.setSendLayer(new NullNetworkLayer()); 81 assertEquals(++channelCount, channelManager.getChannels().length); 82 assertTrue(channel3.isOpen()); 83 84 channel1.notifyTransportDisconnected(null); 88 assertEquals(channelCount, channelManager.getChannels().length); 89 assertFalse(channel1.isClosed()); 90 91 channelManager.notifyChannelEvent(new ChannelEventImpl(ChannelEventType.TRANSPORT_DISCONNECTED_EVENT, channel2)); 95 assertEquals(channelCount, channelManager.getChannels().length); 96 assertFalse(channel2.isClosed()); 97 98 channel3.close(); 101 assertEquals(--channelCount, channelManager.getChannels().length); 102 assertTrue(channel3.isClosed()); 103 } 104 105 public void testTransportDisconnectRemovesChannel() throws Exception { 106 CommunicationsManager clientComms = new CommunicationsManagerImpl(monitor, new PlainNetworkStackHarnessFactory(), 107 new NullConnectionPolicy()); 108 CommunicationsManager serverComms = new CommunicationsManagerImpl(monitor, new PlainNetworkStackHarnessFactory(), 109 new NullConnectionPolicy()); 110 try { 111 NetworkListener lsnr = serverComms.createListener(sessionManager, 112 new TCSocketAddress(TCSocketAddress.LOOPBACK_ADDR, 0), true, 113 new DefaultConnectionIdFactory()); 114 lsnr.start(new HashSet ()); 115 ChannelManager channelManager = lsnr.getChannelManager(); 116 assertEquals(0, channelManager.getChannels().length); 117 118 ClientMessageChannel channel; 119 channel = clientComms 120 .createClientChannel(sessionManager, 0, TCSocketAddress.LOOPBACK_IP, lsnr.getBindPort(), 3000, 121 new FixedValueConfigItem(new ConnectionInfo[] { new ConnectionInfo("localhost", lsnr 122 .getBindPort()) })); 123 channel.open(); 124 assertTrue(channel.isConnected()); 125 126 assertEquals(1, channelManager.getChannels().length); 127 clientComms.getConnectionManager().closeAllConnections(5000); 128 assertFalse(channel.isConnected()); 129 130 for (int i = 0; i < 30; i++) { 131 if (channelManager.getChannels().length == 0) { 132 break; 133 } 134 Thread.sleep(100); 135 } 136 137 assertEquals(0, channelManager.getChannels().length); 138 } finally { 139 try { 140 clientComms.shutdown(); 141 } finally { 142 serverComms.shutdown(); 143 } 144 } 145 } 146 147 static class Events implements ChannelManagerEventListener { 148 Set channels = new HashSet (); 149 150 public void channelCreated(MessageChannel channel) { 151 channels.add(channel); 152 } 153 154 public void channelRemoved(MessageChannel channel) { 155 channels.remove(channel); 156 } 157 } 158 159 static class NullNetworkLayer implements NetworkLayer { 160 public void setSendLayer(NetworkLayer layer) { 161 return; 162 } 163 164 public void setReceiveLayer(NetworkLayer layer) { 165 return; 166 } 167 168 public void send(TCNetworkMessage message) { 169 return; 170 } 171 172 public void receive(TCByteBuffer[] msgData) { 173 return; 174 } 175 176 public boolean isConnected() { 177 return false; 178 } 179 180 public NetworkStackID open() { 181 return null; 182 } 183 184 public void close() { 185 return; 186 } 187 } 188 189 } 190 | Popular Tags |