KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: AckMcastSenderWindowTest.java,v 1.1 2007/07/04 07:29:33 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.Address;
10 import org.jgroups.Message;
11 import org.jgroups.stack.AckMcastSenderWindow;
12 import org.jgroups.stack.IpAddress;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Hashtable JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Vector JavaDoc;
18 import java.net.UnknownHostException JavaDoc;
19
20
21 /**
22  * Test <code>AckMcastSenderWindow</code>
23  * <p>
24  * <code>testAck()</code>:<br>
25  * 1. Create two messages {1,2} each with 3 distinct destinations.<br>
26  * 2. Start a thread that acknowledges messages between sleeping
27  * intervals.<br>
28  * 3. When the callback retransmission function is called, check that the
29  * request is for a destination that is still associated with the given
30  * message sequence number.<br>
31  * <p>
32  * Since <code>AckMcastSenderWindow</code> does not export its state, keep
33  * track of seqnos and address lists in a hashtable.
34  */

35 public class AckMcastSenderWindowTest extends TestCase {
36     private class Cmd
37     implements AckMcastSenderWindow.RetransmitCommand {
38     public void retransmit(long seqno, Message msg, Address addr) {
39         _retransmit(seqno, msg, addr); }
40     }
41
42     private class Acker extends Thread JavaDoc { public void run() { _ackerRun(); } }
43
44
45     /** A list of destination addresses */
46     private static Address[] _RECVS = {
47             new IpAddress(5000),
48             new IpAddress(5001),
49             new IpAddress(5002)
50     };
51
52
53     /** The retransmit command */
54     private AckMcastSenderWindow.RetransmitCommand _cmd;
55     /** The mcast retransmit window */
56     private AckMcastSenderWindow _win;
57     /**
58      * 2-level table
59      * seqNo -> list of destinations
60      */

61     private Hashtable JavaDoc _tbl;
62
63
64     /**
65      * Associate the given addess with this sequence number. This is to
66      * reflect the state of the <code>AckMcastSenderWindow</code> as its state
67      * is not exported
68      *
69      * @param seqno the sequence number
70      * @param addr the address to associate with the seqno
71      */

72     private void _put(long seqno, Address addr) {
73     List JavaDoc list;
74
75     synchronized(_tbl) {
76         if ((list = (List JavaDoc)_tbl.get(new Long JavaDoc(seqno))) == null) {
77         list = new ArrayList JavaDoc();
78         _tbl.put(new Long JavaDoc(seqno), list);
79         }
80         if (!list.contains(addr)) list.add(addr);
81         else { if (list.isEmpty()) _tbl.remove(new Long JavaDoc(seqno)); }
82     } // synchronized(_tbl)
83
}
84
85     /**
86      * Remove the given address from the list of addresses for this seqno
87      *
88      * @param seqno the sequence number associated with a list of addresses
89      * @param addr the address to remove from the list of addresses mapped
90      * to this seqno
91      */

92     private void _remove(long seqno, Address addr) {
93     List JavaDoc list;
94
95     synchronized(_tbl) {
96         if ((list = (List JavaDoc)_tbl.get(new Long JavaDoc(seqno))) == null) return;
97         list.remove(addr);
98         if (list.isEmpty()) _tbl.remove(new Long JavaDoc(seqno));
99     } // synchronized(_tbl)
100
}
101
102     /**
103      * @return true if <code>addr</code> is associated with <code>seqno</code>
104      */

105     private boolean _contains(long seqno, Address addr) {
106     List JavaDoc list;
107
108     synchronized(_tbl) {
109         if ((list = (List JavaDoc)_tbl.get(new Long JavaDoc(seqno))) == null) return(false);
110         return(list.contains(addr));
111     } // synchronized(_tbl)
112
}
113
114
115     /**
116      * Thread acknowledging messages
117      */

118     private void _ackerRun() {
119     // Ack {2, _RECVS[2]}
120
_win.ack(2, _RECVS[2]); _remove(2, _RECVS[2]);
121     try { Thread.sleep(1000);
122     } catch(InterruptedException JavaDoc ex) { ex.printStackTrace(); }
123
124     // Ack {1, _RECVS[1]}
125
_win.ack(1, _RECVS[1]); _remove(1, _RECVS[1]);
126     try { Thread.sleep(500);
127     } catch(InterruptedException JavaDoc ex) { ex.printStackTrace(); }
128
129     // Ack {1, _RECVS[0]}
130
// Ack {2, _RECVS[0]}
131
// Ack {2, _RECVS[1]}
132
_win.ack(1, _RECVS[0]); _remove(1, _RECVS[0]);
133     _win.ack(2, _RECVS[0]); _remove(2, _RECVS[0]);
134     _win.ack(2, _RECVS[1]); _remove(2, _RECVS[1]);
135     try { Thread.sleep(500);
136     } catch(InterruptedException JavaDoc ex) { ex.printStackTrace(); }
137
138     // Ack {1, _RECVS[2]}
139
_win.ack(1, _RECVS[2]); _remove(1, _RECVS[2]);
140     }
141
142
143     /**
144      * Check if retransmission is expected
145      */

146     private void _retransmit(long seqno, Message msg, Address addr) {
147     if (!_contains(seqno, addr))
148         fail("Acknowledging a non-existent msg, great!");
149     else
150         System.out.println("retransmitting " + seqno);
151     }
152
153
154     /**
155      * Add 2 messages to 3 destinations
156      *
157      * Start acknowledging messages while checking the validity of
158      * retransmissions
159      */

160     public void test1() {
161     Vector JavaDoc dests = new Vector JavaDoc();
162     Message msg = new Message();
163     Acker acker = new Acker();
164     long seqno;
165
166     for (int i = 0; i < _RECVS.length; ++i) dests.add(_RECVS[i]);
167
168     // seqno/1
169
seqno = 1;
170     for (int i = 0; i < _RECVS.length; ++i) _put(seqno, _RECVS[i]);
171     _win.add(seqno, msg, dests);
172
173     // seqno/2
174
seqno = 2;
175     for (int i = 0; i < _RECVS.length; ++i) _put(seqno, _RECVS[i]);
176     _win.add(seqno, msg, dests);
177
178     // start
179
acker.start();
180     try { acker.join();
181     } catch(InterruptedException JavaDoc ex) { ex.printStackTrace(); }
182
183     _win.stop();
184     } // testAck()
185

186
187     public void testRemove() throws UnknownHostException JavaDoc {
188         AckMcastSenderWindow mywin=new AckMcastSenderWindow(new MyCommand(), new long[]{1000,2000,3000});
189         Address sender1=new IpAddress("127.0.0.1", 10000);
190         Address sender2=new IpAddress("127.0.0.1", 10001);
191         Address sender3=new IpAddress("127.0.0.1", 10002);
192         Vector JavaDoc senders=new Vector JavaDoc();
193         Message msg=new Message();
194         long seqno=322649;
195
196         senders.addElement(sender1);
197         senders.addElement(sender2);
198         senders.addElement(sender3);
199
200         mywin.add(seqno, msg, (Vector JavaDoc)senders.clone()); // clone() for the fun of it...
201

202         mywin.ack(seqno, sender1);
203         mywin.ack(seqno, sender2);
204
205         System.out.println("entry is " + mywin.printDetails(seqno));
206         assertTrue(mywin.getNumberOfResponsesExpected(seqno) == 3);
207         assertTrue(mywin.getNumberOfResponsesReceived(seqno) == 2);
208         mywin.waitUntilAllAcksReceived(4000);
209         mywin.suspect(sender3);
210         assertTrue(mywin.size() == 0); // because suspect() removed that entry
211
}
212
213
214     public AckMcastSenderWindowTest(String JavaDoc name) { super(name); }
215
216
217     public void setUp() {
218     _cmd = new Cmd();
219     _win = new AckMcastSenderWindow(_cmd);
220     _tbl = new Hashtable JavaDoc();
221     }
222
223     public void tearDown() {
224     _win.stop();
225     }
226
227
228     class MyCommand implements AckMcastSenderWindow.RetransmitCommand {
229
230     public void retransmit(long seqno, Message msg, Address dest) {
231         System.out.println("-- retransmitting " + seqno);
232     }
233     }
234
235
236     public static Test suite() {
237     TestSuite suite;
238     suite = new TestSuite(AckMcastSenderWindowTest.class);
239     return(suite);
240     }
241     public static void main(String JavaDoc[] args) {
242     String JavaDoc[] name = {AckMcastSenderWindowTest.class.getName()};
243     junit.textui.TestRunner.main(name);
244     }
245 }
246
Popular Tags