1 package org.jgroups.tests; 3 4 5 import junit.framework.Test; 6 import junit.framework.TestCase; 7 import junit.framework.TestSuite; 8 import org.jgroups.stack.Interval; 9 import org.jgroups.util.Util; 10 11 import java.util.HashMap ; 12 import java.util.Timer ; 13 import java.util.TimerTask ; 14 15 16 22 public class TimerTest extends TestCase { 23 Timer win=null; 24 final int NUM_MSGS=1000; 25 long[] xmit_timeouts={1000, 2000, 4000, 8000}; 26 double PERCENTAGE_OFF=0.3; HashMap msgs=new HashMap (); 29 30 class MyTask extends TimerTask { 31 Entry entry; 32 33 MyTask(Entry entry) { 34 this.entry=entry; 35 } 36 37 public void run() { 38 entry.retransmit(); 39 } 40 41 } 42 43 44 class Entry { 45 long start_time=0; long first_xmit=0; long second_xmit=0; long third_xmit=0; long fourth_xmit=0; Interval interval=new Interval(xmit_timeouts); 51 long seqno=0; 52 53 Entry(long seqno) { 54 this.seqno=seqno; 55 start_time=System.currentTimeMillis(); 56 } 57 58 59 public void retransmit() { 60 if(first_xmit == 0) 61 first_xmit=System.currentTimeMillis(); 62 else 63 if(second_xmit == 0) 64 second_xmit=System.currentTimeMillis(); 65 else 66 if(third_xmit == 0) 67 third_xmit=System.currentTimeMillis(); 68 else 69 if(fourth_xmit == 0) 70 fourth_xmit=System.currentTimeMillis(); 71 72 win.schedule(new MyTask(this), interval.next()); 73 } 74 75 76 public long next() { 77 return interval.next(); 78 } 79 80 81 84 boolean isCorrect() { 85 long t; 86 long expected; 87 long diff, delta; 88 boolean off=false; 89 90 t=first_xmit - start_time; 91 expected=xmit_timeouts[0]; 92 diff=Math.abs(expected - t); 93 delta=(long)(expected * PERCENTAGE_OFF); 94 if(diff >= delta) off=true; 95 96 t=second_xmit - first_xmit; 97 expected=xmit_timeouts[1]; 98 diff=Math.abs(expected - t); 99 delta=(long)(expected * PERCENTAGE_OFF); 100 if(diff >= delta) off=true; 101 102 t=third_xmit - second_xmit; 103 expected=xmit_timeouts[2]; 104 diff=Math.abs(expected - t); 105 delta=(long)(expected * PERCENTAGE_OFF); 106 if(diff >= delta) off=true; 107 108 t=fourth_xmit - third_xmit; 109 expected=xmit_timeouts[3]; 110 diff=Math.abs(expected - t); 111 delta=(long)(expected * PERCENTAGE_OFF); 112 if(diff >= delta) off=true; 113 114 if(off) { 115 System.err.println("#" + seqno + ": " + this + ": (" + "entry is more than " + 116 PERCENTAGE_OFF + " percentage off "); 117 return false; 118 } 119 return true; 120 } 121 122 123 public String toString() { 124 StringBuffer sb=new StringBuffer (); 125 sb.append(first_xmit - start_time).append(", ").append(second_xmit - first_xmit).append(", "); 126 sb.append(third_xmit - second_xmit).append(", ").append(fourth_xmit - third_xmit); 127 return sb.toString(); 128 } 129 } 130 131 132 public TimerTest(String name) { 133 super(name); 134 } 135 136 137 141 public void testRetransmits() { 142 Entry entry; 143 MyTask task; 144 int num_non_correct_entries=0; 145 146 win=new Timer (); 147 148 System.out.println("-- adding " + NUM_MSGS + " messages:"); 150 for(long i=0; i < NUM_MSGS; i++) { 151 entry=new Entry(i); 152 task=new MyTask(entry); 153 msgs.put(new Long (i), entry); 154 win.schedule(task, entry.next()); 155 } 156 System.out.println("-- done"); 157 158 System.out.println("-- waiting for 20 secs for all retransmits"); 160 Util.sleep(20000); 161 162 for(long i=0; i < NUM_MSGS; i++) { 164 entry=(Entry)msgs.get(new Long (i)); 165 if(!entry.isCorrect()) { 166 num_non_correct_entries++; 167 } 168 } 169 170 if(num_non_correct_entries > 0) 171 System.err.println("Number of incorrect retransmission timeouts: " + num_non_correct_entries); 172 else { 173 for(long i=0; i < NUM_MSGS; i++) { 174 entry=(Entry)msgs.get(new Long (i)); 175 if(entry != null) 176 System.out.println(i + ": " + entry); 177 } 178 } 179 assertTrue(num_non_correct_entries == 0); 180 try { 181 win.cancel(); 182 } 183 catch(Exception ex) { 184 System.err.println(ex); 185 } 186 } 187 188 189 public static Test suite() { 190 TestSuite suite; 191 suite=new TestSuite(TimerTest.class); 192 return (suite); 193 } 194 195 public static void main(String [] args) { 196 String [] name={TimerTest.class.getName()}; 197 junit.textui.TestRunner.main(name); 198 } 199 } 200 | Popular Tags |