/**
   * Test that the timeout method on a singleton bean, which creates a timer, is invoked at
   * appropriate intervals.
   *
   * @throws Exception
   */
  @Test
  public void testTimeout() throws Exception {
    TimerUtil timerUtil = (TimerUtil) this.getInitialContext().lookup(TimerSingleton.JNDI_NAME);
    Date fiveSecondsFromNow = new Date(System.currentTimeMillis() + 5000);
    long everyTwoSeconds = 2000;
    int fiveTimes = 5;
    timerUtil.createTimer(fiveSecondsFromNow, everyTwoSeconds, fiveTimes);

    // wait for the timers to be invoked
    Thread.sleep(5000 + 10000 + 5000);
    TimeoutTracker timeoutTracker = timerUtil.getTimeoutTracker();

    Assert.assertNotNull("Could not get the timeout tracker", timeoutTracker);
    Assert.assertEquals(
        "Unexpected number of timeouts", fiveTimes, timeoutTracker.getTimeoutCount());
    List<Date> timeouts = timeoutTracker.getTimeouts();
    // 1 second grace period
    Date lastExpectedTimeout =
        new Date(fiveSecondsFromNow.getTime() + (everyTwoSeconds * fiveTimes) + 1000);
    for (Date timeout : timeouts) {
      logger.debug("Timeout was tracked at " + timeout);
      Assert.assertFalse(
          "Timeout " + timeout + " happened before the first timeout " + fiveSecondsFromNow,
          timeout.before(fiveSecondsFromNow));
      Assert.assertTrue(
          "Timeout " + timeout + " happened after the last expected timeout " + lastExpectedTimeout,
          timeout.before(lastExpectedTimeout));
    }
  }