001: // $Id: AckCollectorTest.java,v 1.5 2006/05/16 11:14:28 belaban Exp $
002:
003: package org.jgroups.tests;
004:
005: import junit.framework.TestCase;
006: import org.jgroups.util.AckCollector;
007: import org.jgroups.util.Util;
008: import org.jgroups.TimeoutException;
009: import org.jgroups.Address;
010: import org.jgroups.stack.IpAddress;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014: import java.net.UnknownHostException;
015:
016: public class AckCollectorTest extends TestCase {
017: List l = new ArrayList(5);
018: AckCollector ac;
019: private List new_list = new ArrayList(3);
020:
021: public AckCollectorTest(String name) {
022: super (name);
023: }
024:
025: public void setUp() throws Exception {
026: super .setUp();
027: l.add("one");
028: l.add("two");
029: l.add("three");
030: l.add("four");
031: l.add("five");
032: ac = new AckCollector(null, l);
033: new_list.add("six");
034: new_list.add("seven");
035: new_list.add("eight");
036: }
037:
038: public void tearDown() throws Exception {
039: super .tearDown();
040: l.clear();
041: new_list.clear();
042: }
043:
044: public void testConstructor() {
045: System.out.println("AckCollector is " + ac);
046: assertEquals(5, ac.size());
047: }
048:
049: public void testWaitForAllAcksNoTimeout() {
050: new Thread() {
051: public void run() {
052: ac.ack("one");
053: System.out.println("AckCollector: " + ac);
054: Util.sleep(100);
055: ac.ack("two");
056: System.out.println("AckCollector: " + ac);
057: Util.sleep(100);
058: ac.ack("three");
059: System.out.println("AckCollector: " + ac);
060: Util.sleep(100);
061: ac.ack("four");
062: System.out.println("AckCollector: " + ac);
063: Util.sleep(100);
064: ac.ack("five");
065: System.out.println("AckCollector: " + ac);
066: }
067: }.start();
068: ac.waitForAllAcks();
069: assertEquals(0, ac.size());
070: }
071:
072: public void testWaitForAllAcksWithTimeoutException() {
073: try {
074: ac.waitForAllAcks(200);
075: fail("we should get a timeout exception here");
076: } catch (TimeoutException e) {
077: System.out
078: .println("received timeout exception, as expected");
079: }
080: }
081:
082: public void testWaitForAllAcksWithTimeout() {
083: new Thread() {
084: public void run() {
085: ac.ack("one");
086: System.out.println("AckCollector: " + ac);
087: Util.sleep(100);
088: ac.ack("two");
089: System.out.println("AckCollector: " + ac);
090: Util.sleep(100);
091: ac.ack("three");
092: System.out.println("AckCollector: " + ac);
093: Util.sleep(100);
094: ac.ack("four");
095: System.out.println("AckCollector: " + ac);
096: Util.sleep(100);
097: ac.ack("five");
098: System.out.println("AckCollector: " + ac);
099: }
100: }.start();
101: try {
102: ac.waitForAllAcks(1000);
103: assertTrue("we should not get a timeout exception here",
104: true);
105: } catch (TimeoutException e) {
106: fail("we should not get a timeout exception here");
107: }
108: assertEquals(0, ac.size());
109: }
110:
111: public void testWaitForAllAcksWithTimeoutException2() {
112: new Thread() {
113: public void run() {
114: ac.ack("one");
115: System.out.println("AckCollector: " + ac);
116: Util.sleep(100);
117: ac.ack("two");
118: System.out.println("AckCollector: " + ac);
119: Util.sleep(100);
120: ac.ack("three");
121: System.out.println("AckCollector: " + ac);
122: Util.sleep(100);
123: ac.ack("four");
124: System.out.println("AckCollector: " + ac);
125: Util.sleep(100);
126: ac.ack("five");
127: System.out.println("AckCollector: " + ac);
128: }
129: }.start();
130: try {
131: ac.waitForAllAcks(300);
132: fail("we should get a timeout exception here");
133: } catch (TimeoutException e) {
134: assertTrue("we should get a timeout exception here", true);
135: }
136: }
137:
138: public void testReset() {
139: new Thread() {
140: public void run() {
141: Util.sleep(500);
142: System.out.println("resetting AckCollector");
143: ac.reset(null, new_list);
144: System.out.println("reset AckCollector: " + ac);
145: }
146: }.start();
147: System.out.println("initial AckCollector: " + ac);
148: try {
149: ac.waitForAllAcks(1000);
150: fail("needs to throw TimeoutException");
151: } catch (TimeoutException e) {
152: assertTrue("expected TimeoutException", e != null);
153: }
154: System.out.println("new AckCollector: " + ac);
155: }
156:
157: public void testReset2() throws TimeoutException {
158: new Thread() {
159: public void run() {
160: Util.sleep(500);
161: System.out.println("resetting AckCollector");
162: ac.reset(null, new_list);
163: System.out.println("reset AckCollector: " + ac);
164: Util.sleep(100);
165: ac.ack("six");
166: System.out.println("AckCollector: " + ac);
167: Util.sleep(100);
168: ac.ack("seven");
169: System.out.println("AckCollector: " + ac);
170: Util.sleep(100);
171: ac.ack("eight");
172: System.out.println("AckCollector: " + ac);
173: }
174: }.start();
175: System.out.println("initial AckCollector: " + ac);
176: ac.waitForAllAcks(2000);
177: System.out.println("new AckCollector: " + ac);
178: }
179:
180: public void testNullList() throws TimeoutException {
181: AckCollector coll = new AckCollector();
182: coll.waitForAllAcks(1000);
183: }
184:
185: public void testOneList() throws TimeoutException,
186: UnknownHostException {
187: List tmp = new ArrayList();
188: Address addr = new IpAddress("127.0.0.1", 5555);
189: tmp.add(addr);
190: AckCollector coll = new AckCollector(null, tmp);
191: coll.ack(addr);
192: coll.waitForAllAcks(1000);
193: }
194:
195: public static void main(String[] args) {
196: String[] testCaseName = { AckCollectorTest.class.getName() };
197: junit.textui.TestRunner.main(testCaseName);
198: }
199:
200: }
|