1 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 26 public class DisconnectTest extends TestCase { 27 28 private JChannel channel; 29 private int routerPort; 30 31 public DisconnectTest(String name) { 32 super(name); 33 } 34 35 public void setUp() throws Exception { 36 super.setUp(); 37 } 38 39 public void tearDown() throws Exception { 40 super.tearDown(); 41 if(channel != null) { 42 channel.close(); 43 channel=null; 44 } 45 } 46 47 48 private String 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 70 public void testNullLocalAddress_TUNNEL() throws Exception { 71 try { 72 routerPort=Utilities.startGossipRouter(); 73 String 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 90 public void testDisconnectConnectOne_Default() throws Exception { 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 105 public void testDisconnectConnectTwo_Default() throws Exception { 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 129 public void testDisconnectConnectSendTwo_Default() throws Exception { 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 158 public void testDisconnectConnectOne_TUNNEL() throws Exception { 159 try { 160 routerPort = Utilities.startGossipRouter(); 161 String 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 180 public void testDisconnectConnectTwo_TUNNEL() throws Exception { 181 try { 182 routerPort = Utilities.startGossipRouter(); 183 String props=getTUNNELProps(routerPort, routerPort); 184 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 214 public void testDisconnectConnectSendTwo_TUNNEL() throws Exception { 215 try { 216 routerPort = Utilities.startGossipRouter(); 217 String 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 [] args) { 252 String [] 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 |