View Javadoc

1   package com.atlassian.vcache.internal.core.metrics;
2   
3   import com.atlassian.marshalling.api.MarshallingException;
4   import com.atlassian.marshalling.api.Unmarshaller;
5   import org.junit.Before;
6   import org.junit.Test;
7   import org.junit.runner.RunWith;
8   import org.mockito.Mock;
9   import org.mockito.runners.MockitoJUnitRunner;
10  
11  import static com.atlassian.vcache.internal.MetricLabel.NUMBER_OF_BYTES_UNMARSHALLED;
12  import static com.atlassian.vcache.internal.MetricLabel.NUMBER_OF_FAILED_UNMARSHALL;
13  import static com.atlassian.vcache.internal.MetricLabel.TIMED_UNMARSHALL_CALL;
14  import static com.atlassian.vcache.internal.core.metrics.CacheType.EXTERNAL;
15  import static org.junit.Assert.fail;
16  import static org.mockito.Matchers.anyLong;
17  import static org.mockito.Matchers.eq;
18  import static org.mockito.Mockito.verify;
19  import static org.mockito.Mockito.verifyNoMoreInteractions;
20  import static org.mockito.Mockito.when;
21  
22  @RunWith(MockitoJUnitRunner.class)
23  public class TimedUnmarshallerTest {
24      private static final String CACHE_NAME = "bigKev";
25  
26      @Mock
27      private Unmarshaller<String> delegate;
28  
29      @Mock
30      private MetricsRecorder metricsRecorder;
31  
32      private TimedUnmarshaller<String> timed;
33  
34      @Before
35      public void init() {
36          timed = new TimedUnmarshaller<>(delegate, metricsRecorder, CACHE_NAME);
37      }
38  
39      @Test
40      public void unmarshall_success() throws Exception {
41          final byte[] raw = new byte[666];
42          when(delegate.unmarshallFrom(raw)).thenReturn("John Butler");
43  
44          timed.unmarshallFrom(raw);
45  
46          verify(metricsRecorder).record(eq(CACHE_NAME), eq(EXTERNAL), eq(TIMED_UNMARSHALL_CALL), anyLong());
47          verify(metricsRecorder).record(CACHE_NAME, EXTERNAL, NUMBER_OF_BYTES_UNMARSHALLED, 666);
48          verifyNoMoreInteractions(metricsRecorder);
49      }
50  
51      @Test
52      public void unmarshall_failure() {
53          final byte[] raw = new byte[666];
54          when(delegate.unmarshallFrom(raw)).thenThrow(new MarshallingException("Kimberley"));
55  
56          try {
57              timed.unmarshallFrom(raw);
58              fail("Exception should have been thrown");
59          } catch (MarshallingException e) {
60              verify(metricsRecorder).record(CACHE_NAME, EXTERNAL, NUMBER_OF_BYTES_UNMARSHALLED, 666);
61              verify(metricsRecorder).record(CACHE_NAME, EXTERNAL, NUMBER_OF_FAILED_UNMARSHALL, 1);
62              verify(metricsRecorder).record(eq(CACHE_NAME), eq(EXTERNAL), eq(TIMED_UNMARSHALL_CALL), anyLong());
63              verifyNoMoreInteractions(metricsRecorder);
64          }
65      }
66  }