KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > blocks > GroupRequestTest


1 // $Id: GroupRequestTest.java,v 1.3 2007/07/13 10:06:30 belaban Exp $$
2

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 JavaDoc;
14
15 public class GroupRequestTest extends TestCase {
16     // GroupRequest req;
17
Address a1, a2;
18     Vector JavaDoc<Address> dests=null;
19     // private MyTransport transport;
20

21     public GroupRequestTest(String JavaDoc testName) {
22         super(testName);
23     }
24
25     protected void setUp() throws Exception JavaDoc {
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 JavaDoc<Address>(2);
30         dests.add(a1);
31         dests.add(a2);
32     }
33
34     protected void tearDown() throws Exception JavaDoc {
35         dests.clear();
36         super.tearDown();
37     }
38
39     public void testMessageTimeout() throws Exception JavaDoc {
40         _testMessageTimeout(true);
41     }
42
43     public void testMessageReception() throws Exception JavaDoc {
44         _testMessageReception(true);
45         _testMessageReception(false);
46     }
47
48
49     public void testMessageReceptionWithSuspect() throws Exception JavaDoc {
50         _testMessageReceptionWithSuspect(true);
51         _testMessageReceptionWithSuspect(false);
52     }
53
54
55     public void testMessageReceptionWithViewChange() throws Exception JavaDoc {
56         _testMessageReceptionWithViewChange(true);
57         _testMessageReceptionWithViewChange(false);
58     }
59
60     public void testMessageReceptionWithViewChangeMemberLeft() throws Exception JavaDoc {
61         _testMessageReceptionWithViewChangeMemberLeft(true);
62         _testMessageReceptionWithViewChangeMemberLeft(false);
63     }
64
65     /**
66      * test group timeout. demonstrates that the timeout mechanism times out too
67      * quickly as multiple responses are received by the GroupRequest.
68      * Demonstrates by group request receiving multiple messages in a timeframe
69      * less than the total timeout. the request will fail, as after each
70      * received message, the request alters the total timeout.
71      *
72      * @throws Exception
73      */

74     private void _testMessageTimeout(boolean async) throws Exception JavaDoc {
75         
76         // need multiple destinations to replicate error
77
int destCount = 10;
78         
79         // total timeout to hear from all members
80
final long timeout = destCount * 300;
81         
82         // how long each destination should delay
83
final long delay = 75L;
84         Object JavaDoc[] responses = new Message[destCount];
85         
86         dests = new Vector JavaDoc<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             // how long does this simulated destination take to execute? the sum is just less than the total timeout
91
responses[i] = new Message(null, addr, new Long JavaDoc(i));
92         }
93         
94         MyDelayedTransport tp = new MyDelayedTransport(async, responses, delay);
95         
96         // instantiate request with dummy correlator
97
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 JavaDoc {
111         Object JavaDoc[] responses=new Message[]{new Message(null, a1, new Long JavaDoc(1)),new Message(null, a2, new Long JavaDoc(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 JavaDoc {
125          Object JavaDoc[] responses=new Object JavaDoc[]{new Message(null, a1, new Long JavaDoc(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 JavaDoc {
140         Vector JavaDoc<Address> new_dests=new Vector JavaDoc<Address>();
141         new_dests.add(a1);
142         new_dests.add(a2);
143         new_dests.add(new IpAddress("127.0.0.1", 3333));
144         Object JavaDoc[] responses=new Object JavaDoc[]{new Message(null, a1, new Long JavaDoc(1)),
145                                         new View(new IpAddress("127.0.0.1", 9999), 322649, new_dests),
146                                         new Message(null, a2, new Long JavaDoc(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 JavaDoc {
161         Vector JavaDoc<Address> new_dests=new Vector JavaDoc<Address>();
162         new_dests.add(a2);
163         Object JavaDoc[] responses=new Object JavaDoc[]{new Message(null, a2, new Long JavaDoc(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 JavaDoc[] 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 JavaDoc[] responses=null;
194
195         public MyTransport(boolean async, Object JavaDoc[] 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 JavaDoc {
205             if(async) {
206                 new Thread JavaDoc() {
207                     public void run() {
208                         sendResponses();
209                     }
210                 }.start();
211             }
212             else {
213                 sendResponses();
214             }
215         }
216
217         public Object JavaDoc receive(long timeout) throws Exception JavaDoc {
218             return null;
219         }
220
221         void sendResponses() {
222             if(responses != null) {
223                 Object JavaDoc 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 JavaDoc retval=null;
234                         try {
235                             retval=Util.objectFromByteBuffer(msg.getBuffer());
236                         }
237                         catch(Exception JavaDoc 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     /**
255      * transport with set delays between messages
256      *
257      * @author bgodfrey
258      *
259      */

260     private static final class MyDelayedTransport extends MyTransport {
261         long delay;
262
263         public MyDelayedTransport(boolean async, Object JavaDoc[] responses) {
264             super(async, responses);
265         }
266
267         public MyDelayedTransport(boolean async, Object JavaDoc[] responses, long delay) {
268             super(async, responses);
269             this.delay = delay;
270         }
271
272
273         void sendResponses() {
274             if (responses != null) {
275                 Object JavaDoc obj;
276                 for (int i = 0; i < responses.length; i++) {
277                     try {
278                         Thread.sleep(delay);
279                     } catch (InterruptedException JavaDoc 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 JavaDoc retval = null;
292                         try {
293                             retval = Util.objectFromByteBuffer(msg.getBuffer());
294                         } catch (Exception JavaDoc 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