1 3 package org.jgroups.blocks; 4 5 import junit.framework.Test; 6 import junit.framework.TestCase; 7 import junit.framework.TestSuite; 8 import org.jgroups.*; 9 import org.jgroups.stack.IpAddress; 10 import org.jgroups.util.RspList; 11 import org.jgroups.util.Util; 12 13 import java.util.Vector ; 14 15 public class GroupRequestTest extends TestCase { 16 Address a1, a2; 18 Vector <Address> dests=null; 19 21 public GroupRequestTest(String testName) { 22 super(testName); 23 } 24 25 protected void setUp() throws Exception { 26 super.setUp(); 27 a1=new IpAddress("127.0.0.1", 1111); 28 a2=new IpAddress("127.0.0.1", 2222); 29 dests=new Vector <Address>(2); 30 dests.add(a1); 31 dests.add(a2); 32 } 33 34 protected void tearDown() throws Exception { 35 dests.clear(); 36 super.tearDown(); 37 } 38 39 public void testMessageTimeout() throws Exception { 40 _testMessageTimeout(true); 41 } 42 43 public void testMessageReception() throws Exception { 44 _testMessageReception(true); 45 _testMessageReception(false); 46 } 47 48 49 public void testMessageReceptionWithSuspect() throws Exception { 50 _testMessageReceptionWithSuspect(true); 51 _testMessageReceptionWithSuspect(false); 52 } 53 54 55 public void testMessageReceptionWithViewChange() throws Exception { 56 _testMessageReceptionWithViewChange(true); 57 _testMessageReceptionWithViewChange(false); 58 } 59 60 public void testMessageReceptionWithViewChangeMemberLeft() throws Exception { 61 _testMessageReceptionWithViewChangeMemberLeft(true); 62 _testMessageReceptionWithViewChangeMemberLeft(false); 63 } 64 65 74 private void _testMessageTimeout(boolean async) throws Exception { 75 76 int destCount = 10; 78 79 final long timeout = destCount * 300; 81 82 final long delay = 75L; 84 Object [] responses = new Message[destCount]; 85 86 dests = new Vector <Address>(); 87 for (int i = 0; i < destCount; i++) { 88 Address addr = new IpAddress("127.0.0.1", Integer.parseInt(String.valueOf(i) + i + i + i)); 89 dests.add(addr); 90 responses[i] = new Message(null, addr, new Long (i)); 92 } 93 94 MyDelayedTransport tp = new MyDelayedTransport(async, responses, delay); 95 96 GroupRequest req=new GroupRequest(new Message(), tp, dests, GroupRequest.GET_ALL, timeout, dests.size()); 98 tp.setGroupRequest(req); 99 boolean rc = req.execute(); 100 System.out.println("group request is " + req); 101 assertTrue(rc); 102 assertEquals(0, req.getSuspects().size()); 103 assertTrue(req.isDone()); 104 RspList results = req.getResults(); 105 assertEquals(dests.size(), results.size()); 106 } 107 108 109 110 private void _testMessageReception(boolean async) throws Exception { 111 Object [] responses=new Message[]{new Message(null, a1, new Long (1)),new Message(null, a2, new Long (2))}; 112 MyTransport transport=new MyTransport(async, responses); 113 GroupRequest req=new GroupRequest(new Message(), transport, dests, GroupRequest.GET_ALL, 0, 2); 114 transport.setGroupRequest(req); 115 boolean rc=req.execute(); 116 System.out.println("group request is " + req); 117 assertTrue(rc); 118 assertEquals(0, req.getSuspects().size()); 119 assertTrue(req.isDone()); 120 RspList results=req.getResults(); 121 assertEquals(2, results.size()); 122 } 123 124 private void _testMessageReceptionWithSuspect(boolean async) throws Exception { 125 Object [] responses=new Object []{new Message(null, a1, new Long (1)), new SuspectEvent(a2)}; 126 MyTransport transport=new MyTransport(async, responses); 127 GroupRequest req=new GroupRequest(new Message(), transport, dests, GroupRequest.GET_ALL, 0, 2); 128 transport.setGroupRequest(req); 129 boolean rc=req.execute(); 130 System.out.println("group request is " + req); 131 assertTrue(rc); 132 assertEquals(1, req.getSuspects().size()); 133 assertTrue(req.isDone()); 134 RspList results=req.getResults(); 135 assertEquals(2, results.size()); 136 } 137 138 139 private void _testMessageReceptionWithViewChange(boolean async) throws Exception { 140 Vector <Address> new_dests=new Vector <Address>(); 141 new_dests.add(a1); 142 new_dests.add(a2); 143 new_dests.add(new IpAddress("127.0.0.1", 3333)); 144 Object [] responses=new Object []{new Message(null, a1, new Long (1)), 145 new View(new IpAddress("127.0.0.1", 9999), 322649, new_dests), 146 new Message(null, a2, new Long (2))}; 147 MyTransport transport=new MyTransport(async, responses); 148 GroupRequest req=new GroupRequest(new Message(), transport, dests, GroupRequest.GET_ALL, 0, 2); 149 transport.setGroupRequest(req); 150 boolean rc=req.execute(); 151 System.out.println("group request is " + req); 152 assertTrue(rc); 153 assertEquals("suspects are " + req.getSuspects(), 0, req.getSuspects().size()); 154 assertTrue(req.isDone()); 155 RspList results=req.getResults(); 156 assertEquals(2, results.size()); 157 } 158 159 160 private void _testMessageReceptionWithViewChangeMemberLeft(boolean async) throws Exception { 161 Vector <Address> new_dests=new Vector <Address>(); 162 new_dests.add(a2); 163 Object [] responses=new Object []{new Message(null, a2, new Long (1)), 164 new View(new IpAddress("127.0.0.1", 9999), 322649, new_dests)}; 165 MyTransport transport=new MyTransport(async, responses); 166 GroupRequest req=new GroupRequest(new Message(), transport, dests, GroupRequest.GET_ALL, 0, 2); 167 168 transport.setGroupRequest(req); 169 System.out.println("group request before execution: " + req); 170 boolean rc=req.execute(); 171 System.out.println("group request after execution: " + req); 172 assertTrue(rc); 173 assertEquals("suspects are " + req.getSuspects(), 1, req.getSuspects().size()); 174 assertTrue(req.isDone()); 175 RspList results=req.getResults(); 176 assertEquals(2, results.size()); 177 } 178 179 180 181 public static Test suite() { 182 return new TestSuite(GroupRequestTest.class); 183 } 184 185 186 public static void main(String [] args) { 187 junit.textui.TestRunner.run(suite()); 188 } 189 190 protected static class MyTransport implements Transport { 191 GroupRequest request; 192 boolean async=true; 193 Object [] responses=null; 194 195 public MyTransport(boolean async, Object [] responses) { 196 this.async=async; 197 this.responses=responses; 198 } 199 200 public void setGroupRequest(GroupRequest r) { 201 request=r; 202 } 203 204 public void send(Message msg) throws Exception { 205 if(async) { 206 new Thread () { 207 public void run() { 208 sendResponses(); 209 } 210 }.start(); 211 } 212 else { 213 sendResponses(); 214 } 215 } 216 217 public Object receive(long timeout) throws Exception { 218 return null; 219 } 220 221 void sendResponses() { 222 if(responses != null) { 223 Object obj; 224 for(int i=0; i < responses.length; i++) { 225 obj=responses[i]; 226 if(obj == null) { 227 System.err.println("object was null"); 228 continue; 229 } 230 if(obj instanceof Message) { 231 Message msg=(Message)obj; 232 Address sender=msg.getSrc(); 233 Object retval=null; 234 try { 235 retval=Util.objectFromByteBuffer(msg.getBuffer()); 236 } 237 catch(Exception e) { 238 e.printStackTrace(); 239 } 240 request.receiveResponse(retval, sender); 241 } 242 else if(obj instanceof SuspectEvent) 243 request.suspect((Address)((SuspectEvent)obj).getMember()); 244 else if(obj instanceof View) 245 request.viewChange((View)obj); 246 else 247 System.err.println("Object needs to be Message, SuspectEvent or View"); 248 } 249 } 250 } 251 } 252 253 254 260 private static final class MyDelayedTransport extends MyTransport { 261 long delay; 262 263 public MyDelayedTransport(boolean async, Object [] responses) { 264 super(async, responses); 265 } 266 267 public MyDelayedTransport(boolean async, Object [] responses, long delay) { 268 super(async, responses); 269 this.delay = delay; 270 } 271 272 273 void sendResponses() { 274 if (responses != null) { 275 Object obj; 276 for (int i = 0; i < responses.length; i++) { 277 try { 278 Thread.sleep(delay); 279 } catch (InterruptedException e1) { 280 e1.printStackTrace(); 281 } 282 283 obj = responses[i]; 284 if (obj == null) { 285 System.err.println("object was null"); 286 continue; 287 } 288 if (obj instanceof Message) { 289 Message msg = (Message) obj; 290 Address sender = msg.getSrc(); 291 Object retval = null; 292 try { 293 retval = Util.objectFromByteBuffer(msg.getBuffer()); 294 } catch (Exception e) { 295 e.printStackTrace(); 296 } 297 request.receiveResponse(retval, sender); 298 } else if (obj instanceof SuspectEvent) 299 request.suspect((Address) ((SuspectEvent) obj).getMember()); 300 else if (obj instanceof View) 301 request.viewChange((View) obj); 302 else 303 System.err.println("Object needs to be Message, SuspectEvent or View"); 304 } 305 } 306 } 307 } 308 309 } 310 | Popular Tags |