View Javadoc

1   package com.atlassian.vcache.internal.core.metrics;
2   
3   import java.util.Optional;
4   
5   import com.atlassian.vcache.LocalCacheOperations;
6   
7   import org.junit.Test;
8   import org.junit.runner.RunWith;
9   import org.mockito.Mock;
10  import org.mockito.runners.MockitoJUnitRunner;
11  
12  import static com.atlassian.vcache.internal.MetricLabel.NUMBER_OF_HITS;
13  import static com.atlassian.vcache.internal.MetricLabel.NUMBER_OF_MISSES;
14  import static com.atlassian.vcache.internal.MetricLabel.TIMED_GET_CALL;
15  import static com.atlassian.vcache.internal.MetricLabel.TIMED_PUT_CALL;
16  import static com.atlassian.vcache.internal.MetricLabel.TIMED_REMOVE_ALL_CALL;
17  import static com.atlassian.vcache.internal.MetricLabel.TIMED_REMOVE_CALL;
18  import static org.hamcrest.Matchers.equalTo;
19  import static org.junit.Assert.assertThat;
20  import static org.mockito.Matchers.any;
21  import static org.mockito.Matchers.anyLong;
22  import static org.mockito.Matchers.eq;
23  import static org.mockito.Mockito.verify;
24  import static org.mockito.Mockito.verifyNoMoreInteractions;
25  import static org.mockito.Mockito.when;
26  
27  @RunWith(MockitoJUnitRunner.class)
28  public abstract class TimedLocalCacheOperationsTest
29  {
30      protected static final String CACHE_NAME = "peanuts";
31  
32      @Mock
33      protected MetricsCollector metricsCollector;
34  
35      protected abstract CacheType getCacheType();
36      protected abstract LocalCacheOperations<String, String> getDelegateOps();
37      protected abstract TimedLocalCacheOperations<String, String> getTimed();
38  
39      @Test
40      public void get_hit() throws Exception
41      {
42          final Optional<String> result = Optional.of("micro");
43          when(getDelegateOps().get("foo")).thenReturn(result);
44  
45          final Optional<String> passedResult = getTimed().get("foo");
46  
47          assertThat(passedResult, equalTo(result));
48  
49          verify(metricsCollector).record(eq(getCacheType()), eq(CACHE_NAME), eq(TIMED_GET_CALL), anyLong());
50          verify(metricsCollector).record(eq(getCacheType()), eq(CACHE_NAME), eq(NUMBER_OF_HITS), eq(1L));
51          verifyNoMoreInteractions(metricsCollector);
52      }
53  
54      @Test
55      public void get_miss() throws Exception
56      {
57          final Optional<String> result = Optional.empty();
58          when(getDelegateOps().get("foo")).thenReturn(result);
59  
60          final Optional<String> passedResult = getTimed().get("foo");
61  
62          assertThat(passedResult, equalTo(result));
63  
64          verify(metricsCollector).record(eq(getCacheType()), eq(CACHE_NAME), eq(TIMED_GET_CALL), anyLong());
65          verify(metricsCollector).record(eq(getCacheType()), eq(CACHE_NAME), eq(NUMBER_OF_MISSES), eq(1L));
66          verifyNoMoreInteractions(metricsCollector);
67      }
68  
69      @Test
70      public void getSupplier_hit() throws Exception
71      {
72          final String result = "chaos";
73          when(getDelegateOps().get(eq("foo"), any())).thenReturn(result);
74  
75          final String passedResult = getTimed().get("foo", () -> "xxx");
76  
77          assertThat(passedResult, equalTo(result));
78  
79          verify(metricsCollector).record(eq(getCacheType()), eq(CACHE_NAME), eq(TIMED_GET_CALL), anyLong());
80          verify(metricsCollector).record(eq(getCacheType()), eq(CACHE_NAME), eq(NUMBER_OF_HITS), eq(1L));
81          verifyNoMoreInteractions(metricsCollector);
82      }
83  
84      @Test
85      public void put() throws Exception
86      {
87          getTimed().put("abc", "def");
88  
89          verify(metricsCollector).record(eq(getCacheType()), eq(CACHE_NAME), eq(TIMED_PUT_CALL), anyLong());
90          verifyNoMoreInteractions(metricsCollector);
91      }
92  
93      @Test
94      public void putIfAbsent() throws Exception
95      {
96          final Optional<String> result = Optional.empty();
97          when(getDelegateOps().putIfAbsent("foo", "pain")).thenReturn(result);
98  
99          final Optional<String> passedResult = getTimed().putIfAbsent("foo", "pain");
100 
101         assertThat(passedResult, equalTo(result));
102 
103         verify(metricsCollector).record(eq(getCacheType()), eq(CACHE_NAME), eq(TIMED_PUT_CALL), anyLong());
104         verifyNoMoreInteractions(metricsCollector);
105     }
106 
107     @Test
108     public void replaceIf() throws Exception
109     {
110         when(getDelegateOps().replaceIf("foo", "pain", "hurt")).thenReturn(false);
111 
112         final boolean passedResult = getTimed().replaceIf("foo", "pain", "hurt");
113 
114         assertThat(passedResult, equalTo(false));
115 
116         verify(metricsCollector).record(eq(getCacheType()), eq(CACHE_NAME), eq(TIMED_PUT_CALL), anyLong());
117         verifyNoMoreInteractions(metricsCollector);
118     }
119 
120     @Test
121     public void removeIf() throws Exception
122     {
123         when(getDelegateOps().removeIf("foo", "hurt")).thenReturn(false);
124 
125         final boolean passedResult = getTimed().removeIf("foo", "hurt");
126 
127         assertThat(passedResult, equalTo(false));
128 
129         verify(metricsCollector).record(eq(getCacheType()), eq(CACHE_NAME), eq(TIMED_REMOVE_CALL), anyLong());
130         verifyNoMoreInteractions(metricsCollector);
131     }
132 
133     @Test
134     public void remove() throws Exception
135     {
136         getTimed().remove("foo", "hurt");
137 
138         verify(metricsCollector).record(eq(getCacheType()), eq(CACHE_NAME), eq(TIMED_REMOVE_CALL), anyLong());
139         verifyNoMoreInteractions(metricsCollector);
140     }
141 
142     @Test
143     public void removeAll() throws Exception
144     {
145         getTimed().removeAll();
146 
147         verify(metricsCollector).record(eq(getCacheType()), eq(CACHE_NAME), eq(TIMED_REMOVE_ALL_CALL), anyLong());
148         verifyNoMoreInteractions(metricsCollector);
149     }
150 }