use of org.eclipse.microprofile.metrics.annotation.SimplyTimed in project Payara by payara.
the class SimplyTimedInterceptorTest method assertTimed.
private void assertTimed(int expectedStartCount) throws Exception {
Method element = TestUtils.getTestMethod();
Class<?> bean = getClass();
AnnotationReader<SimplyTimed> reader = AnnotationReader.SIMPLY_TIMED;
SimpleTimer timer = MetricUtils.getOrRegisterByMetadataAndTags(registry, SimpleTimer.class, reader.metadata(bean, element), reader.tags(reader.annotation(bean, element)));
SimplyTimedInterceptor.proceedTimed(context, element, bean, registry::getMetric);
assertEquals(expectedStartCount + 1, timer.getCount());
SimplyTimedInterceptor.proceedTimed(context, element, bean, registry::getMetric);
assertEquals(expectedStartCount + 2, timer.getCount());
verify(context, times(2)).proceed();
// now test error when it does not exist
try {
SimplyTimedInterceptor.proceedTimed(context, element, bean, (metricId, type) -> null);
fail("Expected a IllegalStateException because the metric does not exist");
} catch (IllegalStateException ex) {
assertEquals("No SimpleTimer with ID [" + reader.metricID(bean, element) + "] found in application registry", ex.getMessage());
}
verify(context, times(2)).proceed();
// test a annotated method that throws an exception
when(context.proceed()).thenThrow(new RuntimeException("Error in method"));
try {
SimplyTimedInterceptor.proceedTimed(context, element, bean, registry::getMetric);
fail("Expected a RuntimeException");
} catch (RuntimeException ex) {
assertEquals("Error in method", ex.getMessage());
}
verify(context, times(3)).proceed();
// need to remove the 'thenThrow' behaviour
reset(context);
assertEquals(expectedStartCount + 3, timer.getCount());
assertTrue(timer.getElapsedTime().toNanos() > 0);
}
Aggregations