KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: AckSenderWindowTest.java,v 1.1 2007/07/04 07:29:34 belaban Exp $
2
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 JavaDoc;
14
15
16
17
18
19 /**
20  * Test cases for AckSenderWindow
21  * @author Bela Ban
22  */

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; // how much can expected xmit_timeout and real timeout differ to still be okay ?
28
HashMap JavaDoc msgs=new HashMap JavaDoc(); // keys=seqnos (Long), values=Entries
29

30
31     class Entry {
32     long start_time=0; // time message was added
33
long first_xmit=0; // time between start_time and first_xmit should be ca. 1000ms
34
long second_xmit=0; // time between first_xmit and second_xmit should be ca. 2000ms
35
long third_xmit=0; // time between third_xmit and second_xmit should be ca. 4000ms
36
long fourth_xmit=0; // time between third_xmit and second_xmit should be ca. 8000ms
37

38     Entry() {
39         start_time=System.currentTimeMillis();
40     }
41
42     /** Entry is correct if xmit timeouts are not more than 30% off the mark */
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 JavaDoc toString() {
84         StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
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 JavaDoc(seqno));
96
97         // System.out.println(" -- retransmit(" + seqno + ")");
98

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 JavaDoc name) { super(name); }
127     
128
129     
130     
131     /** Tests whether retransmits are called at correct times for 1000 messages */
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     // 1. Send NUM_MSGS messages:
139
System.out.println("-- sending " + NUM_MSGS + " messages:");
140     for(long i=0; i < NUM_MSGS; i++) {
141         msgs.put(new Long JavaDoc(i), new Entry());
142         win.add(i, new Message());
143     }
144     System.out.println("-- done");
145
146     // 2. Wait for at least 4 xmits/msg: total of 1000 + 2000 + 4000 + 8000ms = 15000ms; wait for 20000ms
147
System.out.println("-- waiting for 20 secs for all retransmits");
148     Util.sleep(20000);
149     
150     // 3. Check whether all Entries have correct retransmission times
151
for(long i=0; i < NUM_MSGS; i++) {
152         entry=(Entry)msgs.get(new Long JavaDoc(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 JavaDoc[] args) {
173     String JavaDoc[] name = {AckSenderWindowTest.class.getName()};
174     junit.textui.TestRunner.main(name);
175     }
176 }
177
Popular Tags