1 8 9 package test.mx4j.remote; 10 11 import java.io.IOException ; 12 import java.util.HashMap ; 13 import java.util.Map ; 14 import javax.management.Notification ; 15 import javax.management.NotificationListener ; 16 import javax.management.ObjectName ; 17 import javax.management.remote.NotificationResult ; 18 import javax.management.remote.TargetedNotification ; 19 20 import mx4j.remote.AbstractRemoteNotificationClientHandler; 21 import mx4j.remote.MX4JRemoteConstants; 22 import mx4j.remote.NotificationTuple; 23 import mx4j.remote.RemoteNotificationClientHandler; 24 import test.MX4JTestCase; 25 import test.MutableBoolean; 26 import test.MutableLong; 27 import test.MutableObject; 28 29 32 public class RemoteNotificationClientHandlerTest extends MX4JTestCase 33 { 34 public RemoteNotificationClientHandlerTest(String s) 35 { 36 super(s); 37 } 38 39 protected void tearDown() throws Exception 40 { 41 sleep(2000); 42 } 43 44 public void testListenerEquality() throws Exception 45 { 46 AbstractRemoteNotificationClientHandler handler = new AbstractRemoteNotificationClientHandler(null, null, null) 47 { 48 protected NotificationResult fetchNotifications(long sequence, int maxNumber, long timeout) 49 { 50 sleep(timeout); 51 return null; 52 } 53 54 protected void sendConnectionNotificationLost(long number) 55 { 56 } 57 58 protected long getRetryPeriod() 59 { 60 return 1000; 61 } 62 63 protected int getMaxRetries() 64 { 65 return 2; 66 } 67 }; 68 69 try 70 { 71 handler.start(); 72 73 NotificationListener listener = new NotificationListener () 74 { 75 public void handleNotification(Notification notification, Object handback) 76 { 77 } 78 }; 79 80 ObjectName name = ObjectName.getInstance(":name=emitter"); 81 handler.addNotificationListener(new Integer (1), new NotificationTuple(name, listener, null, null)); 82 handler.addNotificationListener(new Integer (2), new NotificationTuple(name, listener, null, new Object ())); 83 84 assertTrue(handler.isActive()); 85 86 Integer [] ids = handler.getNotificationListeners(new NotificationTuple(name, listener)); 87 assertEquals(ids.length, 2); 88 89 handler.removeNotificationListeners(new Integer []{new Integer (1), new Integer (2)}); 90 assertTrue(handler.isActive()); 91 92 handler.stop(); 93 assertTrue(!handler.isActive()); 94 } 95 finally 96 { 97 handler.stop(); 98 } 99 } 100 101 public void testAddRemove() throws Exception 102 { 103 RemoteNotificationClientHandler handler = new AbstractRemoteNotificationClientHandler(null, null, null) 104 { 105 public NotificationResult fetchNotifications(long sequenceNumber, int maxNumber, long timeout) 106 { 107 sleep(timeout); 108 return null; 109 } 110 111 protected void sendConnectionNotificationLost(long number) 112 { 113 } 114 115 protected long getRetryPeriod() 116 { 117 return 1000; 118 } 119 120 protected int getMaxRetries() 121 { 122 return 2; 123 } 124 }; 125 126 NotificationListener listener = new NotificationListener () 127 { 128 public void handleNotification(Notification notification, Object handback) 129 { 130 } 131 }; 132 133 try 134 { 135 handler.start(); 136 137 ObjectName name = ObjectName.getInstance(":name=emitter"); 138 handler.addNotificationListener(new Integer (1), new NotificationTuple(name, listener, null, null)); 139 Object handback = new Object (); 140 handler.addNotificationListener(new Integer (2), new NotificationTuple(name, listener, null, handback)); 141 handler.removeNotificationListeners(new Integer []{new Integer (2)}); 142 143 assertFalse(handler.contains(new NotificationTuple(name, listener, null, handback))); 144 assertTrue(handler.contains(new NotificationTuple(name, listener, null, null))); 145 146 Integer id = handler.getNotificationListener(new NotificationTuple(name, listener, null, null)); 147 assertEquals(id.intValue(), 1); 148 149 handler.removeNotificationListeners(new Integer []{new Integer (1)}); 150 assertFalse(handler.contains(new NotificationTuple(name, listener, null, null))); 151 } 152 finally 153 { 154 handler.stop(); 155 } 156 } 157 158 public void testNotificationDelivery() throws Exception 159 { 160 final MutableObject holder = new MutableObject(null); 161 final ObjectName name = ObjectName.getInstance(":name=emitter"); 162 final int nextSequence = 1; 163 RemoteNotificationClientHandler handler = new AbstractRemoteNotificationClientHandler(null, null, null) 164 { 165 public NotificationResult fetchNotifications(long sequenceNumber, int maxNumber, long timeout) 166 { 167 synchronized (holder) 168 { 169 Notification notification = new Notification ("type", name, 0); 170 TargetedNotification targeted = new TargetedNotification (notification, new Integer (1)); 171 if (sequenceNumber < 0) 172 { 173 NotificationResult result1 = new NotificationResult (0, nextSequence, new TargetedNotification []{targeted}); 175 holder.set(result1); 176 return result1; 177 } 178 179 if (sequenceNumber == nextSequence) 180 { 181 NotificationResult result2 = new NotificationResult (1, nextSequence + 1, new TargetedNotification []{targeted}); 182 holder.set(result2); 183 return result2; 184 } 185 186 try 187 { 188 holder.wait(timeout); 189 } 190 catch (InterruptedException x) 191 { 192 Thread.currentThread().interrupt(); 193 } 194 holder.set(null); 195 return null; 196 } 197 } 198 199 protected void sendConnectionNotificationLost(long number) 200 { 201 } 202 203 protected int getMaxRetries() 204 { 205 return 2; 206 } 207 208 protected long getRetryPeriod() 209 { 210 return 1000; 211 } 212 }; 213 214 final MutableObject notifHolder = new MutableObject(null); 215 NotificationListener listener = new NotificationListener () 216 { 217 public void handleNotification(Notification notification, Object handback) 218 { 219 notifHolder.set(notification); 220 } 221 }; 222 223 try 224 { 225 handler.start(); 226 227 handler.addNotificationListener(new Integer (1), new NotificationTuple(name, listener, null, null)); 228 229 synchronized (holder) 230 { 231 while (holder.get() == null) holder.wait(10); 233 NotificationResult result = (NotificationResult )holder.get(); 234 assertEquals(result.getNextSequenceNumber(), nextSequence); 235 holder.set(null); 236 237 while (notifHolder.get() == null) holder.wait(10); 239 Notification notif = (Notification )notifHolder.get(); 240 assertEquals(notif.getSource(), name); 241 242 while (holder.get() == null) holder.wait(10); 244 result = (NotificationResult )holder.get(); 245 assertEquals(result.getNextSequenceNumber(), nextSequence + 1); 246 } 247 248 handler.removeNotificationListeners(new Integer []{new Integer (1)}); 249 sleep(2000); 250 } 251 finally 252 { 253 handler.stop(); 254 } 255 } 256 257 public void testNotificationsLost() throws Exception 258 { 259 final MutableObject holder = new MutableObject(null); 260 final MutableLong lost = new MutableLong(0); 261 final ObjectName name = ObjectName.getInstance(":name=emitter"); 262 final long losts = 1; 263 RemoteNotificationClientHandler handler = new AbstractRemoteNotificationClientHandler(null, null, null) 264 { 265 public NotificationResult fetchNotifications(long sequenceNumber, int maxNumber, long timeout) 266 { 267 synchronized (holder) 268 { 269 if (sequenceNumber < 0) return new NotificationResult (0, 0, new TargetedNotification [0]); 270 271 sleep(1000); 273 274 return new NotificationResult (losts, losts, new TargetedNotification [0]); 276 } 277 } 278 279 protected void sendConnectionNotificationLost(long number) 280 { 281 synchronized (holder) 282 { 283 lost.set(number); 284 } 285 } 286 287 protected int getMaxRetries() 288 { 289 return 2; 290 } 291 292 protected long getRetryPeriod() 293 { 294 return 1000; 295 } 296 }; 297 298 Integer id = new Integer (1); 299 NotificationListener listener = new NotificationListener () 300 { 301 public void handleNotification(Notification notification, Object handback) 302 { 303 } 304 }; 305 306 try 307 { 308 handler.start(); 309 310 handler.addNotificationListener(id, new NotificationTuple(name, listener, null, null)); 311 312 synchronized (holder) 313 { 314 while (lost.get() == 0) holder.wait(10); 315 assertEquals(lost.get(), losts); 316 } 317 318 handler.removeNotificationListeners(new Integer []{id}); 319 } 320 finally 321 { 322 handler.stop(); 323 } 324 } 325 326 public void testConnectionFailure() throws Exception 327 { 328 final int retries = 2; 329 final long period = 1000; 330 AbstractRemoteNotificationClientHandler handler = new AbstractRemoteNotificationClientHandler(null, null, null) 331 { 332 protected NotificationResult fetchNotifications(long sequence, int maxNumber, long timeout) throws IOException 333 { 334 throw new IOException (); 335 } 336 337 protected void sendConnectionNotificationLost(long number) 338 { 339 } 340 341 protected int getMaxRetries() 342 { 343 return retries; 344 } 345 346 protected long getRetryPeriod() 347 { 348 return period; 349 } 350 }; 351 352 NotificationListener listener = new NotificationListener () 353 { 354 public void handleNotification(Notification notification, Object handback) 355 { 356 } 357 }; 358 359 try 360 { 361 handler.start(); 362 363 ObjectName name = ObjectName.getInstance(":name=emitter"); 364 Integer id = new Integer (1); 365 handler.addNotificationListener(id, new NotificationTuple(name, listener, null, null)); 366 367 sleep(5000 + period * retries); 368 369 assertTrue(!handler.isActive()); 370 } 371 finally 372 { 373 handler.stop(); 374 } 375 } 376 377 public void testQueueOverflow() throws Exception 378 { 379 final Object lock = new Object (); 380 int queueCapacity = 10; 381 final int count = 4; 382 final long sleep = 500; 383 final Integer id = new Integer (1); 384 final ObjectName name = ObjectName.getInstance(":name=emitter"); 385 final MutableBoolean notify = new MutableBoolean(true); 386 final MutableLong queued = new MutableLong(0); 387 final MutableLong delivered = new MutableLong(0); 388 389 Map environment = new HashMap (); 390 environment.put(MX4JRemoteConstants.NOTIFICATION_QUEUE_CAPACITY, new Integer (queueCapacity)); 391 RemoteNotificationClientHandler handler = new AbstractRemoteNotificationClientHandler(null, null, environment) 392 { 393 protected NotificationResult fetchNotifications(long sequenceNumber, int maxNumber, long timeout) 394 { 395 if (sequenceNumber < 0) return new NotificationResult (0, 0, new TargetedNotification [0]); 396 397 boolean doNotify = false; 398 synchronized (lock) 399 { 400 doNotify = notify.get(); 401 } 402 403 if (doNotify) 404 { 405 sleep(sleep); 407 TargetedNotification [] notifications = new TargetedNotification [count]; 408 for (int i = 0; i < count; ++i) notifications[i] = new TargetedNotification (new Notification ("type", name, sequenceNumber + i), id); 409 long nextSequence = sequenceNumber + count; 410 NotificationResult result = new NotificationResult (0, nextSequence, notifications); 411 synchronized (lock) 412 { 413 queued.set(getNotificationsCount()); 414 } 415 return result; 416 } 417 else 418 { 419 sleep(timeout); 420 return new NotificationResult (0, sequenceNumber, new TargetedNotification [0]); 421 } 422 } 423 424 protected long getRetryPeriod() 425 { 426 return 1000; 427 } 428 429 protected int getMaxRetries() 430 { 431 return 5; 432 } 433 434 protected void sendConnectionNotificationLost(long number) 435 { 436 System.out.println("Lost notifications: " + number); 437 synchronized (lock) 439 { 440 notify.set(false); 441 queued.set(getNotificationsCount()); 443 } 444 } 445 }; 446 447 NotificationListener listener = new NotificationListener () 448 { 449 public void handleNotification(Notification notification, Object handback) 450 { 451 long sequence = notification.getSequenceNumber(); 452 synchronized (lock) 453 { 454 delivered.set(sequence); 455 } 456 System.out.println("Received notification, sequence is " + sequence); 457 sleep(sleep * 2); 459 System.out.println("Handled notification, sequence is " + sequence); 460 } 461 }; 462 463 try 464 { 465 handler.start(); 466 handler.addNotificationListener(id, new NotificationTuple(name, listener, null, null)); 467 synchronized (lock) 469 { 470 while (notify.get()) 471 { 472 lock.wait(50); 473 if (queued.get() > queueCapacity) fail("Queued notifications " + queued.get() + " must not pass max capacity " + queueCapacity); 474 } 475 476 while (delivered.get() < queued.get()) lock.wait(10); 478 } 479 } 480 finally 481 { 482 handler.stop(); 483 } 484 } 485 } 486 | Popular Tags |