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 ; 13 import java.util.LinkedList ; 14 import java.util.List ; 15 16 17 23 public class SequencerFailoverTest extends TestCase { 24 JChannel ch1, ch2, ch3; static final String GROUP="demo-group"; 26 static final int NUM_MSGS=50; 27 28 29 String 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 name) { 46 super(name); 47 } 48 49 public void setUp() throws Exception { 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 { 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 { 74 new Thread () { 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 (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 { 101 List 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 it=msgs.iterator(); it.hasNext();) { 106 tmp=((Integer )it.next()).intValue(); 107 if(tmp != i) 108 throw new Exception ("expected " + i + ", but got " + tmp); 109 i++; 110 } 111 } 112 113 private List getMessages(JChannel ch) throws Exception { 114 List retval=new LinkedList (); 115 Object 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 [] args) { 128 String [] testCaseName={SequencerFailoverTest.class.getName()}; 129 junit.textui.TestRunner.main(testCaseName); 130 } 131 132 133 } 134 | Popular Tags |