PageRenderTime 42ms CodeModel.GetById 28ms app.highlight 12ms 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
  1package com.yammer.metrics.guice.tests;
  2
  3import com.google.inject.Guice;
  4import com.google.inject.Injector;
  5import com.yammer.metrics.core.*;
  6import com.yammer.metrics.guice.ExceptionMetered;
  7import com.yammer.metrics.guice.InstrumentationModule;
  8
  9import org.junit.Before;
 10import org.junit.Test;
 11
 12import java.util.concurrent.TimeUnit;
 13
 14import static org.hamcrest.Matchers.instanceOf;
 15import static org.hamcrest.Matchers.is;
 16import static org.hamcrest.Matchers.notNullValue;
 17import static org.junit.Assert.assertThat;
 18import static org.junit.Assert.fail;
 19
 20public class ExceptionMeteredTest {
 21
 22    InstrumentedWithExceptionMetered instance;
 23    MetricsRegistry registry;
 24
 25    @Before
 26    public void setup() {
 27        Injector injector = Guice.createInjector(new InstrumentationModule());
 28        instance = injector.getInstance(InstrumentedWithExceptionMetered.class);
 29        registry = injector.getInstance(MetricsRegistry.class);
 30    }
 31
 32    @Test
 33    public void anExceptionMeteredAnnotatedMethodWithPublicScope() throws Exception {
 34
 35        final Metric metric = registry.allMetrics()
 36                                      .get(new MetricName(InstrumentedWithExceptionMetered.class,
 37                                                          "exceptionCounter"));
 38        assertMetricIsSetup(metric);
 39
 40        assertThat("Metric intialises to zero",
 41                   ((MeterMetric) metric).count(),
 42                   is(0L));
 43
 44        try {
 45            instance.explodeWithPublicScope(true);
 46            fail("Expected an exception to be thrown");
 47        } catch (RuntimeException e) {
 48            // Swallow the expected exception
 49        }
 50
 51        assertThat("Metric is marked",
 52                   ((MeterMetric) metric).count(),
 53                   is(1L));
 54    }
 55
 56    @Test
 57    public void anExceptionMeteredAnnotatedMethod_WithNoMetricName() throws Exception {
 58
 59        final Metric metric = registry.allMetrics()
 60                                      .get(new MetricName(InstrumentedWithExceptionMetered.class,
 61                                                          "explodeForUnnamedMetric" + ExceptionMetered.DEFAULT_NAME_SUFFIX));
 62        assertMetricIsSetup(metric);
 63
 64        assertThat("Metric intialises to zero",
 65                   ((MeterMetric) metric).count(),
 66                   is(0L));
 67
 68        try {
 69            instance.explodeForUnnamedMetric();
 70            fail("Expected an exception to be thrown");
 71        } catch (RuntimeException e) {
 72            // Swallow the expected exception
 73        }
 74
 75        assertThat("Metric is marked",
 76                   ((MeterMetric) metric).count(),
 77                   is(1L));
 78    }
 79
 80    @Test
 81    public void anExceptionMeteredAnnotatedMethod_WithPublicScopeButNoExceptionThrown() throws Exception {
 82
 83        final Metric metric = registry.allMetrics()
 84                                      .get(new MetricName(InstrumentedWithExceptionMetered.class,
 85                                                          "exceptionCounter"));
 86        assertMetricIsSetup(metric);
 87
 88        assertThat("Metric intialises to zero",
 89                   ((MeterMetric) metric).count(),
 90                   is(0L));
 91
 92        instance.explodeWithPublicScope(false);
 93
 94        assertThat("Metric should remain at zero if no exception is thrown",
 95                   ((MeterMetric) metric).count(),
 96                   is(0L));
 97    }
 98
 99    @Test
100    public void anExceptionMeteredAnnotatedMethod_WithDefaultScope() throws Exception {
101
102        final Metric metric = registry.allMetrics()
103                                      .get(new MetricName(InstrumentedWithExceptionMetered.class,
104                                                          "explodeWithDefaultScopeExceptionMetric"));
105        assertMetricIsSetup(metric);
106
107        assertThat("Metric intialises to zero",
108                   ((MeterMetric) metric).count(),
109                   is(0L));
110
111        try {
112            instance.explodeWithDefaultScope();
113            fail("Expected an exception to be thrown");
114        } catch (RuntimeException e) {
115        }
116
117        assertThat("Metric is marked",
118                   ((MeterMetric) metric).count(),
119                   is(1L));
120    }
121
122    @Test
123    public void anExceptionMeteredAnnotatedMethod_WithProtectedScope() throws Exception {
124
125        final Metric metric = registry.allMetrics()
126                                      .get(new MetricName(InstrumentedWithExceptionMetered.class,
127                                                          "explodeWithProtectedScopeExceptionMetric"));
128
129        assertMetricIsSetup(metric);
130
131        assertThat("Metric intialises to zero",
132                   ((MeterMetric) metric).count(),
133                   is(0L));
134
135        try {
136            instance.explodeWithProtectedScope();
137            fail("Expected an exception to be thrown");
138        } catch (RuntimeException e) {
139        }
140
141        assertThat("Metric is marked",
142                   ((MeterMetric) metric).count(),
143                   is(1L));
144    }
145
146    @Test
147    public void anExceptionMeteredAnnotatedMethod_WithPublicScope_AndSpecificTypeOfException() throws Exception {
148
149        final Metric metric = registry.allMetrics()
150                                      .get(new MetricName(InstrumentedWithExceptionMetered.class,
151                                                          "failures"));
152        assertMetricIsSetup(metric);
153
154        assertThat("Metric intialises to zero",
155                   ((MeterMetric) metric).count(),
156                   is(0L));
157        try {
158            instance.errorProneMethod(new MyException());
159            fail("Expected an exception to be thrown");
160        } catch (MyException e) {
161        }
162
163        assertThat("Metric should be marked when the specified exception type is thrown",
164                   ((MeterMetric) metric).count(),
165                   is(1L));
166    }
167
168    @Test
169    public void anExceptionMeteredAnnotatedMethod_WithPublicScope_AndSubclassesOfSpecifiedException() throws Exception {
170
171        final Metric metric = registry.allMetrics()
172                                      .get(new MetricName(InstrumentedWithExceptionMetered.class,
173                                                          "failures"));
174        assertMetricIsSetup(metric);
175
176        assertThat("Metric intialises to zero",
177                   ((MeterMetric) metric).count(),
178                   is(0L));
179        try {
180            instance.errorProneMethod(new MySpecialisedException());
181            fail("Expected an exception to be thrown");
182        } catch (MyException e) {
183        }
184
185        assertThat(
186                "Metric should be marked when a subclass of the specified exception type is thrown",
187                ((MeterMetric) metric).count(),
188                is(1L));
189    }
190
191    @Test
192    public void anExceptionMeteredAnnotatedMethod_WithPublicScope_ButDifferentTypeOfException() throws Exception {
193
194        final Metric metric = registry.allMetrics()
195                                      .get(new MetricName(InstrumentedWithExceptionMetered.class,
196                                                          "failures"));
197        assertMetricIsSetup(metric);
198
199        assertThat("Metric intialises to zero",
200                   ((MeterMetric) metric).count(),
201                   is(0L));
202        try {
203            instance.errorProneMethod(new MyOtherException());
204            fail("Expected an exception to be thrown");
205        } catch (MyOtherException e) {
206        }
207
208        assertThat("Metric should not be marked if the exception is a different type",
209                   ((MeterMetric) metric).count(),
210                   is(0L));
211    }
212
213    @Test
214    public void anExceptionMeteredAnnotatedMethod_WithExtraOptions() throws Exception {
215
216        try {
217            instance.causeAnOutOfBoundsException();
218        } catch (ArrayIndexOutOfBoundsException e) {
219
220        }
221
222        final Metric metric = registry.allMetrics()
223                                      .get(new MetricName(InstrumentedWithExceptionMetered.class,
224                                                          "things"));
225
226        assertMetricIsSetup(metric);
227
228        assertThat("Guice creates a meter which gets marked",
229                   ((MeterMetric) metric).count(),
230                   is(1L));
231
232        assertThat("Guice creates a meter with the given event type",
233                   ((MeterMetric) metric).eventType(),
234                   is("poops"));
235
236        assertThat("Guice creates a meter with the given rate unit",
237                   ((MeterMetric) metric).rateUnit(),
238                   is(TimeUnit.MINUTES));
239    }
240
241
242    @Test
243    public void aMethodAnnotatedWithBothATimerAndAnExceptionCounter() throws Exception {
244
245        final Metric timedMetric = registry.allMetrics()
246                                           .get(new MetricName(InstrumentedWithExceptionMetered.class,
247                                                               "timedAndException"));
248
249        final Metric errorMetric = registry.allMetrics()
250                                           .get(new MetricName(InstrumentedWithExceptionMetered.class,
251                                                               "timedAndExceptionExceptionMetric"));
252
253        assertThat("Guice creates a metric",
254                   timedMetric,
255                   is(notNullValue()));
256
257        assertThat("Guice creates a timer",
258                   timedMetric,
259                   is(instanceOf(TimerMetric.class)));
260
261        assertThat("Guice creates a metric",
262                   errorMetric,
263                   is(notNullValue()));
264
265        assertThat("Guice creates a meter",
266                   errorMetric,
267                   is(instanceOf(MeterMetric.class)));
268
269        // Counts should start at zero        
270        assertThat("Timer Metric should be zero when initialised",
271                   ((TimerMetric) timedMetric).count(),
272                   is(0L));
273
274
275        assertThat("Error Metric should be zero when initialised",
276                   ((MeterMetric) errorMetric).count(),
277                   is(0L));
278
279        // Invoke, but don't throw an exception
280        instance.timedAndException(null);
281
282        assertThat("Expected the meter metric to be marked on invocation",
283                   ((TimerMetric) timedMetric).count(),
284                   is(1L));
285
286        assertThat("Expected the exception metric to be zero since no exceptions thrown",
287                   ((MeterMetric) errorMetric).count(),
288                   is(0L));
289
290        // Invoke and throw an exception
291        try {
292            instance.timedAndException(new RuntimeException());
293            fail("Should have thrown an exception");
294        } catch (Exception e) {}
295
296        assertThat("Expected a count of 2, one for each invocation",
297                   ((TimerMetric) timedMetric).count(),
298                   is(2L));
299
300        assertThat("Expected exception count to be 1 as one (of two) invocations threw an exception",
301                   ((MeterMetric) errorMetric).count(),
302                   is(1L));
303
304    }
305
306    @Test
307    public void aMethodAnnotatedWithBothAMeteredAndAnExceptionCounter() throws Exception {
308
309        final Metric meteredMetric = registry.allMetrics()
310                                             .get(new MetricName(InstrumentedWithExceptionMetered.class,
311                                                                 "meteredAndException"));
312
313        final Metric errorMetric = registry.allMetrics()
314                                           .get(new MetricName(InstrumentedWithExceptionMetered.class,
315                                                               "meteredAndExceptionExceptionMetric"));
316
317        assertThat("Guice creates a metric",
318                   meteredMetric,
319                   is(notNullValue()));
320
321        assertThat("Guice creates a meter",
322                   meteredMetric,
323                   is(instanceOf(MeterMetric.class)));
324
325        assertThat("Guice creates a metric",
326                   errorMetric,
327                   is(notNullValue()));
328
329        assertThat("Guice creates an exception meter",
330                   errorMetric,
331                   is(instanceOf(MeterMetric.class)));
332
333        // Counts should start at zero        
334        assertThat("Meter Metric should be zero when initialised",
335                   ((MeterMetric) meteredMetric).count(),
336                   is(0L));
337
338
339        assertThat("Error Metric should be zero when initialised",
340                   ((MeterMetric) errorMetric).count(),
341                   is(0L));
342
343        // Invoke, but don't throw an exception
344        instance.meteredAndException(null);
345
346        assertThat("Expected the meter metric to be marked on invocation",
347                   ((MeterMetric) meteredMetric).count(),
348                   is(1L));
349
350        assertThat("Expected the exception metric to be zero since no exceptions thrown",
351                   ((MeterMetric) errorMetric).count(),
352                   is(0L));
353
354        // Invoke and throw an exception
355        try {
356            instance.meteredAndException(new RuntimeException());
357            fail("Should have thrown an exception");
358        } catch (Exception e) {}
359
360        assertThat("Expected a count of 2, one for each invocation",
361                   ((MeterMetric) meteredMetric).count(),
362                   is(2L));
363
364        assertThat("Expected exception count to be 1 as one (of two) invocations threw an exception",
365                   ((MeterMetric) errorMetric).count(),
366                   is(1L));
367
368    }
369
370    private void assertMetricIsSetup(final Metric metric) {
371        assertThat("Guice creates a metric",
372                   metric,
373                   is(notNullValue()));
374
375        assertThat("Guice creates a meter",
376                   metric,
377                   is(instanceOf(MeterMetric.class)));
378    }
379
380    @SuppressWarnings("serial")
381    private static class MyOtherException extends RuntimeException {
382    }
383
384    @SuppressWarnings("serial")
385    private static class MySpecialisedException extends MyException {
386    }
387}