PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

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