1 5 package com.tc.net.protocol.transport; 6 7 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue; 8 import EDU.oswego.cs.dl.util.concurrent.SynchronizedRef; 9 10 import com.tc.exception.ImplementMe; 11 import com.tc.net.TCSocketAddress; 12 import com.tc.net.core.ConnectionInfo; 13 import com.tc.net.core.MockConnectionManager; 14 import com.tc.net.core.MockTCConnection; 15 import com.tc.net.core.TCConnection; 16 import com.tc.net.core.event.TCConnectionEvent; 17 import com.tc.net.protocol.PlainNetworkStackHarnessFactory; 18 import com.tc.net.protocol.tcm.CommunicationsManager; 19 import com.tc.net.protocol.tcm.CommunicationsManagerImpl; 20 import com.tc.net.protocol.tcm.NetworkListener; 21 import com.tc.net.protocol.tcm.NullMessageMonitor; 22 import com.tc.object.session.NullSessionManager; 23 import com.tc.test.TCTestCase; 24 25 import java.util.Collections ; 26 import java.util.List ; 27 28 31 public class ClientMessageTransportTest extends TCTestCase { 32 private ConnectionID connectionId; 33 private ClientMessageTransport transport; 34 private MockConnectionManager connectionManager; 35 private MockTCConnection connection; 36 private TransportHandshakeMessageFactory transportMessageFactory; 37 private TransportHandshakeErrorHandler handshakeErrorHandler; 38 private int maxRetries = 10; 39 40 public void setUp() { 41 DefaultConnectionIdFactory connectionIDProvider = new DefaultConnectionIdFactory(); 42 this.connectionId = connectionIDProvider.nextConnectionId(); 43 this.connectionManager = new MockConnectionManager(); 44 this.connection = new MockTCConnection(); 45 this.connectionManager.setConnection(connection); 46 this.transportMessageFactory = new TransportHandshakeMessageFactoryImpl(); 47 handshakeErrorHandler = new TransportHandshakeErrorHandler() { 48 49 public void handleHandshakeError(TransportHandshakeErrorContext e) { 50 throw new ImplementMe(); 51 52 } 53 54 public void handleHandshakeError(TransportHandshakeErrorContext e, TransportHandshakeMessage m) { 55 throw new ImplementMe(); 56 57 } 58 59 }; 60 transport = new ClientMessageTransport(maxRetries, new ConnectionInfo("", 0), 5000, this.connectionManager, 61 handshakeErrorHandler, this.transportMessageFactory, 62 new WireProtocolAdaptorFactoryImpl()); 63 } 64 65 public void testRoundRobinReconnect() throws Exception { 66 SynchronizedRef errorRef = new SynchronizedRef(null); 67 ClientHandshakeMessageResponder tester = new ClientHandshakeMessageResponder(new LinkedQueue(), new LinkedQueue(), 68 this.transportMessageFactory, 69 this.connectionId, this.transport, 70 errorRef); 71 this.connection.setMessageSink(tester); 72 73 transport.open(); 74 while (!connection.connectCalls.isEmpty()) { 75 connection.connectCalls.take(); 76 } 77 78 connection.fail = true; 79 transport.closeEvent(new TCConnectionEvent() { 80 public TCConnection getSource() { 81 return connection; 82 } 83 }); 84 85 88 } 89 90 public void testConnectAndHandshake() throws Exception { 91 SynchronizedRef errorRef = new SynchronizedRef(null); 92 ClientHandshakeMessageResponder tester = new ClientHandshakeMessageResponder(new LinkedQueue(), new LinkedQueue(), 93 this.transportMessageFactory, 94 this.connectionId, this.transport, 95 errorRef); 96 97 this.connection.setMessageSink(tester); 98 99 transport.open(); 100 101 assertTrue(errorRef.get() == null); 102 103 List sentMessages = connection.getSentMessages(); 104 105 assertEquals(2, sentMessages.size()); 106 assertEquals(this.connectionId, transport.getConnectionId()); 107 Thread.sleep(1000); 108 assertTrue(tester.waitForAckToBeReceived(3000)); 109 } 110 111 114 public void testConnectAndHandshakeActuallyConnected() throws Exception { 115 CommunicationsManager commsMgr = new CommunicationsManagerImpl(new NullMessageMonitor(), 116 new PlainNetworkStackHarnessFactory(), 117 new NullConnectionPolicy()); 118 NetworkListener listener = commsMgr.createListener(new NullSessionManager(), new TCSocketAddress(0), true, 119 new DefaultConnectionIdFactory()); 120 listener.start(Collections.EMPTY_SET); 121 int port = listener.getBindPort(); 122 123 transport = new ClientMessageTransport(0, new ConnectionInfo(TCSocketAddress.LOOPBACK_IP, port), 1000, commsMgr 124 .getConnectionManager(), this.handshakeErrorHandler, this.transportMessageFactory, 125 new WireProtocolAdaptorFactoryImpl()); 126 transport.open(); 127 assertTrue(transport.isConnected()); 128 listener.stop(5000); 129 130 } 131 } 132 | Popular Tags |