KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: TUNNELDeadLockTest.java,v 1.11 2007/04/02 07:08:54 belaban Exp $
2

3 package org.jgroups.tests;
4
5
6 import junit.framework.Test;
7 import junit.framework.TestCase;
8 import junit.framework.TestSuite;
9 import org.jgroups.JChannel;
10 import org.jgroups.Message;
11 import org.jgroups.TimeoutException;
12 import org.jgroups.tests.stack.Utilities;
13 import org.jgroups.util.Promise;
14
15 /**
16  * Test designed to make sure the TUNNEL doesn't lock the client and the GossipRouter
17  * under heavy load.
18  *
19  * @author Ovidiu Feodorov <ovidiu@feodorov.com>
20  * @version $Revision: 1.11 $
21  * @see TUNNELDeadLockTest#testStress
22  */

23 public class TUNNELDeadLockTest extends TestCase {
24     private JChannel channel;
25     private Promise promise;
26     private int receivedCnt;
27
28     // the total number of the messages pumped down the channel
29
private int msgCount=20000;
30     // the message payload size (in bytes);
31
private int payloadSize=32;
32     // the time (in ms) the main thread waits for all the messages to arrive,
33
// before declaring the test failed.
34
private int mainTimeout=60000;
35
36     int routerPort=-1;
37
38
39     public TUNNELDeadLockTest(String JavaDoc name) {
40         super(name);
41     }
42
43     public void setUp() throws Exception JavaDoc {
44         super.setUp();
45         promise=new Promise();
46         routerPort=Utilities.startGossipRouter("127.0.0.1");
47     }
48
49     public void tearDown() throws Exception JavaDoc {
50
51         super.tearDown();
52
53         // I prefer to close down the channel inside the test itself, for the
54
// reason that the channel might be brought in an uncloseable state by
55
// the test.
56

57         // TO_DO: no elegant way to stop the Router threads and clean-up
58
// resources. Use the Router administrative interface, when
59
// available.
60

61         channel=null;
62         promise.reset();
63         promise=null;
64         Utilities.stopGossipRouter();
65     }
66
67
68     private String JavaDoc getTUNNELProps(int routerPort) {
69         String JavaDoc props;
70
71         props="TUNNEL(router_host=127.0.0.1;router_port=" + routerPort + "):" +
72                 "PING(timeout=3000;gossip_refresh=10000;num_initial_members=3;" +
73                 "gossip_host=127.0.0.1;gossip_port=" + routerPort + "):" +
74                 "FD_SOCK:" +
75                 "pbcast.NAKACK(gc_lag=100;retransmit_timeout=600,1200,2400,4800):" +
76                 "pbcast.STABLE(stability_delay=1000;desired_avg_gossip=20000;max_bytes=100000):" +
77                 "pbcast.GMS(print_local_addr=true;join_timeout=5000;join_retry_timeout=2000;shun=true):" +
78                 "SFC(max_credits=100000)";
79         return props;
80     }
81
82     /**
83      * Pushes messages down the channel as fast as possible. Sometimes this
84      * manages to bring the channel and the Router into deadlock. On the
85      * machine I run it usually happens after 700 - 1000 messages and I
86      * suspect that this number it is related to the socket buffer size.
87      * (the comments are written when I didn't solve the bug yet). <br>
88      * <p/>
89      * The number of messages sent can be controlled with msgCount.
90      * The time (in ms) the main threads wait for the all messages to come can
91      * be controlled with mainTimeout. If this time passes and the test
92      * doesn't see all the messages, it declares itself failed.
93      */

94     public void testStress() throws Exception JavaDoc {
95         String JavaDoc props=getTUNNELProps(routerPort);
96         channel=new JChannel(props);
97         channel.connect("agroup");
98
99         // receiver thread
100
new Thread JavaDoc(new Runnable JavaDoc() {
101             public void run() {
102                 try {
103                     while(true) {
104                         if(channel == null)
105                             return;
106                         Object JavaDoc o=channel.receive(10000);
107                         if(o instanceof Message) {
108                             receivedCnt++;
109                             if(receivedCnt % 2000 == 0)
110                                 System.out.println("-- received " + receivedCnt);
111                             if(receivedCnt == msgCount) {
112                                 // let the main thread know I got all msgs
113
promise.setResult(new Object JavaDoc());
114                                 return;
115                             }
116                         }
117                     }
118                 }
119                 catch(TimeoutException e) {
120                     System.err.println("Timeout receiving from the channel. " + receivedCnt +
121                             " msgs received so far.");
122                 }
123                 catch(Exception JavaDoc e) {
124                     System.err.println("Error receiving data");
125                     e.printStackTrace();
126                 }
127             }
128         }).start();
129
130         // stress send messages - the sender thread
131
new Thread JavaDoc(new Runnable JavaDoc() {
132             public void run() {
133                 try {
134                     for(int i=0; i < msgCount; i++) {
135                         channel.send(null, null, new byte[payloadSize]);
136                         if(i % 2000 == 0)
137                             System.out.println("-- sent " + i);
138                     }
139                 }
140                 catch(Exception JavaDoc e) {
141                     System.err.println("Error sending data over ...");
142                     e.printStackTrace();
143                 }
144             }
145         }).start();
146
147
148         // wait for all the messages to come; if I don't see all of them in
149
// mainTimeout ms, I fail the test
150

151         Object JavaDoc result=promise.getResult(mainTimeout);
152         if(result == null) {
153             String JavaDoc msg=
154                     "The channel has failed to send/receive " + msgCount + " messages " +
155                     "possibly because of the channel deadlock or too short " +
156                     "timeout (currently " + mainTimeout + " ms). " + receivedCnt +
157                     " messages received so far.";
158             fail(msg);
159         }
160
161         // don't close it in tearDown() because it hangs forever for a failed
162
// test.
163
channel.close();
164     }
165
166
167     public static Test suite() {
168         return new TestSuite(TUNNELDeadLockTest.class);
169     }
170
171     public static void main(String JavaDoc[] args) {
172         junit.textui.TestRunner.run(suite());
173         System.exit(0);
174     }
175
176
177 }
178
Popular Tags