1 3 package org.jgroups.tests; 4 5 6 import junit.framework.TestCase; 7 import org.jgroups.util.AckCollector; 8 import org.jgroups.util.Util; 9 import org.jgroups.TimeoutException; 10 import org.jgroups.Address; 11 import org.jgroups.stack.IpAddress; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 import java.net.UnknownHostException ; 16 17 18 public class AckCollectorTest extends TestCase { 19 List l=new ArrayList (5); 20 AckCollector ac; 21 private List new_list=new ArrayList (3); 22 23 public AckCollectorTest(String name) { 24 super(name); 25 } 26 27 public void setUp() throws Exception { 28 super.setUp(); 29 l.add("one"); 30 l.add("two"); 31 l.add("three"); 32 l.add("four"); 33 l.add("five"); 34 ac=new AckCollector(null, l); 35 new_list.add("six"); 36 new_list.add("seven"); 37 new_list.add("eight"); 38 } 39 40 41 public void tearDown() throws Exception { 42 super.tearDown(); 43 l.clear(); 44 new_list.clear(); 45 } 46 47 public void testConstructor() { 48 System.out.println("AckCollector is " + ac); 49 assertEquals(5, ac.size()); 50 } 51 52 53 public void testWaitForAllAcksNoTimeout() { 54 new Thread () { 55 public void run() { 56 ac.ack("one"); 57 System.out.println("AckCollector: " + ac); 58 Util.sleep(100); 59 ac.ack("two"); 60 System.out.println("AckCollector: " + ac); 61 Util.sleep(100); 62 ac.ack("three"); 63 System.out.println("AckCollector: " + ac); 64 Util.sleep(100); 65 ac.ack("four"); 66 System.out.println("AckCollector: " + ac); 67 Util.sleep(100); 68 ac.ack("five"); 69 System.out.println("AckCollector: " + ac); 70 } 71 }.start(); 72 ac.waitForAllAcks(); 73 assertEquals(0, ac.size()); 74 } 75 76 public void testWaitForAllAcksWithTimeoutException() { 77 try { 78 ac.waitForAllAcks(200); 79 fail("we should get a timeout exception here"); 80 } 81 catch(TimeoutException e) { 82 System.out.println("received timeout exception, as expected"); 83 } 84 } 85 86 87 public void testWaitForAllAcksWithTimeout() { 88 new Thread () { 89 public void run() { 90 ac.ack("one"); 91 System.out.println("AckCollector: " + ac); 92 Util.sleep(100); 93 ac.ack("two"); 94 System.out.println("AckCollector: " + ac); 95 Util.sleep(100); 96 ac.ack("three"); 97 System.out.println("AckCollector: " + ac); 98 Util.sleep(100); 99 ac.ack("four"); 100 System.out.println("AckCollector: " + ac); 101 Util.sleep(100); 102 ac.ack("five"); 103 System.out.println("AckCollector: " + ac); 104 } 105 }.start(); 106 try { 107 ac.waitForAllAcks(1000); 108 assertTrue("we should not get a timeout exception here", true); 109 } 110 catch(TimeoutException e) { 111 fail("we should not get a timeout exception here"); 112 } 113 assertEquals(0, ac.size()); 114 } 115 116 public void testWaitForAllAcksWithTimeoutException2() { 117 new Thread () { 118 public void run() { 119 ac.ack("one"); 120 System.out.println("AckCollector: " + ac); 121 Util.sleep(100); 122 ac.ack("two"); 123 System.out.println("AckCollector: " + ac); 124 Util.sleep(100); 125 ac.ack("three"); 126 System.out.println("AckCollector: " + ac); 127 Util.sleep(100); 128 ac.ack("four"); 129 System.out.println("AckCollector: " + ac); 130 Util.sleep(100); 131 ac.ack("five"); 132 System.out.println("AckCollector: " + ac); 133 } 134 }.start(); 135 try { 136 ac.waitForAllAcks(300); 137 fail("we should get a timeout exception here"); 138 } 139 catch(TimeoutException e) { 140 assertTrue("we should get a timeout exception here", true); 141 } 142 } 143 144 145 public void testReset() { 146 new Thread () { 147 public void run() { 148 Util.sleep(500); 149 System.out.println("resetting AckCollector"); 150 ac.reset(null, new_list); 151 System.out.println("reset AckCollector: " + ac); 152 } 153 }.start(); 154 System.out.println("initial AckCollector: " + ac); 155 try { 156 ac.waitForAllAcks(1000); 157 fail("needs to throw TimeoutException"); 158 } 159 catch(TimeoutException e) { 160 assertTrue("expected TimeoutException", e != null); 161 } 162 System.out.println("new AckCollector: " + ac); 163 } 164 165 166 public void testReset2() throws TimeoutException { 167 new Thread () { 168 public void run() { 169 Util.sleep(500); 170 System.out.println("resetting AckCollector"); 171 ac.reset(null, new_list); 172 System.out.println("reset AckCollector: " + ac); 173 Util.sleep(100); 174 ac.ack("six"); 175 System.out.println("AckCollector: " + ac); 176 Util.sleep(100); 177 ac.ack("seven"); 178 System.out.println("AckCollector: " + ac); 179 Util.sleep(100); 180 ac.ack("eight"); 181 System.out.println("AckCollector: " + ac); 182 } 183 }.start(); 184 System.out.println("initial AckCollector: " + ac); 185 ac.waitForAllAcks(2000); 186 System.out.println("new AckCollector: " + ac); 187 } 188 189 public void testNullList() throws TimeoutException { 190 AckCollector coll=new AckCollector(); 191 coll.waitForAllAcks(1000); 192 } 193 194 public void testOneList() throws TimeoutException, UnknownHostException { 195 List tmp=new ArrayList (); 196 Address addr=new IpAddress("127.0.0.1", 5555); 197 tmp.add(addr); 198 AckCollector coll=new AckCollector(null, tmp); 199 coll.ack(addr); 200 coll.waitForAllAcks(1000); 201 } 202 203 204 public static void main(String [] args) { 205 String [] testCaseName={AckCollectorTest.class.getName()}; 206 junit.textui.TestRunner.main(testCaseName); 207 } 208 209 } 210 | Popular Tags |