View Javadoc
1   package com.atlassian.cache.hazelcast;
2   
3   import com.atlassian.cache.AbstractCacheLazyTest;
4   import com.atlassian.cache.Cache;
5   import com.atlassian.cache.CacheFactory;
6   import com.atlassian.cache.CacheSettingsBuilder;
7   
8   import org.hamcrest.MatcherAssert;
9   import org.junit.After;
10  import org.junit.Before;
11  import org.junit.ClassRule;
12  import org.junit.Ignore;
13  import org.junit.Test;
14  
15  import static java.text.MessageFormat.format;
16  import static org.hamcrest.MatcherAssert.assertThat;
17  import static org.hamcrest.Matchers.is;
18  import static org.hamcrest.Matchers.lessThan;
19  import static org.hamcrest.core.IsEqual.equalTo;
20  
21  public class HazelcastRemoteCacheLazyTest extends AbstractCacheLazyTest
22  {
23      private static final int HAZELCAST_INSTANCES = 2;
24      @ClassRule
25      public static InitOnceHazelcastCluster cluster = InitOnceHazelcastCluster.getInstance();
26  
27      private HazelcastCacheManager factory1;
28      private HazelcastCacheManager factory2;
29  
30      @Before
31      public void setUp()
32      {
33          cluster.reset();
34          factory1 = HazelcastTestSupport.createDistributedFactory(cluster.getNode(0));
35          factory2 = HazelcastTestSupport.createDistributedFactory(cluster.getNode(1));
36  
37          factory = factory1;
38      }
39  
40      @After
41      public void tearDown()
42      {
43          factory1.destroy();
44          factory2.destroy();
45      }
46  
47      @Override
48      @Test
49      public void testMaxEntries()
50      {
51          // new eviction process triggers data eviction right after a put operation.
52          // http://docs.hazelcast.org/docs/latest-development/manual/html/Distributed_Data_Structures/Map/Map_Eviction.html
53          int maxSize = 3000;
54          Cache<String, Long> cache = makeSizeLimitedCache(maxSize, "HazelcastLazy.testMaxEntries");
55  
56          int earlyEvictionMark = (90 * HAZELCAST_INSTANCES * maxSize) / 100;
57          for (int i = 0; i < earlyEvictionMark; ++i)
58          {
59              String key = cluster.generateKeyOwnedByNode(i % 2); // spread the data evenly distributed across the cluster
60              long value = i;
61              cache.put(key, value);
62              MatcherAssert.assertThat(cache.get(key), equalTo(value));
63          }
64          // fill the map until we go over, but don't check the cache values - Hazelcast could evict the value that we're
65          // adding.
66          for (int i = earlyEvictionMark; i <= HAZELCAST_INSTANCES * maxSize + 1; ++i)  // maxSize per node, and we have 2 nodes
67          {
68              String key = cluster.generateKeyOwnedByNode(i % 2);
69              long value = i;
70              cache.put(key, value);
71          }
72  
73          assertThat(format("Total number of the cached objects should be < than 6000", HAZELCAST_INSTANCES * maxSize),
74                  cache.getKeys().size(), is(lessThan(HAZELCAST_INSTANCES * maxSize)));
75      }
76  
77      @Ignore("CACHE-106/CACHE-108: Needs to be fixed (currently hard fails)")
78      @Override
79      @Test
80      public void testRemoveConcurrentWithLoaderLocal()
81      {
82          super.testRemoveConcurrentWithLoaderLocal();
83      }
84  
85      @Ignore("CACHE-106/CACHE-108: Needs to be fixed (currently hard fails)")
86      @Test
87      public void testRemoveConcurrentWithLoadClustered()
88      {
89          final CacheFactory factory2 = HazelcastTestSupport.createDistributedFactory(cluster.getNode(1));
90          removeConcurrentWithLoader(factory, factory2, REMOVE_0);
91      }
92  
93      @Ignore("CACHE-106/CACHE-108: Needs to be fixed (currently hard fails)")
94      @Override
95      @Test
96      public void testRemoveConcurrentWithSupplierLocal()
97      {
98          super.testRemoveConcurrentWithSupplierLocal();
99      }
100 
101     @Ignore("CACHE-106/CACHE-108: Needs to be fixed (currently hard fails)")
102     @Override
103     @Test
104     public void testRemoveAllConcurrentWithSupplierLocal()
105     {
106         super.testRemoveAllConcurrentWithSupplierLocal();
107     }
108 
109     @Override
110     protected CacheSettingsBuilder settingsBuilder()
111     {
112         return new CacheSettingsBuilder().remote();
113     }
114 }