001: // $Id: FragTest.java,v 1.9 2006/05/12 09:49:40 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.Address;
009: import org.jgroups.Event;
010: import org.jgroups.Message;
011: import org.jgroups.debug.ProtocolTester;
012: import org.jgroups.stack.IpAddress;
013: import org.jgroups.stack.Protocol;
014: import org.jgroups.util.Util;
015:
016: /**
017: * Class to test FRAG protocol. It uses ProtocolTester to assemble a minimal stack which only consists of
018: * FRAG and LOOPBACK (messages are immediately resent up the stack). Sends NUM_MSGS with MSG_SIZE size down
019: * the stack, they should be received as well.
020: *
021: * @author Bela Ban
022: */
023: public class FragTest extends TestCase {
024: public final long NUM_MSGS = 10;
025: public final int MSG_SIZE = 100000;
026: public final int FRAG_SIZE = 24000;
027:
028: public FragTest(String name) {
029: super (name);
030: }
031:
032: private Message createBigMessage(int size) {
033: byte[] buf = new byte[size];
034: for (int i = 0; i < buf.length; i++)
035: buf[i] = (byte) 'x';
036: return new Message(null, null, buf);
037: }
038:
039: public void test0() throws Exception {
040: Object mutex = new Object();
041: FragReceiver frag_receiver = new FragReceiver(this , mutex);
042: ProtocolTester t = new ProtocolTester("FRAG(frag_size="
043: + FRAG_SIZE + ')', frag_receiver);
044: Message big_msg;
045: IpAddress local_addr = new IpAddress(5555);
046:
047: System.out.println("\nProtocol for protocol tester: "
048: + t.getProtocolSpec() + '\n');
049:
050: synchronized (mutex) {
051: for (int i = 0; i < NUM_MSGS; i++) {
052: big_msg = createBigMessage(MSG_SIZE);
053: big_msg.setSrc(local_addr);
054: System.out.println("sending msg #" + i + " ["
055: + big_msg.getLength() + " bytes]");
056: frag_receiver.down(new Event(Event.MSG, big_msg));
057: Util.sleep(10);
058: }
059: }
060: t.stop();
061: }
062:
063: public static Test suite() {
064: return new TestSuite(FragTest.class);
065: }
066:
067: public static void main(String[] args) {
068: junit.textui.TestRunner.run(suite());
069: }
070:
071: private static class FragReceiver extends Protocol {
072: long num_msgs = 0;
073: FragTest t = null;
074: Object mut = null;
075:
076: FragReceiver(FragTest t, Object mut) {
077: this .t = t;
078: this .mut = mut;
079: }
080:
081: public String getName() {
082: return "FragReceiver";
083: }
084:
085: public void up(Event evt) {
086: Message msg = null;
087: Address sender;
088:
089: if (evt == null || evt.getType() != Event.MSG)
090: return;
091: msg = (Message) evt.getArg();
092: sender = msg.getSrc();
093: if (sender == null) {
094: log
095: .error("FragTest.FragReceiver.up(): sender is null; discarding msg");
096: return;
097: }
098: System.out.println("Received msg from " + sender + " ["
099: + msg.getLength() + " bytes]");
100: }
101:
102: }
103:
104: }
|