001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.net.protocol.tcm;
006:
007: import com.tc.bytes.TCByteBuffer;
008: import com.tc.net.TCSocketAddress;
009: import com.tc.net.core.ConnectionAddressProvider;
010: import com.tc.net.core.ConnectionInfo;
011: import com.tc.net.protocol.NetworkLayer;
012: import com.tc.net.protocol.NetworkStackID;
013: import com.tc.net.protocol.PlainNetworkStackHarnessFactory;
014: import com.tc.net.protocol.TCNetworkMessage;
015: import com.tc.net.protocol.transport.DefaultConnectionIdFactory;
016: import com.tc.net.protocol.transport.NullConnectionPolicy;
017: import com.tc.object.session.NullSessionManager;
018:
019: import java.util.HashSet;
020: import java.util.Set;
021:
022: import junit.framework.TestCase;
023:
024: public class ChannelManagerTest extends TestCase {
025:
026: MessageMonitor monitor = new NullMessageMonitor();
027: final NullSessionManager sessionManager = new NullSessionManager();
028: final TCMessageFactory msgFactory = new TCMessageFactoryImpl(
029: sessionManager, monitor);
030: final TCMessageRouter msgRouter = new TCMessageRouterImpl();
031:
032: final ServerMessageChannelFactory channelFactory = new ServerMessageChannelFactory() {
033: public MessageChannelInternal createNewChannel(ChannelID id) {
034: return new ServerMessageChannelImpl(id, msgRouter,
035: msgFactory);
036: }
037: };
038:
039: public void testEvents() {
040: Events events = new Events();
041:
042: ChannelManagerImpl channelManager = new ChannelManagerImpl(
043: false, channelFactory);
044:
045: // make sure things work even w/o an event listener attached
046: channelManager.createNewChannel(new ChannelID(1));
047:
048: channelManager.addEventListener(events);
049: assertEquals(0, events.channels.size());
050: MessageChannelInternal c1 = channelManager
051: .createNewChannel(new ChannelID(2));
052: channelManager.notifyChannelEvent(new ChannelEventImpl(
053: ChannelEventType.TRANSPORT_CONNECTED_EVENT, c1));
054: c1.setSendLayer(new NullNetworkLayer());
055: assertEquals(1, events.channels.size());
056: assertTrue(events.channels.contains(c1));
057: c1.close();
058: assertEquals(0, events.channels.size());
059:
060: try {
061: channelManager.addEventListener(null);
062: fail();
063: } catch (IllegalArgumentException iae) {
064: // expected
065: }
066: }
067:
068: public void testCreateChannel() throws Exception {
069: ChannelManagerImpl channelManager = new ChannelManagerImpl(
070: false, channelFactory);
071:
072: int channelCount = 0;
073: long sequence = 1;
074:
075: MessageChannelInternal channel1 = channelManager
076: .createNewChannel(new ChannelID(sequence++));
077: channel1.setSendLayer(new NullNetworkLayer());
078: assertEquals(++channelCount,
079: channelManager.getChannels().length);
080: assertTrue(channel1.isOpen());
081:
082: MessageChannelInternal channel2 = channelManager
083: .createNewChannel(new ChannelID(sequence++));
084: channel2.setSendLayer(new NullNetworkLayer());
085: assertEquals(++channelCount,
086: channelManager.getChannels().length);
087: assertTrue(channel2.isOpen());
088:
089: MessageChannelInternal channel3 = channelManager
090: .createNewChannel(new ChannelID(sequence++));
091: channel3.setSendLayer(new NullNetworkLayer());
092: assertEquals(++channelCount,
093: channelManager.getChannels().length);
094: assertTrue(channel3.isOpen());
095:
096: // Make sure that sending a transport disconnected event to the channel does
097: // NOT remove the channel from the
098: // channel manager.
099: channel1.notifyTransportDisconnected(null);
100: assertEquals(channelCount, channelManager.getChannels().length);
101: assertFalse(channel1.isClosed());
102:
103: // this is the same as the test just above, but more explicitly excercising
104: // the ChannelManager event notification
105: // interface.
106: channelManager
107: .notifyChannelEvent(new ChannelEventImpl(
108: ChannelEventType.TRANSPORT_DISCONNECTED_EVENT,
109: channel2));
110: assertEquals(channelCount, channelManager.getChannels().length);
111: assertFalse(channel2.isClosed());
112:
113: // Make sure that closing the channel causes it to be removed from the
114: // channel manager.
115: channel3.close();
116: assertEquals(--channelCount,
117: channelManager.getChannels().length);
118: assertTrue(channel3.isClosed());
119: }
120:
121: public void testTransportDisconnectRemovesChannel()
122: throws Exception {
123: CommunicationsManager clientComms = new CommunicationsManagerImpl(
124: monitor, new PlainNetworkStackHarnessFactory(),
125: new NullConnectionPolicy());
126: CommunicationsManager serverComms = new CommunicationsManagerImpl(
127: monitor, new PlainNetworkStackHarnessFactory(),
128: new NullConnectionPolicy());
129: try {
130: NetworkListener lsnr = serverComms.createListener(
131: sessionManager, new TCSocketAddress(
132: TCSocketAddress.LOOPBACK_ADDR, 0), true,
133: new DefaultConnectionIdFactory());
134: lsnr.start(new HashSet());
135: ChannelManager channelManager = lsnr.getChannelManager();
136: assertEquals(0, channelManager.getChannels().length);
137:
138: ClientMessageChannel channel;
139: channel = clientComms
140: .createClientChannel(
141: sessionManager,
142: 0,
143: TCSocketAddress.LOOPBACK_IP,
144: lsnr.getBindPort(),
145: 3000,
146: new ConnectionAddressProvider(
147: new ConnectionInfo[] { new ConnectionInfo(
148: "localhost", lsnr
149: .getBindPort()) }));
150: channel.open();
151: assertTrue(channel.isConnected());
152:
153: assertEquals(1, channelManager.getChannels().length);
154: clientComms.getConnectionManager()
155: .closeAllConnections(5000);
156: assertFalse(channel.isConnected());
157:
158: for (int i = 0; i < 30; i++) {
159: if (channelManager.getChannels().length == 0) {
160: break;
161: }
162: Thread.sleep(100);
163: }
164:
165: assertEquals(0, channelManager.getChannels().length);
166: } finally {
167: try {
168: clientComms.shutdown();
169: } finally {
170: serverComms.shutdown();
171: }
172: }
173: }
174:
175: static class Events implements ChannelManagerEventListener {
176: Set channels = new HashSet();
177:
178: public void channelCreated(MessageChannel channel) {
179: channels.add(channel);
180: }
181:
182: public void channelRemoved(MessageChannel channel) {
183: channels.remove(channel);
184: }
185: }
186:
187: static class NullNetworkLayer implements NetworkLayer {
188: public void setSendLayer(NetworkLayer layer) {
189: return;
190: }
191:
192: public void setReceiveLayer(NetworkLayer layer) {
193: return;
194: }
195:
196: public void send(TCNetworkMessage message) {
197: return;
198: }
199:
200: public void receive(TCByteBuffer[] msgData) {
201: return;
202: }
203:
204: public boolean isConnected() {
205: return false;
206: }
207:
208: public NetworkStackID open() {
209: return null;
210: }
211:
212: public void close() {
213: return;
214: }
215: }
216:
217: }
|