KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > protocol > transport > ClientMessageTransportTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

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 JavaDoc;
26 import java.util.List JavaDoc;
27
28 /**
29  * x normal connect and handshake o reconnect and handshake
30  */

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 JavaDoc {
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     // FIXME 2005-12-14 -- We should restore this test.
86
// assertNull(connection.connectCalls.poll(3000));
87

88   }
89
90   public void testConnectAndHandshake() throws Exception JavaDoc {
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 JavaDoc 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   /**
112    * Test interaction with a real network listener.
113    */

114   public void testConnectAndHandshakeActuallyConnected() throws Exception JavaDoc {
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