View Javadoc
1   package com.atlassian.cache.memory.jmx;
2   
3   import com.atlassian.cache.Cache;
4   import com.atlassian.cache.CacheLoader;
5   import com.atlassian.cache.CacheSettings;
6   import com.atlassian.cache.CacheSettingsBuilder;
7   import com.atlassian.cache.CachedReference;
8   import com.atlassian.cache.Supplier;
9   import com.atlassian.cache.memory.JMXMemoryCacheManager;
10  import org.junit.After;
11  import org.junit.Before;
12  import org.junit.Test;
13  
14  import javax.annotation.Nonnull;
15  import javax.management.MBeanServer;
16  import javax.management.ObjectName;
17  import java.lang.management.ManagementFactory;
18  
19  import static org.hamcrest.MatcherAssert.assertThat;
20  import static org.hamcrest.Matchers.closeTo;
21  import static org.hamcrest.Matchers.is;
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertTrue;
24  
25  /**
26   * Tests cases for {@link JMXMemoryCacheManager}
27   */
28  public class JMXMemoryCacheManagerTest
29  {
30      private static final String CACHE_NAME ="SimpleCache";
31      private static final double PRECISION = 0.00001;
32  
33      private ObjectName mbeanName;
34      private JMXMemoryCacheManager cacheManager;
35  
36      private MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
37  
38      @Before
39      public void setUp() throws Exception
40      {
41          mbeanName = new ObjectName(String.format(MemoryCacheMXBeanRegistrar.MBEAN_NAME_PATTERN, CACHE_NAME));
42          cacheManager = new JMXMemoryCacheManager();
43  
44          assertFalse(mbeanServer.isRegistered(mbeanName));
45      }
46  
47      @After
48      public void tearDown() throws Exception
49      {
50          if(mbeanServer.isRegistered(mbeanName))
51          {
52              mbeanServer.unregisterMBean(mbeanName);
53          }
54      }
55  
56      @Test
57      public void testGetSimpleCacheWithMetricsEnabled() throws Exception
58      {
59          testGetCacheWithMetricsEnabled(CACHE_NAME, null);
60      }
61  
62      @Test
63      public void testGetComputingCacheWithMetricsEnabled() throws Exception
64      {
65          testGetCacheWithMetricsEnabled(CACHE_NAME, new SimpleLoader());
66      }
67  
68  
69      @Test
70      public void testGetSimpleCacheWithMetricsDisabled() throws Exception
71      {
72          testGetCacheWithMetricsDisabled(CACHE_NAME, null);
73      }
74  
75      @Test
76      public void testGetComputingCacheWithMetricsDisabled() throws Exception
77      {
78          testGetCacheWithMetricsDisabled(CACHE_NAME, new SimpleLoader());
79      }
80  
81      @Test
82      public void testGetCachedReferenceWithMetricsEnabled() throws Exception {
83          cacheManager.registerMBeans(mbeanServer);
84  
85          CacheSettings settings = new CacheSettingsBuilder().statisticsEnabled().build();
86          CachedReference cacheReference = cacheManager.getCachedReference(CACHE_NAME, new SimpleSupplier(), settings);
87  
88          Object result = cacheReference.get();
89  
90          assertTrue(mbeanServer.isRegistered(mbeanName));
91  
92          checkAttributeLong("Size", 1L);
93          checkAttributeLong("HitCount", 0L);
94          checkAttributeLong("MissCount", 2L);
95          checkAttributeLong("RequestCount", 2L);
96          checkAttributeLong("LoadCount", 1L);
97          checkAttributeDouble("HitRate", 0.0);
98          checkAttributeDouble("MissRate", 1.0);
99      }
100 
101     @Test
102     public void testGetCachedReferenceWithMetricsDisabled()
103     {
104         CacheSettings settings = new CacheSettingsBuilder().statisticsEnabled().build();
105         CachedReference cacheReference = cacheManager.getCachedReference(CACHE_NAME, new SimpleSupplier(), settings);
106 
107         Object result = cacheReference.get();
108 
109         assertFalse(mbeanServer.isRegistered(mbeanName));
110     }
111 
112     @Test
113     public void testUnregisteringMBeans()
114     {
115         cacheManager.registerMBeans(mbeanServer);
116 
117         CacheSettings settings = new CacheSettingsBuilder().statisticsEnabled().build();
118         cacheManager.getCache(CACHE_NAME, new SimpleLoader(), settings);
119 
120         assertTrue(mbeanServer.isRegistered(mbeanName));
121 
122         cacheManager.unregisterMBeans(mbeanServer);
123 
124         assertFalse(mbeanServer.isRegistered(mbeanName));
125     }
126 
127     private void testGetCacheWithMetricsEnabled(String cacheName, CacheLoader loader) throws Exception
128     {
129         cacheManager.registerMBeans(mbeanServer);
130         CacheSettings settings = new CacheSettingsBuilder().statisticsEnabled().build();
131 
132         Cache cache = cacheManager.getCache(CACHE_NAME, new SimpleLoader(), settings);
133         cache.put("TestKey1", "TestValue1");
134         cache.put("TestKey2", "TestValue2");
135         cache.get("TestKey1");
136 
137         assertTrue(mbeanServer.isRegistered(mbeanName));
138 
139         checkAttributeLong("Size", 2L);
140         checkAttributeLong("HitCount", 1L);
141         checkAttributeLong("MissCount", 0L);
142         checkAttributeLong("RequestCount", 1L);
143         checkAttributeLong("LoadCount", 0L);
144         checkAttributeDouble("HitRate", 1.0);
145         checkAttributeDouble("MissRate", 0.0);
146     }
147 
148     private void testGetCacheWithMetricsDisabled(String cacheName, CacheLoader loader)
149     {
150         CacheSettings settings = new CacheSettingsBuilder().statisticsEnabled().build();
151 
152         Cache cache = cacheManager.getCache(cacheName, loader, settings);
153 
154         assertFalse(mbeanServer.isRegistered(mbeanName));
155     }
156 
157     private void checkAttributeLong(String attribute, Long expectedValue) throws Exception
158     {
159         assertThat(mbeanServer.getAttribute(mbeanName, attribute), is(expectedValue));
160     }
161 
162     private void checkAttributeDouble(String attribute, Double expectedValue) throws Exception
163     {
164         assertThat((Double)mbeanServer.getAttribute(mbeanName, attribute), closeTo(expectedValue, PRECISION));
165     }
166 
167     private class SimpleLoader implements CacheLoader
168     {
169         @Nonnull
170         @Override
171         public Object load(@Nonnull Object key) {
172             return key + "Value";
173         }
174     }
175 
176     private class SimpleSupplier implements Supplier
177     {
178         @Override
179         public Object get() {
180             return "Some Value";
181         }
182     }
183 
184 }