KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > SequencerFailoverTest


1
2
3 package org.jgroups.tests;
4
5
6 import junit.framework.TestCase;
7 import org.jgroups.JChannel;
8 import org.jgroups.Message;
9 import org.jgroups.View;
10 import org.jgroups.util.Util;
11
12 import java.util.Iterator JavaDoc;
13 import java.util.LinkedList JavaDoc;
14 import java.util.List JavaDoc;
15
16
17 /**
18  * Tests a SEQUENCER based stack: A, B and C. B starts multicasting messages with a monotonically increasing
19  * number. Then A is crashed. C and B should receive *all* numbers *without* a gap.
20  * @author Bela Ban
21  * @version $Id: SequencerFailoverTest.java,v 1.4 2007/03/06 17:43:01 belaban Exp $
22  */

23 public class SequencerFailoverTest extends TestCase {
24     JChannel ch1, ch2, ch3; // ch1 is the coordinator
25
static final String JavaDoc GROUP="demo-group";
26     static final int NUM_MSGS=50;
27
28
29     String JavaDoc props="UDP(mcast_addr=228.8.8.8;mcast_port=45566;ip_ttl=32;" +
30             "mcast_send_buf_size=150000;mcast_recv_buf_size=80000;" +
31             "enable_bundling=true;use_incoming_packet_handler=true;loopback=true):" +
32             "PING(timeout=2000;num_initial_members=3):" +
33             "MERGE2(min_interval=5000;max_interval=10000):" +
34             "FD(timeout=2000;max_tries=2):" +
35             "VERIFY_SUSPECT(timeout=1500):" +
36             "pbcast.NAKACK(gc_lag=50;retransmit_timeout=600,1200,2400,4800):" +
37             "UNICAST(timeout=600,1200,2400):" +
38             "pbcast.STABLE(desired_avg_gossip=20000):" +
39             "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" +
40             "shun=true;print_local_addr=true;view_ack_collection_timeout=2000):" +
41             "SEQUENCER";
42
43
44
45     public SequencerFailoverTest(String JavaDoc name) {
46         super(name);
47     }
48
49     public void setUp() throws Exception JavaDoc {
50         super.setUp();
51         ch1=new JChannel(props);
52         ch1.connect(GROUP);
53
54         ch2=new JChannel(props);
55         ch2.connect(GROUP);
56
57         ch3=new JChannel(props);
58         ch3.connect(GROUP);
59     }
60
61     public void tearDown() throws Exception JavaDoc {
62         super.tearDown();
63         if(ch3 != null) {
64             ch3.close();
65             ch3 = null;
66         }
67         if(ch2 != null) {
68             ch2.close();
69             ch2 = null;
70         }
71     }
72
73     public void testBroadcastSequence() throws Exception JavaDoc {
74         new Thread JavaDoc() {
75             public void run() {
76                 Util.sleepRandom(100);
77                 ch1.shutdown(); ch1=null;
78             }
79         }.start();
80
81         for(int i=1; i <= NUM_MSGS; i++) {
82             Util.sleep(300);
83             ch2.send(new Message(null, null, new Integer JavaDoc(i)));
84             System.out.print("-- messages sent: " + i + "/" + NUM_MSGS + "\r");
85         }
86         System.out.println("");
87         View view=ch2.getView();
88         System.out.println("ch2's view is " + view);
89         assertEquals(2, view.getMembers().size());
90         for(int i=10000; i > 0; i-=1000) {
91             Util.sleep(1000);
92             System.out.print("sleeping for " + (i/1000) + " seconds\r");
93         }
94         System.out.println("-- verifying messages on ch2 and ch3");
95         System.out.println("ch2 has " + ch2.getNumMessages() + " messages, ch3 has " + ch3.getNumMessages() + " messages");
96         verifyNumberOfMessages(NUM_MSGS, ch2);
97         verifyNumberOfMessages(NUM_MSGS, ch3);
98     }
99
100     private void verifyNumberOfMessages(int num_msgs, JChannel ch) throws Exception JavaDoc {
101         List JavaDoc msgs=getMessages(ch);
102         System.out.println("list has " + msgs.size() + " msgs (should have " + NUM_MSGS + ")");
103         assertEquals(num_msgs, msgs.size());
104         int tmp, i=1;
105         for(Iterator JavaDoc it=msgs.iterator(); it.hasNext();) {
106             tmp=((Integer JavaDoc)it.next()).intValue();
107             if(tmp != i)
108                 throw new Exception JavaDoc("expected " + i + ", but got " + tmp);
109             i++;
110         }
111     }
112
113     private List JavaDoc getMessages(JChannel ch) throws Exception JavaDoc {
114         List JavaDoc retval=new LinkedList JavaDoc();
115         Object JavaDoc obj;
116         while(ch.getNumMessages() > 0) {
117             obj=ch.receive(1000);
118             if(obj instanceof Message) {
119                 Message msg=(Message)obj;
120                 retval.add(msg.getObject());
121             }
122         }
123         return retval;
124     }
125
126
127     public static void main(String JavaDoc[] args) {
128         String JavaDoc[] testCaseName={SequencerFailoverTest.class.getName()};
129         junit.textui.TestRunner.main(testCaseName);
130     }
131
132
133 }
134
Popular Tags