1 8 9 package test.javax.management.timer; 10 11 import java.util.Date ; 12 import java.util.Vector ; 13 import javax.management.InstanceNotFoundException ; 14 import javax.management.MBeanServer ; 15 import javax.management.MBeanServerInvocationHandler ; 16 import javax.management.Notification ; 17 import javax.management.NotificationFilter ; 18 import javax.management.NotificationListener ; 19 import javax.management.ObjectName ; 20 import javax.management.timer.Timer ; 21 import javax.management.timer.TimerMBean ; 22 import javax.management.timer.TimerNotification ; 23 24 import test.MX4JTestCase; 25 import test.MutableBoolean; 26 import test.MutableInteger; 27 import test.MutableLong; 28 29 32 public class TimerTest extends MX4JTestCase 33 { 34 private MBeanServer m_server; 35 private ObjectName m_timerName; 36 private TimerMBean m_timer; 37 38 public TimerTest(String s) 39 { 40 super(s); 41 } 42 43 protected void setUp() throws Exception 44 { 45 m_server = newMBeanServer(); 46 m_timerName = new ObjectName ("Service:type=Timer"); 47 m_server.createMBean("javax.management.timer.Timer", m_timerName, null); 48 m_timer = (TimerMBean )MBeanServerInvocationHandler.newProxyInstance(m_server, m_timerName, TimerMBean .class, false); 49 } 50 51 protected void tearDown() throws Exception 52 { 53 m_server.unregisterMBean(m_timerName); 54 } 55 56 public void testStartStop() throws Exception 57 { 58 m_timer.start(); 59 assertTrue(m_timer.isActive()); 60 m_timer.stop(); 61 assertFalse(m_timer.isActive()); 62 } 63 64 public void testStartStopStart() throws Exception 65 { 66 m_timer.start(); 67 assertTrue(m_timer.isActive()); 68 m_timer.stop(); 69 assertFalse(m_timer.isActive()); 70 m_timer.start(); 71 assertTrue(m_timer.isActive()); 72 } 74 75 public void testOneShotNotification() throws Exception 76 { 77 m_timer.start(); 78 79 final long now = System.currentTimeMillis(); 80 final MutableInteger mid = new MutableInteger(-1); 81 final MutableInteger occurrencesCount = new MutableInteger(0); 82 83 final String notifType = "timer-test"; 84 final long delay = 3 * Timer.ONE_SECOND; 85 86 NotificationListener listener = new NotificationListener () 87 { 88 public void handleNotification(Notification notification, Object handback) 89 { 90 long elapsed = System.currentTimeMillis() - now; 92 assertTrue(elapsed >= delay); 93 assertFalse(elapsed - delay > 50); 94 95 assertTrue(notification instanceof TimerNotification ); 96 97 Integer id = ((TimerNotification )notification).getNotificationID(); 98 assertEquals(mid.get(), id.intValue()); 99 100 occurrencesCount.set(occurrencesCount.get() + 1); 101 } 102 }; 103 104 m_server.addNotificationListener(m_timerName, listener, new NotificationFilter () 105 { 106 public boolean isNotificationEnabled(Notification notification) 107 { 108 return notification.getType().equals(notifType); 109 } 110 }, null); 111 112 Date date = new Date (now + delay); 114 Integer id = m_timer.addNotification(notifType, "timer-message", "user-data", date); 116 mid.set(id.intValue()); 117 118 sleep(delay * 2); 120 121 assertTrue(occurrencesCount.get() == 1); 123 124 assertTrue(m_timer.getNbNotifications() == 0); 126 } 127 128 public void testPeriodicNotification() throws Exception 129 { 130 m_timer.start(); 131 132 final String notifType = "timer-test"; 133 final String periodicNotifType = "timer-test-periodic"; 134 135 final MutableInteger occurrencesCount = new MutableInteger(0); 136 137 NotificationListener listener = new NotificationListener () 138 { 139 public void handleNotification(Notification notification, Object handback) 140 { 141 occurrencesCount.set(occurrencesCount.get() + 1); 142 } 143 }; 144 145 final MutableInteger periodicOccurrences = new MutableInteger(0); 146 NotificationListener periodicListener = new NotificationListener () 147 { 148 public void handleNotification(Notification notification, Object handback) 149 { 150 periodicOccurrences.set(periodicOccurrences.get() + 1); 151 } 152 }; 153 154 m_server.addNotificationListener(m_timerName, listener, new NotificationFilter () 155 { 156 public boolean isNotificationEnabled(Notification notification) 157 { 158 return notification.getType().equals(notifType); 159 } 160 }, null); 161 m_server.addNotificationListener(m_timerName, periodicListener, new NotificationFilter () 162 { 163 public boolean isNotificationEnabled(Notification notification) 164 { 165 return notification.getType().equals(periodicNotifType); 166 } 167 }, null); 168 169 long now = System.currentTimeMillis(); 171 Date date = new Date (now + Timer.ONE_SECOND); 173 String message = "timer-message"; 174 Integer id = m_timer.addNotification(notifType, message, "user-data", date, Timer.ONE_SECOND, 3L); 175 176 date = new Date (now + Timer.ONE_SECOND); 179 String userDataPeriodic = "user-data-periodic"; 180 Integer periodicID = m_timer.addNotification(periodicNotifType, "timer-message-periodic", userDataPeriodic, date, Timer.ONE_SECOND); 181 182 sleep(Timer.ONE_SECOND); 184 185 Vector v = m_timer.getAllNotificationIDs(); 186 assertEquals(v.size(), 2); 187 assertTrue(v.contains(id)); 188 assertTrue(v.contains(periodicID)); 189 190 v = m_timer.getNotificationIDs(periodicNotifType); 191 assertEquals(v.size(), 1); 192 assertTrue(v.contains(periodicID)); 193 194 assertEquals(m_timer.getNotificationMessage(id), message); 195 196 assertEquals(m_timer.getNotificationUserData(periodicID), userDataPeriodic); 197 198 sleep(Timer.ONE_SECOND * 6); 200 201 assertEquals(occurrencesCount.get(), 3); 203 204 v = m_timer.getAllNotificationIDs(); 206 assertEquals(v.size(), 1); 207 assertTrue(v.contains(periodicID)); 208 209 Long p = m_timer.getPeriod(periodicID); 210 assertEquals(p.longValue(), Timer.ONE_SECOND); 211 212 assertEquals(m_timer.getNotificationType(periodicID), periodicNotifType); 213 214 try 216 { 217 m_timer.removeNotifications("dummy"); 218 fail("Removed non-existing notification"); 219 } 220 catch (InstanceNotFoundException ignored) 221 { 222 } 223 224 try 226 { 227 m_timer.removeNotification(id); 228 fail("Removed non-existing notification"); 229 } 230 catch (InstanceNotFoundException ignored) 231 { 232 } 233 234 sleep(Timer.ONE_SECOND * 3); 236 237 m_timer.removeNotification(periodicID); 239 240 assertTrue(m_timer.isEmpty()); 242 243 int periodTimes = periodicOccurrences.get(); 245 assertTrue(periodTimes > 0); 246 247 sleep(Timer.ONE_SECOND * 5); 248 249 assertEquals(periodicOccurrences.get(), periodTimes); 250 } 251 252 public void testTimerNotStarted() throws Exception 253 { 254 257 final MutableBoolean bool = new MutableBoolean(false); 258 NotificationListener listener = new NotificationListener () 259 { 260 public void handleNotification(Notification notification, Object handback) 261 { 262 bool.set(true); 263 } 264 }; 265 m_server.addNotificationListener(m_timerName, listener, null, null); 266 267 long now = System.currentTimeMillis(); 268 m_timer.addNotification("timer-notif", "Must not be emitted", null, new Date (now + Timer.ONE_SECOND)); 269 270 sleep(Timer.ONE_SECOND * 2); 272 273 assertFalse(bool.get()); 274 } 275 276 public void testAddStopRemoveNotification() throws Exception 277 { 278 280 final MutableInteger count = new MutableInteger(0); 281 NotificationListener listener = new NotificationListener () 282 { 283 public void handleNotification(Notification notification, Object handback) 284 { 285 count.set(count.get() + 1); 286 } 287 }; 288 m_server.addNotificationListener(m_timerName, listener, null, null); 289 290 long now = System.currentTimeMillis(); 291 Date date = new Date (now + Timer.ONE_SECOND); 292 293 Integer id = m_timer.addNotification("notif-type", "notif-message", "notif-data", date, Timer.ONE_SECOND); 295 m_timer.start(); 296 297 sleep(Timer.ONE_SECOND * 2); 299 300 m_timer.stop(); 301 302 int counted = count.get(); 303 304 assertEquals(m_timer.getNbNotifications(), 1); 305 306 m_timer.removeNotification(id); 307 assertTrue(m_timer.isEmpty()); 308 309 Thread.sleep(Timer.ONE_SECOND * 5); 311 312 assertEquals(counted, count.get()); 313 } 314 315 public void testSendPastNotifications1() throws Exception 316 { 317 final MutableBoolean bool = new MutableBoolean(false); 318 NotificationListener listener = new NotificationListener () 319 { 320 public void handleNotification(Notification notification, Object handback) 321 { 322 bool.set(true); 323 } 324 }; 325 m_server.addNotificationListener(m_timerName, listener, null, null); 326 327 long now = System.currentTimeMillis(); 328 329 Date date = new Date (now - Timer.ONE_SECOND); 332 m_timer.setSendPastNotifications(false); 333 m_timer.addNotification("notif-type", "notif-message", "notif-data", date); 334 m_timer.start(); 335 336 sleep(Timer.ONE_SECOND); 338 339 assertFalse(bool.get()); 340 assertTrue(m_timer.isEmpty()); 341 } 342 343 public void testNotificationsWithOldDate() throws Exception 344 { 345 final MutableBoolean bool = new MutableBoolean(false); 346 NotificationListener listener = new NotificationListener () 347 { 348 public void handleNotification(Notification notification, Object handback) 349 { 350 bool.set(true); 351 } 352 }; 353 m_server.addNotificationListener(m_timerName, listener, null, null); 354 355 long now = System.currentTimeMillis(); 356 357 m_timer.start(); 358 Date date = new Date (now - Timer.ONE_SECOND); 359 m_timer.addNotification("notif-type", "notif-message", "notif-data", date); 360 361 sleep(Timer.ONE_SECOND); 363 364 assertTrue(bool.get()); 365 assertTrue(m_timer.isEmpty()); 366 } 367 368 public void testSendPastNotifications2() throws Exception 369 { 370 final MutableBoolean bool = new MutableBoolean(false); 371 NotificationListener listener = new NotificationListener () 372 { 373 public void handleNotification(Notification notification, Object handback) 374 { 375 bool.set(true); 376 } 377 }; 378 m_server.addNotificationListener(m_timerName, listener, null, null); 379 380 long now = System.currentTimeMillis(); 381 382 Date date = new Date (now - Timer.ONE_SECOND); 385 m_timer.setSendPastNotifications(true); 386 m_timer.addNotification("notif-type", "notif-message", "notif-data", date); 387 m_timer.start(); 388 389 sleep(Timer.ONE_SECOND); 391 392 assertTrue(bool.get()); 393 assertTrue(m_timer.isEmpty()); 394 } 395 396 public void testSendPastNotifications3() throws Exception 397 { 398 final MutableInteger count = new MutableInteger(0); 399 NotificationListener listener = new NotificationListener () 400 { 401 public void handleNotification(Notification notification, Object handback) 402 { 403 count.set(count.get() + 1); 404 } 405 }; 406 m_server.addNotificationListener(m_timerName, listener, null, null); 407 408 long now = System.currentTimeMillis(); 409 410 long occurrences = 10; 413 long skip = 4; 414 Date date = new Date (now - Timer.ONE_SECOND * skip); 415 m_timer.setSendPastNotifications(false); 416 m_timer.addNotification("notif-type", "notif-message", "notif-data", date, Timer.ONE_SECOND, occurrences); 417 m_timer.start(); 418 419 sleep(Timer.ONE_SECOND * (occurrences + 1)); 421 422 long expected = occurrences - skip; 424 if (count.get() != expected && count.get() != expected - 1) 425 fail("Expected notifications not emitted: expecting " + expected + " got " + count.get()); 426 assertTrue(m_timer.isEmpty()); 427 } 428 429 public void testSendPastNotifications4() throws Exception 430 { 431 final MutableInteger count = new MutableInteger(0); 432 NotificationListener listener = new NotificationListener () 433 { 434 public void handleNotification(Notification notification, Object handback) 435 { 436 count.set(count.get() + 1); 437 } 438 }; 439 m_server.addNotificationListener(m_timerName, listener, null, null); 440 441 long now = System.currentTimeMillis(); 442 443 long occurrences = 10; 446 long skip = 4; 447 Date date = new Date (now - Timer.ONE_SECOND * skip); 448 m_timer.setSendPastNotifications(true); 449 m_timer.addNotification("notif-type", "notif-message", "notif-data", date, Timer.ONE_SECOND, occurrences); 450 m_timer.start(); 451 452 sleep(Timer.ONE_SECOND * (occurrences + 1)); 454 455 assertEquals(count.get(), occurrences); 456 assertTrue(m_timer.isEmpty()); 457 } 458 459 public void testSendPastNotifications5() throws Exception 460 { 461 final MutableInteger count = new MutableInteger(0); 462 NotificationListener listener = new NotificationListener () 463 { 464 public void handleNotification(Notification notification, Object handback) 465 { 466 count.set(count.get() + 1); 467 } 468 }; 469 m_server.addNotificationListener(m_timerName, listener, null, null); 470 471 long now = System.currentTimeMillis(); 472 473 long occurrences = 10; 476 long pre = 2; 477 long skip = 4; 478 Date date = new Date (now + Timer.ONE_SECOND); 479 m_timer.setSendPastNotifications(false); 480 m_timer.addNotification("notif-type", "notif-message", "notif-data", date, Timer.ONE_SECOND, occurrences); 481 m_timer.start(); 482 483 sleep(Timer.ONE_SECOND * pre); 485 m_timer.stop(); 486 487 if (count.get() != pre && count.get() != pre - 1) 489 fail("Expected notifications not emitted: expecting " + pre + " got " + count.get()); 490 assertEquals(m_timer.getNbNotifications(), 1); 491 492 sleep(Timer.ONE_SECOND * skip); 494 495 m_timer.start(); 497 498 sleep(Timer.ONE_SECOND * (occurrences - pre - skip + 1)); 500 501 m_timer.stop(); 502 503 long expected = occurrences - skip; 505 if (count.get() != expected && count.get() != expected - 1) 506 fail("Expected notifications not emitted. Expected " + expected + " or " + (expected - 1) + ". got " + count.get()); 507 assertTrue(m_timer.isEmpty()); 508 } 509 510 public void testSendPastNotifications6() throws Exception 511 { 512 final MutableInteger count = new MutableInteger(0); 513 NotificationListener listener = new NotificationListener () 514 { 515 public void handleNotification(Notification notification, Object handback) 516 { 517 count.set(count.get() + 1); 518 } 519 }; 520 m_server.addNotificationListener(m_timerName, listener, null, null); 521 522 long now = System.currentTimeMillis(); 523 524 long occurrences = 10; 527 long pre = 2; 528 long skip = 4; 529 Date date = new Date (now + Timer.ONE_SECOND); 530 m_timer.setSendPastNotifications(true); 531 m_timer.addNotification("notif-type", "notif-message", "notif-data", date, Timer.ONE_SECOND, occurrences, true); 532 m_timer.start(); 533 534 sleep(Timer.ONE_SECOND * pre); 536 m_timer.stop(); 537 538 if (count.get() != pre && count.get() != pre - 1) 540 fail("Expected notifications not emitted: expecting " + pre + " got " + count.get()); 541 assertFalse(m_timer.isEmpty()); 542 543 sleep(Timer.ONE_SECOND * skip); 545 546 m_timer.start(); 548 549 sleep(Timer.ONE_SECOND * (occurrences - pre - skip + 1)); 551 552 m_timer.stop(); 553 assertEquals(count.get(), occurrences); 554 assertTrue(m_timer.isEmpty()); 555 } 556 557 public void testFixedDelay() throws Exception 558 { 559 m_timer.start(); 560 561 final int occurrences = 100; 562 final String fdNotifType = "timer-test-fixed-delay"; 563 final String frNotifType = "timer-test-fixed-rate"; 564 565 final MutableInteger frOccurrences = new MutableInteger(0); 566 final MutableLong frElapsedTime = new MutableLong(0); 567 final MutableLong frLastTime = new MutableLong(System.currentTimeMillis()); 568 569 NotificationListener frListener = new NotificationListener () 570 { 571 public void handleNotification(Notification notification, Object handback) 572 { 573 if (frOccurrences.get() < occurrences) 574 { 575 long now = System.currentTimeMillis(); 576 frElapsedTime.set(frElapsedTime.get() + (now - frLastTime.get())); 577 frLastTime.set(now); 578 frOccurrences.set(frOccurrences.get() + 1); 579 } 580 } 581 }; 582 583 final MutableInteger fdOccurrences = new MutableInteger(0); 584 final MutableLong fdElapsedTime = new MutableLong(0); 585 final MutableLong fdLastTime = new MutableLong(System.currentTimeMillis()); 586 587 NotificationListener fdListener = new NotificationListener () 588 { 589 public void handleNotification(Notification notification, Object handback) 590 { 591 if (fdOccurrences.get() < occurrences) 592 { 593 long now = System.currentTimeMillis(); 594 fdElapsedTime.set(fdElapsedTime.get() + (now - fdLastTime.get())); 595 fdLastTime.set(now); 596 fdOccurrences.set(fdOccurrences.get() + 1); 597 } 598 } 599 }; 600 601 m_server.addNotificationListener(m_timerName, fdListener, new NotificationFilter () 602 { 603 public boolean isNotificationEnabled(Notification notification) 604 { 605 return notification.getType().equals(fdNotifType); 606 } 607 }, null); 608 609 m_server.addNotificationListener(m_timerName, frListener, new NotificationFilter () 610 { 611 public boolean isNotificationEnabled(Notification notification) 612 { 613 return notification.getType().equals(frNotifType); 614 } 615 }, null); 616 617 long now = System.currentTimeMillis(); 619 Date date = new Date (now + Timer.ONE_SECOND); 621 m_timer.addNotification(fdNotifType, "timer-message", "user-data", date, 10, occurrences, false); 623 m_timer.addNotification(frNotifType, "timer-message", "user-data", date, 10, occurrences, true); 624 625 while (frOccurrences.get() < occurrences || fdOccurrences.get() < occurrences) 627 { 628 sleep(10); 629 System.gc(); 630 } 631 632 assertEquals(frOccurrences.get(), occurrences); 633 assertEquals(fdOccurrences.get(), occurrences); 634 635 if (((1.0f * frElapsedTime.get()) / fdElapsedTime.get()) > 0.95) 636 fail("Fixed rate and fixed delay exhibit no execution rate differences"); 637 } 638 639 public void testRemoveNotifications() throws Exception 640 { 641 m_timer.addNotification("mx4j.timer.test", "test notification", null, new Date (System.currentTimeMillis()), 8); 642 m_timer.addNotification("mx4j.timer.ignore", "ignore me", null, new Date (System.currentTimeMillis()), 4); 643 m_timer.addNotification("mx4j.timer.test", "another test", null, new Date (System.currentTimeMillis()), 8); 644 assertEquals(m_timer.getNbNotifications(), 3); 645 m_timer.start(); 646 m_timer.removeNotifications("mx4j.timer.ignore"); 647 assertEquals(m_timer.getNbNotifications(), 2); 648 } 649 650 public void testRemoveNonexistentNotifications() throws Exception 651 { 652 m_timer.addNotification("mx4j.timer.test", "test notification", null, new Date (System.currentTimeMillis()), 4); 653 m_timer.start(); 654 try 655 { 656 m_timer.removeNotifications("mx4j.timer.bogus"); 657 fail("Expecting InstanceNotFoundException"); 658 } 659 catch (InstanceNotFoundException x) 660 { 661 } 662 } 663 } 664 | Popular Tags |