Beispiel #1
0
  @Test
  public void testTimer() throws Exception {
    ManualClock clock = new ManualClock(0);
    DurationTimer timer = new DurationTimer(MonitorConfig.builder("test").build(), clock);
    Stopwatch s = timer.start();
    clock.set(10 * 1000L);
    assertEquals(10, s.getDuration());

    Monitor<Long> duration = getDuration(timer.getMonitors());
    Monitor<Long> activeTasks = getActiveTasks(timer.getMonitors());
    assertEquals(10L, duration.getValue().longValue());
    assertEquals(1L, activeTasks.getValue().longValue());
    clock.set(20 * 1000L);

    assertEquals(20L, duration.getValue().longValue());
    assertEquals(1L, activeTasks.getValue().longValue());

    Stopwatch anotherTask = timer.start();
    assertEquals(20L, duration.getValue().longValue());
    assertEquals(2L, activeTasks.getValue().longValue());

    clock.set(30 * 1000L);
    assertEquals(40L, duration.getValue().longValue()); // 30s for the first, 10s for the second
    assertEquals(2L, activeTasks.getValue().longValue());

    s.stop();
    assertEquals(10L, duration.getValue().longValue()); // 30s for the first, 10s for the second
    assertEquals(1L, activeTasks.getValue().longValue());

    anotherTask.stop();
    assertEquals(0L, duration.getValue().longValue());
    assertEquals(0L, activeTasks.getValue().longValue());
  }
Beispiel #2
0
 @Test
 public void testValue() throws Exception {
   ManualClock clock = new ManualClock(0L);
   DurationTimer timer = new DurationTimer(MonitorConfig.builder("test").build(), clock);
   assertEquals(0L, timer.getValue().longValue());
   Stopwatch s = timer.start();
   clock.set(10 * 1000L);
   assertEquals(10L, timer.getValue().longValue());
   s.stop();
   assertEquals(0L, timer.getValue().longValue());
 }