KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: TimerTest.java,v 1.1 2007/07/04 07:29:33 belaban Exp $
2
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 JavaDoc;
12 import java.util.Timer JavaDoc;
13 import java.util.TimerTask JavaDoc;
14
15
16 /**
17  * Test cases for Timer. May fail sometimes, for example if there is GC during the run. Therefore, if run
18  * standalone, the likelyhood of GC is smaller than when run with the entire testsuite
19  *
20  * @author Bela Ban
21  */

22 public class TimerTest extends TestCase {
23     Timer JavaDoc win=null;
24     final int NUM_MSGS=1000;
25     long[] xmit_timeouts={1000, 2000, 4000, 8000};
26     double PERCENTAGE_OFF=0.3; // how much can expected xmit_timeout and real timeout differ to still be okay ?
27
HashMap JavaDoc msgs=new HashMap JavaDoc(); // keys=seqnos (Long), values=Entries
28

29
30     class MyTask extends TimerTask JavaDoc {
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; // time message was added
46
long first_xmit=0; // time between start_time and first_xmit should be ca. 1000ms
47
long second_xmit=0; // time between first_xmit and second_xmit should be ca. 2000ms
48
long third_xmit=0; // time between third_xmit and second_xmit should be ca. 4000ms
49
long fourth_xmit=0; // time between third_xmit and second_xmit should be ca. 8000ms
50
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         /**
82          * Entry is correct if xmit timeouts are not more than 30% off the mark
83          */

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 JavaDoc toString() {
124             StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
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 JavaDoc name) {
133         super(name);
134     }
135
136
137     /**
138      * Tests whether retransmits are called at correct times for 1000 messages. A retransmit should not be
139      * more than 30% earlier or later than the scheduled retransmission time
140      */

141     public void testRetransmits() {
142         Entry entry;
143         MyTask task;
144         int num_non_correct_entries=0;
145
146         win=new Timer JavaDoc();
147
148         // 1. Add NUM_MSGS messages:
149
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 JavaDoc(i), entry);
154             win.schedule(task, entry.next());
155         }
156         System.out.println("-- done");
157
158         // 2. Wait for at least 4 xmits/msg: total of 1000 + 2000 + 4000 + 8000ms = 15000ms; wait for 20000ms
159
System.out.println("-- waiting for 20 secs for all retransmits");
160         Util.sleep(20000);
161
162         // 3. Check whether all Entries have correct retransmission times
163
for(long i=0; i < NUM_MSGS; i++) {
164             entry=(Entry)msgs.get(new Long JavaDoc(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 JavaDoc(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 JavaDoc 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 JavaDoc[] args) {
196         String JavaDoc[] name={TimerTest.class.getName()};
197         junit.textui.TestRunner.main(name);
198     }
199 }
200
Popular Tags