View Javadoc

1   package com.atlassian.cache.hazelcast;
2   
3   import com.atlassian.cache.AbstractCacheEagerTest;
4   import com.atlassian.cache.Cache;
5   import com.atlassian.cache.CacheSettingsBuilder;
6   
7   import org.junit.Before;
8   import org.junit.ClassRule;
9   import org.junit.Test;
10  import org.junit.runner.RunWith;
11  import org.mockito.runners.MockitoJUnitRunner;
12  
13  import static org.hamcrest.MatcherAssert.assertThat;
14  import static org.hamcrest.Matchers.greaterThan;
15  import static org.hamcrest.Matchers.lessThan;
16  import static org.hamcrest.core.IsEqual.equalTo;
17  
18  @RunWith (MockitoJUnitRunner.class)
19  public class HazelcastRemoteCacheEagerTest extends AbstractCacheEagerTest
20  {
21      @ClassRule
22      public static InitOnceHazelcastCluster cluster = InitOnceHazelcastCluster.getInstance();
23  
24      @Before
25      public void setUp() throws Exception
26      {
27          cluster.reset();
28          factory = HazelcastTestSupport.createDistributedFactory(cluster.getNode(0));
29      }
30  
31      @Override
32      @Test (timeout = 10000L)
33      public void testMaxEntries() throws Exception
34      {
35          // overridden because Hazelcast cache eviction is performed asynchronously and does evicts in bulk a percentage
36          // of the cached value. Also note that MapEvictTask translates the cache maxSize to a per-partition max size.
37          // Given that Hazelcast uses hundreds of partitions even when it is running on a small number of nodes, this leads
38          // to very low per-partition maxes when configuring a low max value. (often rounded down to 0)
39          int maxSize = 1000;
40          Cache<String, Long> cache = makeSizeLimitedCache(maxSize);
41  
42          int actualMaxSize = maxSize * cluster.size();
43          for (int i = 0; i <= actualMaxSize + 1; ++i)
44          {
45              String key = Integer.toString(i);
46              long value = 10L + i;
47              cache.put(key, value);
48              assertThat(cache.get(key), equalTo(value));
49          }
50  
51          // allow the map eviction to happen - cache eviction happens every second (async)
52          while (cache.getKeys().size() > actualMaxSize)
53          {
54              Thread.sleep(5L);
55          }
56  
57          assertThat(cache.getKeys().size(), lessThan(actualMaxSize));
58          assertThat(cache.getKeys().size(), greaterThan(maxSize));
59      }
60  
61      @Override
62      protected CacheSettingsBuilder settingsBuilder()
63      {
64          return new CacheSettingsBuilder().remote();
65      }
66  }