/metrics-guice/src/test/java/com/yammer/metrics/guice/tests/ExceptionMeteredTest.java

https://github.com/pjjw/metrics · Java · 373 lines · 273 code · 92 blank · 8 comment · 0 complexity · a648e500a00dcc84e4487d9699c69f9f MD5 · raw file

  1. package com.yammer.metrics.guice.tests;
  2. import com.google.inject.Guice;
  3. import com.google.inject.Injector;
  4. import com.yammer.metrics.core.*;
  5. import com.yammer.metrics.guice.ExceptionMetered;
  6. import com.yammer.metrics.guice.InstrumentationModule;
  7. import org.junit.Before;
  8. import org.junit.Test;
  9. import java.util.concurrent.TimeUnit;
  10. import static org.hamcrest.Matchers.instanceOf;
  11. import static org.hamcrest.Matchers.is;
  12. import static org.hamcrest.Matchers.notNullValue;
  13. import static org.junit.Assert.assertThat;
  14. import static org.junit.Assert.fail;
  15. public class ExceptionMeteredTest {
  16. InstrumentedWithExceptionMetered instance;
  17. MetricsRegistry registry;
  18. @Before
  19. public void setup() {
  20. Injector injector = Guice.createInjector(new InstrumentationModule());
  21. instance = injector.getInstance(InstrumentedWithExceptionMetered.class);
  22. registry = injector.getInstance(MetricsRegistry.class);
  23. }
  24. @Test
  25. public void anExceptionMeteredAnnotatedMethodWithPublicScope() throws Exception {
  26. final Metric metric = registry.allMetrics().get(new MetricName(InstrumentedWithExceptionMetered.class,
  27. "exceptionCounter"));
  28. assertMetricIsSetup(metric);
  29. assertThat("Metric intialises to zero",
  30. ((MeterMetric) metric).count(),
  31. is(0L));
  32. try {
  33. instance.explodeWithPublicScope(true);
  34. fail("Expected an exception to be thrown");
  35. } catch(RuntimeException e) {
  36. // Swallow the expected exception
  37. }
  38. assertThat("Metric is marked",
  39. ((MeterMetric) metric).count(),
  40. is(1L));
  41. }
  42. @Test
  43. public void anExceptionMeteredAnnotatedMethod_WithNoMetricName() throws Exception {
  44. final Metric metric = registry.allMetrics().get(new MetricName(InstrumentedWithExceptionMetered.class,
  45. "explodeForUnnamedMetric" + ExceptionMetered.DEFAULT_NAME_SUFFIX));
  46. assertMetricIsSetup(metric);
  47. assertThat("Metric intialises to zero",
  48. ((MeterMetric) metric).count(),
  49. is(0L));
  50. try {
  51. instance.explodeForUnnamedMetric();
  52. fail("Expected an exception to be thrown");
  53. } catch(RuntimeException e) {
  54. // Swallow the expected exception
  55. }
  56. assertThat("Metric is marked",
  57. ((MeterMetric) metric).count(),
  58. is(1L));
  59. }
  60. @Test
  61. public void anExceptionMeteredAnnotatedMethod_WithPublicScopeButNoExceptionThrown() throws Exception {
  62. final Metric metric = registry.allMetrics().get(new MetricName(InstrumentedWithExceptionMetered.class,
  63. "exceptionCounter"));
  64. assertMetricIsSetup(metric);
  65. assertThat("Metric intialises to zero",
  66. ((MeterMetric) metric).count(),
  67. is(0L));
  68. instance.explodeWithPublicScope(false);
  69. assertThat("Metric should remain at zero if no exception is thrown",
  70. ((MeterMetric) metric).count(),
  71. is(0L));
  72. }
  73. @Test
  74. public void anExceptionMeteredAnnotatedMethod_WithDefaultScope() throws Exception {
  75. final Metric metric = registry.allMetrics().get(new MetricName(InstrumentedWithExceptionMetered.class,
  76. "explodeWithDefaultScopeExceptionMetric"));
  77. assertMetricIsSetup(metric);
  78. assertThat("Metric intialises to zero",
  79. ((MeterMetric) metric).count(),
  80. is(0L));
  81. try {
  82. instance.explodeWithDefaultScope();
  83. fail("Expected an exception to be thrown");
  84. } catch(RuntimeException e) {
  85. }
  86. assertThat("Metric is marked",
  87. ((MeterMetric) metric).count(),
  88. is(1L));
  89. }
  90. @Test
  91. public void anExceptionMeteredAnnotatedMethod_WithProtectedScope() throws Exception {
  92. final Metric metric = registry.allMetrics().get(new MetricName(InstrumentedWithExceptionMetered.class,
  93. "explodeWithProtectedScopeExceptionMetric"));
  94. assertMetricIsSetup(metric);
  95. assertThat("Metric intialises to zero",
  96. ((MeterMetric) metric).count(),
  97. is(0L));
  98. try {
  99. instance.explodeWithProtectedScope();
  100. fail("Expected an exception to be thrown");
  101. } catch(RuntimeException e) {
  102. }
  103. assertThat("Metric is marked",
  104. ((MeterMetric) metric).count(),
  105. is(1L));
  106. }
  107. @Test
  108. public void anExceptionMeteredAnnotatedMethod_WithPublicScope_AndSpecificTypeOfException() throws Exception {
  109. final Metric metric = registry.allMetrics().get(new MetricName(InstrumentedWithExceptionMetered.class,
  110. "failures"));
  111. assertMetricIsSetup(metric);
  112. assertThat("Metric intialises to zero",
  113. ((MeterMetric) metric).count(),
  114. is(0L));
  115. try {
  116. instance.errorProneMethod(new MyException());
  117. fail("Expected an exception to be thrown");
  118. } catch(MyException e) {
  119. }
  120. assertThat("Metric should be marked when the specified exception type is thrown",
  121. ((MeterMetric) metric).count(),
  122. is(1L));
  123. }
  124. @Test
  125. public void anExceptionMeteredAnnotatedMethod_WithPublicScope_AndSubclassesOfSpecifiedException() throws Exception {
  126. final Metric metric = registry.allMetrics().get(new MetricName(InstrumentedWithExceptionMetered.class,
  127. "failures"));
  128. assertMetricIsSetup(metric);
  129. assertThat("Metric intialises to zero",
  130. ((MeterMetric) metric).count(),
  131. is(0L));
  132. try {
  133. instance.errorProneMethod(new MySpecialisedException());
  134. fail("Expected an exception to be thrown");
  135. } catch(MyException e) {
  136. }
  137. assertThat("Metric should be marked when a subclass of the specified exception type is thrown",
  138. ((MeterMetric) metric).count(),
  139. is(1L));
  140. }
  141. @Test
  142. public void anExceptionMeteredAnnotatedMethod_WithPublicScope_ButDifferentTypeOfException() throws Exception {
  143. final Metric metric = registry.allMetrics().get(new MetricName(InstrumentedWithExceptionMetered.class,
  144. "failures"));
  145. assertMetricIsSetup(metric);
  146. assertThat("Metric intialises to zero",
  147. ((MeterMetric) metric).count(),
  148. is(0L));
  149. try {
  150. instance.errorProneMethod(new MyOtherException());
  151. fail("Expected an exception to be thrown");
  152. } catch(MyOtherException e) {
  153. }
  154. assertThat("Metric should not be marked if the exception is a different type",
  155. ((MeterMetric) metric).count(),
  156. is(0L));
  157. }
  158. @Test
  159. public void anExceptionMeteredAnnotatedMethod_WithExtraOptions() throws Exception {
  160. try {
  161. instance.causeAnOutOfBoundsException();
  162. } catch (ArrayIndexOutOfBoundsException e) {
  163. }
  164. final Metric metric = registry.allMetrics().get(new MetricName(InstrumentedWithExceptionMetered.class,
  165. "things"));
  166. assertMetricIsSetup(metric);
  167. assertThat("Guice creates a meter which gets marked",
  168. ((MeterMetric) metric).count(),
  169. is(1L));
  170. assertThat("Guice creates a meter with the given event type",
  171. ((MeterMetric) metric).eventType(),
  172. is("poops"));
  173. assertThat("Guice creates a meter with the given rate unit",
  174. ((MeterMetric) metric).rateUnit(),
  175. is(TimeUnit.MINUTES));
  176. }
  177. @Test
  178. public void aMethodAnnotatedWithBothATimerAndAnExceptionCounter() throws Exception {
  179. final Metric timedMetric = registry.allMetrics().get(new MetricName(InstrumentedWithExceptionMetered.class,
  180. "timedAndException"));
  181. final Metric errorMetric = registry.allMetrics().get(new MetricName(InstrumentedWithExceptionMetered.class,
  182. "timedAndExceptionExceptionMetric"));
  183. assertThat("Guice creates a metric",
  184. timedMetric,
  185. is(notNullValue()));
  186. assertThat("Guice creates a timer",
  187. timedMetric,
  188. is(instanceOf(TimerMetric.class)));
  189. assertThat("Guice creates a metric",
  190. errorMetric,
  191. is(notNullValue()));
  192. assertThat("Guice creates a meter",
  193. errorMetric,
  194. is(instanceOf(MeterMetric.class)));
  195. // Counts should start at zero
  196. assertThat("Timer Metric should be zero when initialised",
  197. ((TimerMetric) timedMetric).count(),
  198. is(0L));
  199. assertThat("Error Metric should be zero when initialised",
  200. ((MeterMetric) errorMetric).count(),
  201. is(0L));
  202. // Invoke, but don't throw an exception
  203. instance.timedAndException(null);
  204. assertThat("Expected the meter metric to be marked on invocation",
  205. ((TimerMetric) timedMetric).count(),
  206. is(1L));
  207. assertThat("Expected the exception metric to be zero since no exceptions thrown",
  208. ((MeterMetric) errorMetric).count(),
  209. is(0L));
  210. // Invoke and throw an exception
  211. try {
  212. instance.timedAndException(new RuntimeException());
  213. fail("Should have thrown an exception");
  214. } catch (Exception e) {}
  215. assertThat("Expected a count of 2, one for each invocation",
  216. ((TimerMetric) timedMetric).count(),
  217. is(2L));
  218. assertThat("Expected exception count to be 1 as one (of two) invocations threw an exception",
  219. ((MeterMetric) errorMetric).count(),
  220. is(1L));
  221. }
  222. @Test
  223. public void aMethodAnnotatedWithBothAMeteredAndAnExceptionCounter() throws Exception {
  224. final Metric meteredMetric = registry.allMetrics().get(new MetricName(InstrumentedWithExceptionMetered.class,
  225. "meteredAndException"));
  226. final Metric errorMetric = registry.allMetrics().get(new MetricName(InstrumentedWithExceptionMetered.class,
  227. "meteredAndExceptionExceptionMetric"));
  228. assertThat("Guice creates a metric",
  229. meteredMetric,
  230. is(notNullValue()));
  231. assertThat("Guice creates a meter",
  232. meteredMetric,
  233. is(instanceOf(MeterMetric.class)));
  234. assertThat("Guice creates a metric",
  235. errorMetric,
  236. is(notNullValue()));
  237. assertThat("Guice creates an exception meter",
  238. errorMetric,
  239. is(instanceOf(MeterMetric.class)));
  240. // Counts should start at zero
  241. assertThat("Meter Metric should be zero when initialised",
  242. ((MeterMetric) meteredMetric).count(),
  243. is(0L));
  244. assertThat("Error Metric should be zero when initialised",
  245. ((MeterMetric) errorMetric).count(),
  246. is(0L));
  247. // Invoke, but don't throw an exception
  248. instance.meteredAndException(null);
  249. assertThat("Expected the meter metric to be marked on invocation",
  250. ((MeterMetric) meteredMetric).count(),
  251. is(1L));
  252. assertThat("Expected the exception metric to be zero since no exceptions thrown",
  253. ((MeterMetric) errorMetric).count(),
  254. is(0L));
  255. // Invoke and throw an exception
  256. try {
  257. instance.meteredAndException(new RuntimeException());
  258. fail("Should have thrown an exception");
  259. } catch (Exception e) {}
  260. assertThat("Expected a count of 2, one for each invocation",
  261. ((MeterMetric) meteredMetric).count(),
  262. is(2L));
  263. assertThat("Expected exception count to be 1 as one (of two) invocations threw an exception",
  264. ((MeterMetric) errorMetric).count(),
  265. is(1L));
  266. }
  267. private void assertMetricIsSetup(final Metric metric) {
  268. assertThat("Guice creates a metric",
  269. metric,
  270. is(notNullValue()));
  271. assertThat("Guice creates a meter",
  272. metric,
  273. is(instanceOf(MeterMetric.class)));
  274. }
  275. @SuppressWarnings("serial")
  276. private static class MyOtherException extends RuntimeException {
  277. }
  278. @SuppressWarnings("serial")
  279. private static class MySpecialisedException extends MyException {
  280. }
  281. }