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  
12  import org.junit.Before;
13  import org.junit.Test;
14  
15  import junit.framework.AssertionFailedError;
16  
17  import static java.util.concurrent.TimeUnit.MILLISECONDS;
18  import static org.hamcrest.CoreMatchers.is;
19  import static org.junit.Assert.assertSame;
20  import static org.junit.Assert.assertThat;
21  import static org.mockito.Mockito.mock;
22  import static org.mockito.Mockito.times;
23  import static org.mockito.Mockito.verify;
24  import static org.mockito.Mockito.verifyNoMoreInteractions;
25  import static org.mockito.Mockito.when;
26  
27  /**
28   * Test the Delegated Lazy Cache
29   *
30   * @since 2.0
31   */
32  public class DelegatingCacheLazyTest extends AbstractCacheLazyTest
33  {
34      @Before
35      public void setUp() throws Exception
36      {
37          factory = new EhCacheManager();
38          // Make sure we are not getting old caches for our test.
39          ((EhCacheManager) factory).getEh().removeAllCaches();
40      }
41  
42      @Test
43      @Override
44      public void testNullValue() throws Exception
45      {
46          // Override and ignore this test because EHCache handles null values
47      }
48  
49      @Test
50      public void shouldBlockOnConcurrentGet() throws Exception
51      {
52          // Set up
53          final AtomicBoolean weFailed = new AtomicBoolean();
54          final CacheLoader<String, Long> mockLoader = mock(CacheLoader.class);
55          final int threads = 20;
56          final String key = "anything";
57          // Create a specific Long object so we can test we always get the same.
58          final Long result = 123456L;
59          when(mockLoader.load(key)).thenReturn(result);
60          final ExecutorService executor = Executors.newFixedThreadPool(threads);
61          final Cache<String, Long> cache = factory.getCache("mycache", mockLoader);
62          final CountDownLatch startLatch = new CountDownLatch(1);
63          for (int i = 0; i < threads; i++)
64          {
65              executor.execute(new Runnable()
66              {
67                  @Override
68                  public void run()
69                  {
70                      try
71                      {
72                          startLatch.await();
73                          assertSame(result, cache.get(key));
74                      }
75                      catch (InterruptedException e)
76                      {
77                          weFailed.set(true);
78                      }
79                      catch (AssertionFailedError e)
80                      {
81                          weFailed.set(true);
82                      }
83                  }
84              });
85          }
86  
87          // Invoke
88          startLatch.countDown();
89          executor.shutdown();
90          executor.awaitTermination(500L, MILLISECONDS);
91  
92          // Check
93          verify(mockLoader, times(1)).load(key);
94          verifyNoMoreInteractions(mockLoader);
95  
96          assertThat(weFailed.get(), is(false));
97      }
98  
99  }