001: // $Id: NakReceiverWindowTest.java,v 1.8 2005/07/18 14:23:35 belaban Exp $
002:
003: package org.jgroups.tests;
004:
005: import junit.framework.Test;
006: import junit.framework.TestCase;
007: import junit.framework.TestSuite;
008: import org.jgroups.Message;
009: import org.jgroups.stack.NakReceiverWindow;
010:
011: public class NakReceiverWindowTest extends TestCase {
012:
013: public NakReceiverWindowTest(String name) {
014: super (name);
015: }
016:
017: public void test1() throws Exception {
018: NakReceiverWindow win = new NakReceiverWindow(null, 1);
019: assertTrue(win.getLowestSeen() == 0);
020: assertTrue(win.getHighestSeen() == 0);
021: assertNull(win.get(23));
022: }
023:
024: public void test2() throws Exception {
025: NakReceiverWindow win = new NakReceiverWindow(null, 100);
026: assertEquals(0, win.getLowestSeen());
027: assertEquals(0, win.getHighestSeen());
028: }
029:
030: public void test3() throws Exception {
031: NakReceiverWindow win = new NakReceiverWindow(null, 1);
032: win.add(1, new Message());
033: assertNotNull(win.get(1));
034: assertTrue(win.getLowestSeen() == 1);
035: assertTrue(win.getHighestSeen() == 1);
036: win.add(2, new Message());
037: assertTrue(win.getLowestSeen() == 1);
038: assertTrue(win.getHighestSeen() == 2);
039: assertNotNull(win.get(2));
040: }
041:
042: public void test4() throws Exception {
043: NakReceiverWindow win = new NakReceiverWindow(null, 1);
044: win.add(2, new Message());
045: assertTrue(win.getLowestSeen() == 0);
046: assertTrue(win.getHighestSeen() == 0);
047: }
048:
049: public void test5() throws Exception {
050: NakReceiverWindow win = new NakReceiverWindow(null, 100);
051: win.add(101, new Message());
052: win.add(100, new Message());
053: assertEquals(100, win.getLowestSeen());
054: assertEquals(101, win.getHighestSeen());
055: }
056:
057: public void test6() throws Exception {
058: NakReceiverWindow win = new NakReceiverWindow(null, 100);
059: win.add(101, new Message());
060: win.add(100, new Message());
061: while ((win.remove()) != null)
062: ;
063: assertNotNull(win.get(100));
064: assertNotNull(win.get(101));
065: assertTrue(win.getLowestSeen() == 100);
066: assertTrue(win.getHighestSeen() == 101);
067: }
068:
069: public void test7() throws Exception {
070: NakReceiverWindow win = new NakReceiverWindow(null, 1);
071: win.add(1, new Message());
072: win.add(2, new Message());
073: win.add(3, new Message());
074: win.add(4, new Message());
075: win.stable(4);
076: assertTrue(win.getLowestSeen() == 1);
077: assertTrue(win.getHighestSeen() == 4);
078: assertNotNull(win.get(2));
079: }
080:
081: public void test8() throws Exception {
082: NakReceiverWindow win = new NakReceiverWindow(null, 1);
083: win.add(1, new Message());
084: win.add(2, new Message());
085: win.add(3, new Message());
086: win.add(4, new Message());
087: win.add(6, new Message());
088:
089: //System.out.println("highest received=" + win.getHighestReceived() +
090: // "\nhighest_seen=" + win.getHighestSeen() +
091: // "\nhighest_delivered=" + win.getHighestDelivered());
092: assertTrue(win.getHighestSeen() == 4);
093: }
094:
095: public void testAdd() throws Exception {
096: NakReceiverWindow win = new NakReceiverWindow(null, 0);
097: assertEquals(0, win.getHighestSeen());
098: win.add(0, new Message());
099: assertEquals(0, win.getHighestSeen());
100: win.add(1, new Message());
101: assertEquals(1, win.getHighestSeen());
102: win.add(2, new Message());
103: win.add(3, new Message());
104: win.add(4, new Message());
105: assertEquals(4, win.getHighestSeen());
106: win.add(6, new Message());
107: assertEquals(4, win.getHighestSeen());
108: win.add(5, new Message());
109: assertEquals(6, win.getHighestSeen());
110: while (win.remove() != null)
111: ;
112: assertEquals(6, win.getHighestSeen());
113: }
114:
115: public void test9() throws Exception {
116: NakReceiverWindow win = new NakReceiverWindow(null, 1);
117: win.add(1, new Message());
118: win.add(2, new Message());
119: win.add(3, new Message());
120: win.add(4, new Message());
121: win.add(6, new Message());
122: while ((win.remove()) != null)
123: ;
124: win.stable(6);
125: assertNull(win.get(2));
126:
127: //System.out.println(win);
128: //System.out.println("highest received=" + win.getHighestReceived() +
129: // "\nhighest_seen=" + win.getHighestSeen() +
130: // "\nhighest_delivered=" + win.getHighestDelivered());
131: assertTrue(win.getLowestSeen() == 4);
132: assertTrue(win.getHighestSeen() == 4);
133: }
134:
135: public void testHighestSeen() throws Exception {
136: NakReceiverWindow win = new NakReceiverWindow(null, 1);
137: win.add(1, new Message());
138: win.add(2, new Message());
139: win.add(3, new Message());
140: win.add(4, new Message());
141: assertEquals(win.getHighestSeen(), 4);
142: win.add(10, new Message());
143: assertEquals(win.getHighestSeen(), 4);
144: assertEquals(win.getHighestReceived(), 10);
145: System.out.println("win: " + win);
146: win.add(9, new Message());
147: win.add(7, new Message());
148: win.add(8, new Message());
149: win.add(6, new Message());
150: win.add(5, new Message());
151: System.out.println("win: " + win);
152: while ((win.remove()) != null)
153: ;
154: assertEquals(win.getHighestSeen(), 10);
155: }
156:
157: public void testMissingMessages() throws Exception {
158: NakReceiverWindow win = new NakReceiverWindow(null, 1);
159: win.add(1, new Message());
160: win.add(5, new Message());
161: assertEquals(1, win.getHighestSeen());
162: win.add(6, new Message());
163: assertEquals(1, win.getHighestSeen());
164: System.out.println("win: " + win);
165: }
166:
167: public void testMissingMessages2() throws Exception {
168: NakReceiverWindow win = new NakReceiverWindow(null, 1);
169: win.add(1, new Message());
170: win.add(5, new Message());
171: assertEquals(1, win.getHighestSeen());
172: win.add(8, new Message());
173: assertEquals(1, win.getHighestSeen());
174: win.add(9, new Message());
175: assertEquals(1, win.getHighestSeen());
176: System.out.println("win: " + win);
177: }
178:
179: public void testMissingMessages3() throws Exception {
180: NakReceiverWindow win = new NakReceiverWindow(null, 1);
181: win.add(1, new Message());
182: win.add(5, new Message());
183: assertEquals(1, win.getHighestSeen());
184: win.add(8, new Message());
185: assertEquals(1, win.getHighestSeen());
186: win.add(9, new Message());
187: assertEquals(1, win.getHighestSeen());
188: System.out.println("win: " + win);
189: win.add(2, new Message());
190: assertEquals(2, win.getHighestSeen());
191: win.add(3, new Message());
192: win.add(4, new Message());
193: assertEquals(5, win.getHighestSeen());
194: win.add(7, new Message());
195: assertEquals(5, win.getHighestSeen());
196: win.add(6, new Message());
197: assertEquals(9, win.getHighestSeen());
198: win.add(10, new Message());
199: assertEquals(10, win.getHighestSeen());
200: win.add(11, new Message());
201: assertEquals(11, win.getHighestSeen());
202: System.out.println("win: " + win);
203: }
204:
205: public void testMissingMessages4() throws Exception {
206: NakReceiverWindow win = new NakReceiverWindow(null, 101);
207: win.add(101, new Message());
208: win.add(105, new Message());
209: assertEquals(101, win.getHighestSeen());
210: win.add(108, new Message());
211: assertEquals(101, win.getHighestSeen());
212: win.add(109, new Message());
213: assertEquals(101, win.getHighestSeen());
214: System.out.println("win: " + win);
215: win.add(102, new Message());
216: assertEquals(102, win.getHighestSeen());
217: win.add(103, new Message());
218: win.add(104, new Message());
219: assertEquals(105, win.getHighestSeen());
220: win.add(107, new Message());
221: assertEquals(105, win.getHighestSeen());
222: win.add(106, new Message());
223: assertEquals(109, win.getHighestSeen());
224: win.add(110, new Message());
225: assertEquals(110, win.getHighestSeen());
226: win.add(110, new Message());
227: assertEquals(110, win.getHighestSeen());
228: System.out.println("win: " + win);
229: }
230:
231: public void testMissingMessages5() throws Exception {
232: NakReceiverWindow win = new NakReceiverWindow(null, 101);
233: win.add(101, new Message());
234: assertEquals(101, win.getHighestSeen());
235: win.add(108, new Message());
236: assertEquals(101, win.getHighestSeen());
237: win.add(109, new Message());
238: assertEquals(101, win.getHighestSeen());
239: System.out.println("win: " + win);
240: win.add(102, new Message());
241: assertEquals(102, win.getHighestSeen());
242: win.add(103, new Message());
243: win.add(104, new Message());
244: assertEquals(104, win.getHighestSeen());
245: win.add(107, new Message());
246: assertEquals(104, win.getHighestSeen());
247: win.add(106, new Message());
248: win.add(105, new Message());
249: assertEquals(109, win.getHighestSeen());
250: win.add(110, new Message());
251: assertEquals(110, win.getHighestSeen());
252: win.add(110, new Message());
253: assertEquals(110, win.getHighestSeen());
254: System.out.println("win: " + win);
255: }
256:
257: public void test10() throws Exception {
258: NakReceiverWindow win = new NakReceiverWindow(null, 1);
259: win.add(1, new Message());
260: win.add(2, new Message());
261: win.add(3, new Message());
262: win.add(4, new Message());
263: while ((win.remove()) != null)
264: ;
265: assertTrue(win.getLowestSeen() == 1);
266: assertTrue(win.getHighestSeen() == 4);
267: }
268:
269: public void test10a() throws Exception {
270: NakReceiverWindow win = new NakReceiverWindow(null, 1);
271: win.add(1, new Message());
272: win.add(2, new Message());
273: win.add(3, new Message());
274: win.add(4, new Message());
275: while ((win.remove()) != null)
276: ;
277: win.stable(4);
278: assertTrue(win.getLowestSeen() == 4);
279: assertTrue(win.getHighestSeen() == 4);
280:
281: }
282:
283: public void test11() throws Exception {
284: NakReceiverWindow win = new NakReceiverWindow(null, 1);
285: win.add(1, new Message());
286: win.add(2, new Message());
287: win.add(3, new Message());
288: win.add(4, new Message());
289: while ((win.remove()) != null)
290: ;
291: win.reset();
292: assertTrue(win.getLowestSeen() == 0);
293: assertTrue(win.getHighestSeen() == 0);
294: }
295:
296: public void test12() throws Exception {
297: NakReceiverWindow win = new NakReceiverWindow(null, 1);
298:
299: win.add(1, new Message(null, null, new Integer(1)));
300: win.add(2, new Message(null, null, new Integer(2)));
301: win.add(3, new Message(null, null, new Integer(3)));
302:
303: assertTrue(((Integer) win.remove().getObject()).intValue() == 1);
304: assertTrue(((Integer) win.remove().getObject()).intValue() == 2);
305: assertTrue(((Integer) win.remove().getObject()).intValue() == 3);
306: }
307:
308: public void test13() throws Exception {
309: NakReceiverWindow win = new NakReceiverWindow(null, 1);
310: win.add(1, new Message());
311: win.add(2, new Message());
312: win.add(3, new Message());
313: win.add(4, new Message());
314: assertEquals(1, win.getLowestSeen());
315: assertEquals(4, win.getHighestSeen());
316:
317: win.remove();
318: win.remove();
319: win.add(5, new Message());
320: win.add(6, new Message());
321: assertEquals(1, win.getLowestSeen());
322: assertEquals(6, win.getHighestSeen());
323: win.stable(2);
324: assertEquals(2, win.getLowestSeen());
325: }
326:
327: public void testUpdateHighestSeen() {
328: add(1000);
329: add(2000);
330: add(3000);
331: add(4000);
332: add(5000);
333: add(10000);
334: add(15000);
335: add(20000);
336: add(30000);
337: }
338:
339: public void test1000() {
340: add(1000);
341: }
342:
343: public void test10000() {
344: add(10000);
345: }
346:
347: void add(int num_msgs) {
348: long start, stop;
349: double time_per_msg;
350: NakReceiverWindow win = new NakReceiverWindow(null, 1);
351: start = System.currentTimeMillis();
352: for (int i = 1; i < 1 + num_msgs; i++) {
353: win.add(i, new Message());
354: }
355: stop = System.currentTimeMillis();
356: time_per_msg = (stop - start) / (double) num_msgs;
357: System.out.println("-- time for " + num_msgs + " msgs: "
358: + (stop - start) + ", " + time_per_msg + " ms/msg");
359: }
360:
361: public static Test suite() {
362: TestSuite s = new TestSuite(NakReceiverWindowTest.class);
363: return s;
364: }
365:
366: public static void main(String[] args) {
367: junit.textui.TestRunner.run(suite());
368: }
369: }
|