KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: SendAndReceiveTest.java,v 1.4 2004/07/05 14:15:04 belaban Exp $
2

3 package org.jgroups.tests;
4
5
6 import junit.framework.TestCase;
7 import org.jgroups.JChannel;
8 import org.jgroups.Message;
9
10
11 /**
12  * Tests sending and receiving of messages within the same VM. Sends N messages
13  * and expects reception of N messages within a given time. Fails otherwise.
14  * @author Bela Ban
15  */

16 public class SendAndReceiveTest extends TestCase {
17     JChannel channel;
18     final int NUM_MSGS=1000;
19     final long TIMEOUT=30000;
20
21     String JavaDoc props1="UDP(loopback=true;mcast_addr=228.8.8.8;mcast_port=27000;ip_ttl=1;" +
22             "mcast_send_buf_size=64000;mcast_recv_buf_size=64000):" +
23             //"PIGGYBACK(max_wait_time=100;max_size=32000):" +
24
"PING(timeout=2000;num_initial_members=3):" +
25             "MERGE2(min_interval=5000;max_interval=10000):" +
26             "FD_SOCK:" +
27             "VERIFY_SUSPECT(timeout=1500):" +
28             "pbcast.NAKACK(max_xmit_size=8096;gc_lag=50;retransmit_timeout=300,600,1200,2400,4800):" +
29             "UNICAST(timeout=600,1200,2400,4800):" +
30             "pbcast.STABLE(desired_avg_gossip=20000):" +
31             "FRAG(frag_size=8096;down_thread=false;up_thread=false):" +
32             "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" +
33             "shun=false;print_local_addr=true)";
34
35         String JavaDoc props2="UDP(loopback=false;mcast_addr=228.8.8.8;mcast_port=27000;ip_ttl=1;" +
36                 "mcast_send_buf_size=64000;mcast_recv_buf_size=64000):" +
37                 //"PIGGYBACK(max_wait_time=100;max_size=32000):" +
38
"PING(timeout=2000;num_initial_members=3):" +
39                 "MERGE2(min_interval=5000;max_interval=10000):" +
40                 "FD_SOCK:" +
41                 "VERIFY_SUSPECT(timeout=1500):" +
42                 "pbcast.NAKACK(max_xmit_size=8096;gc_lag=50;retransmit_timeout=300,600,1200,2400,4800):" +
43                 "UNICAST(timeout=600,1200,2400,4800):" +
44                 "pbcast.STABLE(desired_avg_gossip=20000):" +
45                 "FRAG(frag_size=8096;down_thread=false;up_thread=false):" +
46                 "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" +
47                 "shun=false;print_local_addr=true)";
48
49     String JavaDoc props3="LOOPBACK:" +
50             "PING(timeout=2000;num_initial_members=3):" +
51             "MERGE2(min_interval=5000;max_interval=10000):" +
52             "FD_SOCK:" +
53             "VERIFY_SUSPECT(timeout=1500):" +
54             "pbcast.NAKACK(max_xmit_size=8096;gc_lag=50;retransmit_timeout=300,600,1200,2400,4800):" +
55             "UNICAST(timeout=600,1200,2400,4800):" +
56              "pbcast.STABLE(desired_avg_gossip=20000):" +
57             "FRAG(frag_size=8096;down_thread=false;up_thread=false):" +
58             "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" +
59             "shun=false;print_local_addr=true)";
60
61
62     public SendAndReceiveTest(String JavaDoc n) {
63         super(n);
64
65     }
66
67     public void setUp(String JavaDoc props) {
68         try {
69             channel=new JChannel(props);
70             channel.connect("test1");
71         }
72         catch(Throwable JavaDoc t) {
73             t.printStackTrace(System.err);
74             fail("channel could not be created");
75         }
76     }
77
78
79     public void tearDown() {
80         if(channel != null) {
81             channel.close();
82             channel=null;
83         }
84     }
85
86
87     /**
88      * Sends NUM messages and expects NUM messages to be received. If
89      * NUM messages have not been received after 20 seconds, the test failed.
90      */

91     public void testSendAndReceiveWithDefaultUDP_Loopback() {
92         setUp(props1);
93         sendMessages(NUM_MSGS);
94         int received_msgs=receiveMessages(NUM_MSGS, TIMEOUT);
95         assertTrue(received_msgs >= NUM_MSGS);
96     }
97
98     public void testSendAndReceiveWithDefaultUDP_NoLoopback() {
99         setUp(props2);
100         sendMessages(NUM_MSGS);
101         int received_msgs=receiveMessages(NUM_MSGS, TIMEOUT);
102         assertTrue(received_msgs >= NUM_MSGS);
103     }
104
105     public void testSendAndReceiveWithLoopback() {
106         setUp(props3);
107         sendMessages(NUM_MSGS);
108         int received_msgs=receiveMessages(NUM_MSGS, TIMEOUT);
109         assertTrue(received_msgs >= NUM_MSGS);
110     }
111
112     private void sendMessages(int num) {
113         Message msg;
114         for(int i=0; i < num; i++) {
115             try {
116                 msg=new Message();
117                 channel.send(msg);
118                 System.out.print(i + " ");
119             }
120             catch(Throwable JavaDoc t) {
121                 fail("could not send message #" + i);
122             }
123         }
124     }
125
126
127     /**
128      * Receive at least <tt>num</tt> messages. Total time should not exceed <tt>timeout</tt>
129      * @param num
130      * @param timeout Must be > 0
131      * @return
132      */

133     private int receiveMessages(int num, long timeout) {
134         int received=0;
135         Object JavaDoc msg;
136
137         if(timeout <= 0)
138             timeout=5000;
139
140         long start=System.currentTimeMillis(), current, wait_time;
141         while(true) {
142             current=System.currentTimeMillis();
143             wait_time=timeout - (current - start);
144             if(wait_time <= 0)
145                 break;
146             try {
147                 msg=channel.receive(wait_time);
148                 if(msg instanceof Message) {
149                     received++;
150                     System.out.print("+" + received + ' ');
151                 }
152                 if(received >= num)
153                     break;
154             }
155             catch(Throwable JavaDoc t) {
156                 fail("failed receiving message");
157             }
158         }
159         return received;
160     }
161
162
163     public static void main(String JavaDoc[] args) {
164         String JavaDoc[] testCaseName={SendAndReceiveTest.class.getName()};
165         junit.textui.TestRunner.main(testCaseName);
166     } //public static void main(String[] args)
167

168 }
169
Popular Tags