1 3 package org.jgroups.tests; 4 5 import java.util.Vector ; 6 7 import org.jgroups.Address; 8 import org.jgroups.Channel; 9 import org.jgroups.ChannelClosedException; 10 import org.jgroups.View; 11 import org.jgroups.util.Util; 12 13 14 18 public class CloseTest extends ChannelTestBase { 19 Channel channel, channel1, channel2, c1, c2, c3; 20 21 public void setUp() throws Exception { 22 super.setUp(); 23 } 24 25 26 public void tearDown() throws Exception { 27 closeChannel(channel); 28 closeChannel(channel1); 29 closeChannel(channel2); 30 closeChannel(c1); 31 closeChannel(c2); 32 closeChannel(c3); 33 34 super.tearDown(); 35 } 36 37 protected boolean useBlocking() 38 { 39 return false; 40 } 41 42 43 private void closeChannel(Channel c) { 44 if(c != null && (c.isOpen() || c.isConnected())) { 45 c.close(); 46 } 47 } 48 49 50 public void testDoubleClose() throws Exception { 51 System.out.println("-- creating channel1 --"); 52 channel1=createChannel(); 53 System.out.println("-- connecting channel1 --"); 54 channel1.connect("bla"); 55 56 assertTrue("channel open", channel1.isOpen()); 57 assertTrue("channel connected", channel1.isConnected()); 58 59 System.out.println("-- closing channel1 --"); 60 channel1.close(); 61 System.out.println("-- closing channel1 (again) --"); 62 channel1.close(); 63 assertFalse("channel not connected", channel1.isConnected()); 64 System.out.println("-- done, threads are "); 65 Util.printThreads(); 66 } 67 68 public void testCreationAndClose() throws Exception { 69 System.out.println("-- creating channel1 --"); 70 Channel c = null; 71 c = createChannel(); 72 c.connect("CloseTest1"); 73 assertTrue("channel open", c.isOpen()); 74 assertTrue("channel connected", c.isConnected()); 75 c.close(); 76 assertFalse("channel not connected", c.isConnected()); 77 c.close(); 78 } 79 80 public void testViewChangeReceptionOnChannelCloseByParticipant() throws Exception { 81 Address a1, a2; 82 Vector members; 83 c1 = createChannel("A"); 84 System.out.println("-- connecting c1"); 85 c1.connect("X"); 86 Util.sleep(500); dumpMessages("c1", c1); 88 a1=c1.getLocalAddress(); 89 c2 = createChannel("A"); 90 System.out.println("-- connecting c2"); 91 c2.connect("X"); 92 Util.sleep(500); a2=c2.getLocalAddress(); 94 dumpMessages("c2", c2); 95 96 System.out.println("-- closing c2"); 97 c2.close(); 98 Object obj=c1.receive(100); 99 assertTrue(obj instanceof View); 100 View v=(View)obj; 101 members=v.getMembers(); 102 System.out.println("-- first view of c1: " + v); 103 assertEquals(2, members.size()); 104 assertTrue(members.contains(a1)); 105 assertTrue(members.contains(a2)); 106 107 obj=c1.receive(100); 108 assertTrue(obj instanceof View); 109 v=(View)obj; 110 members=v.getMembers(); 111 System.out.println("-- second view of c1: " + v); 112 assertEquals(1, members.size()); 113 assertTrue(members.contains(a1)); 114 assertFalse(members.contains(a2)); 115 } 116 117 public void testViewChangeReceptionOnChannelCloseByCoordinator() throws Exception { 118 Address a1, a2; 119 Vector members; 120 Object obj; 121 View v; 122 c1=createChannel("A"); 123 c1.connect("X"); 124 Util.sleep(500); dumpMessages("c1", c1); 126 a1=c1.getLocalAddress(); 127 c2=createChannel("A"); 128 c2.connect("X"); 129 Util.sleep(500); a2=c2.getLocalAddress(); 131 v=(View)c2.receive(1); 132 members=v.getMembers(); 133 assertEquals(2, members.size()); 134 assertTrue(members.contains(a2)); 135 136 c1.close(); 137 Util.sleep(500); 138 139 System.out.println("queue of c2 is " + c2.dumpQueue()); 140 assertTrue("found 0 messages in channel", c2.getNumMessages() > 0); 141 obj=c2.receive(0); 142 assertTrue(obj instanceof View); 143 v=(View)obj; 144 members=v.getMembers(); 145 assertEquals(1, members.size()); 146 assertFalse(members.contains(a1)); 147 assertTrue(members.contains(a2)); 148 149 assertEquals(0, c2.getNumMessages()); 150 } 151 152 private void dumpMessages(String msg, Channel ch) throws Exception { 153 while(ch.getNumMessages() > 0) { 154 Object obj=ch.receive(0); 155 if(obj instanceof View) 156 System.out.println(msg + ": " + obj); 157 } 158 } 159 160 public void testConnectDisconnectConnectCloseSequence() throws Exception { 161 System.out.println("-- creating channel --"); 162 channel=createChannel(); 163 System.out.println("-- connecting channel to CloseTest1--"); 164 channel.connect("CloseTest1"); 165 System.out.println("view is " + channel.getView()); 166 System.out.println("-- disconnecting channel --"); 167 channel.disconnect(); 168 System.out.println("-- connecting channel to OtherGroup --"); 169 channel.connect("OtherGroup"); 170 System.out.println("view is " + channel.getView()); 171 System.out.println("-- closing channel --"); 172 channel.close(); 173 System.out.println("-- done, threads are "); 174 Util.printThreads(); 175 } 176 177 178 public void testConnectCloseSequenceWith2Members() throws Exception { 179 System.out.println("-- creating channel --"); 180 channel=createChannel("A"); 181 System.out.println("-- connecting channel --"); 182 channel.connect("X"); 183 System.out.println("view is " + channel.getView()); 184 185 System.out.println("-- creating channel1 --"); 186 channel1=createChannel("A"); 187 System.out.println("-- connecting channel1 --"); 188 channel1.connect("X"); 189 System.out.println("view is " + channel1.getView()); 190 191 System.out.println("-- closing channel1 --"); 192 channel1.close(); 193 194 Util.sleep(2000); 195 System.out.println("-- closing channel --"); 196 channel.close(); 197 } 198 199 200 public void testCreationAndClose2() throws Exception { 201 System.out.println("-- creating channel2 --"); 202 channel2=createChannel(); 203 System.out.println("-- connecting channel2 --"); 204 channel2.connect("CloseTest2"); 205 System.out.println("-- closing channel --"); 206 channel2.close(); 207 Util.sleep(2000); 208 Util.printThreads(); 209 } 210 211 212 public void testChannelClosedException() throws Exception { 213 System.out.println("-- creating channel --"); 214 channel=createChannel(); 215 System.out.println("-- connecting channel --"); 216 channel.connect("CloseTestLoop"); 217 System.out.println("-- closing channel --"); 218 channel.close(); 219 Util.sleep(2000); 220 221 try { 222 channel.connect("newGroup"); 223 fail(); } 225 catch(ChannelClosedException ex) { 226 assertTrue(true); 227 } 228 } 229 230 public void testCreationAndCloseLoop() throws Exception { 231 System.out.println("-- creating channel --"); 232 channel=createChannel(); 233 234 for(int i=1; i <= 10; i++) { 235 System.out.println("-- connecting channel (attempt #" + i + " ) --"); 236 channel.connect("CloseTestLoop2"); 237 System.out.println("-- closing channel --"); 238 channel.close(); 239 240 System.out.println("-- reopening channel --"); 241 channel.open(); 242 } 243 channel.close(); 244 } 245 246 247 public void testMultipleConnectsAndDisconnects() throws Exception { 248 c1=createChannel("A"); 249 assertTrue(c1.isOpen()); 250 assertFalse(c1.isConnected()); 251 c1.connect("bla"); 252 System.out.println("view after c1.connect(): " + c1.getView()); 253 assertTrue(c1.isOpen()); 254 assertTrue(c1.isConnected()); 255 assertServiceAndClusterView(c1, 1); 256 257 c2=createChannel("A"); 258 assertTrue(c2.isOpen()); 259 assertFalse(c2.isConnected()); 260 261 c2.connect("bla"); 262 System.out.println("view after c2.connect(): " + c2.getView()); 263 assertTrue(c2.isOpen()); 264 assertTrue(c2.isConnected()); 265 assertServiceAndClusterView(c2, 2); 266 Util.sleep(500); 267 assertServiceAndClusterView(c1, 2); 268 269 c2.disconnect(); 270 System.out.println("view after c2.disconnect(): " + c2.getView()); 271 assertTrue(c2.isOpen()); 272 assertFalse(c2.isConnected()); 273 Util.sleep(500); 274 assertServiceAndClusterView(c1, 1); 275 276 c2.connect("bla"); 277 System.out.println("view after c2.connect(): " + c2.getView()); 278 assertTrue(c2.isOpen()); 279 assertTrue(c2.isConnected()); 280 assertServiceAndClusterView(c2, 2); 281 Util.sleep(300); 282 assertServiceAndClusterView(c1, 2); 283 284 c3=createChannel("A"); 286 assertTrue(c3.isOpen()); 287 assertFalse(c3.isConnected()); 288 assertServiceAndClusterView(c1, 2); 289 assertServiceAndClusterView(c2, 2); 290 291 c1.disconnect(); 292 assertTrue(c1.isOpen()); 293 assertFalse(c1.isConnected()); 294 assertServiceAndClusterView(c2, 1); 295 assertTrue(c3.isOpen()); 296 assertFalse(c3.isConnected()); 297 298 c1.connect("bla"); 299 System.out.println("view after c1.connect(): " + c1.getView()); 300 assertTrue(c1.isOpen()); 301 assertTrue(c1.isConnected()); 302 assertServiceAndClusterView(c1, 2); 303 Util.sleep(500); 304 assertServiceAndClusterView(c2, 2); 305 assertTrue(c3.isOpen()); 306 assertFalse(c3.isConnected()); 307 } 308 309 310 private void assertServiceAndClusterView(Channel ch, int num) { 311 View view=ch.getView(); 312 String msg="view=" + view; 313 assertNotNull(view); 314 assertEquals(msg, num, view.size()); 315 } 316 317 public static void main(String [] args) { 318 String [] testCaseName={CloseTest.class.getName()}; 319 junit.textui.TestRunner.main(testCaseName); 320 } 321 } 322 | Popular Tags |