KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: FCTest.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.FC;
13 import org.jgroups.stack.IpAddress;
14 import org.jgroups.stack.Protocol;
15 import org.jgroups.util.Util;
16
17 import java.util.Properties JavaDoc;
18 import java.util.Vector JavaDoc;
19
20
21 /**
22  * Tests the flow control (FC) protocol
23  * @author Bela Ban
24  */

25 public class FCTest extends TestCase {
26     IpAddress a1;
27     Vector JavaDoc members;
28     View v;
29     Simulator s;
30
31     final int SIZE=1000; // bytes
32
final int NUM_MSGS=100000;
33
34
35     public FCTest(String JavaDoc name) {
36         super(name);
37     }
38
39
40     public void setUp() throws Exception JavaDoc {
41         super.setUp();
42         a1=new IpAddress(1111);
43         members=new Vector JavaDoc();
44         members.add(a1);
45         v=new View(a1, 1, members);
46         s=new Simulator();
47         s.setLocalAddress(a1);
48         s.setView(v);
49         s.addMember(a1);
50         FC fc=new FC();
51         Properties JavaDoc props=new Properties JavaDoc();
52         props.setProperty("max_credits", "10000");
53         props.setProperty("min_credits", "1000");
54         props.setProperty("max_block_time", "1000");
55         fc.setProperties(props);
56         Protocol[] stack=new Protocol[]{fc};
57         s.setProtocolStack(stack);
58         s.start();
59     }
60
61     public void tearDown() throws Exception JavaDoc {
62         super.tearDown();
63         s.stop();
64     }
65
66
67     public void testReceptionOfAllMessages() {
68         int num_received=0;
69         Receiver r=new Receiver();
70         s.setReceiver(r);
71         for(int i=1; i <= NUM_MSGS; i++) {
72             Message msg=new Message(null, null, createPayload(SIZE));
73             Event evt=new Event(Event.MSG, msg);
74             s.send(evt);
75             if(i % 1000 == 0)
76                 System.out.println("==> " + i);
77         }
78         int num_tries=10;
79         while(num_tries > 0) {
80             Util.sleep(1000);
81             num_received=r.getNumberOfReceivedMessages();
82             System.out.println("-- num received=" + num_received + ", stats:\n" + s.dumpStats());
83             if(num_received >= NUM_MSGS)
84                 break;
85             num_tries--;
86         }
87         assertEquals(num_received, NUM_MSGS);
88     }
89
90
91
92     private static byte[] createPayload(int size) {
93         byte[] retval=new byte[size];
94         for(int i=0; i < size; i++)
95             retval[i]='0';
96         return retval;
97     }
98
99
100     static class Receiver implements Simulator.Receiver {
101         int num_mgs_received=0;
102
103         public void receive(Event evt) {
104             if(evt.getType() == Event.MSG) {
105                 num_mgs_received++;
106                 if(num_mgs_received % 1000 == 0)
107                     System.out.println("<== " + num_mgs_received);
108             }
109         }
110
111         public int getNumberOfReceivedMessages() {
112             return num_mgs_received;
113         }
114     }
115     
116    
117
118     public static Test suite() {
119         return new TestSuite(FCTest.class);
120     }
121
122     public static void main(String JavaDoc[] args) {
123         junit.textui.TestRunner.run(suite());
124     }
125 }
126
Popular Tags