KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: UNICAST_Test.java,v 1.1 2007/07/04 07:29:33 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.Event;
9 import org.jgroups.Message;
10 import org.jgroups.View;
11 import org.jgroups.debug.Simulator;
12 import org.jgroups.protocols.DISCARD;
13 import org.jgroups.protocols.UNICAST;
14 import org.jgroups.stack.IpAddress;
15 import org.jgroups.stack.Protocol;
16
17 import java.nio.ByteBuffer JavaDoc;
18 import java.util.Properties JavaDoc;
19 import java.util.Vector JavaDoc;
20
21
22 /**
23  * Tests the UNICAST protocol
24  * @author Bela Ban
25  */

26 public class UNICAST_Test extends TestCase {
27     IpAddress a1, a2;
28     Vector JavaDoc members;
29     View v;
30     Simulator simulator;
31
32     final int SIZE=1000; // bytes
33
final int NUM_MSGS=10000;
34
35
36     public UNICAST_Test(String JavaDoc name) {
37         super(name);
38     }
39
40
41     public void setUp() throws Exception JavaDoc {
42         super.setUp();
43     }
44
45     public void tearDown() throws Exception JavaDoc {
46         super.tearDown();
47         if(simulator != null)
48             simulator.stop();
49     }
50
51
52     public void testReceptionOfAllMessages() throws Throwable JavaDoc {
53         UNICAST unicast=new UNICAST();
54         Properties JavaDoc props=new Properties JavaDoc();
55         props.setProperty("timeout", "500,1000,2000,3000");
56         unicast.setProperties(props);
57         Protocol[] stack=new Protocol[]{unicast};
58         createStack(stack);
59         _testReceptionOfAllMessages();
60     }
61
62
63     public void testReceptionOfAllMessagesWithDISCARD() throws Throwable JavaDoc {
64         UNICAST unicast=new UNICAST();
65         Properties JavaDoc props=new Properties JavaDoc();
66         props.setProperty("timeout", "500,1000,2000,3000");
67         unicast.setProperties(props);
68
69         DISCARD discard=new DISCARD();
70         props.clear();
71         props.setProperty("down", "0.1"); // discard all down message with 10% probability
72
discard.setProperties(props);
73
74         Protocol[] stack=new Protocol[]{unicast,discard};
75         createStack(stack);
76         _testReceptionOfAllMessages();
77     }
78
79
80
81     private static byte[] createPayload(int size, int seqno) {
82         ByteBuffer JavaDoc buf=ByteBuffer.allocate(size);
83         buf.putInt(seqno);
84         return buf.array();
85     }
86
87
88     /** Checks that messages 1 - NUM_MSGS are received in order */
89     class Receiver implements Simulator.Receiver {
90         int num_mgs_received=0, next=1;
91         Throwable JavaDoc exception=null;
92         boolean received_all=false;
93
94         public void receive(Event evt) {
95             if(evt.getType() == Event.MSG) {
96                 if(exception != null)
97                 return;
98                 Message msg=(Message)evt.getArg();
99                 ByteBuffer JavaDoc buf=ByteBuffer.wrap(msg.getRawBuffer());
100                 int seqno=buf.getInt();
101                 if(seqno != next) {
102                     exception=new Exception JavaDoc("expected seqno was " + next + ", but received " + seqno);
103                     return;
104                 }
105                 next++;
106                 num_mgs_received++;
107                 if(num_mgs_received % 1000 == 0)
108                     System.out.println("<== " + num_mgs_received);
109                 if(num_mgs_received == NUM_MSGS) {
110                     synchronized(this) {
111                         received_all=true;
112                         this.notifyAll();
113                     }
114                 }
115             }
116         }
117
118         public int getNumberOfReceivedMessages() {
119             return num_mgs_received;
120         }
121
122         public boolean receivedAll() {return received_all;}
123
124         public Throwable JavaDoc getException() {
125             return exception;
126         }
127     }
128
129
130     private void _testReceptionOfAllMessages() throws Throwable JavaDoc {
131         int num_received=0;
132         Receiver r=new Receiver();
133         simulator.setReceiver(r);
134         for(int i=1; i <= NUM_MSGS; i++) {
135             Message msg=new Message(a1, null, createPayload(SIZE, i)); // unicast message
136
Event evt=new Event(Event.MSG, msg);
137             simulator.send(evt);
138             if(i % 1000 == 0)
139                 System.out.println("==> " + i);
140         }
141         int num_tries=10;
142         while((num_received=r.getNumberOfReceivedMessages()) != NUM_MSGS && num_tries > 0) {
143             if(r.getException() != null)
144                 throw r.getException();
145             synchronized(r) {
146                 try {r.wait(3000);}
147                 catch(InterruptedException JavaDoc e) {}
148             }
149             num_tries--;
150         }
151         printStats(num_received);
152         assertEquals(num_received, NUM_MSGS);
153     }
154
155     private void createStack(Protocol[] stack) throws Exception JavaDoc {
156         a1=new IpAddress(1111);
157         members=new Vector JavaDoc();
158         members.add(a1);
159         v=new View(a1, 1, members);
160         simulator=new Simulator();
161         simulator.setLocalAddress(a1);
162         simulator.setView(v);
163         simulator.addMember(a1);
164         simulator.setProtocolStack(stack);
165         simulator.start();
166     }
167
168     private void printStats(int num_received) {
169         System.out.println("-- num received=" + num_received + ", stats:\n" + simulator.dumpStats());
170     }
171
172
173     public static Test suite() {
174         return new TestSuite(UNICAST_Test.class);
175     }
176
177     public static void main(String JavaDoc[] args) {
178         junit.textui.TestRunner.run(suite());
179     }
180 }
181
Popular Tags