Skip to content
Browse files

Merge pull request #185 from ryantenney/mockclock

Use a mock `Clock` in `TimerTest`, as the real thing fails intermittently
  • Loading branch information...
2 parents 39ee209 + 4a7d6ab commit a91263c74283130fd116014451430addbd512ece @codahale codahale committed Mar 5, 2012
Showing with 21 additions and 8 deletions.
  1. +21 −8 metrics-core/src/test/java/com/yammer/metrics/core/tests/TimerTest.java
View
29 metrics-core/src/test/java/com/yammer/metrics/core/tests/TimerTest.java
@@ -1,8 +1,8 @@
package com.yammer.metrics.core.tests;
+import com.yammer.metrics.core.Clock;
import com.yammer.metrics.core.MetricsRegistry;
import com.yammer.metrics.core.Timer;
-import com.yammer.metrics.core.TimerContext;
import com.yammer.metrics.stats.Snapshot;
import org.junit.After;
import org.junit.Before;
@@ -20,7 +20,15 @@
@Before
public void setUp() throws Exception {
- this.registry = new MetricsRegistry();
+ this.registry = new MetricsRegistry(new Clock() {
+ // a mock clock that increments its ticker by 50msec per call
+ private long val = 0;
+
+ @Override
+ public long tick() {
+ return val += 50000000;
+ }
+ });
this.timer = registry.newTimer(TimerTest.class, "timer");
}
@@ -162,28 +170,33 @@ public void timingCallableInstances() throws Exception {
final String value = timer.time(new Callable<String>() {
@Override
public String call() throws Exception {
- Thread.sleep(50);
return "one";
}
});
+ assertThat("the timer has a count of 1",
+ timer.count(),
+ is(1L));
+
assertThat("returns the result of the callable",
value,
is("one"));
assertThat("records the duration of the Callable#call()",
timer.max(),
- is(closeTo(50, 5)));
+ is(closeTo(50.0, 0.001)));
}
@Test
public void timingContexts() throws Exception {
- final TimerContext context = timer.time();
- Thread.sleep(50);
- context.stop();
+ timer.time().stop();
+
+ assertThat("the timer has a count of 1",
+ timer.count(),
+ is(1L));
assertThat("records the duration of the context",
timer.max(),
- is(closeTo(50, 5)));
+ is(closeTo(50.0, 0.001)));
}
}

0 comments on commit a91263c

Please sign in to comment.
Something went wrong with that request. Please try again.