KickJava   Java API By Example, From Geeks To Geeks.

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


1
2 package org.jgroups.tests;
3
4
5 import junit.framework.Test;
6 import junit.framework.TestCase;
7 import junit.framework.TestSuite;
8 import org.jgroups.Address;
9 import org.jgroups.JChannel;
10 import org.jgroups.View;
11
12 import java.util.concurrent.CyclicBarrier JavaDoc;
13
14
15 /**
16  * Tests concurrent leaves of all members of a channel
17  * @author Bela Ban
18  * @version $Id: DisconnectStressTest.java,v 1.3 2006/12/31 14:53:38 belaban Exp $
19  */

20 public class DisconnectStressTest extends TestCase {
21     static CyclicBarrier JavaDoc all_disconnected=null;
22     static CyclicBarrier JavaDoc start_disconnecting=null;
23     static final int NUM=30;
24     static final long TIMEOUT=50000;
25     static final MyThread[] threads=new MyThread[NUM];
26     static String JavaDoc groupname="ConcurrentTestDemo";
27
28
29     static String JavaDoc props="UDP(mcast_addr=228.8.8.9;mcast_port=7788;ip_ttl=1;" +
30             "mcast_send_buf_size=150000;mcast_recv_buf_size=80000):" +
31             "PING(timeout=3000;num_initial_members=3):" +
32             "MERGE2(min_interval=3000;max_interval=5000):" +
33             "FD_SOCK:" +
34             "VERIFY_SUSPECT(timeout=1500):" +
35             "pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800):" +
36             "UNICAST(timeout=300,600,1200,2400):" +
37             "pbcast.STABLE(desired_avg_gossip=5000):" +
38             "FRAG(frag_size=4096;down_thread=false;up_thread=false):" +
39             "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" +
40             "shun=false;print_local_addr=false;view_ack_collection_timeout=5000;" +
41             "digest_timeout=0;merge_timeout=30000;handle_concurrent_startup=true)";
42
43
44
45     public DisconnectStressTest(String JavaDoc name) {
46         super(name);
47     }
48
49
50     static void log(String JavaDoc msg) {
51         System.out.println("-- [" + Thread.currentThread().getName() + "] " + msg);
52     }
53
54
55     public void testConcurrentStartupAndMerging() throws Exception JavaDoc {
56         all_disconnected=new CyclicBarrier JavaDoc(NUM+1);
57         start_disconnecting=new CyclicBarrier JavaDoc(NUM+1);
58
59         for(int i=0; i < threads.length; i++) {
60             threads[i]=new MyThread(i);
61             synchronized(threads[i]) {
62                 threads[i].start();
63                 threads[i].wait(20000);
64             }
65         }
66
67         log("DISCONNECTING");
68         start_disconnecting.await(); // causes all channels to disconnect
69

70         all_disconnected.await(); // notification when all threads have disconnected
71
}
72
73
74
75
76
77     public static class MyThread extends Thread JavaDoc {
78         int index=-1;
79         long total_connect_time=0, total_disconnect_time=0;
80         private JChannel ch=null;
81         private Address my_addr=null;
82
83         public MyThread(int i) {
84             super("thread #" + i);
85             index=i;
86         }
87
88         public void closeChannel() {
89             if(ch != null) {
90                 ch.close();
91             }
92         }
93
94         public int numMembers() {
95             return ch.getView().size();
96         }
97
98         public void run() {
99             View view;
100
101             try {
102                 ch=new JChannel(props);
103                 log("connecting to channel");
104                 long start=System.currentTimeMillis(), stop;
105                 ch.connect(groupname);
106                 stop=System.currentTimeMillis();
107                 synchronized(this) {
108                     this.notify();
109                 }
110                 total_connect_time=stop-start;
111                 view=ch.getView();
112                 my_addr=ch.getLocalAddress();
113                 log(my_addr + " connected in " + total_connect_time + " msecs (" +
114                     view.getMembers().size() + " members). VID=" + ch.getView());
115
116                 start_disconnecting.await();
117
118                 start=System.currentTimeMillis();
119                 ch.disconnect();
120                 stop=System.currentTimeMillis();
121
122                 log(my_addr + " disconnected in " + (stop-start) + " msecs");
123                 all_disconnected.await();
124             }
125             catch(Exception JavaDoc e) {
126                 e.printStackTrace();
127             }
128         }
129
130
131     }
132
133
134     public static Test suite() {
135         return new TestSuite(DisconnectStressTest.class);
136     }
137
138     public static void main(String JavaDoc[] args) {
139         String JavaDoc[] testCaseName={DisconnectStressTest.class.getName()};
140         junit.textui.TestRunner.main(testCaseName);
141     }
142
143
144 }
145
Popular Tags