001: // $Id: FCTest.java,v 1.10 2006/04/23 13:17:44 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.Event;
009: import org.jgroups.Message;
010: import org.jgroups.View;
011: import org.jgroups.debug.Simulator;
012: import org.jgroups.protocols.FC;
013: import org.jgroups.stack.IpAddress;
014: import org.jgroups.stack.Protocol;
015: import org.jgroups.util.Util;
016:
017: import java.util.Properties;
018: import java.util.Vector;
019:
020: /**
021: * Tests the flow control (FC) protocol
022: * @author Bela Ban
023: */
024: public class FCTest extends TestCase {
025: IpAddress a1;
026: Vector members;
027: View v;
028: Simulator s;
029:
030: final int SIZE = 1000; // bytes
031: final int NUM_MSGS = 100000;
032:
033: public FCTest(String name) {
034: super (name);
035: }
036:
037: public void setUp() throws Exception {
038: super .setUp();
039: a1 = new IpAddress(1111);
040: members = new Vector();
041: members.add(a1);
042: v = new View(a1, 1, members);
043: s = new Simulator();
044: s.setLocalAddress(a1);
045: s.setView(v);
046: s.addMember(a1);
047: FC fc = new FC();
048: Properties props = new Properties();
049: props.setProperty("max_credits", "10000");
050: props.setProperty("min_credits", "1000");
051: props.setProperty("max_block_time", "1000");
052: fc.setProperties(props);
053: Protocol[] stack = new Protocol[] { fc };
054: s.setProtocolStack(stack);
055: s.start();
056: }
057:
058: public void tearDown() throws Exception {
059: super .tearDown();
060: s.stop();
061: }
062:
063: public void testReceptionOfAllMessages() {
064: int num_received = 0;
065: Receiver r = new Receiver();
066: s.setReceiver(r);
067: for (int i = 1; i <= NUM_MSGS; i++) {
068: Message msg = new Message(null, null, createPayload(SIZE));
069: Event evt = new Event(Event.MSG, msg);
070: s.send(evt);
071: if (i % 1000 == 0)
072: System.out.println("==> " + i);
073: }
074: int num_tries = 10;
075: while (num_tries > 0) {
076: Util.sleep(1000);
077: num_received = r.getNumberOfReceivedMessages();
078: System.out.println("-- num received=" + num_received
079: + ", stats:\n" + s.dumpStats());
080: if (num_received >= NUM_MSGS)
081: break;
082: num_tries--;
083: }
084: assertEquals(num_received, NUM_MSGS);
085: }
086:
087: private static byte[] createPayload(int size) {
088: byte[] retval = new byte[size];
089: for (int i = 0; i < size; i++)
090: retval[i] = '0';
091: return retval;
092: }
093:
094: static class Receiver implements Simulator.Receiver {
095: int num_mgs_received = 0;
096:
097: public void receive(Event evt) {
098: if (evt.getType() == Event.MSG) {
099: num_mgs_received++;
100: if (num_mgs_received % 1000 == 0)
101: System.out.println("<== " + num_mgs_received);
102: }
103: }
104:
105: public int getNumberOfReceivedMessages() {
106: return num_mgs_received;
107: }
108: }
109:
110: public static Test suite() {
111: return new TestSuite(FCTest.class);
112: }
113:
114: public static void main(String[] args) {
115: junit.textui.TestRunner.run(suite());
116: }
117: }
|