1 3 package org.jgroups.tests; 4 5 import junit.framework.Test; 6 import junit.framework.TestCase; 7 import junit.framework.TestSuite; 8 import org.jgroups.Event; 9 import org.jgroups.Message; 10 import org.jgroups.View; 11 import org.jgroups.debug.Simulator; 12 import org.jgroups.protocols.DISCARD; 13 import org.jgroups.protocols.UNICAST; 14 import org.jgroups.stack.IpAddress; 15 import org.jgroups.stack.Protocol; 16 17 import java.nio.ByteBuffer ; 18 import java.util.Properties ; 19 import java.util.Vector ; 20 21 22 26 public class UNICAST_Test extends TestCase { 27 IpAddress a1, a2; 28 Vector members; 29 View v; 30 Simulator simulator; 31 32 final int SIZE=1000; final int NUM_MSGS=10000; 34 35 36 public UNICAST_Test(String name) { 37 super(name); 38 } 39 40 41 public void setUp() throws Exception { 42 super.setUp(); 43 } 44 45 public void tearDown() throws Exception { 46 super.tearDown(); 47 if(simulator != null) 48 simulator.stop(); 49 } 50 51 52 public void testReceptionOfAllMessages() throws Throwable { 53 UNICAST unicast=new UNICAST(); 54 Properties props=new Properties (); 55 props.setProperty("timeout", "500,1000,2000,3000"); 56 unicast.setProperties(props); 57 Protocol[] stack=new Protocol[]{unicast}; 58 createStack(stack); 59 _testReceptionOfAllMessages(); 60 } 61 62 63 public void testReceptionOfAllMessagesWithDISCARD() throws Throwable { 64 UNICAST unicast=new UNICAST(); 65 Properties props=new Properties (); 66 props.setProperty("timeout", "500,1000,2000,3000"); 67 unicast.setProperties(props); 68 69 DISCARD discard=new DISCARD(); 70 props.clear(); 71 props.setProperty("down", "0.1"); discard.setProperties(props); 73 74 Protocol[] stack=new Protocol[]{unicast,discard}; 75 createStack(stack); 76 _testReceptionOfAllMessages(); 77 } 78 79 80 81 private static byte[] createPayload(int size, int seqno) { 82 ByteBuffer buf=ByteBuffer.allocate(size); 83 buf.putInt(seqno); 84 return buf.array(); 85 } 86 87 88 89 class Receiver implements Simulator.Receiver { 90 int num_mgs_received=0, next=1; 91 Throwable exception=null; 92 boolean received_all=false; 93 94 public void receive(Event evt) { 95 if(evt.getType() == Event.MSG) { 96 if(exception != null) 97 return; 98 Message msg=(Message)evt.getArg(); 99 ByteBuffer buf=ByteBuffer.wrap(msg.getRawBuffer()); 100 int seqno=buf.getInt(); 101 if(seqno != next) { 102 exception=new Exception ("expected seqno was " + next + ", but received " + seqno); 103 return; 104 } 105 next++; 106 num_mgs_received++; 107 if(num_mgs_received % 1000 == 0) 108 System.out.println("<== " + num_mgs_received); 109 if(num_mgs_received == NUM_MSGS) { 110 synchronized(this) { 111 received_all=true; 112 this.notifyAll(); 113 } 114 } 115 } 116 } 117 118 public int getNumberOfReceivedMessages() { 119 return num_mgs_received; 120 } 121 122 public boolean receivedAll() {return received_all;} 123 124 public Throwable getException() { 125 return exception; 126 } 127 } 128 129 130 private void _testReceptionOfAllMessages() throws Throwable { 131 int num_received=0; 132 Receiver r=new Receiver(); 133 simulator.setReceiver(r); 134 for(int i=1; i <= NUM_MSGS; i++) { 135 Message msg=new Message(a1, null, createPayload(SIZE, i)); Event evt=new Event(Event.MSG, msg); 137 simulator.send(evt); 138 if(i % 1000 == 0) 139 System.out.println("==> " + i); 140 } 141 int num_tries=10; 142 while((num_received=r.getNumberOfReceivedMessages()) != NUM_MSGS && num_tries > 0) { 143 if(r.getException() != null) 144 throw r.getException(); 145 synchronized(r) { 146 try {r.wait(3000);} 147 catch(InterruptedException e) {} 148 } 149 num_tries--; 150 } 151 printStats(num_received); 152 assertEquals(num_received, NUM_MSGS); 153 } 154 155 private void createStack(Protocol[] stack) throws Exception { 156 a1=new IpAddress(1111); 157 members=new Vector (); 158 members.add(a1); 159 v=new View(a1, 1, members); 160 simulator=new Simulator(); 161 simulator.setLocalAddress(a1); 162 simulator.setView(v); 163 simulator.addMember(a1); 164 simulator.setProtocolStack(stack); 165 simulator.start(); 166 } 167 168 private void printStats(int num_received) { 169 System.out.println("-- num received=" + num_received + ", stats:\n" + simulator.dumpStats()); 170 } 171 172 173 public static Test suite() { 174 return new TestSuite(UNICAST_Test.class); 175 } 176 177 public static void main(String [] args) { 178 junit.textui.TestRunner.run(suite()); 179 } 180 } 181 | Popular Tags |