KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > SizeTest


1 // $Id: SizeTest.java,v 1.1 2007/07/04 07:29:34 belaban Exp $$
2

3 package org.jgroups.tests;
4
5 import junit.framework.Test;
6 import junit.framework.TestCase;
7 import junit.framework.TestSuite;
8 import org.jgroups.*;
9 import org.jgroups.mux.ServiceInfo;
10 import org.jgroups.mux.MuxHeader;
11 import org.jgroups.blocks.RequestCorrelator;
12 import org.jgroups.conf.ClassConfigurator;
13 import org.jgroups.protocols.*;
14 import org.jgroups.protocols.FD;
15 import org.jgroups.protocols.pbcast.*;
16 import org.jgroups.protocols.pbcast.NakAckHeader;
17 import org.jgroups.protocols.pbcast.STABLE;
18 import org.jgroups.util.Digest;
19 import org.jgroups.protocols.pbcast.GMS;
20 import org.jgroups.protocols.pbcast.STATE_TRANSFER;
21 import org.jgroups.stack.IpAddress;
22 import org.jgroups.util.Util;
23 import org.jgroups.util.Streamable;
24 import org.jgroups.util.MutableDigest;
25
26 import java.util.*;
27 import java.io.ByteArrayOutputStream JavaDoc;
28 import java.io.DataOutputStream JavaDoc;
29 import java.io.ByteArrayInputStream JavaDoc;
30 import java.io.DataInputStream JavaDoc;
31
32
33 /**
34  * Tests whether method size() of a header and its serialized size correspond
35  */

