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

http://github.com/codahale/metrics · Java · 102 lines · 75 code · 27 blank · 0 comment · 0 complexity · 12736b1d743ae6123afe4ec556f26c19 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.InstrumentationModule;
  6. import org.junit.Before;
  7. import org.junit.Test;
  8. import java.util.concurrent.TimeUnit;
  9. import static org.hamcrest.Matchers.instanceOf;
  10. import static org.hamcrest.Matchers.is;
  11. import static org.hamcrest.Matchers.notNullValue;
  12. import static org.junit.Assert.assertThat;
  13. public class MeteredTest {
  14. InstrumentedWithMetered instance;
  15. MetricsRegistry registry;
  16. @Before
  17. public void setup() {
  18. final Injector injector = Guice.createInjector(new InstrumentationModule());
  19. instance = injector.getInstance(InstrumentedWithMetered.class);
  20. registry = injector.getInstance(MetricsRegistry.class);
  21. }
  22. @Test
  23. public void aMeteredAnnotatedMethod() throws Exception {
  24. instance.doAThing();
  25. final Metric metric = registry.allMetrics()
  26. .get(new MetricName(InstrumentedWithMetered.class,
  27. "things"));
  28. assertMetricIsSetup(metric);
  29. assertThat("Guice creates a meter which gets marked",
  30. ((Meter) metric).count(),
  31. is(1L));
  32. assertThat("Guice creates a meter with the given event type",
  33. ((Meter) metric).eventType(),
  34. is("poops"));
  35. assertThat("Guice creates a meter with the given rate unit",
  36. ((Meter) metric).rateUnit(),
  37. is(TimeUnit.MINUTES));
  38. }
  39. @Test
  40. public void aMeteredAnnotatedMethodWithDefaultScope() throws Exception {
  41. final Metric metric = registry.allMetrics()
  42. .get(new MetricName(InstrumentedWithMetered.class,
  43. "doAThingWithDefaultScope"));
  44. assertMetricIsSetup(metric);
  45. assertThat("Metric intialises to zero",
  46. ((Meter) metric).count(),
  47. is(0L));
  48. instance.doAThingWithDefaultScope();
  49. assertThat("Metric is marked",
  50. ((Meter) metric).count(),
  51. is(1L));
  52. }
  53. @Test
  54. public void aMeteredAnnotatedMethodWithProtectedScope() throws Exception {
  55. final Metric metric = registry.allMetrics()
  56. .get(new MetricName(InstrumentedWithMetered.class,
  57. "doAThingWithProtectedScope"));
  58. assertMetricIsSetup(metric);
  59. assertThat("Metric intialises to zero",
  60. ((Meter) metric).count(),
  61. is(0L));
  62. instance.doAThingWithProtectedScope();
  63. assertThat("Metric is marked",
  64. ((Meter) metric).count(),
  65. is(1L));
  66. }
  67. private void assertMetricIsSetup(final Metric metric) {
  68. assertThat("Guice creates a metric",
  69. metric,
  70. is(notNullValue()));
  71. assertThat("Guice creates a meter",
  72. metric,
  73. is(instanceOf(Meter.class)));
  74. }
  75. }