001: // $Id: ConnectTest.java,v 1.8 2006/08/08 08:13:10 belaban Exp $
002:
003: package org.jgroups.tests;
004:
005: import junit.framework.TestCase;
006: import org.jgroups.JChannel;
007: import org.jgroups.Message;
008: import org.jgroups.MessageListener;
009: import org.jgroups.View;
010: import org.jgroups.blocks.PullPushAdapter;
011: import org.jgroups.util.Promise;
012: import org.jgroups.util.Util;
013:
014: /**
015: * Runs through multiple channel connect and disconnects, without closing the channel.
016: */
017: public class ConnectTest extends TestCase {
018: JChannel channel;
019: final int TIMES = 10;
020:
021: String props = "UDP(mcast_addr=228.8.8.8;mcast_port=45566;ip_ttl=32;"
022: + "mcast_send_buf_size=150000;mcast_recv_buf_size=80000;"
023: + "enable_bundling=true;use_incoming_packet_handler=true;loopback=true):"
024: + "PING(timeout=2000;num_initial_members=3):"
025: + "MERGE2(min_interval=5000;max_interval=10000):"
026: + "FD_SOCK:"
027: + "VERIFY_SUSPECT(timeout=1500):"
028: + "pbcast.NAKACK(gc_lag=50;retransmit_timeout=600,1200,2400,4800):"
029: + "UNICAST(timeout=600,1200,2400):"
030: + "pbcast.STABLE(desired_avg_gossip=20000):"
031: + "FRAG(frag_size=4096;down_thread=false;up_thread=false):"
032: + "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;"
033: + "shun=true;print_local_addr=false)";
034:
035: public ConnectTest(String name) {
036: super (name);
037: }
038:
039: public void tearDown() throws Exception {
040: super .tearDown();
041: if (channel != null) {
042: channel.close();
043: channel = null;
044: }
045: }
046:
047: void doIt(int times) {
048: for (int i = 0; i < times; i++) {
049: System.out.println("\nAttempt #" + (i + 1));
050: System.out.print("Connecting to channel: ");
051: try {
052: channel.connect("ConnectTest");
053: System.out.println("-- connected: " + channel.getView()
054: + " --");
055: } catch (Exception e) {
056: System.out.println("-- connection failed --");
057: System.err.println(e);
058: }
059: System.out.print("Disconnecting from channel: ");
060: channel.disconnect();
061: System.out.println("-- disconnected --");
062: }
063: }
064:
065: public void testConnectAndDisconnect() throws Exception {
066: System.out.print("Creating channel: ");
067: channel = new JChannel(props);
068: System.out.println("-- created --");
069: doIt(TIMES);
070: System.out.print("Closing channel: ");
071: channel.close();
072: System.out.println("-- closed --");
073: System.out.println("Remaining threads are:");
074: Util.printThreads();
075: }
076:
077: public void testDisconnectConnectOne() throws Exception {
078: channel = new JChannel(props);
079: channel.connect("testgroup1");
080: channel.disconnect();
081: channel.connect("testgroup2");
082: View view = channel.getView();
083: assertEquals(1, view.size());
084: assertTrue(view.containsMember(channel.getLocalAddress()));
085: channel.close();
086: System.out.println("Remaining threads are:");
087: Util.printThreads();
088: }
089:
090: /**
091: * Tests connect-disconnect-connect sequence for a group with two members
092: **/
093: public void testDisconnectConnectTwo() throws Exception {
094: View view;
095: JChannel coordinator = new JChannel(props);
096: coordinator.connect("testgroup");
097: view = coordinator.getView();
098: System.out.println("-- view for coordinator: " + view);
099:
100: channel = new JChannel(props);
101: channel.connect("testgroup1");
102: view = channel.getView();
103: System.out.println("-- view for channel: " + view);
104:
105: channel.disconnect();
106:
107: channel.connect("testgroup");
108: view = channel.getView();
109: System.out.println("-- view for channel: " + view);
110:
111: assertEquals(2, view.size());
112: assertTrue(view.containsMember(channel.getLocalAddress()));
113: assertTrue(view.containsMember(coordinator.getLocalAddress()));
114: coordinator.close();
115: channel.close();
116: System.out.println("Remaining threads are:");
117: Util.printThreads();
118: }
119:
120: /**
121: * Tests connect-disconnect-connect-send sequence for a group with two
122: * members. Test case introduced before fixing pbcast.NAKACK
123: * bug, which used to leave pbcast.NAKACK in a broken state after
124: * DISCONNECT. Because of this problem, the channel couldn't be used to
125: * multicast messages.
126: **/
127: public void testDisconnectConnectSendTwo() throws Exception {
128: final Promise msgPromise = new Promise();
129: JChannel coordinator = new JChannel(props);
130: coordinator.connect("testgroup");
131: PullPushAdapter ppa = new PullPushAdapter(coordinator,
132: new PromisedMessageListener(msgPromise));
133: ppa.start();
134:
135: channel = new JChannel(props);
136: channel.connect("testgroup1");
137: channel.disconnect();
138: channel.connect("testgroup");
139: channel.send(new Message(null, null, "payload"));
140: Message msg = (Message) msgPromise.getResult(20000);
141: assertTrue(msg != null);
142: assertEquals("payload", msg.getObject());
143: ppa.stop();
144: coordinator.close();
145: channel.close();
146: System.out.println("Remaining threads are:");
147: Util.printThreads();
148: }
149:
150: private static class PromisedMessageListener implements
151: MessageListener {
152:
153: private Promise promise;
154:
155: public PromisedMessageListener(Promise promise) {
156: this .promise = promise;
157: }
158:
159: public byte[] getState() {
160: return null;
161: }
162:
163: public void receive(Message msg) {
164: promise.setResult(msg);
165: }
166:
167: public void setState(byte[] state) {
168: }
169: }
170:
171: public static void main(String[] args) {
172: String[] testCaseName = { ConnectTest.class.getName() };
173: junit.textui.TestRunner.main(testCaseName);
174: }
175:
176: }
|