KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: DisconnectTest.java,v 1.11 2006/10/23 16:16:20 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.MessageListener;
12 import org.jgroups.View;
13 import org.jgroups.tests.stack.Utilities;
14 import org.jgroups.blocks.PullPushAdapter;
15 import org.jgroups.util.Promise;
16
17
18 /**
19  * Ensures that a disconnected channel reconnects correctly, for different
20  * stack configurations.
21  *
22  * @author Ovidiu Feodorov <ovidiu@feodorov.com>
23  * @author Bela Ban belaban@yahoo.com
24  * @version $Revision: 1.11 $
25  **/

26 public class DisconnectTest extends TestCase {
27
28     private JChannel channel;
29     private int routerPort;
30
31     public DisconnectTest(String JavaDoc name) {
32         super(name);
33     }
34
35     public void setUp() throws Exception JavaDoc {
36         super.setUp();
37     }
38
39     public void tearDown() throws Exception JavaDoc {
40         super.tearDown();
41         if(channel != null) {
42             channel.close();
43             channel=null;
44         }
45     }
46
47
48     private String JavaDoc getTUNNELProps(int routerPort, int gossipPort) {
49         return
50                 "TUNNEL(router_host=127.0.0.1;router_port=" + routerPort + "):" +
51                 "PING(gossip_host=127.0.0.1;gossip_port=" + gossipPort + "):" +
52                 "FD:" +
53                 "VERIFY_SUSPECT(timeout=1500;down_thread=false;up_thread=false):" +
54                 "pbcast.NAKACK(gc_lag=100;retransmit_timeout=3000;" +
55                 "down_thread=true;up_thread=true):" +
56                 "pbcast.STABLE(desired_avg_gossip=20000;down_thread=false;" +
57                 "up_thread=false):" +
58                 "pbcast.GMS(join_timeout=50000;join_retry_timeout=2000;shun=false;" +
59                 "print_local_addr=true;down_thread=true;up_thread=true)";
60     }
61
62     /**
63      * Tests if the channel has a null local address after disconnect (using
64      * TUNNEL).
65      *
66      * TO_DO: uncomment or delete after clarifying the semantics of
67      * getLocalAddress() on a disconnected channel.
68      *
69      **/

70     public void testNullLocalAddress_TUNNEL() throws Exception JavaDoc {
71         try {
72             routerPort=Utilities.startGossipRouter();
73             String JavaDoc props = getTUNNELProps(routerPort, routerPort);
74             channel = new JChannel(props);
75             channel.connect("testgroup");
76             assertTrue(channel.getLocalAddress() != null);
77             channel.disconnect();
78             assertNull(channel.getLocalAddress());
79         }
80         finally {
81             Utilities.stopGossipRouter();
82         }
83     }
84
85
86     /**
87      * Tests connect-disconnect-connect sequence for a group with one member
88      * (using default configuration).
89      **/

90     public void testDisconnectConnectOne_Default() throws Exception JavaDoc {
91         channel=new JChannel();
92         channel.connect("testgroup1");
93         channel.disconnect();
94         channel.connect("testgroup2");
95         View view=channel.getView();
96         assertEquals(1, view.size());
97         assertTrue(view.containsMember(channel.getLocalAddress()));
98     }
99
100
101     /**
102      * Tests connect-disconnect-connect sequence for a group with two members
103      * (using default configuration).
104      **/

105     public void testDisconnectConnectTwo_Default() throws Exception JavaDoc {
106         JChannel coordinator=new JChannel();
107         coordinator.connect("testgroup");
108         channel=new JChannel();
109         channel.connect("testgroup1");
110         channel.disconnect();
111         channel.connect("testgroup");
112         View view=channel.getView();
113         assertEquals(2, view.size());
114         assertTrue(view.containsMember(channel.getLocalAddress()));
115         assertTrue(view.containsMember(coordinator.getLocalAddress()));
116
117         coordinator.close();
118     }
119
120
121     /**
122      * Tests connect-disconnect-connect-send sequence for a group with two
123      * members, using the default stack configuration. Assumes that default
124      * configuration includes pbcast.NAKACK. Test case introduced before fixing
125      * pbcast.NAKACK bug, which used to leave pbcast.NAKACK in a broken state
126      * after DISCONNECT. Because of this problem, the channel couldn't be used
127      * to multicast messages.
128      **/

129     public void testDisconnectConnectSendTwo_Default() throws Exception JavaDoc {
130
131         final Promise msgPromise=new Promise();
132         JChannel coordinator=new JChannel();
133         coordinator.connect("testgroup");
134         PullPushAdapter ppa= new PullPushAdapter(coordinator,
135                                                  new PromisedMessageListener(msgPromise));
136         ppa.start();
137
138         channel=new JChannel();
139         channel.connect("testgroup1");
140         channel.disconnect();
141         channel.connect("testgroup");
142
143         channel.send(new Message(null, null, "payload"));
144
145         Message msg=(Message)msgPromise.getResult(20000);
146         assertTrue(msg != null);
147         assertEquals("payload", msg.getObject());
148
149         ppa.stop();
150         coordinator.close();
151     }
152
153
154     /**
155       * Tests connect-disconnect-connect sequence for a group with one member
156       * (using TUNNEL).
157       **/

158      public void testDisconnectConnectOne_TUNNEL() throws Exception JavaDoc {
159         try {
160             routerPort = Utilities.startGossipRouter();
161             String JavaDoc props=getTUNNELProps(routerPort, routerPort);
162             channel=new JChannel(props);
163             channel.connect("testgroup1");
164             channel.disconnect();
165             channel.connect("testgroup2");
166             View view=channel.getView();
167             assertEquals(1, view.size());
168             assertTrue(view.containsMember(channel.getLocalAddress()));
169         }
170         finally {
171             Utilities.stopGossipRouter();
172         }
173      }
174
175
176      /**
177       * Tests connect-disconnect-connect sequence for a group with two members
178       * (using TUNNEL).
179       **/

180      public void testDisconnectConnectTwo_TUNNEL() throws Exception JavaDoc {
181          try {
182              routerPort = Utilities.startGossipRouter();
183              String JavaDoc props=getTUNNELProps(routerPort, routerPort);
184              // String props="tunnel.xml";
185
JChannel coordinator=new JChannel(props);
186              coordinator.connect("testgroup");
187              channel=new JChannel(props);
188              channel.connect("testgroup1");
189              channel.disconnect();
190              channel.connect("testgroup");
191
192              Thread.sleep(1000);
193
194              View view=channel.getView();
195              assertEquals(2, view.size());
196              assertTrue(view.containsMember(channel.getLocalAddress()));
197              assertTrue(view.containsMember(coordinator.getLocalAddress()));
198
199              coordinator.close();
200          }
201          finally {
202              Utilities.stopGossipRouter();
203          }
204      }
205
206
207     /**
208      * Tests connect-disconnect-connect-send sequence for a group with two
209       * members, using TUNNEL. Test case introduced before fixing pbcast.NAKACK
210       * bug, which used to leave pbcast.NAKACK in a broken state after
211       * DISCONNECT. Because of this problem, the channel couldn't be used to
212       * multicast messages.
213       **/

214      public void testDisconnectConnectSendTwo_TUNNEL() throws Exception JavaDoc {
215         try {
216             routerPort = Utilities.startGossipRouter();
217             String JavaDoc props=getTUNNELProps(routerPort, routerPort);
218
219             final Promise msgPromise=new Promise();
220             JChannel coordinator=new JChannel(props);
221             coordinator.connect("testgroup");
222             PullPushAdapter ppa=
223                     new PullPushAdapter(coordinator,
224                                         new PromisedMessageListener(msgPromise));
225             ppa.start();
226
227             channel=new JChannel(props);
228             channel.connect("testgroup1");
229             channel.disconnect();
230             channel.connect("testgroup");
231
232             channel.send(new Message(null, null, "payload"));
233
234             Message msg=(Message)msgPromise.getResult(20000);
235             assertTrue(msg != null);
236             assertEquals("payload", msg.getObject());
237
238             ppa.stop();
239             coordinator.close();
240         }
241         finally {
242             Utilities.stopGossipRouter();
243         }
244     }
245
246
247     public static Test suite() {
248         return new TestSuite(DisconnectTest.class);
249     }
250
251     public static void main(String JavaDoc[] args) {
252         String JavaDoc[] testCaseName={DisconnectTest.class.getName()};
253         junit.textui.TestRunner.main(testCaseName);
254     }
255
256     private static class PromisedMessageListener implements MessageListener {
257
258         private Promise promise;
259
260         public PromisedMessageListener(Promise promise) {
261             this.promise=promise;
262         }
263
264         public byte[] getState() {
265             return null;
266         }
267
268         public void receive(Message msg) {
269             promise.setResult(msg);
270         }
271
272         public void setState(byte[] state) {
273         }
274     }
275
276 }
277
Popular Tags