001: package org.jgroups.tests;
002:
003: import junit.framework.TestCase;
004: import org.jgroups.JChannel;
005: import org.jgroups.Message;
006: import org.jgroups.View;
007: import org.jgroups.util.Util;
008:
009: import java.util.Iterator;
010: import java.util.LinkedList;
011: import java.util.List;
012:
013: /**
014: * Tests a SEQUENCER based stack: A, B and C. B starts multicasting messages with a monotonically increasing
015: * number. Then A is crashed. C and B should receive *all* numbers *without* a gap.
016: * @author Bela Ban
017: * @version $Id: SequencerFailoverTest.java,v 1.3 2006/08/08 08:24:50 belaban Exp $
018: */
019: public class SequencerFailoverTest extends TestCase {
020: JChannel ch1, ch2, ch3; // ch1 is the coordinator
021: final String GROUP = "demo-group";
022: final int NUM_MSGS = 50;
023:
024: String props = "UDP(mcast_addr=228.8.8.8;mcast_port=45566;ip_ttl=32;"
025: + "mcast_send_buf_size=150000;mcast_recv_buf_size=80000;"
026: + "enable_bundling=true;use_incoming_packet_handler=true;loopback=true):"
027: + "PING(timeout=2000;num_initial_members=3):"
028: + "MERGE2(min_interval=5000;max_interval=10000):"
029: + "FD(timeout=2000;max_tries=2):"
030: + "VERIFY_SUSPECT(timeout=1500):"
031: + "pbcast.NAKACK(gc_lag=50;retransmit_timeout=600,1200,2400,4800):"
032: + "UNICAST(timeout=600,1200,2400):"
033: + "pbcast.STABLE(desired_avg_gossip=20000):"
034: + "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;"
035: + "shun=true;print_local_addr=true;view_ack_collection_timeout=2000):"
036: + "SEQUENCER";
037:
038: public SequencerFailoverTest(String name) {
039: super (name);
040: }
041:
042: public void setUp() throws Exception {
043: super .setUp();
044: ch1 = new JChannel(props);
045: ch1.connect(GROUP);
046:
047: ch2 = new JChannel(props);
048: ch2.connect(GROUP);
049:
050: ch3 = new JChannel(props);
051: ch3.connect(GROUP);
052: }
053:
054: public void tearDown() throws Exception {
055: super .tearDown();
056: if (ch3 != null) {
057: ch3.close();
058: ch3 = null;
059: }
060: if (ch2 != null) {
061: ch2.close();
062: ch2 = null;
063: }
064: }
065:
066: public void testBroadcastSequence() throws Exception {
067: new Thread() {
068: public void run() {
069: Util.sleepRandom(100);
070: ch1.shutdown();
071: ch1 = null;
072: }
073: }.start();
074:
075: for (int i = 1; i <= NUM_MSGS; i++) {
076: Util.sleep(300);
077: ch2.send(new Message(null, null, new Integer(i)));
078: System.out.print("-- messages sent: " + i + "/" + NUM_MSGS
079: + "\r");
080: }
081: System.out.println("");
082: View view = ch2.getView();
083: System.out.println("ch2's view is " + view);
084: assertEquals(2, view.getMembers().size());
085: long total = 0;
086: for (int i = 10000; i > 0; i -= 1000) {
087: Util.sleep(1000);
088: System.out.print("sleeping for " + (i / 1000)
089: + " seconds\r");
090: }
091: System.out.println("-- verifying messages on ch2 and ch3");
092: System.out.println("ch2 has " + ch2.getNumMessages()
093: + " messages, ch3 has " + ch3.getNumMessages()
094: + " messages");
095: verifyNumberOfMessages(NUM_MSGS, ch2);
096: verifyNumberOfMessages(NUM_MSGS, ch3);
097: }
098:
099: private void verifyNumberOfMessages(int num_msgs, JChannel ch)
100: throws Exception {
101: List msgs = getMessages(ch);
102: System.out.println("list has " + msgs.size()
103: + " msgs (should have " + NUM_MSGS + ")");
104: assertEquals(num_msgs, msgs.size());
105: int tmp, i = 1;
106: for (Iterator it = msgs.iterator(); it.hasNext();) {
107: tmp = ((Integer) it.next()).intValue();
108: if (tmp != i)
109: throw new Exception("expected " + i + ", but got "
110: + tmp);
111: i++;
112: }
113: }
114:
115: private List getMessages(JChannel ch) throws Exception {
116: List retval = new LinkedList();
117: Object obj;
118: while (ch.getNumMessages() > 0) {
119: obj = ch.receive(1000);
120: if (obj instanceof Message) {
121: Message msg = (Message) obj;
122: retval.add(msg.getObject());
123: }
124: }
125: return retval;
126: }
127:
128: public static void main(String[] args) {
129: String[] testCaseName = { SequencerFailoverTest.class.getName() };
130: junit.textui.TestRunner.main(testCaseName);
131: }
132:
133: }
|