View Javadoc

1   package com.atlassian.cache.hazelcast;
2   
3   import java.util.concurrent.atomic.AtomicInteger;
4   
5   import com.atlassian.cache.AbstractCacheLazyTest;
6   import com.atlassian.cache.Cache;
7   import com.atlassian.cache.CacheFactory;
8   import com.atlassian.cache.CacheSettingsBuilder;
9   
10  import org.junit.Before;
11  import org.junit.ClassRule;
12  import org.junit.Ignore;
13  import org.junit.Test;
14  import org.junit.runner.RunWith;
15  import org.mockito.runners.MockitoJUnitRunner;
16  
17  import static org.hamcrest.Matchers.greaterThan;
18  import static org.hamcrest.Matchers.lessThan;
19  import static org.hamcrest.core.IsEqual.equalTo;
20  import static org.junit.Assert.assertThat;
21  
22  @RunWith (MockitoJUnitRunner.class)
23  public class HazelcastRemoteCacheLazyTest extends AbstractCacheLazyTest
24  {
25      @ClassRule
26      public static InitOnceHazelcastCluster cluster = InitOnceHazelcastCluster.getInstance();
27  
28      @Before
29      public void setUp() throws Exception
30      {
31          cluster.reset();
32          factory = HazelcastTestSupport.createDistributedFactory(cluster.getNode(0));
33      }
34  
35      @Override
36      @Test (timeout = 10000L)
37      public void testMaxEntries() throws Exception
38      {
39          // overridden because Hazelcast cache eviction is performed asynchronously and does evicts in bulk a percentage
40          // of the cached value. Also note that MapEvictTask translates the cache maxSize to a per-partition max size.
41          // Given that Hazelcast uses hundreds of partitions even when it is running on a small number of nodes, this leads
42          // to very low per-partition maxes when configuring a low max value. (often rounded down to 0)
43  
44          int maxSize = 1000;
45          AtomicInteger loadCounter = new AtomicInteger();
46          Cache<String, Long> cache = makeSizeLimitedCache(maxSize, loadCounter);
47  
48          // Add some entries using the builder
49          assertThat(cache.get("1"), equalTo(1L));
50          assertThat(cache.get("1"), equalTo(1L));
51          assertThat(cache.get("2"), equalTo(2L));
52          assertThat(cache.get("3"), equalTo(3L));
53          assertThat("loadCounter", loadCounter.get(), equalTo(3));
54          assertSize(cache, 3);
55          assertThat(cache.get("3"), equalTo(3L));
56          assertThat(cache.get("3"), equalTo(3L));
57          assertThat("loadCounter", loadCounter.get(), equalTo(3));
58          assertSize(cache, 3);
59  
60          int actualMaxSize = maxSize * cluster.size();
61          for (int i = 4; i <= actualMaxSize + 1; ++i)
62          {
63              assertThat(cache.get(Integer.toString(i)), equalTo((long) i));
64              assertThat("loadCounter", loadCounter.get(), equalTo(i));
65          }
66  
67          // allow the map eviction to happen - cache eviction happens every second (async)
68          while (cache.getKeys().size() > actualMaxSize)
69          {
70              Thread.sleep(5L);
71          }
72  
73          assertThat(cache.getKeys().size(), lessThan(actualMaxSize));
74          assertThat(cache.getKeys().size(), greaterThan(maxSize));
75      }
76  
77      @Ignore("CACHE-106/CACHE-108: Needs to be fixed (currently hard fails)")
78      @Override
79      @Test
80      public void testRemoveAllConcurrentWithLoadLocal()
81      {
82          super.testRemoveAllConcurrentWithLoadLocal();
83      }
84  
85      @Test
86      public void testRemoveConcurrentWithLoadClustered()
87      {
88          final CacheFactory factory2 = HazelcastTestSupport.createDistributedFactory(cluster.getNode(1));
89          removeConcurrentWithLoad(factory, factory2, REMOVE_0);
90      }
91  
92      @Ignore("CACHE-106/CACHE-108: Needs to be fixed (currently hard fails)")
93      @Test
94      public void testRemoveAllConcurrentWithLoadClustered()
95      {
96          final CacheFactory factory2 = HazelcastTestSupport.createDistributedFactory(cluster.getNode(1));
97          removeConcurrentWithLoad(factory, factory2, REMOVE_ALL);
98      }
99  
100     @Override
101     protected CacheSettingsBuilder settingsBuilder()
102     {
103         return new CacheSettingsBuilder().remote();
104     }
105 }