1
package net.sf.ehcache.store.disk;
3
import static java.util.concurrent.TimeUnit.SECONDS;
4
import static org.junit.Assert.assertEquals;
5
import static org.junit.Assert.assertNotSame;
6
import static org.junit.Assert.assertNull;
7
import static org.junit.Assert.assertTrue;
8
import static org.junit.Assert.fail;
10
import java.util.HashSet;
11
import java.util.List;
13
import java.util.concurrent.Callable;
14
import java.util.concurrent.atomic.AtomicLong;
16
import junit.framework.Assert;
17
import net.sf.ehcache.Cache;
18
import net.sf.ehcache.CacheException;
19
import net.sf.ehcache.CacheManager;
20
import net.sf.ehcache.Ehcache;
21
import net.sf.ehcache.Element;
22
import net.sf.ehcache.config.CacheConfiguration;
23
import net.sf.ehcache.config.Configuration;
24
import net.sf.ehcache.event.CacheEventListener;
25
import net.sf.ehcache.pool.impl.UnboundedPool;
26
import net.sf.ehcache.store.DiskBackedMemoryStore;
27
import net.sf.ehcache.store.MemoryOnlyStore;
28
import net.sf.ehcache.store.Store;
29
import net.sf.ehcache.util.RetryAssert;
31
import org.hamcrest.core.Is;
32
import org.junit.Before;
33
import org.junit.Test;
37
* @author Ludovic Orban
39
public class DiskStoreTest {
41
private static final String KEY = "KEY";
44
private Store xaStore;
47
private Cache xaCache;
51
cache = new Cache(new CacheConfiguration("SomeCache", 1000).overflowToDisk(true).diskPersistent(true));
52
store = DiskBackedMemoryStore.create(cache, System.getProperty("java.io.tmpdir"), new UnboundedPool(), new UnboundedPool());
53
xaCache = new Cache(new CacheConfiguration("SomeXaCache", 1000).transactionalMode("xa_strict"));
54
xaStore = MemoryOnlyStore.create(xaCache, new UnboundedPool());
58
public void testPersistenceWithPinnedElements() {
59
final Element[] lastEvicted = new Element[1];
61
cache.getCacheEventNotificationService().registerListener(new CacheEventListener() {
63
public Object clone() throws CloneNotSupportedException {
66
public void notifyElementRemoved(Ehcache cache, Element element) throws CacheException {
68
public void notifyElementPut(Ehcache cache, Element element) throws CacheException {
70
public void notifyElementUpdated(Ehcache cache, Element element) throws CacheException {
72
public void notifyElementExpired(Ehcache cache, Element element) {
74
public void notifyElementEvicted(Ehcache cache, Element element) {
75
lastEvicted[0] = element;
77
public void notifyRemoveAll(Ehcache cache) {
79
public void dispose() {
83
store.put(new Element(1, "one"));
84
Element element2 = new Element(2, new Object());
85
store.setPinned(element2.getObjectKey(), true);
89
assertNull("element should not have been evicted: " + lastEvicted[0], lastEvicted[0]);
91
store = DiskStore.create(new Cache(new CacheConfiguration("SomeCache", 1000).overflowToDisk(true)
92
.diskPersistent(true)), System.getProperty("java.io.tmpdir"), new UnboundedPool(), new UnboundedPool());
93
assertEquals("one", store.get(1).getObjectValue());
94
assertNull(store.get(2));
98
public void testDiskStoreSize() throws Exception {
99
CacheManager cm = new CacheManager(
101
.cache(new CacheConfiguration("aCache", 10000)
102
.overflowToDisk(true)
104
.timeToLiveSeconds(1000)
105
.timeToLiveSeconds(360)
108
final Cache cache = cm.getCache("aCache");
111
cache.put(new Element(-1, -1));
112
assertEquals(-1, cache.get(-1).getValue());
114
assertEquals(null, cache.get(-1));
116
cache.put(new Element(-2, -2));
117
assertEquals(-2, cache.get(-2).getValue());
119
assertEquals(null, cache.get(-2));
121
assertEquals(0, cache.getDiskStoreSize());
123
for (int i = 0; i < 10010; i++) {
124
cache.put(new Element(i, i));
129
RetryAssert.assertBy(1, SECONDS, new Callable<Integer>() {
130
public Integer call() throws Exception {
131
return cache.getDiskStoreSize();
139
public void testSupportsCopyOnRead() {
140
Element element = new Element(KEY, "Some String", 1);
141
xaStore.put(element);
142
Element copy = xaStore.get(KEY);
143
Assert.assertNotNull(copy);
144
assertNotSame(copy, xaStore.get(KEY));
145
Assert.assertEquals("Some String", copy.getValue());
146
Assert.assertEquals(copy.getValue(), xaStore.get(KEY).getValue());
147
assertNotSame(copy.getValue(), xaStore.get(KEY).getValue());
151
public void testSupportsCopyOnWrite() {
153
AtomicLong atomicLong = new AtomicLong(0);
155
Element element = new Element(KEY, atomicLong, 1);
156
atomicLong.getAndIncrement();
157
xaStore.put(element);
159
atomicLong.getAndIncrement();
160
element.setVersion(2);
162
Assert.assertEquals(1, ((AtomicLong)xaStore.get(KEY).getValue()).get());
163
Assert.assertEquals(1, xaStore.get(KEY).getVersion());
165
xaStore.put(new Element(KEY, atomicLong, 1));
166
Assert.assertEquals(2, ((AtomicLong)xaStore.get(KEY).getValue()).get());
167
atomicLong.getAndIncrement();
169
Assert.assertEquals(2, ((AtomicLong)xaStore.get(KEY).getValue()).get());
170
Assert.assertEquals(1, xaStore.get(KEY).getVersion());
174
public void testThrowsExceptionOnNonSerializableValue() {
176
xaStore.put(new Element(KEY, new Object()));
177
fail("Should have thrown an Exception");
178
} catch (Exception e) {
180
assertTrue("Expected " + CacheException.class.getName() + ", but was " + e.getClass().getName(), e instanceof CacheException);
182
assertNull(xaStore.get(KEY));
186
public void testGetKeys() throws InterruptedException {
187
int unpinCount = 500;
188
int pinCount = 500; //please make sure that pinCount is even
189
Set<Object> pinnedKeys = new HashSet<Object>();
190
Set<Object> removedPinnedKeys = new HashSet<Object>();
191
Set<Object> unpinnedKeys = new HashSet<Object>();
193
for (int i = 0; i < unpinCount; i++) {
195
unpinnedKeys.add(key);
196
Element element = new Element(key, i);
197
xaStore.put(element);
202
Assert.assertEquals(unpinCount, xaStore.getSize());
204
for (int i = 0; i < pinCount; i++) {
207
Element element = new Element(key, i);
208
xaStore.setPinned(element.getObjectKey(), true);
209
xaStore.put(element);
211
Assert.assertEquals(pinCount+unpinCount, xaStore.getSize());
212
int halfPinned = pinCount/2;
213
for (int i = 0; i < halfPinned; i++) {
215
removedPinnedKeys.add(key);
220
pinnedKeys.removeAll(removedPinnedKeys);
221
Assert.assertEquals(pinCount-halfPinned, pinnedKeys.size());
222
Assert.assertEquals(unpinCount+halfPinned, xaStore.getSize());
224
List keys = xaStore.getKeys();
225
Assert.assertEquals(unpinCount+halfPinned, xaStore.getSize());
226
Assert.assertEquals(unpinCount+halfPinned, keys.size());
228
for(Object okey : keys) {
229
System.out.println(okey);
230
Assert.assertFalse(removedPinnedKeys.contains(okey));
231
Assert.assertTrue(pinnedKeys.contains(okey) || unpinnedKeys.contains(okey));
235
keys = xaStore.getKeys();
236
Assert.assertEquals(0, xaStore.getSize());
237
Assert.assertEquals(0, keys.size());