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.After;
8   import org.junit.Before;
9   import org.junit.ClassRule;
10  import org.junit.Test;
11  
12  import static java.text.MessageFormat.format;
13  import static org.hamcrest.MatcherAssert.assertThat;
14  import static org.hamcrest.Matchers.is;
15  import static org.hamcrest.Matchers.lessThan;
16  import static org.hamcrest.core.IsEqual.equalTo;
17  
18  public class HazelcastRemoteCacheEagerTest extends AbstractCacheEagerTest
19  {
20      private static final int HAZELCAST_INSTANCES = 2;
21      @ClassRule
22      public static InitOnceHazelcastCluster cluster = InitOnceHazelcastCluster.getInstance();
23  
24      private HazelcastCacheManager factory1;
25      private HazelcastCacheManager factory2;
26  
27      @Before
28      public void setUp()
29      {
30          cluster.reset();
31          factory1 = HazelcastTestSupport.createDistributedFactory(cluster.getNode(0));
32          factory2 = HazelcastTestSupport.createDistributedFactory(cluster.getNode(1));
33  
34          factory = factory1;
35      }
36  
37      @After
38      public void tearDown()
39      {
40          factory1.destroy();
41          factory2.destroy();
42      }
43  
44      @Override
45      @Test
46      public void testMaxEntries()
47      {
48          // new eviction process triggers data eviction right after a put operation.
49          // http://docs.hazelcast.org/docs/latest-development/manual/html/Distributed_Data_Structures/Map/Map_Eviction.html
50          int maxSize = 3000;
51          Cache<String, Long> cache = makeSizeLimitedCache(maxSize,"HazelcastRemote.testMaxEntries");
52          int earlyEvictionMark = (90 * HAZELCAST_INSTANCES * maxSize) / 100;
53  
54          for(int i = 0; i < earlyEvictionMark; i++)
55          {
56              String key = cluster.generateKeyOwnedByNode(i % 2); // spread the data evenly distributed across the cluster
57              long value = i;
58              cache.put(key, value);
59              assertThat(cache.get(key), equalTo(value));
60          }
61  
62          // fill the map until we go over, but don't check the cache values - Hazelcast could evict the value that we're
63          // adding.
64          for (int i = earlyEvictionMark; i <= HAZELCAST_INSTANCES * maxSize + 1; ++i) // maxSize per node, and we have 2 nodes
65          {
66              String key = cluster.generateKeyOwnedByNode(i % 2);
67              long value = i;
68              cache.put(key, value);
69          }
70  
71          assertThat(format("Total number of the cached objects should be < than {0}", HAZELCAST_INSTANCES * maxSize),
72                  cache.getKeys().size(), is(lessThan(HAZELCAST_INSTANCES * maxSize)));
73      }
74  
75      @Override
76      protected CacheSettingsBuilder settingsBuilder()
77      {
78          return new CacheSettingsBuilder().remote();
79      }
80  }