36 public class SizeTest extends TestCase {
37
38
39     public SizeTest(String JavaDoc name) {
40         super(name);
41     }
42
43
44
45     static {
46         try {
47             ClassConfigurator.getInstance(true);
48         }
49         catch(ChannelException e) {
50             e.printStackTrace();
51         }
52     }
53
54
55
56     public void testUdpHeader() throws Exception JavaDoc {
57         _testSize(new UdpHeader("DemoChannel"));
58     }
59
60     public void testTpHeader() throws Exception JavaDoc {
61         _testSize(new TpHeader("DemoChannel"));
62     }
63
64     public void testPingHeader() throws Exception JavaDoc {
65         _testSize(new PingHeader(PingHeader.GET_MBRS_REQ, null));
66         IpAddress self=new IpAddress("127.0.0.1", 5555);
67         PingRsp rsp=new PingRsp(self, self, true);
68         _testSize(new PingHeader(PingHeader.GET_MBRS_RSP, rsp));
69     }
70
71     public void testNakackHeader() throws Exception JavaDoc {
72         _testSize(new NakAckHeader(NakAckHeader.MSG, 322649));
73         _testSize(new NakAckHeader(NakAckHeader.XMIT_REQ, 100, 104, new IpAddress("127.0.0.1", 5655)));
74         _testSize(new NakAckHeader(NakAckHeader.XMIT_RSP, 100, 104, new IpAddress("127.0.0.1", 5655)));
75         _testSize(new NakAckHeader(NakAckHeader.XMIT_RSP, 322649));
76     }
77
78     public void testFdHeaders() throws Exception JavaDoc {
79         FD.FdHeader hdr=new FD.FdHeader(FD.FdHeader.HEARTBEAT_ACK);
80         _testSize(hdr);
81
82         IpAddress a1=new IpAddress("127.0.0.1", 5555);
83         IpAddress a2=new IpAddress("127.0.0.1", 6666);
84         Vector suspects=new Vector();
85         suspects.add(a1);
86         suspects.add(a2);
87         hdr=new FD.FdHeader(FD.FdHeader.SUSPECT, suspects, a1);
88         _testSize(hdr);
89
90         FD_SOCK.FdHeader sockhdr=new FD_SOCK.FdHeader(FD_SOCK.FdHeader.GET_CACHE);
91         _testSize(sockhdr);
92
93         sockhdr=new FD_SOCK.FdHeader(FD_SOCK.FdHeader.SUSPECT, new IpAddress("127.0.0.1", 5555));
94         _testSize(sockhdr);
95
96         Set tmp=new HashSet();
97         tmp.add(a1);
98         tmp.add(a2);
99         sockhdr=new FD_SOCK.FdHeader(FD_SOCK.FdHeader.SUSPECT, tmp);
100         _testSize(sockhdr);
101
102
103         Hashtable cache=new Hashtable();
104         cache.put(a1, a2);
105         cache.put(a2, a1);
106         sockhdr=new FD_SOCK.FdHeader(FD_SOCK.FdHeader.SUSPECT, cache);
107         _testSize(sockhdr);
108     }
109
110
111     public void testFdSockHeaders() throws Exception JavaDoc {
112         FD_SOCK.FdHeader hdr=new FD_SOCK.FdHeader(FD_SOCK.FdHeader.GET_CACHE);
113         _testSize(hdr);
114
115         hdr=new FD_SOCK.FdHeader(FD_SOCK.FdHeader.GET_CACHE, new IpAddress("127.0.0.1", 4567));
116         _testSize(hdr);
117
118         Set<Address> set=new HashSet<Address>();
119         set.add(new IpAddress(3452));
120         set.add(new IpAddress("127.0.0.1", 5000));
121
122         hdr=new FD_SOCK.FdHeader(FD_SOCK.FdHeader.GET_CACHE, set);
123         _testSize(hdr);
124
125         Hashtable<Address,IpAddress> map=new Hashtable<Address,IpAddress>();
126         map.put(new IpAddress("127.0.0.1", 5000), new IpAddress(4553));
127         map.put(new IpAddress("127.0.0.1", 6000), new IpAddress(4523));
128         map.put(new IpAddress(7000), new IpAddress(4553));
129          hdr=new FD_SOCK.FdHeader(FD_SOCK.FdHeader.GET_CACHE, map);
130         _testSize(hdr);
131     }
132
133
134
135     public void testUnicastHeader() throws Exception JavaDoc {
136         UNICAST.UnicastHeader hdr=new UNICAST.UnicastHeader(UNICAST.UnicastHeader.DATA, 322649);
137         _testSize(hdr);
138     }
139
140     public void testStableHeader() throws Exception JavaDoc {
141         org.jgroups.protocols.pbcast.STABLE.StableHeader hdr;
142         IpAddress addr=new IpAddress("127.0.0.1", 5555);
143         Map map=new HashMap();
144         map.put(addr, new Digest.Entry(100, 200, 205));
145         Digest digest=new Digest(map);
146         hdr=new STABLE.StableHeader(STABLE.StableHeader.STABLE_GOSSIP, digest);
147         _testSize(hdr);
148
149         hdr=new STABLE.StableHeader(STABLE.StableHeader.STABILITY, null);
150         _testSize(hdr);
151     }
152
153     public void testStableHeader2() throws Exception JavaDoc {
154         org.jgroups.protocols.pbcast.STABLE.StableHeader hdr;
155         IpAddress addr=new IpAddress("127.0.0.1", 5555);
156         MutableDigest digest=new MutableDigest(2);
157         digest.add(addr, 100, 200, 205);
158         hdr=new STABLE.StableHeader(STABLE.StableHeader.STABLE_GOSSIP, digest);
159         _testSize(hdr);
160
161         hdr=new STABLE.StableHeader(STABLE.StableHeader.STABILITY, null);
162         _testSize(hdr);
163     }
164
165     public void testSequencerHeader() throws Exception JavaDoc {
166         org.jgroups.protocols.SEQUENCER.SequencerHeader hdr;
167         IpAddress addr=new IpAddress("127.0.0.1", 5555);
168         hdr=new SEQUENCER.SequencerHeader((byte)1, addr, 1L);
169         _testSize(hdr);
170         hdr=new SEQUENCER.SequencerHeader((byte)2, null, -1L);
171         _testSize(hdr);
172     }
173
174     public void testAddressVector() throws Exception JavaDoc {
175         Vector v=new Vector();
176         _testSize(v);
177         v.add(new IpAddress(1111));
178         _testSize(v);
179         v.add(new IpAddress(2222));
180         _testSize(v);
181     }
182
183     public void testViewId() throws Exception JavaDoc {
184         ViewId vid=new ViewId();
185         _testSize(vid);
186
187         vid=new ViewId(new IpAddress(5555));
188         _testSize(vid);
189
190         vid=new ViewId(new IpAddress(5555), 322649);
191         _testSize(vid);
192     }
193
194     public void testView() throws Exception JavaDoc {
195         View v=new View();
196         _testSize(v);
197
198         ViewId vid=new ViewId(new IpAddress(1111), 322649);
199         Vector mbrs=new Vector();
200         v=new View(vid, mbrs);
201         _testSize(v);
202         mbrs.add(new IpAddress(3333));
203         _testSize(v);
204         mbrs.add(new IpAddress(1111));
205         _testSize(v);
206     }
207
208     public void testMergeView() throws Exception JavaDoc {
209         View v=new MergeView();
210         _testSize(v);
211
212         ViewId vid=new ViewId(new IpAddress(1111), 322649);
213         Vector mbrs=new Vector();
214         v=new MergeView(vid, mbrs, null);
215         _testSize(v);
216         mbrs.add(new IpAddress(3333));
217         _testSize(v);
218         mbrs.add(new IpAddress(1111));
219         _testSize(v);
220     }
221
222
223     public void testMergeView2() throws Exception JavaDoc {
224         Vector m1, m2 , m3, all, subgroups;
225         Address a,b,c,d,e,f;
226         View v1, v2, v3, view_all;
227
228         a=new IpAddress(1000);
229         b=new IpAddress(2000);
230         c=new IpAddress(3000);
231         d=new IpAddress(4000);
232         e=new IpAddress(5000);
233         f=new IpAddress(6000);
234
235         m1=new Vector(); m2=new Vector(); m3=new Vector(); all=new Vector(); subgroups=new Vector();
236         m1.add(a); m1.add(b); m1.add(c);
237         m2.add(d);
238         m3.add(e); m3.add(f);
239         all.add(a); all.add(b); all.add(c); all.add(d); all.add(e); all.add(f);
240
241         v1=new View(a, 1, m1);
242         v2=new View(d, 2, m2);
243         v3=new View(e, 3, m3);
244         subgroups.add(v1);
245         subgroups.add(v2);
246         subgroups.add(v3);
247
248         view_all=new MergeView(a, 5, all, subgroups);
249         System.out.println("MergeView: " + view_all);
250         _testSize(view_all);
251     }
252
253
254     public void testMergeView3() throws Exception JavaDoc {
255         Vector m1, m2 , m3, all, subgroups;
256         Address a,b,c,d,e,f;
257         View v1, v2, v3, v4, view_all;
258
259         a=new IpAddress(1000);
260         b=new IpAddress(2000);
261         c=new IpAddress(3000);
262         d=new IpAddress(4000);
263         e=new IpAddress(5000);
264         f=new IpAddress(6000);
265
266         m1=new Vector(); m2=new Vector(); m3=new Vector(); all=new Vector(); subgroups=new Vector();
267         m1.add(a); m1.add(b); m1.add(c);
268         m2.add(d);
269         m3.add(e); m3.add(f);
270         all.add(a); all.add(b); all.add(c); all.add(d); all.add(e); all.add(f);
271
272         v1=new View(a, 1, m1);
273         v2=new MergeView(d, 2, m2, new Vector());
274         v3=new View(e, 3, m3);
275         v4=new MergeView(e, 4, m3, null);
276         subgroups.add(v1);
277         subgroups.add(v2);
278         subgroups.add(v3);
279         subgroups.add(v4);
280
281         view_all=new MergeView(a, 5, all, subgroups);
282         System.out.println("MergeView: " + view_all);
283         _testSize(view_all);
284     }
285
286
287     public void testViewSyncHeader() throws Exception JavaDoc {
288         Address creator=new IpAddress("localhost", 12345);
289         Vector members=new Vector();
290         members.add(new IpAddress(5555));
291         members.add(creator);
292         View view=new View(creator, 322649, members);
293         VIEW_SYNC.ViewSyncHeader hdr=new VIEW_SYNC.ViewSyncHeader(VIEW_SYNC.ViewSyncHeader.VIEW_SYNC, view);
294         _testSize(hdr);
295
296         view=new MergeView();
297         hdr=new VIEW_SYNC.ViewSyncHeader(VIEW_SYNC.ViewSyncHeader.VIEW_SYNC, view);
298         _testSize(hdr);
299
300         Vector subgroups=new Vector();
301         subgroups.add(view);
302         view=new MergeView(creator, 322649, members, subgroups);
303         hdr=new VIEW_SYNC.ViewSyncHeader(VIEW_SYNC.ViewSyncHeader.VIEW_SYNC, view);
304         _testSize(hdr);
305     }
306
307
308     public void testJoinRsp() throws Exception JavaDoc {
309         JoinRsp rsp;
310         Vector members=new Vector();
311
312         members.add(new IpAddress(1111));
313         members.add(new IpAddress(2222));
314         View v=new View(new IpAddress(1234), 322649, members);
315         MutableDigest d=new MutableDigest(3);
316         d.add(new IpAddress(3524), 1,2,3);
317         d.add(new IpAddress(1324), 3,4,5);
318         rsp=new JoinRsp();
319         _testSize(rsp);
320         rsp=new JoinRsp(v, d);
321         _testSize(rsp);
322         rsp=new JoinRsp("this is a failure");
323         _testSize(rsp);
324     }
325
326     public void testGmsHeader() throws Exception JavaDoc {
327         IpAddress addr=new IpAddress("127.0.0.1", 5555);
328         GMS.GmsHeader hdr=new GMS.GmsHeader(GMS.GmsHeader.JOIN_REQ, addr);
329         _testSize(hdr);
330
331         Vector members=new Vector();
332         members.add(addr);
333         members.add(addr);
334         View v=new View(addr, 33, members);
335         hdr=new GMS.GmsHeader(GMS.GmsHeader.JOIN_RSP, v);
336         _testSize(hdr);
337
338     }
339
340     public void testFCHeader() throws Exception JavaDoc {
341         FC.FcHeader hdr=new FC.FcHeader(FC.FcHeader.REPLENISH);
342         _testSize(hdr);
343     }
344
345     public void testFragHeader() throws Exception JavaDoc {
346         FragHeader hdr=new FragHeader(322649, 1, 10);
347         _testSize(hdr);
348     }
349
350     public void testCompressHeader() throws Exception JavaDoc {
351         COMPRESS.CompressHeader hdr=new COMPRESS.CompressHeader(2002);
352         _testSize(hdr);
353     }
354
355
356     public void testStateHeader() throws Exception JavaDoc {
357         IpAddress addr=new IpAddress("127.0.0.1", 5555);
358         STATE_TRANSFER.StateHeader hdr;
359         hdr=new STATE_TRANSFER.StateHeader(STATE_TRANSFER.StateHeader.STATE_REQ, addr, 322649, null);
360         _testSize(hdr);
361
362         hdr=new STATE_TRANSFER.StateHeader(STATE_TRANSFER.StateHeader.STATE_REQ, addr, 322649, null, "my_state");
363         _testSize(hdr);
364
365
366         MutableDigest digest=new MutableDigest(2);
367         digest.add(addr, 100, 200, 205);
368         digest.add(new IpAddress(2314), 102, 104, 105);
369         hdr=new STATE_TRANSFER.StateHeader(STATE_TRANSFER.StateHeader.STATE_RSP, addr, 322649, digest);
370         _testSize(hdr);
371
372         hdr=new STATE_TRANSFER.StateHeader(STATE_TRANSFER.StateHeader.STATE_RSP, addr, 322649, digest, "my_state");
373         _testSize(hdr);
374     }
375
376
377     public void testEncryptHeader() throws Exception JavaDoc {
378         ENCRYPT.EncryptHeader hdr=new ENCRYPT.EncryptHeader((short)1, null);
379         _testSize(hdr);
380         hdr=new ENCRYPT.EncryptHeader((short)2, "Hello world");
381         _testSize(hdr);
382     }
383
384     public void testIpAddress() throws Exception JavaDoc {
385         IpAddress addr=new IpAddress();
386         _testSize(addr);
387     }
388
389     public void testIpAddress1() throws Exception JavaDoc {
390         IpAddress addr=new IpAddress("127.0.0.1", 5555);
391         _testSize(addr);
392     }
393
394     public void testIpAddressWithHighPort() throws Exception JavaDoc {
395         IpAddress addr=new IpAddress("127.0.0.1", 65535);
396         _testSize(addr);
397     }
398
399     public void testIpAddress2() throws Exception JavaDoc {
400         IpAddress addr=new IpAddress(3456);
401         _testSize(addr);
402     }
403
404     public void testIpAddress3() throws Exception JavaDoc {
405         IpAddress addr=new IpAddress(5555, false);
406         _testSize(addr);
407     }
408
409     public void testIpAddressWithAdditionalData() throws Exception JavaDoc {
410         IpAddress addr=new IpAddress(5555, false);
411         addr.setAdditionalData("bela".getBytes());
412         _testSize(addr);
413     }
414
415
416     public void testRequestCorrelatorHeader() throws Exception JavaDoc {
417         RequestCorrelator.Header hdr;
418
419         hdr=new RequestCorrelator.Header(RequestCorrelator.Header.REQ, 322649, false, "HelloWorld");
420         _testSize(hdr);
421
422         hdr=new RequestCorrelator.Header(RequestCorrelator.Header.RSP, 322649, true, "bla");
423         java.util.List JavaDoc l=new LinkedList();
424         l.add(new IpAddress(1111));
425         l.add(new IpAddress(2222));
426         hdr.dest_mbrs=l;
427         hdr.callStack=new Stack();
428         hdr.callStack.push(new IpAddress(2222));
429         hdr.callStack.push(new IpAddress(3333));
430         _testSize(hdr);
431
432         hdr=new RequestCorrelator.Header(RequestCorrelator.Header.RSP, 322649, true, "bla");
433         hdr.callStack=new Stack();
434         hdr.callStack.push(new IpAddress(2222));
435         hdr.callStack.push(new IpAddress(3333));
436
437         ByteArrayOutputStream JavaDoc output=new ByteArrayOutputStream JavaDoc();
438         DataOutputStream JavaDoc out=new DataOutputStream JavaDoc(output);
439         hdr.writeTo(out);
440         out.flush();
441
442         byte[] buf=output.toByteArray();
443         out.close();
444
445         ByteArrayInputStream JavaDoc input=new ByteArrayInputStream JavaDoc(buf);
446         DataInputStream JavaDoc in=new DataInputStream JavaDoc(input);
447
448         hdr=new RequestCorrelator.Header();
449         hdr.readFrom(in);
450         System.out.println("call stack is " + hdr.callStack);
451
452         Address tmp=(Address)hdr.callStack.pop();
453         assertEquals(tmp, new IpAddress(3333));
454         tmp=(Address)hdr.callStack.pop();
455         assertEquals(tmp, new IpAddress(2222));
456         assertEquals(322649, hdr.id);
457         assertTrue(hdr.rsp_expected);
458         assertEquals("bla", hdr.corrName);
459         assertEquals(hdr.type, RequestCorrelator.Header.RSP);
460     }
461
462
463     public void testServiceInfo() throws Exception JavaDoc {
464         ServiceInfo si=new ServiceInfo();
465         _testSize(si);
466
467         si=new ServiceInfo(ServiceInfo.STATE_REQ, null, null, null);
468         _testSize(si);
469
470         si=new ServiceInfo(ServiceInfo.STATE_REQ, "bla", null, null);
471         _testSize(si);
472
473         si=new ServiceInfo(ServiceInfo.STATE_REQ, null, new IpAddress(3333), null);
474         _testSize(si);
475
476         si=new ServiceInfo(ServiceInfo.STATE_REQ, null, null, new byte[]{'b', 'e', 'l', 'a'});
477         _testSize(si);
478
479         si=new ServiceInfo(ServiceInfo.STATE_REQ, "bla", new IpAddress(3333), new byte[]{'b', 'e', 'l', 'a'});
480         _testSize(si);
481     }
482
483
484     public void testMuxHeader() throws Exception JavaDoc {
485         MuxHeader hdr=new MuxHeader();
486         _testSize(hdr);
487
488         hdr=new MuxHeader("bla");
489         _testSize(hdr);
490
491         ServiceInfo si=new ServiceInfo();
492         hdr=new MuxHeader(si);
493         _testSize(hdr);
494
495         si=new ServiceInfo(ServiceInfo.STATE_REQ, null, null, null);
496         _testSize(new MuxHeader(si));
497
498         si=new ServiceInfo(ServiceInfo.STATE_REQ, "bla", null, null);
499         _testSize(new MuxHeader(si));
500
501         si=new ServiceInfo(ServiceInfo.STATE_REQ, null, new IpAddress(3333), null);
502         _testSize(new MuxHeader(si));
503
504         si=new ServiceInfo(ServiceInfo.STATE_REQ, null, null, new byte[]{'b', 'e', 'l', 'a'});
505         _testSize(new MuxHeader(si));
506
507         si=new ServiceInfo(ServiceInfo.STATE_REQ, "bla", new IpAddress(3333), new byte[]{'b', 'e', 'l', 'a'});
508         _testSize(new MuxHeader(si));
509     }
510
511
512     private void _testSize(Header hdr) throws Exception JavaDoc {
513         long size=hdr.size();
514         byte[] serialized_form=Util.streamableToByteBuffer((Streamable)hdr);
515         System.out.println("size=" + size + ", serialized size=" + serialized_form.length);
516         assertEquals(serialized_form.length, size);
517     }
518
519
520     private void _testSize(VIEW_SYNC.ViewSyncHeader hdr) throws Exception JavaDoc {
521         long size=hdr.size();
522         byte[] serialized_form=Util.streamableToByteBuffer(hdr);
523         System.out.println("size=" + size + ", serialized size=" + serialized_form.length);
524         assertEquals(serialized_form.length, size);
525
526         VIEW_SYNC.ViewSyncHeader hdr2=(VIEW_SYNC.ViewSyncHeader)Util.streamableFromByteBuffer(VIEW_SYNC.ViewSyncHeader.class, serialized_form);
527
528         int my_type=hdr.getType(), other_type=hdr2.getType();
529         View my_view=hdr.getView(), other_view=hdr2.getView();
530         System.out.println("my_type=" + my_type + ", other_type=" + other_type);
531         System.out.println("my_view=" + my_view + ", other_view=" + other_view);
532         assertEquals(my_type, other_type);
533         assertEquals(my_view, other_view);
534     }
535
536
537      private void _testSize(Address addr) throws Exception JavaDoc {
538         long size=addr.size();
539         byte[] serialized_form=Util.streamableToByteBuffer(addr);
540         System.out.println("size=" + size + ", serialized size=" + serialized_form.length);
541         assertEquals(serialized_form.length, size);
542     }
543
544
545     private void _testSize(ViewId vid) throws Exception JavaDoc {
546         long size=vid.serializedSize();
547         byte[] serialized_form=Util.streamableToByteBuffer(vid);
548         System.out.println("size=" + size + ", serialized size=" + serialized_form.length);
549         assertEquals(serialized_form.length, size);
550     }
551
552     private void _testSize(View v) throws Exception JavaDoc {
553         long size=v.serializedSize();
554         byte[] serialized_form=Util.streamableToByteBuffer(v);
555         System.out.println("size=" + size + ", serialized size=" + serialized_form.length);
556         assertEquals(serialized_form.length, size);
557     }
558
559     private void _testSize(Collection coll) throws Exception JavaDoc {
560         long size=Util.size(coll);
561         byte[] serialized_form=Util.collectionToByteBuffer(coll);
562         System.out.println("size=" + size + ", serialized size=" + serialized_form.length);
563         assertEquals(serialized_form.length, size);
564     }
565
566     private void _testSize(JoinRsp rsp) throws Exception JavaDoc {
567         long size=rsp.serializedSize();
568         byte[] serialized_form=Util.streamableToByteBuffer(rsp);
569         System.out.println("size=" + size + ", serialized size=" + serialized_form.length);
570         assertEquals(serialized_form.length, size);
571     }
572
573     private void _testSize(ServiceInfo si) throws Exception JavaDoc {
574         long size=si.size();
575         byte[] serialized_form=Util.streamableToByteBuffer(si);
576         System.out.println("size=" + size + ", serialized size=" + serialized_form.length);
577         assertEquals(serialized_form.length, size);
578     }
579
580
581     private void _testSize(MuxHeader hdr) throws Exception JavaDoc {
582          long size=hdr.size();
583          byte[] serialized_form=Util.streamableToByteBuffer(hdr);
584          System.out.println("size=" + size + ", serialized size=" + serialized_form.length);
585          assertEquals(serialized_form.length, size);
586      }
587
588
589
590     public static Test suite() {
591         return new TestSuite(SizeTest.class);
592     }
593
594     public static void main(String JavaDoc[] args) {
595         junit.textui.TestRunner.run(suite());
596     }
597 }
598
Popular Tags