001: // $Id: SizeTest.java,v 1.17 2006/09/09 13:16:35 belaban Exp $$
002:
003: package org.jgroups.tests;
004:
005: import junit.framework.Test;
006: import junit.framework.TestCase;
007: import junit.framework.TestSuite;
008: import org.jgroups.*;
009: import org.jgroups.mux.ServiceInfo;
010: import org.jgroups.mux.MuxHeader;
011: import org.jgroups.blocks.RequestCorrelator;
012: import org.jgroups.conf.ClassConfigurator;
013: import org.jgroups.protocols.*;
014: import org.jgroups.protocols.FD;
015: import org.jgroups.protocols.pbcast.*;
016: import org.jgroups.protocols.pbcast.NakAckHeader;
017: import org.jgroups.protocols.pbcast.STABLE;
018: import org.jgroups.protocols.pbcast.Digest;
019: import org.jgroups.protocols.pbcast.GMS;
020: import org.jgroups.protocols.pbcast.STATE_TRANSFER;
021: import org.jgroups.stack.IpAddress;
022: import org.jgroups.util.Util;
023: import org.jgroups.util.Streamable;
024:
025: import java.util.*;
026: import java.io.ByteArrayOutputStream;
027: import java.io.DataOutputStream;
028: import java.io.ByteArrayInputStream;
029: import java.io.DataInputStream;
030:
031: /**
032: * Tests whether method size() of a header and its serialized size correspond
033: */
034: public class SizeTest extends TestCase {
035:
036: public SizeTest(String name) {
037: super (name);
038: }
039:
040: static {
041: try {
042: ClassConfigurator.getInstance(true);
043: } catch (ChannelException e) {
044: e.printStackTrace();
045: }
046: }
047:
048: public void testUdpHeader() throws Exception {
049: _testSize(new UdpHeader("DemoChannel"));
050: }
051:
052: public void testTpHeader() throws Exception {
053: _testSize(new TpHeader("DemoChannel"));
054: }
055:
056: public void testPingHeader() throws Exception {
057: _testSize(new PingHeader(PingHeader.GET_MBRS_REQ, null));
058: IpAddress self = new IpAddress("127.0.0.1", 5555);
059: PingRsp rsp = new PingRsp(self, self, true);
060: _testSize(new PingHeader(PingHeader.GET_MBRS_RSP, rsp));
061: }
062:
063: public void testNakackHeader() throws Exception {
064: _testSize(new NakAckHeader(NakAckHeader.MSG, 322649));
065: _testSize(new NakAckHeader(NakAckHeader.XMIT_REQ, 100, 104,
066: new IpAddress("127.0.0.1", 5655)));
067: _testSize(new NakAckHeader(NakAckHeader.XMIT_RSP, 100, 104,
068: new IpAddress("127.0.0.1", 5655)));
069: _testSize(new NakAckHeader(NakAckHeader.XMIT_RSP, 322649));
070: }
071:
072: public void testFdHeaders() throws Exception {
073: FD.FdHeader hdr = new FD.FdHeader(FD.FdHeader.HEARTBEAT_ACK);
074: _testSize(hdr);
075:
076: IpAddress a1 = new IpAddress("127.0.0.1", 5555);
077: IpAddress a2 = new IpAddress("127.0.0.1", 6666);
078: Vector suspects = new Vector();
079: suspects.add(a1);
080: suspects.add(a2);
081: hdr = new FD.FdHeader(FD.FdHeader.SUSPECT, suspects, a1);
082: _testSize(hdr);
083:
084: FD_SOCK.FdHeader sockhdr = new FD_SOCK.FdHeader(
085: FD_SOCK.FdHeader.GET_CACHE);
086: _testSize(sockhdr);
087:
088: sockhdr = new FD_SOCK.FdHeader(FD_SOCK.FdHeader.SUSPECT,
089: new IpAddress("127.0.0.1", 5555));
090: _testSize(sockhdr);
091:
092: sockhdr = new FD_SOCK.FdHeader(FD_SOCK.FdHeader.SUSPECT,
093: suspects);
094: _testSize(sockhdr);
095:
096: Hashtable cache = new Hashtable();
097: cache.put(a1, a2);
098: cache.put(a2, a1);
099: sockhdr = new FD_SOCK.FdHeader(FD_SOCK.FdHeader.SUSPECT, cache);
100: _testSize(sockhdr);
101: }
102:
103: public void testUnicastHeader() throws Exception {
104: UNICAST.UnicastHeader hdr = new UNICAST.UnicastHeader(
105: UNICAST.UnicastHeader.DATA, 322649);
106: _testSize(hdr);
107: }
108:
109: public void testStableHeader() throws Exception {
110: org.jgroups.protocols.pbcast.STABLE.StableHeader hdr;
111: Digest digest = new Digest(2);
112: IpAddress addr = new IpAddress("127.0.0.1", 5555);
113: digest.add(addr, 100, 200, 205);
114: hdr = new STABLE.StableHeader(
115: STABLE.StableHeader.STABLE_GOSSIP, digest);
116: _testSize(hdr);
117:
118: hdr = new STABLE.StableHeader(STABLE.StableHeader.STABILITY,
119: null);
120: _testSize(hdr);
121: }
122:
123: public void testSequencerHeader() throws Exception {
124: org.jgroups.protocols.SEQUENCER.SequencerHeader hdr;
125: IpAddress addr = new IpAddress("127.0.0.1", 5555);
126: hdr = new SEQUENCER.SequencerHeader((byte) 1, addr, 1L);
127: _testSize(hdr);
128: hdr = new SEQUENCER.SequencerHeader((byte) 2, null, -1L);
129: _testSize(hdr);
130: }
131:
132: public void testAddressVector() throws Exception {
133: Vector v = new Vector();
134: _testSize(v);
135: v.add(new IpAddress(1111));
136: _testSize(v);
137: v.add(new IpAddress(2222));
138: _testSize(v);
139: }
140:
141: public void testViewId() throws Exception {
142: ViewId vid = new ViewId();
143: _testSize(vid);
144:
145: vid = new ViewId(new IpAddress(5555));
146: _testSize(vid);
147:
148: vid = new ViewId(new IpAddress(5555), 322649);
149: _testSize(vid);
150: }
151:
152: public void testView() throws Exception {
153: View v = new View();
154: _testSize(v);
155:
156: ViewId vid = new ViewId(new IpAddress(1111), 322649);
157: Vector mbrs = new Vector();
158: v = new View(vid, mbrs);
159: _testSize(v);
160: mbrs.add(new IpAddress(3333));
161: _testSize(v);
162: mbrs.add(new IpAddress(1111));
163: _testSize(v);
164: }
165:
166: public void testMergeView() throws Exception {
167: View v = new MergeView();
168: _testSize(v);
169:
170: ViewId vid = new ViewId(new IpAddress(1111), 322649);
171: Vector mbrs = new Vector();
172: v = new MergeView(vid, mbrs, null);
173: _testSize(v);
174: mbrs.add(new IpAddress(3333));
175: _testSize(v);
176: mbrs.add(new IpAddress(1111));
177: _testSize(v);
178: }
179:
180: public void testMergeView2() throws Exception {
181: Vector m1, m2, m3, all, subgroups;
182: Address a, b, c, d, e, f;
183: View v1, v2, v3, view_all;
184:
185: a = new IpAddress(1000);
186: b = new IpAddress(2000);
187: c = new IpAddress(3000);
188: d = new IpAddress(4000);
189: e = new IpAddress(5000);
190: f = new IpAddress(6000);
191:
192: m1 = new Vector();
193: m2 = new Vector();
194: m3 = new Vector();
195: all = new Vector();
196: subgroups = new Vector();
197: m1.add(a);
198: m1.add(b);
199: m1.add(c);
200: m2.add(d);
201: m3.add(e);
202: m3.add(f);
203: all.add(a);
204: all.add(b);
205: all.add(c);
206: all.add(d);
207: all.add(e);
208: all.add(f);
209:
210: v1 = new View(a, 1, m1);
211: v2 = new View(d, 2, m2);
212: v3 = new View(e, 3, m3);
213: subgroups.add(v1);
214: subgroups.add(v2);
215: subgroups.add(v3);
216:
217: view_all = new MergeView(a, 5, all, subgroups);
218: System.out.println("MergeView: " + view_all);
219: _testSize(view_all);
220: }
221:
222: public void testMergeView3() throws Exception {
223: Vector m1, m2, m3, all, subgroups;
224: Address a, b, c, d, e, f;
225: View v1, v2, v3, v4, v5, view_all;
226:
227: a = new IpAddress(1000);
228: b = new IpAddress(2000);
229: c = new IpAddress(3000);
230: d = new IpAddress(4000);
231: e = new IpAddress(5000);
232: f = new IpAddress(6000);
233:
234: m1 = new Vector();
235: m2 = new Vector();
236: m3 = new Vector();
237: all = new Vector();
238: subgroups = new Vector();
239: m1.add(a);
240: m1.add(b);
241: m1.add(c);
242: m2.add(d);
243: m3.add(e);
244: m3.add(f);
245: all.add(a);
246: all.add(b);
247: all.add(c);
248: all.add(d);
249: all.add(e);
250: all.add(f);
251:
252: v1 = new View(a, 1, m1);
253: v2 = new MergeView(d, 2, m2, new Vector());
254: v3 = new View(e, 3, m3);
255: v4 = new MergeView(e, 4, m3, null);
256: subgroups.add(v1);
257: subgroups.add(v2);
258: subgroups.add(v3);
259: subgroups.add(v4);
260:
261: view_all = new MergeView(a, 5, all, subgroups);
262: System.out.println("MergeView: " + view_all);
263: _testSize(view_all);
264: }
265:
266: public void testViewSyncHeader() throws Exception {
267: Address creator = new IpAddress("localhost", 12345);
268: Vector members = new Vector();
269: members.add(new IpAddress(5555));
270: members.add(creator);
271: View view = new View(creator, 322649, members);
272: VIEW_SYNC.ViewSyncHeader hdr = new VIEW_SYNC.ViewSyncHeader(
273: VIEW_SYNC.ViewSyncHeader.VIEW_SYNC, view);
274: _testSize(hdr);
275:
276: view = new MergeView();
277: hdr = new VIEW_SYNC.ViewSyncHeader(
278: VIEW_SYNC.ViewSyncHeader.VIEW_SYNC, view);
279: _testSize(hdr);
280:
281: Vector subgroups = new Vector();
282: subgroups.add(view);
283: view = new MergeView(creator, 322649, members, subgroups);
284: hdr = new VIEW_SYNC.ViewSyncHeader(
285: VIEW_SYNC.ViewSyncHeader.VIEW_SYNC, view);
286: _testSize(hdr);
287: }
288:
289: public void testJoinRsp() throws Exception {
290: JoinRsp rsp;
291: Vector members = new Vector();
292:
293: members.add(new IpAddress(1111));
294: members.add(new IpAddress(2222));
295: View v = new View(new IpAddress(1234), 322649, members);
296: Digest d = new Digest(3);
297: d.add(new IpAddress(3524), 1, 2, 3);
298: d.add(new IpAddress(1324), 3, 4, 5);
299: rsp = new JoinRsp();
300: _testSize(rsp);
301: rsp = new JoinRsp(v, d);
302: _testSize(rsp);
303: rsp = new JoinRsp("this is a failure");
304: _testSize(rsp);
305: }
306:
307: public void testGmsHeader() throws Exception {
308: IpAddress addr = new IpAddress("127.0.0.1", 5555);
309: GMS.GmsHeader hdr = new GMS.GmsHeader(GMS.GmsHeader.JOIN_REQ,
310: addr);
311: _testSize(hdr);
312:
313: Vector members = new Vector();
314: members.add(addr);
315: members.add(addr);
316: View v = new View(addr, 33, members);
317: hdr = new GMS.GmsHeader(GMS.GmsHeader.JOIN_RSP, v);
318: _testSize(hdr);
319:
320: }
321:
322: public void testFCHeader() throws Exception {
323: FC.FcHeader hdr = new FC.FcHeader(FC.FcHeader.REPLENISH);
324: _testSize(hdr);
325: }
326:
327: public void testFragHeader() throws Exception {
328: FragHeader hdr = new FragHeader(322649, 1, 10);
329: _testSize(hdr);
330: }
331:
332: public void testCompressHeader() throws Exception {
333: COMPRESS.CompressHeader hdr = new COMPRESS.CompressHeader(2002);
334: _testSize(hdr);
335: }
336:
337: public void testStateHeader() throws Exception {
338: IpAddress addr = new IpAddress("127.0.0.1", 5555);
339: STATE_TRANSFER.StateHeader hdr;
340: hdr = new STATE_TRANSFER.StateHeader(
341: STATE_TRANSFER.StateHeader.STATE_REQ, addr, 322649,
342: null);
343: _testSize(hdr);
344:
345: hdr = new STATE_TRANSFER.StateHeader(
346: STATE_TRANSFER.StateHeader.STATE_REQ, addr, 322649,
347: null, "my_state");
348: _testSize(hdr);
349:
350: Digest digest = new Digest(2);
351: digest.add(addr, 100, 200, 205);
352: digest.add(new IpAddress(2314), 102, 104, 105);
353: hdr = new STATE_TRANSFER.StateHeader(
354: STATE_TRANSFER.StateHeader.STATE_RSP, addr, 322649,
355: digest);
356: _testSize(hdr);
357:
358: hdr = new STATE_TRANSFER.StateHeader(
359: STATE_TRANSFER.StateHeader.STATE_RSP, addr, 322649,
360: digest, "my_state");
361: _testSize(hdr);
362: }
363:
364: public void testEncryptHeader() throws Exception {
365: ENCRYPT.EncryptHeader hdr = new ENCRYPT.EncryptHeader(
366: (short) 1, null);
367: _testSize(hdr);
368: hdr = new ENCRYPT.EncryptHeader((short) 2, "Hello world");
369: _testSize(hdr);
370: }
371:
372: public void testIpAddress() throws Exception {
373: IpAddress addr = new IpAddress();
374: _testSize(addr);
375: }
376:
377: public void testIpAddress1() throws Exception {
378: IpAddress addr = new IpAddress("127.0.0.1", 5555);
379: _testSize(addr);
380: }
381:
382: public void testIpAddress2() throws Exception {
383: IpAddress addr = new IpAddress(3456);
384: _testSize(addr);
385: }
386:
387: public void testIpAddress3() throws Exception {
388: IpAddress addr = new IpAddress(5555, false);
389: _testSize(addr);
390: }
391:
392: public void testIpAddressWithAdditionalData() throws Exception {
393: IpAddress addr = new IpAddress(5555, false);
394: addr.setAdditionalData("bela".getBytes());
395: _testSize(addr);
396: }
397:
398: public void testRequestCorrelatorHeader() throws Exception {
399: RequestCorrelator.Header hdr;
400:
401: hdr = new RequestCorrelator.Header(
402: RequestCorrelator.Header.REQ, 322649, false,
403: "HelloWorld");
404: _testSize(hdr);
405:
406: hdr = new RequestCorrelator.Header(
407: RequestCorrelator.Header.RSP, 322649, true, "bla");
408: java.util.List l = new LinkedList();
409: l.add(new IpAddress(1111));
410: l.add(new IpAddress(2222));
411: hdr.dest_mbrs = l;
412: hdr.callStack = new Stack();
413: hdr.callStack.push(new IpAddress(2222));
414: hdr.callStack.push(new IpAddress(3333));
415: _testSize(hdr);
416:
417: hdr = new RequestCorrelator.Header(
418: RequestCorrelator.Header.RSP, 322649, true, "bla");
419: hdr.callStack = new Stack();
420: hdr.callStack.push(new IpAddress(2222));
421: hdr.callStack.push(new IpAddress(3333));
422:
423: ByteArrayOutputStream output = new ByteArrayOutputStream();
424: DataOutputStream out = new DataOutputStream(output);
425: hdr.writeTo(out);
426: out.flush();
427:
428: byte[] buf = output.toByteArray();
429: out.close();
430:
431: ByteArrayInputStream input = new ByteArrayInputStream(buf);
432: DataInputStream in = new DataInputStream(input);
433:
434: hdr = new RequestCorrelator.Header();
435: hdr.readFrom(in);
436: System.out.println("call stack is " + hdr.callStack);
437:
438: Address tmp = (Address) hdr.callStack.pop();
439: assertEquals(tmp, new IpAddress(3333));
440: tmp = (Address) hdr.callStack.pop();
441: assertEquals(tmp, new IpAddress(2222));
442: assertEquals(322649, hdr.id);
443: assertTrue(hdr.rsp_expected);
444: assertEquals("bla", hdr.corrName);
445: assertEquals(hdr.type, RequestCorrelator.Header.RSP);
446: }
447:
448: public void testServiceInfo() throws Exception {
449: ServiceInfo si = new ServiceInfo();
450: _testSize(si);
451:
452: si = new ServiceInfo(ServiceInfo.STATE_REQ, null, null, null);
453: _testSize(si);
454:
455: si = new ServiceInfo(ServiceInfo.STATE_REQ, "bla", null, null);
456: _testSize(si);
457:
458: si = new ServiceInfo(ServiceInfo.STATE_REQ, null,
459: new IpAddress(3333), null);
460: _testSize(si);
461:
462: si = new ServiceInfo(ServiceInfo.STATE_REQ, null, null,
463: new byte[] { 'b', 'e', 'l', 'a' });
464: _testSize(si);
465:
466: si = new ServiceInfo(ServiceInfo.STATE_REQ, "bla",
467: new IpAddress(3333), new byte[] { 'b', 'e', 'l', 'a' });
468: _testSize(si);
469: }
470:
471: public void testMuxHeader() throws Exception {
472: MuxHeader hdr = new MuxHeader();
473: _testSize(hdr);
474:
475: hdr = new MuxHeader("bla");
476: _testSize(hdr);
477:
478: ServiceInfo si = new ServiceInfo();
479: hdr = new MuxHeader(si);
480: _testSize(hdr);
481:
482: si = new ServiceInfo(ServiceInfo.STATE_REQ, null, null, null);
483: _testSize(new MuxHeader(si));
484:
485: si = new ServiceInfo(ServiceInfo.STATE_REQ, "bla", null, null);
486: _testSize(new MuxHeader(si));
487:
488: si = new ServiceInfo(ServiceInfo.STATE_REQ, null,
489: new IpAddress(3333), null);
490: _testSize(new MuxHeader(si));
491:
492: si = new ServiceInfo(ServiceInfo.STATE_REQ, null, null,
493: new byte[] { 'b', 'e', 'l', 'a' });
494: _testSize(new MuxHeader(si));
495:
496: si = new ServiceInfo(ServiceInfo.STATE_REQ, "bla",
497: new IpAddress(3333), new byte[] { 'b', 'e', 'l', 'a' });
498: _testSize(new MuxHeader(si));
499: }
500:
501: private void _testSize(Header hdr) throws Exception {
502: long size = hdr.size();
503: byte[] serialized_form = Util
504: .streamableToByteBuffer((Streamable) hdr);
505: System.out.println("size=" + size + ", serialized size="
506: + serialized_form.length);
507: assertEquals(serialized_form.length, size);
508: }
509:
510: private void _testSize(VIEW_SYNC.ViewSyncHeader hdr)
511: throws Exception {
512: long size = hdr.size();
513: byte[] serialized_form = Util.streamableToByteBuffer(hdr);
514: System.out.println("size=" + size + ", serialized size="
515: + serialized_form.length);
516: assertEquals(serialized_form.length, size);
517:
518: VIEW_SYNC.ViewSyncHeader hdr2 = (VIEW_SYNC.ViewSyncHeader) Util
519: .streamableFromByteBuffer(
520: VIEW_SYNC.ViewSyncHeader.class, serialized_form);
521:
522: int my_type = hdr.getType(), other_type = hdr2.getType();
523: View my_view = hdr.getView(), other_view = hdr2.getView();
524: System.out.println("my_type=" + my_type + ", other_type="
525: + other_type);
526: System.out.println("my_view=" + my_view + ", other_view="
527: + other_view);
528: assertEquals(my_type, other_type);
529: assertEquals(my_view, other_view);
530: }
531:
532: private void _testSize(Address addr) throws Exception {
533: long size = addr.size();
534: byte[] serialized_form = Util.streamableToByteBuffer(addr);
535: System.out.println("size=" + size + ", serialized size="
536: + serialized_form.length);
537: assertEquals(serialized_form.length, size);
538: }
539:
540: private void _testSize(ViewId vid) throws Exception {
541: long size = vid.serializedSize();
542: byte[] serialized_form = Util.streamableToByteBuffer(vid);
543: System.out.println("size=" + size + ", serialized size="
544: + serialized_form.length);
545: assertEquals(serialized_form.length, size);
546: }
547:
548: private void _testSize(View v) throws Exception {
549: long size = v.serializedSize();
550: byte[] serialized_form = Util.streamableToByteBuffer(v);
551: System.out.println("size=" + size + ", serialized size="
552: + serialized_form.length);
553: assertEquals(serialized_form.length, size);
554: }
555:
556: private void _testSize(Collection coll) throws Exception {
557: long size = Util.size(coll);
558: byte[] serialized_form = Util.collectionToByteBuffer(coll);
559: System.out.println("size=" + size + ", serialized size="
560: + serialized_form.length);
561: assertEquals(serialized_form.length, size);
562: }
563:
564: private void _testSize(JoinRsp rsp) throws Exception {
565: long size = rsp.serializedSize();
566: byte[] serialized_form = Util.streamableToByteBuffer(rsp);
567: System.out.println("size=" + size + ", serialized size="
568: + serialized_form.length);
569: assertEquals(serialized_form.length, size);
570: }
571:
572: private void _testSize(ServiceInfo si) throws Exception {
573: long size = si.size();
574: byte[] serialized_form = Util.streamableToByteBuffer(si);
575: System.out.println("size=" + size + ", serialized size="
576: + serialized_form.length);
577: assertEquals(serialized_form.length, size);
578: }
579:
580: private void _testSize(MuxHeader hdr) throws Exception {
581: long size = hdr.size();
582: byte[] serialized_form = Util.streamableToByteBuffer(hdr);
583: System.out.println("size=" + size + ", serialized size="
584: + serialized_form.length);
585: assertEquals(serialized_form.length, size);
586: }
587:
588: public static Test suite() {
589: return new TestSuite(SizeTest.class);
590: }
591:
592: public static void main(String[] args) {
593: junit.textui.TestRunner.run(suite());
594: }
595: }
|