KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: NakReceiverWindowTest.java,v 1.1 2007/07/04 07:29:33 belaban Exp $
2

3 package org.jgroups.tests;
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.Address;
11 import org.jgroups.stack.NakReceiverWindow;
12 import org.jgroups.stack.IpAddress;
13 import org.jgroups.stack.Retransmitter;
14
15
16 public class NakReceiverWindowTest extends TestCase {
17
18     private Address sender;
19     private MyRetransmitCommand cmd=new MyRetransmitCommand();
20
21     public NakReceiverWindowTest(String JavaDoc name) {
22         super(name);
23     }
24
25
26     protected void setUp() throws Exception JavaDoc {
27         super.setUp();
28         sender=new IpAddress("127.0.0.1", 5555);
29     }
30
31     public void test1() throws Exception JavaDoc {
32         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 1);
33         check(win, 0, 1, 1);
34         assertNull(win.get(23));
35     }
36
37     public void test2() throws Exception JavaDoc {
38         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 100);
39         check(win, 0, 100, 100);
40     }
41
42     public void test3() throws Exception JavaDoc {
43         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 0);
44         win.add(1, new Message());
45         assertNotNull(win.get(1));
46         check(win, 0, 1, 0);
47         win.add(2, new Message());
48         check(win, 0, 2, 0);
49         assertNotNull(win.get(2));
50         win.remove();
51         check(win, 0, 2, 1);
52         win.remove();
53         check(win, 0, 2, 2);
54     }
55
56     public void test4() throws Exception JavaDoc {
57         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 1);
58         win.add(2, new Message());
59         check(win, 0, 2, 1);
60     }
61
62     public void test5() throws Exception JavaDoc {
63         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 100);
64         win.add(101, new Message());
65         win.add(100, new Message());
66         check(win, 0, 101, 100);
67     }
68
69     public void test6() throws Exception JavaDoc {
70         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 100);
71         win.add(101, new Message());
72         System.out.println("win: " + win);
73         win.add(100, new Message());
74         System.out.println("win: " + win);
75         check(win, 0, 101, 100);
76         win.remove();
77         System.out.println("win: " + win);
78         check(win, 0, 101, 101);
79         while((win.remove()) != null);
80         check(win, 0, 101, 101);
81     }
82
83
84     public void testLowerBounds() {
85         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 100, 50, null);
86         win.add(101, new Message());
87         System.out.println("win: " + win);
88         win.add(100, new Message());
89         System.out.println("win: " + win);
90         check(win, 50, 101, 100);
91         win.remove();
92         System.out.println("win: " + win);
93         check(win, 50, 101, 101);
94         while((win.remove()) != null);
95         check(win, 50, 101, 101);
96     }
97
98     public void test7() throws Exception JavaDoc {
99         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 0);
100         win.add(1, new Message());
101         win.add(2, new Message());
102         win.add(3, new Message());
103         win.add(4, new Message());
104         check(win, 0, 4, 0);
105         System.out.println("Note that the subsequent warning is expected:");
106         win.stable(4); // no-op because we haven't even removed 4 messages
107
check(win, 0, 4, 0);
108         while(win.remove() != null);
109         check(win, 0, 4, 4);
110         win.stable(4);
111         check(win, 4, 4, 4);
112     }
113
114
115     public void testLowerBounds2() throws Exception JavaDoc {
116         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 100, 50, null);
117         win.add(100, new Message());
118         win.add(101, new Message());
119         win.add(102, new Message());
120         win.add(103, new Message());
121         System.out.println("win: " + win);
122         check(win, 50, 103, 100);
123         System.out.println("Note that the subsequent warning is expected:");
124         win.stable(103); // no-op because we haven't even removed 4 messages
125
check(win, 50, 103, 100);
126         while(win.remove() != null);
127         check(win, 50, 103, 103);
128         win.stable(103);
129         System.out.println("win: " + win);
130         check(win, 103, 103, 103);
131     }
132
133     public void test8() throws Exception JavaDoc {
134         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 0);
135         win.add(1, new Message());
136         win.add(2, new Message());
137         win.add(3, new Message());
138         win.add(4, new Message());
139         win.add(6, new Message());
140         check(win, 0, 6, 0); // haven't delivered a message yet
141
while(win.remove() != null);
142         check(win, 0, 6, 4);
143         win.add(5, new Message());
144         check(win, 0, 6, 4);
145         win.remove();
146         check(win, 0, 6, 5);
147         win.remove();
148         check(win, 0, 6, 6);
149         win.stable(4);
150         check(win, 4, 6, 6);
151         win.stable(6);
152         check(win, 6, 6, 6);
153     }
154
155
156     public void testAdd() throws Exception JavaDoc {
157         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 0);
158         check(win, 0, 0, 0);
159         win.add(0, new Message()); // discarded, next expected is 1
160
check(win, 0, 0, 0);
161         win.add(1, new Message());
162         check(win, 0, 1, 0);
163         win.add(2, new Message());
164         win.add(3, new Message());
165         win.add(4, new Message());
166         check(win, 0, 4, 0);
167         win.add(6, new Message());
168         check(win, 0, 6, 0);
169         win.add(5, new Message());
170         check(win, 0, 6, 0);
171         while(win.remove() != null) ;
172         check(win, 0, 6, 6);
173         win.stable(4);
174         check(win, 4, 6, 6);
175         win.stable(6);
176         check(win, 6, 6, 6);
177     }
178
179
180     public void test9() throws Exception JavaDoc {
181         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 0);
182         win.add(1, new Message());
183         win.add(2, new Message());
184         win.add(3, new Message());
185         win.add(4, new Message());
186         win.add(6, new Message());
187         System.out.println("win: " + win);
188         while((win.remove()) != null) ;
189         win.stable(6); // 6 is ignore as it is >= highest delivered message
190
System.out.println("win: " + win);
191         assertNotNull(win.get(2));
192         check(win, 0, 6, 4);
193         win.add(5, new Message());
194         check(win, 0, 6, 4);
195         while((win.remove()) != null) ;
196         check(win, 0, 6, 6);
197         win.stable(6);
198         check(win, 6, 6, 6);
199     }
200
201
202     public void testHighestDelivered() throws Exception JavaDoc {
203         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 0);
204         win.add(1, new Message());
205         win.add(2, new Message());
206         win.add(3, new Message());
207         win.add(4, new Message());
208         check(win, 0, 4, 0);
209         win.add(10, new Message());
210         check(win, 0, 10, 0);
211         System.out.println("win: " + win);
212         win.add(9, new Message());
213         win.add(7, new Message());
214         win.add(8, new Message());
215         win.add(6, new Message());
216         win.add(5, new Message());
217         System.out.println("win: " + win);
218         check(win, 0, 10, 0);
219         while((win.remove()) != null) ;
220         check(win, 0, 10, 10);
221         win.stable(5);
222         System.out.println("win: " + win);
223         check(win, 5, 10, 10);
224         win.stable(10);
225         System.out.println("win: " + win);
226         check(win, 10, 10, 10);
227     }
228
229
230     public void testMissingMessages() throws Exception JavaDoc {
231         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 0);
232         win.add(1, new Message());
233         win.add(5, new Message());
234         check(win, 0, 5, 0);
235         win.add(6, new Message());
236         check(win, 0, 6, 0);
237         System.out.println("win: " + win);
238     }
239
240
241     public void testMissingMessages2() throws Exception JavaDoc {
242         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 0);
243         win.add(1, new Message());
244         win.add(5, new Message());
245         check(win, 0, 5, 0);
246         win.add(8, new Message());
247         check(win, 0, 8, 0);
248         win.add(9, new Message());
249         check(win, 0, 9, 0);
250         System.out.println("win: " + win);
251     }
252
253
254     public void testMissingMessages3() throws Exception JavaDoc {
255         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 0);
256         win.add(1, new Message());
257         win.add(5, new Message());
258         check(win, 0, 5, 0);
259         win.add(8, new Message());
260         check(win, 0, 8, 0);
261         win.add(9, new Message());
262         check(win, 0, 9, 0);
263         System.out.println("win: " + win);
264         win.add(2, new Message());
265         check(win, 0, 9, 0);
266         win.add(3, new Message());
267         win.add(4, new Message());
268         check(win, 0, 9, 0);
269         win.add(7, new Message());
270         check(win, 0, 9, 0);
271         win.add(6, new Message());
272         check(win, 0, 9, 0);
273         win.add(10, new Message());
274         check(win, 0, 10, 0);
275         win.add(11, new Message());
276         check(win, 0, 11, 0);
277         System.out.println("win: " + win);
278     }
279
280
281     public void testMissingMessages4() throws Exception JavaDoc {
282         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 100);
283         win.add(101, new Message());
284         win.add(105, new Message());
285         check(win, 0, 105, 100);
286         win.add(108, new Message());
287         check(win, 0, 108, 100);
288         win.add(109, new Message());
289         check(win, 0, 109, 100);
290         System.out.println("win: " + win);
291         win.add(102, new Message());
292         check(win, 0, 109, 100);
293         win.add(103, new Message());
294         win.add(104, new Message());
295         check(win, 0, 109, 100);
296         win.add(107, new Message());
297         check(win, 0, 109, 100);
298         win.add(106, new Message());
299         check(win, 0, 109, 100);
300         win.add(110, new Message());
301         check(win, 0, 110, 100);
302         win.add(110, new Message());
303         check(win, 0, 110, 100);
304         System.out.println("win: " + win);
305     }
306
307
308     public void testMissingMessages5() throws Exception JavaDoc {
309         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 100);
310         win.add(101, new Message());
311         check(win, 0, 101, 100);
312         win.add(108, new Message());
313         check(win, 0, 108, 100);
314         win.remove();
315         win.add(109, new Message());
316         check(win, 0, 109, 101);
317         System.out.println("win: " + win);
318         win.add(102, new Message());
319         check(win, 0, 109, 101);
320         win.add(103, new Message());
321         win.add(104, new Message());
322         check(win, 0, 109, 101);
323         win.add(107, new Message());
324         check(win, 0, 109, 101);
325         win.add(106, new Message());
326         win.add(105, new Message());
327         check(win, 0, 109, 101);
328         win.add(110, new Message());
329         check(win, 0, 110, 101);
330         win.add(110, new Message());
331         check(win, 0, 110, 101);
332         System.out.println("win: " + win);
333     }
334
335     public void test10() throws Exception JavaDoc {
336         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 0);
337         win.add(1, new Message());
338         win.add(2, new Message());
339         win.add(3, new Message());
340         win.add(4, new Message());
341         while((win.remove()) != null) ;
342         check(win, 0, 4, 4);
343     }
344
345     public void test10a() throws Exception JavaDoc {
346         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 0);
347         win.add(1, new Message());
348         win.add(2, new Message());
349         win.add(3, new Message());
350         win.add(4, new Message());
351         while((win.remove()) != null) ;
352         win.stable(4);
353         check(win, 4, 4, 4);
354
355     }
356
357     public void test11() throws Exception JavaDoc {
358         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 0);
359         win.add(1, new Message());
360         win.add(2, new Message());
361         win.add(3, new Message());
362         win.add(4, new Message());
363         while((win.remove()) != null) ;
364         win.reset();
365         check(win, 0, 0, 0);
366     }
367
368
369     public void test12() throws Exception JavaDoc {
370         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 0);
371
372         win.add(1, new Message(null, null, new Integer JavaDoc(1)));
373         win.add(2, new Message(null, null, new Integer JavaDoc(2)));
374         win.add(3, new Message(null, null, new Integer JavaDoc(3)));
375
376         assertEquals(1, ((Integer JavaDoc)win.remove().getObject()).intValue());
377         assertEquals(2, ((Integer JavaDoc)win.remove().getObject()).intValue());
378         assertEquals(3, ((Integer JavaDoc)win.remove().getObject()).intValue());
379     }
380
381
382     public void test13() throws Exception JavaDoc {
383         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 0);
384         win.add(1, new Message());
385         win.add(2, new Message());
386         win.add(3, new Message());
387         win.add(4, new Message());
388         check(win, 0, 4, 0);
389         win.remove();
390         win.remove();
391         win.add(5, new Message());
392         win.add(6, new Message());
393         check(win, 0, 6, 2);
394         win.stable(2);
395         check(win, 2, 6, 2);
396     }
397
398
399
400     public void testAddOOBAtHead() throws Exception JavaDoc {
401         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 0);
402         boolean rc;
403         rc=win.add(0, oob());
404         assertFalse(rc);
405         rc=win.add(1, oob());
406         assertTrue(rc);
407         rc=win.add(1, oob());
408         assertFalse(rc);
409     }
410
411
412     public void testAddOOBAtTail() throws Exception JavaDoc {
413         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 0);
414         boolean rc;
415         rc=win.add(1, oob());
416         assertTrue(rc);
417         rc=win.add(2, oob());
418         assertTrue(rc);
419         rc=win.add(2, oob());
420         assertFalse(rc);
421     }
422
423
424     public void testAddOOBInTheMiddle() throws Exception JavaDoc {
425         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 0);
426         boolean rc;
427         rc=win.add(3, oob());
428         assertTrue(rc);
429         rc=win.add(3, oob());
430         assertFalse(rc);
431         rc=win.add(1, oob());
432         assertTrue(rc);
433         rc=win.add(1, oob());
434         assertFalse(rc);
435         rc=win.add(2, oob());
436         assertTrue(rc);
437         rc=win.add(2, oob());
438         assertFalse(rc);
439     }
440
441
442
443
444     public void testUpdateHighestSeen() {
445         add(1000);
446         add(2000);
447         add(3000);
448         add(4000);
449         add(5000);
450         add(10000);
451         add(15000);
452         add(20000);
453         add(30000);
454     }
455
456     public void test1000() {
457         add(1000);
458     }
459
460     public void test10000() {
461         add(10000);
462     }
463
464
465     void add(int num_msgs) {
466         long start, stop;
467         double time_per_msg;
468         NakReceiverWindow win=new NakReceiverWindow(sender, cmd, 1);
469         start=System.currentTimeMillis();
470         for(int i=1; i < 1 + num_msgs; i++) {
471             win.add(i, new Message());
472         }
473         stop=System.currentTimeMillis();
474         time_per_msg=(stop-start) / (double)num_msgs;
475         System.out.println("-- time for " + num_msgs + " msgs: " + (stop-start) + ", " + time_per_msg + " ms/msg");
476     }
477
478
479     private Message oob() {
480         Message retval=new Message();
481         retval.setFlag(Message.OOB);
482         return retval;
483     }
484
485
486     private void check(NakReceiverWindow win, long lowest, long highest_received, long highest_delivered) {
487         assertEquals("lowest=" + lowest + ", win.lowest=" + win.getLowestSeen(), lowest, win.getLowestSeen());
488         assertEquals("highest_received=" + highest_received + ", win.highest_received=" + win.getHighestReceived(),
489                      highest_received, win.getHighestReceived());
490         assertEquals("highest_delivered=" + highest_delivered + ", win.highest_delivered=" + win.getHighestDelivered(),
491                      highest_delivered, win.getHighestDelivered());
492     }
493
494
495     private static class MyRetransmitCommand implements Retransmitter.RetransmitCommand {
496
497         public void retransmit(long first_seqno, long last_seqno, Address sender) {
498         }
499     }
500
501
502     public static Test suite() {
503         return new TestSuite(NakReceiverWindowTest.class);
504     }
505
506     public static void main(String JavaDoc[] args) {
507         junit.textui.TestRunner.run(suite());
508     }
509 }
510
Popular Tags