1 45 package org.exolab.jms.net.invoke; 46 47 import java.rmi.RemoteException ; 48 import java.util.Map ; 49 import java.util.HashMap ; 50 51 import EDU.oswego.cs.dl.util.concurrent.Latch; 52 53 import org.exolab.jms.net.Callback; 54 import org.exolab.jms.net.CallbackService; 55 import org.exolab.jms.net.CallbackServiceImpl; 56 import org.exolab.jms.net.connector.Caller; 57 import org.exolab.jms.net.connector.CallerListener; 58 import org.exolab.jms.net.orb.ORB; 59 import org.exolab.jms.net.proxy.Proxy; 60 import org.exolab.jms.net.registry.Registry; 61 62 63 69 public class DisconnectionTestCase extends ORBTestCase { 70 71 77 public DisconnectionTestCase(String name, String uri) { 78 super(name, uri); 79 } 80 81 88 public DisconnectionTestCase(String name, String uri, Map properties) { 89 super(name, uri, properties); 90 } 91 92 99 public DisconnectionTestCase(String name, String uri, String routeURI) { 100 super(name, uri, routeURI); 101 } 102 103 112 public DisconnectionTestCase(String name, String uri, String routeURI, 113 Map connectionProps, Map acceptorProps) { 114 super(name, uri, routeURI, connectionProps, acceptorProps); 115 } 116 117 122 public void testServerDisconnect() throws Exception { 123 final Latch latch = new Latch(); 124 CallerListener listener = new CallerListener() { 125 public void disconnected(Caller caller) { 126 latch.release(); 127 } 128 }; 129 ORB client = getClientORB(); 130 client.addCallerListener(getServerURI(), listener); 131 132 ORB server = getORB(); 133 server.getRegistry(); 134 135 Registry registry = getRegistry(); 138 assertNotNull(registry); 139 140 server.shutdown(); 141 142 if (!latch.attempt(10 * 1000)) { 143 fail("CallerListener not notified of disconnection"); 144 } 145 } 146 147 152 public void testClientDisconnect() throws Exception { 153 Latch latch = new Latch(); 154 ORB server = getORB(); 155 CallbackServer serviceImpl = new CallbackServer(server, latch); 156 157 Proxy proxy = server.exportObject(serviceImpl); 158 server.getRegistry().bind("service", proxy); 159 160 ORB client = getClientORB(); 161 Registry registry = client.getRegistry(getConnectionProperties()); 162 CallbackService service = (CallbackService) registry.lookup("service"); 163 164 LoggingCallback callback = new LoggingCallback(); 165 Callback callbackProxy = (Callback) client.exportObjectTo(callback, 166 getServerURI()); 167 service.addCallback(callbackProxy); 168 169 assertNull(serviceImpl.getException()); 170 client.shutdown(); 171 172 if (!latch.attempt(10 * 1000)) { 173 fail("CallerListener not notified of disconnection"); 174 } 175 assertNull(serviceImpl.getException()); 176 } 177 178 184 public void testInactive() throws Exception { 185 final Latch latch = new Latch(); 186 CallerListener listener = new CallerListener() { 187 public void disconnected(Caller caller) { 188 latch.release(); 189 } 190 }; 191 192 ORB server = getORB(); 193 CallbackServer serviceImpl = new CallbackServer(server, latch); 194 195 Proxy proxy = server.exportObject(serviceImpl); 196 server.getRegistry().bind("service", proxy); 197 198 ORB client = getClientORB(); 199 client.addCallerListener(getServerURI(), listener); 200 201 Registry registry = getRegistry(); assertNotNull(registry); 203 CallbackService service = (CallbackService) registry.lookup("service"); 204 assertNotNull(service); 205 206 for (int i = 0; i < 10; ++i) { 209 Runtime.getRuntime().gc(); 210 if (latch.attempt(1000)) { 211 break; 212 } 213 } 214 if (latch.attempt(0)) { 215 fail("Connection terminated when there were active proxies"); 216 } 217 218 222 registry = null; 223 for (int i = 0; i < 10; ++i) { 224 Runtime.getRuntime().gc(); 225 if (latch.attempt(1000)) { 226 break; 227 } 228 } 229 if (latch.attempt(0)) { 230 fail("Connection terminated when there were active proxies"); 231 } 232 233 service = null; 235 236 for (int i = 0; i < 10; ++i) { 238 Runtime.getRuntime().gc(); 239 if (latch.attempt(1000)) { 240 break; 241 } 242 } 243 if (!latch.attempt(0)) { 244 fail("CallerListener not notified of disconnection"); 245 } 246 } 247 248 255 protected Map getClientProperties() { 256 Map properties = super.getClientProperties(); 257 return addReapIntervalProperty(properties); 258 } 259 260 269 protected Map getAcceptorProperties() throws Exception { 270 Map properties = super.getAcceptorProperties(); 271 return addReapIntervalProperty(properties); 272 } 273 274 280 private Map addReapIntervalProperty(Map properties) { 281 if (properties == null) { 282 properties = new HashMap (); 283 } 284 properties.put("org.exolab.jms.net.pool.reapInterval", "5"); 285 return properties; 286 } 287 288 292 private static class CallbackServer extends CallbackServiceImpl 293 implements CallerListener { 294 295 298 private final ORB _orb; 299 300 303 private final Latch _latch; 304 305 308 private Exception _exception; 309 310 311 317 public CallbackServer(ORB orb, Latch latch) { 318 _orb = orb; 319 _latch = latch; 320 } 321 322 327 public synchronized void addCallback(Callback callback) { 328 super.addCallback(callback); 329 try { 330 Caller caller = _orb.getCaller(); 331 _orb.addCallerListener(caller.getRemoteURI().toString(), this); 332 } catch (RemoteException exception) { 333 _exception = exception; 334 } 335 } 336 337 342 public void disconnected(Caller caller) { 343 try { 344 _latch.release(); 345 _orb.removeCallerListener(caller.getRemoteURI().toString(), 346 this); 347 } catch (RemoteException exception) { 348 _exception = exception; 349 } 350 } 351 352 357 public Exception getException() { 358 return _exception; 359 } 360 } 361 } 362 | Popular Tags |