3
3
import net.sf.ehcache.Cache;
4
4
import net.sf.ehcache.CacheManager;
5
import net.sf.ehcache.Ehcache;
5
6
import net.sf.ehcache.Element;
6
7
import net.sf.ehcache.Status;
7
8
import net.sf.ehcache.config.CacheConfiguration;
8
9
import net.sf.ehcache.config.Configuration;
9
import net.sf.ehcache.config.MemoryUnit;
10
import net.sf.ehcache.util.RetryAssert;
11
import org.hamcrest.core.Is;
10
import net.sf.ehcache.config.DiskStoreConfiguration;
11
import net.sf.ehcache.store.disk.DiskStoreHelper;
12
13
import org.junit.After;
13
14
import org.junit.Test;
15
import java.util.concurrent.Callable;
16
import java.util.concurrent.BrokenBarrierException;
17
import java.util.concurrent.CyclicBarrier;
17
import static java.util.concurrent.TimeUnit.SECONDS;
18
import static org.hamcrest.CoreMatchers.is;
19
import static net.sf.ehcache.config.MemoryUnit.KILOBYTES;
20
import static net.sf.ehcache.config.MemoryUnit.MEGABYTES;
21
import static org.hamcrest.core.Is.is;
22
import static org.hamcrest.number.OrderingComparison.greaterThan;
23
import static org.hamcrest.number.OrderingComparison.lessThanOrEqualTo;
19
24
import static org.junit.Assert.assertThat;
55
public void testLoadsFromDiskWithMaxElementsInMemorySet() throws InterruptedException {
59
public void testLoadsFromDiskWithMaxElementsInMemorySet() throws Exception {
56
60
setUp(CacheUT.elementBased);
58
while (cacheElementCountBound.getDiskStoreSize() != ELEMENTS_ON_DISK && waitCycles < 15) {
59
System.err.println("Not all entries have been spooled to disk, waiting a bit ... ");
63
waitForBootstrapLoader(cacheElementCountBoundBootstrapCacheLoader);
64
RetryAssert.assertBy(10, SECONDS, new Callable<Integer>() {
65
public Integer call() throws Exception {
66
return cacheElementCountBound.getDiskStoreSize();
68
}, Is.is(ELEMENTS_ON_DISK));
61
DiskStoreHelper.flushAllEntriesToDisk(cacheElementCountBound).get();
62
int onDiskElements = cacheElementCountBound.getDiskStoreSize();
63
cacheElementCountBoundBootstrapCacheLoader.triggerLoad();
69
64
assertThat(cacheElementCountBound.getMemoryStoreSize(), is(100L));
70
65
manager.shutdown();
71
66
initCacheManager(CacheUT.elementBased);
72
RetryAssert.assertBy(10, SECONDS, new Callable<Integer>() {
73
public Integer call() throws Exception {
74
return cacheElementCountBound.getDiskStoreSize();
76
}, Is.is(ELEMENTS_ON_DISK));
67
assertThat(cacheElementCountBound.getDiskStoreSize(), is(onDiskElements));
77
68
assertThat(cacheElementCountBound.getMemoryStoreSize(), is(0L));
78
waitForBootstrapLoader(cacheElementCountBoundBootstrapCacheLoader);
79
RetryAssert.assertBy(10, SECONDS, new Callable<Integer>() {
80
public Integer call() throws Exception {
81
return cacheElementCountBound.getDiskStoreSize();
83
}, Is.is(ELEMENTS_ON_DISK));
69
cacheElementCountBoundBootstrapCacheLoader.triggerLoad();
70
assertThat(cacheElementCountBound.getDiskStoreSize(), is(onDiskElements));
84
71
assertThat(cacheElementCountBound.getMemoryStoreSize(), is(100L));
85
assertThat(cacheElementCountBoundBootstrapCacheLoader.getLoadedElements(), is(100));
89
public void testLoadsFromDiskWithMaxBytesOnHeapSet() throws InterruptedException {
75
public void testLoadsFromDiskWithMaxBytesOnHeapSet() throws Exception {
90
76
setUp(CacheUT.sizeBased);
92
while (cacheSizeBound.getDiskStoreSize() != ELEMENTS_ON_DISK && waitCycles < 15) {
93
System.err.println("Not all entries have been spooled to disk, waiting a bit ... " + cacheSizeBound.getDiskStoreSize() + " in");
97
waitForBootstrapLoader(cacheSizeBoundBootstrapCacheLoader);
98
RetryAssert.assertBy(10, SECONDS, new Callable<Integer>() {
99
public Integer call() throws Exception {
100
return cacheSizeBound.getDiskStoreSize();
102
}, Is.is(ELEMENTS_ON_DISK));
103
assertThat(cacheSizeBound.getLiveCacheStatistics().getLocalHeapSizeInBytes() <= MemoryUnit.KILOBYTES.toBytes(220), is(true));
77
DiskStoreHelper.flushAllEntriesToDisk(cacheSizeBound).get();
78
cacheSizeBoundBootstrapCacheLoader.triggerLoad();
79
int onDiskSize = cacheSizeBound.getDiskStoreSize();
80
assertThat(cacheSizeBound.getMemoryStoreSize(), greaterThan(0L));
81
assertThat(cacheSizeBound.getLiveCacheStatistics().getLocalHeapSizeInBytes(), lessThanOrEqualTo(KILOBYTES.toBytes(220L)));
82
assertThat(cacheSizeBound.getDiskStoreSize(), is(onDiskSize));
104
83
manager.shutdown();
105
84
initCacheManager(CacheUT.sizeBased);
106
RetryAssert.assertBy(10, SECONDS, new Callable<Integer>() {
107
public Integer call() throws Exception {
108
return cacheSizeBound.getDiskStoreSize();
110
}, Is.is(ELEMENTS_ON_DISK));
85
assertThat(cacheSizeBound.getDiskStoreSize(), is(onDiskSize));
111
86
assertThat(cacheSizeBound.getMemoryStoreSize(), is(0L));
112
waitForBootstrapLoader(cacheSizeBoundBootstrapCacheLoader);
113
RetryAssert.assertBy(10, SECONDS, new Callable<Integer>() {
114
public Integer call() throws Exception {
115
return cacheSizeBound.getDiskStoreSize();
117
}, Is.is(ELEMENTS_ON_DISK));
118
assertThat(cacheSizeBound.getLiveCacheStatistics().getLocalHeapSizeInBytes() <= MemoryUnit.KILOBYTES.toBytes(220), is(true));
121
private int waitForBootstrapLoader(DiskStoreBootstrapCacheLoader bootstrapCacheLoader) throws InterruptedException {
122
while (!bootstrapCacheLoader.isDoneLoading()) {
123
System.err.println("Waiting for the loader to be done... Loaded " + bootstrapCacheLoader.getLoadedElements()
124
+ " elements so far");
125
Thread.sleep(LOADER_DELAY);
127
return bootstrapCacheLoader.getLoadedElements();
87
cacheSizeBoundBootstrapCacheLoader.triggerLoad();
88
assertThat(cacheSizeBound.getMemoryStoreSize(), greaterThan(0L));
89
assertThat(cacheSizeBound.getLiveCacheStatistics().getLocalHeapSizeInBytes(), lessThanOrEqualTo(KILOBYTES.toBytes(220L)));
130
92
private void initCacheManager(CacheUT cut) {
131
manager = new CacheManager(new Configuration());
133
94
case elementBased:
134
cacheElementCountBoundBootstrapCacheLoader = new DiskStoreBootstrapCacheLoader(LOADER_DELAY);
95
manager = new CacheManager(new Configuration().diskStore(new DiskStoreConfiguration().path("java.io.tmpdir/DiskPersistent")));
96
cacheElementCountBoundBootstrapCacheLoader = new TestDiskStoreBootstrapCacheLoader();
135
97
cacheElementCountBound = new Cache(new CacheConfiguration("maxElementsInMemory", 100)
137
99
.diskPersistent(true)
138
100
.overflowToDisk(true)
139
.diskStorePath("caches/DiskPersistent")
140
101
.maxEntriesLocalDisk(ELEMENTS_ON_DISK), null, cacheElementCountBoundBootstrapCacheLoader);
141
102
manager.addCache(cacheElementCountBound);
144
cacheSizeBoundBootstrapCacheLoader = new DiskStoreBootstrapCacheLoader(LOADER_DELAY);
105
manager = new CacheManager(new Configuration().diskStore(new DiskStoreConfiguration().path("java.io.tmpdir/DiskPersistentSize")));
106
cacheSizeBoundBootstrapCacheLoader = new TestDiskStoreBootstrapCacheLoader();
145
107
cacheSizeBound = new Cache(new CacheConfiguration("maxOnHeap", 0)
147
109
.diskPersistent(true)
148
110
.overflowToDisk(true)
149
.maxBytesLocalHeap(220, MemoryUnit.KILOBYTES)
150
.maxBytesLocalDisk(300, MemoryUnit.MEGABYTES)
151
.diskStorePath("caches/DiskPersistentSize"), null, cacheSizeBoundBootstrapCacheLoader);
111
.maxBytesLocalHeap(220, KILOBYTES)
112
.maxBytesLocalDisk(300, MEGABYTES), null, cacheSizeBoundBootstrapCacheLoader);
152
113
manager.addCache(cacheSizeBound);
153
114
cacheSizeBound.setSampledStatisticsEnabled(true);