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.CacheSettingsBuilder;
8
9 import org.junit.Before;
10 import org.junit.ClassRule;
11 import org.junit.Test;
12 import org.junit.runner.RunWith;
13 import org.mockito.runners.MockitoJUnitRunner;
14
15 import static org.hamcrest.Matchers.greaterThan;
16 import static org.hamcrest.Matchers.lessThan;
17 import static org.hamcrest.core.IsEqual.equalTo;
18 import static org.junit.Assert.assertThat;
19
20 @RunWith (MockitoJUnitRunner.class)
21 public class HazelcastRemoteCacheLazyTest extends AbstractCacheLazyTest
22 {
23 @ClassRule
24 public static InitOnceHazelcastCluster cluster = InitOnceHazelcastCluster.getInstance();
25
26 @Before
27 public void setUp() throws Exception
28 {
29 cluster.reset();
30 factory = HazelcastTestSupport.createDistributedFactory(cluster.getNode(0));
31 }
32
33 @Override
34 @Test (timeout = 10000L)
35 public void testMaxEntries() throws Exception
36 {
37
38
39
40
41
42 int maxSize = 1000;
43 AtomicInteger loadCounter = new AtomicInteger();
44 Cache<String, Long> cache = makeSizeLimitedCache(maxSize, loadCounter);
45
46
47 assertThat(cache.get("1"), equalTo(1L));
48 assertThat(cache.get("1"), equalTo(1L));
49 assertThat(cache.get("2"), equalTo(2L));
50 assertThat(cache.get("3"), equalTo(3L));
51 assertThat("loadCounter", loadCounter.get(), equalTo(3));
52 assertSize(cache, 3);
53 assertThat(cache.get("3"), equalTo(3L));
54 assertThat(cache.get("3"), equalTo(3L));
55 assertThat("loadCounter", loadCounter.get(), equalTo(3));
56 assertSize(cache, 3);
57
58 int actualMaxSize = maxSize * cluster.size();
59 for (int i = 4; i <= actualMaxSize + 1; ++i)
60 {
61 assertThat(cache.get(Integer.toString(i)), equalTo((long) i));
62 assertThat("loadCounter", loadCounter.get(), equalTo(i));
63 }
64
65
66 while (cache.getKeys().size() > actualMaxSize)
67 {
68 Thread.sleep(5L);
69 }
70
71 assertThat(cache.getKeys().size(), lessThan(actualMaxSize));
72 assertThat(cache.getKeys().size(), greaterThan(maxSize));
73 }
74
75 @Override
76 protected CacheSettingsBuilder settingsBuilder()
77 {
78 return new CacheSettingsBuilder().remote();
79 }
80 }