001: // $Id: UNICAST_Test.java,v 1.1 2005/08/24 13:35:07 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.DISCARD;
013: import org.jgroups.protocols.UNICAST;
014: import org.jgroups.stack.IpAddress;
015: import org.jgroups.stack.Protocol;
016:
017: import java.nio.ByteBuffer;
018: import java.util.Properties;
019: import java.util.Vector;
020:
021: /**
022: * Tests the UNICAST protocol
023: * @author Bela Ban
024: */
025: public class UNICAST_Test extends TestCase {
026: IpAddress a1, a2;
027: Vector members;
028: View v;
029: Simulator s;
030:
031: final int SIZE = 1000; // bytes
032: final int NUM_MSGS = 10000;
033:
034: public UNICAST_Test(String name) {
035: super (name);
036: }
037:
038: public void setUp() throws Exception {
039: super .setUp();
040: }
041:
042: public void tearDown() throws Exception {
043: super .tearDown();
044: s.stop();
045: }
046:
047: public void testReceptionOfAllMessages() throws Throwable {
048: UNICAST unicast = new UNICAST();
049: Properties props = new Properties();
050: props.setProperty("timeout", "500,1000,2000,3000");
051: unicast.setProperties(props);
052: Protocol[] stack = new Protocol[] { unicast };
053: createStack(stack);
054: _testReceptionOfAllMessages();
055: }
056:
057: public void testReceptionOfAllMessagesWithDISCARD()
058: throws Throwable {
059: UNICAST unicast = new UNICAST();
060: Properties props = new Properties();
061: props.setProperty("timeout", "500,1000,2000,3000");
062: unicast.setProperties(props);
063:
064: DISCARD discard = new DISCARD();
065: props.clear();
066: props.setProperty("down", "0.1"); // discard all down message with 10% probability
067: discard.setProperties(props);
068:
069: Protocol[] stack = new Protocol[] { unicast, discard };
070: createStack(stack);
071: _testReceptionOfAllMessages();
072: }
073:
074: private static byte[] createPayload(int size, int seqno) {
075: ByteBuffer buf = ByteBuffer.allocate(size);
076: buf.putInt(seqno);
077: return buf.array();
078: }
079:
080: /** Checks that messages 1 - NUM_MSGS are received in order */
081: class Receiver implements Simulator.Receiver {
082: int num_mgs_received = 0, next = 1;
083: Throwable exception = null;
084: boolean received_all = false;
085:
086: public void receive(Event evt) {
087: if (evt.getType() == Event.MSG) {
088: if (exception != null)
089: return;
090: Message msg = (Message) evt.getArg();
091: ByteBuffer buf = ByteBuffer.wrap(msg.getRawBuffer());
092: int seqno = buf.getInt();
093: if (seqno != next) {
094: exception = new Exception("expected seqno was "
095: + next + ", but received " + seqno);
096: return;
097: }
098: next++;
099: num_mgs_received++;
100: if (num_mgs_received % 1000 == 0)
101: System.out.println("<== " + num_mgs_received);
102: if (num_mgs_received == NUM_MSGS) {
103: synchronized (this ) {
104: received_all = true;
105: this .notifyAll();
106: }
107: }
108: }
109: }
110:
111: public int getNumberOfReceivedMessages() {
112: return num_mgs_received;
113: }
114:
115: public boolean receivedAll() {
116: return received_all;
117: }
118:
119: public Throwable getException() {
120: return exception;
121: }
122: }
123:
124: private void _testReceptionOfAllMessages() throws Throwable {
125: int num_received = 0;
126: Receiver r = new Receiver();
127: s.setReceiver(r);
128: for (int i = 1; i <= NUM_MSGS; i++) {
129: Message msg = new Message(a1, null, createPayload(SIZE, i)); // unicast message
130: Event evt = new Event(Event.MSG, msg);
131: s.send(evt);
132: if (i % 1000 == 0)
133: System.out.println("==> " + i);
134: }
135: int num_tries = 10;
136: while ((num_received = r.getNumberOfReceivedMessages()) != NUM_MSGS
137: && num_tries > 0) {
138: if (r.getException() != null)
139: throw r.getException();
140: synchronized (r) {
141: try {
142: r.wait(3000);
143: } catch (InterruptedException e) {
144: }
145: }
146: num_tries--;
147: }
148: printStats(num_received);
149: assertTrue(num_received == NUM_MSGS);
150: }
151:
152: private void createStack(Protocol[] stack) throws Exception {
153: a1 = new IpAddress(1111);
154: members = new Vector();
155: members.add(a1);
156: v = new View(a1, 1, members);
157: s = new Simulator();
158: s.setLocalAddress(a1);
159: s.setView(v);
160: s.addMember(a1);
161: s.setProtocolStack(stack);
162: s.start();
163: }
164:
165: private void printStats(int num_received) {
166: System.out.println("-- num received=" + num_received
167: + ", stats:\n" + s.dumpStats());
168: }
169:
170: public static Test suite() {
171: TestSuite s = new TestSuite(UNICAST_Test.class);
172: return s;
173: }
174:
175: public static void main(String[] args) {
176: junit.textui.TestRunner.run(suite());
177: }
178: }
|