View Javadoc

1   package com.atlassian.cache.ehcache;
2   
3   import java.util.concurrent.CountDownLatch;
4   import java.util.concurrent.ExecutorService;
5   import java.util.concurrent.Executors;
6   import java.util.concurrent.atomic.AtomicBoolean;
7   
8   import com.atlassian.cache.AbstractCacheLazyTest;
9   import com.atlassian.cache.Cache;
10  import com.atlassian.cache.CacheLoader;
11  import com.atlassian.cache.ehcache.cluster.EhCacheCluster;
12  
13  import org.junit.Before;
14  import org.junit.Ignore;
15  import org.junit.Test;
16  
17  import junit.framework.AssertionFailedError;
18  
19  import static com.atlassian.cache.ehcache.cluster.EhCacheCluster.clusteredTest;
20  import static java.util.concurrent.TimeUnit.MILLISECONDS;
21  import static org.hamcrest.CoreMatchers.is;
22  import static org.junit.Assert.assertSame;
23  import static org.junit.Assert.assertThat;
24  import static org.mockito.Mockito.mock;
25  import static org.mockito.Mockito.times;
26  import static org.mockito.Mockito.verify;
27  import static org.mockito.Mockito.verifyNoMoreInteractions;
28  import static org.mockito.Mockito.when;
29  
30  /**
31   * Test the Delegated Lazy Cache
32   *
33   * @since 2.0
34   */
35  public class DelegatingCacheLazyTest extends AbstractCacheLazyTest
36  {
37      @Before
38      public void setUp() throws Exception
39      {
40          factory = new EhCacheManager();
41          // Make sure we are not getting old caches for our test.
42          ((EhCacheManager) factory).getEh().removeAllCaches();
43      }
44  
45      @Test
46      @Override
47      public void testNullValue() throws Exception
48      {
49          // Override and ignore this test because EHCache handles null values
50      }
51  
52      @Test
53      public void shouldBlockOnConcurrentGet() throws Exception
54      {
55          // Set up
56          final AtomicBoolean weFailed = new AtomicBoolean();
57          final CacheLoader<String, Long> mockLoader = mock(CacheLoader.class);
58          final int threads = 20;
59          final String key = "anything";
60          // Create a specific Long object so we can test we always get the same.
61          final Long result = 123456L;
62          when(mockLoader.load(key)).thenReturn(result);
63          final ExecutorService executor = Executors.newFixedThreadPool(threads);
64          final Cache<String, Long> cache = factory.getCache("mycache", mockLoader);
65          final CountDownLatch startLatch = new CountDownLatch(1);
66          for (int i = 0; i < threads; i++)
67          {
68              executor.execute(new Runnable()
69              {
70                  @Override
71                  public void run()
72                  {
73                      try
74                      {
75                          startLatch.await();
76                          assertSame(result, cache.get(key));
77                      }
78                      catch (InterruptedException e)
79                      {
80                          weFailed.set(true);
81                      }
82                      catch (AssertionFailedError e)
83                      {
84                          weFailed.set(true);
85                      }
86                  }
87              });
88          }
89  
90          // Invoke
91          startLatch.countDown();
92          executor.shutdown();
93          executor.awaitTermination(500L, MILLISECONDS);
94  
95          // Check
96          verify(mockLoader, times(1)).load(key);
97          verifyNoMoreInteractions(mockLoader);
98  
99          assertThat(weFailed.get(), is(false));
100     }
101 
102     @Ignore("CACHE-106/CACHE-108: Needs to be fixed (currently hard fails)")
103     @Test
104     public void testRemoveConcurrentWithLoadClustered() throws Exception
105     {
106         clusteredTest(2, new EhCacheCluster.ClusteredTest()
107         {
108             @Override
109             public void runTest(EhCacheCluster cluster) throws Exception
110             {
111                 removeConcurrentWithLoader(cluster.getNode(0), cluster.getNode(1), REMOVE_0);
112             }
113         });
114     }
115 
116     @Ignore("CACHE-106/CACHE-108: Needs to be fixed (currently hard fails)")
117     @Test
118     public void testRemoveAllConcurrentWithLoadClustered() throws Exception
119     {
120         clusteredTest(2, new EhCacheCluster.ClusteredTest()
121         {
122             @Override
123             public void runTest(EhCacheCluster cluster) throws Exception
124             {
125                 removeConcurrentWithLoader(cluster.getNode(0), cluster.getNode(1), REMOVE_ALL);
126             }
127         });
128     }
129 
130     @Ignore("CACHE-106/CACHE-108: Needs to be fixed (currently hard fails)")
131     @Test
132     @Override
133     public void testRemoveAllConcurrentWithSupplierLocal()
134     {
135         super.testRemoveAllConcurrentWithSupplierLocal();
136     }
137 
138     @Ignore("CACHE-106/CACHE-108: Needs to be fixed (currently hard fails)")
139     @Test
140     @Override
141     public void testRemoveConcurrentWithSupplierLocal()
142     {
143         super.testRemoveConcurrentWithSupplierLocal();
144     }
145 }