PageRenderTime 876ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

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