1 package org.jgroups.tests; 3 4 5 6 import junit.framework.Test; 7 import junit.framework.TestCase; 8 import junit.framework.TestSuite; 9 import org.jgroups.Message; 10 import org.jgroups.stack.AckSenderWindow; 11 import org.jgroups.util.Util; 12 13 import java.util.HashMap ; 14 15 16 17 18 19 23 public class AckSenderWindowTest extends TestCase { 24 AckSenderWindow win=null; 25 final int NUM_MSGS=100; 26 long[] xmit_timeouts={1000, 2000, 4000, 8000}; 27 double PERCENTAGE_OFF=1.3; HashMap msgs=new HashMap (); 30 31 class Entry { 32 long start_time=0; long first_xmit=0; long second_xmit=0; long third_xmit=0; long fourth_xmit=0; 38 Entry() { 39 start_time=System.currentTimeMillis(); 40 } 41 42 43 boolean isCorrect(long seqno) { 44 long t; 45 long expected; 46 long diff, delta; 47 boolean off=false; 48 49 t=first_xmit - start_time; 50 expected=xmit_timeouts[0]; 51 diff=Math.abs(expected - t); 52 delta=(long)(expected * PERCENTAGE_OFF); 53 if(diff >= delta) off=true; 54 55 t=second_xmit - first_xmit; 56 expected=xmit_timeouts[1]; 57 diff=Math.abs(expected - t); 58 delta=(long)(expected * PERCENTAGE_OFF); 59 if(diff >= delta) off=true; 60 61 t=third_xmit - second_xmit; 62 expected=xmit_timeouts[2]; 63 diff=Math.abs(expected - t); 64 delta=(long)(expected * PERCENTAGE_OFF); 65 if(diff >= delta) off=true; 66 67 t=fourth_xmit - third_xmit; 68 expected=xmit_timeouts[3]; 69 diff=Math.abs(expected - t); 70 delta=(long)(expected * PERCENTAGE_OFF); 71 if(diff >= delta) off=true; 72 73 if(off) { 74 System.err.println("#" + seqno + ": " + this + ": (" + "entry is more than " + 75 PERCENTAGE_OFF + " percentage off "); 76 return false; 77 } 78 return true; 79 } 80 81 82 83 public String toString() { 84 StringBuffer sb=new StringBuffer (); 85 sb.append(first_xmit - start_time).append(", ").append(second_xmit-first_xmit).append(", "); 86 sb.append(third_xmit-second_xmit).append(", ").append(fourth_xmit-third_xmit); 87 return sb.toString(); 88 } 89 } 90 91 92 class MyRetransmitCommand implements AckSenderWindow.RetransmitCommand { 93 94 public void retransmit(long seqno, Message msg) { 95 Entry entry=(Entry)msgs.get(new Long (seqno)); 96 97 99 if(entry != null) { 100 if(entry.first_xmit == 0) { 101 entry.first_xmit=System.currentTimeMillis(); 102 return; 103 } 104 105 if(entry.second_xmit == 0) { 106 entry.second_xmit=System.currentTimeMillis(); 107 return; 108 } 109 110 if(entry.third_xmit == 0) { 111 entry.third_xmit=System.currentTimeMillis(); 112 return; 113 } 114 115 if(entry.fourth_xmit == 0) { 116 entry.fourth_xmit=System.currentTimeMillis(); 117 return; 118 } 119 } 120 } 121 122 } 123 124 125 126 public AckSenderWindowTest(String name) { super(name); } 127 128 129 130 131 132 public void testRetransmits() { 133 Entry entry; 134 int num_non_correct_entries=0; 135 136 win=new AckSenderWindow(new MyRetransmitCommand(), xmit_timeouts); 137 138 System.out.println("-- sending " + NUM_MSGS + " messages:"); 140 for(long i=0; i < NUM_MSGS; i++) { 141 msgs.put(new Long (i), new Entry()); 142 win.add(i, new Message()); 143 } 144 System.out.println("-- done"); 145 146 System.out.println("-- waiting for 20 secs for all retransmits"); 148 Util.sleep(20000); 149 150 for(long i=0; i < NUM_MSGS; i++) { 152 entry=(Entry)msgs.get(new Long (i)); 153 if(!entry.isCorrect(i)) { 154 num_non_correct_entries++; 155 } 156 } 157 158 if(num_non_correct_entries > 0) 159 System.err.println("Number of incorrect retransmission timeouts: " + num_non_correct_entries); 160 assertTrue(num_non_correct_entries == 0); 161 win.reset(); 162 } 163 164 165 166 public static Test suite() { 167 TestSuite suite; 168 suite = new TestSuite(AckSenderWindowTest.class); 169 return(suite); 170 } 171 172 public static void main(String [] args) { 173 String [] name = {AckSenderWindowTest.class.getName()}; 174 junit.textui.TestRunner.main(name); 175 } 176 } 177 | Popular Tags |