~ubuntu-branches/ubuntu/quantal/ehcache/quantal

« back to all changes in this revision

Viewing changes to src/test/java/net/sf/ehcache/store/disk/DiskStoreTest.java

  • Committer: Package Import Robot
  • Author(s): Miguel Landaeta
  • Date: 2012-03-01 19:15:46 UTC
  • mfrom: (1.1.6) (2.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20120301191546-rvlk8tomip0c2m9p
Tags: 2.5.0-1
* Team upload.
* New upstream release. (Closes: #661450).
* Bump Standards-Version to 3.9.3. No changes were required.
* Fix lintian warning with copyright file.
* Remove unnecessary dependencies on JRE for libehcache-java.
* Wrap list of dependencies and uploaders.
* Fix clean target by adding a mh_clean call, to allow twice in a row builds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package net.sf.ehcache.store.disk;
 
2
 
 
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;
 
9
 
 
10
import java.util.HashSet;
 
11
import java.util.List;
 
12
import java.util.Set;
 
13
import java.util.concurrent.Callable;
 
14
import java.util.concurrent.atomic.AtomicLong;
 
15
 
 
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;
 
30
 
 
31
import org.hamcrest.core.Is;
 
32
import org.junit.Before;
 
33
import org.junit.Test;
 
34
 
 
35
/**
 
36
 * @author Alex Snaps
 
37
 * @author Ludovic Orban
 
38
 */
 
39
public class DiskStoreTest {
 
40
 
 
41
    private static final String KEY = "KEY";
 
42
 
 
43
    private Store store;
 
44
    private Store xaStore;
 
45
 
 
46
    private Cache cache;
 
47
    private Cache xaCache;
 
48
 
 
49
    @Before
 
50
    public void init() {
 
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());
 
55
    }
 
56
 
 
57
    @Test
 
58
    public void testPersistenceWithPinnedElements() {
 
59
        final Element[] lastEvicted = new Element[1];
 
60
 
 
61
        cache.getCacheEventNotificationService().registerListener(new CacheEventListener() {
 
62
            @Override
 
63
            public Object clone() throws CloneNotSupportedException {
 
64
                return super.clone();
 
65
            }
 
66
            public void notifyElementRemoved(Ehcache cache, Element element) throws CacheException {
 
67
            }
 
68
            public void notifyElementPut(Ehcache cache, Element element) throws CacheException {
 
69
            }
 
70
            public void notifyElementUpdated(Ehcache cache, Element element) throws CacheException {
 
71
            }
 
72
            public void notifyElementExpired(Ehcache cache, Element element) {
 
73
            }
 
74
            public void notifyElementEvicted(Ehcache cache, Element element) {
 
75
                lastEvicted[0] = element;
 
76
            }
 
77
            public void notifyRemoveAll(Ehcache cache) {
 
78
            }
 
79
            public void dispose() {
 
80
            }
 
81
        });
 
82
 
 
83
        store.put(new Element(1, "one"));
 
84
        Element element2 = new Element(2, new Object());
 
85
        store.setPinned(element2.getObjectKey(), true);
 
86
        store.put(element2);
 
87
        store.dispose();
 
88
 
 
89
        assertNull("element should not have been evicted: " + lastEvicted[0], lastEvicted[0]);
 
90
 
 
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));
 
95
    }
 
96
 
 
97
    @Test
 
98
    public void testDiskStoreSize() throws Exception {
 
99
        CacheManager cm = new CacheManager(
 
100
            new Configuration()
 
101
                .cache(new CacheConfiguration("aCache", 10000)
 
102
                    .overflowToDisk(true)
 
103
                    .eternal(false)
 
104
                    .timeToLiveSeconds(1000)
 
105
                    .timeToLiveSeconds(360)
 
106
                )
 
107
        );
 
108
        final Cache cache = cm.getCache("aCache");
 
109
 
 
110
 
 
111
        cache.put(new Element(-1, -1));
 
112
        assertEquals(-1, cache.get(-1).getValue());
 
113
        cache.remove(-1);
 
114
        assertEquals(null, cache.get(-1));
 
115
 
 
116
        cache.put(new Element(-2, -2));
 
117
        assertEquals(-2, cache.get(-2).getValue());
 
118
        cache.remove(-2);
 
119
        assertEquals(null, cache.get(-2));
 
120
 
 
121
        assertEquals(0, cache.getDiskStoreSize());
 
122
 
 
123
        for (int i = 0; i < 10010; i++) {
 
124
            cache.put(new Element(i, i));
 
125
        }
 
126
 
 
127
        Thread.sleep(3000);
 
128
 
 
129
        RetryAssert.assertBy(1, SECONDS, new Callable<Integer>() {
 
130
                public Integer call() throws Exception {
 
131
                    return cache.getDiskStoreSize();
 
132
                }
 
133
            }, Is.is(10010));
 
134
 
 
135
        cm.shutdown();
 
136
    }
 
137
 
 
138
    @Test
 
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());
 
148
    }
 
149
 
 
150
    @Test
 
151
    public void testSupportsCopyOnWrite() {
 
152
 
 
153
        AtomicLong atomicLong = new AtomicLong(0);
 
154
 
 
155
        Element element = new Element(KEY, atomicLong, 1);
 
156
        atomicLong.getAndIncrement();
 
157
        xaStore.put(element);
 
158
 
 
159
        atomicLong.getAndIncrement();
 
160
        element.setVersion(2);
 
161
 
 
162
        Assert.assertEquals(1, ((AtomicLong)xaStore.get(KEY).getValue()).get());
 
163
        Assert.assertEquals(1, xaStore.get(KEY).getVersion());
 
164
 
 
165
        xaStore.put(new Element(KEY, atomicLong, 1));
 
166
        Assert.assertEquals(2, ((AtomicLong)xaStore.get(KEY).getValue()).get());
 
167
        atomicLong.getAndIncrement();
 
168
 
 
169
        Assert.assertEquals(2, ((AtomicLong)xaStore.get(KEY).getValue()).get());
 
170
        Assert.assertEquals(1, xaStore.get(KEY).getVersion());
 
171
    }
 
172
 
 
173
    @Test
 
174
    public void testThrowsExceptionOnNonSerializableValue() {
 
175
        try {
 
176
            xaStore.put(new Element(KEY, new Object()));
 
177
            fail("Should have thrown an Exception");
 
178
        } catch (Exception e) {
 
179
            e.printStackTrace();
 
180
            assertTrue("Expected " + CacheException.class.getName() + ", but was " + e.getClass().getName(), e instanceof CacheException);
 
181
        }
 
182
        assertNull(xaStore.get(KEY));
 
183
    }
 
184
 
 
185
    @Test
 
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>();
 
192
        Object key = null;
 
193
        for (int i = 0; i < unpinCount; i++) {
 
194
            key = "Ku-" + i;
 
195
            unpinnedKeys.add(key);
 
196
            Element element = new Element(key, i);
 
197
            xaStore.put(element);
 
198
        }
 
199
 
 
200
        Thread.sleep(1000);
 
201
 
 
202
        Assert.assertEquals(unpinCount, xaStore.getSize());
 
203
 
 
204
        for (int i = 0; i < pinCount; i++) {
 
205
            key = "Kp-" + i;
 
206
            pinnedKeys.add(key);
 
207
            Element element = new Element(key, i);
 
208
            xaStore.setPinned(element.getObjectKey(), true);
 
209
            xaStore.put(element);
 
210
        }
 
211
        Assert.assertEquals(pinCount+unpinCount, xaStore.getSize());
 
212
        int halfPinned = pinCount/2;
 
213
        for (int i = 0; i < halfPinned; i++) {
 
214
            key = "Kp-" + i;
 
215
            removedPinnedKeys.add(key);
 
216
            xaStore.remove(key);
 
217
        }
 
218
 
 
219
        Thread.sleep(1000);
 
220
        pinnedKeys.removeAll(removedPinnedKeys);
 
221
        Assert.assertEquals(pinCount-halfPinned, pinnedKeys.size());
 
222
        Assert.assertEquals(unpinCount+halfPinned, xaStore.getSize());
 
223
 
 
224
        List keys = xaStore.getKeys();
 
225
        Assert.assertEquals(unpinCount+halfPinned, xaStore.getSize());
 
226
        Assert.assertEquals(unpinCount+halfPinned, keys.size());
 
227
 
 
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));
 
232
        }
 
233
 
 
234
        xaStore.removeAll();
 
235
        keys = xaStore.getKeys();
 
236
        Assert.assertEquals(0, xaStore.getSize());
 
237
        Assert.assertEquals(0, keys.size());
 
238
    }
 
239
 
 
240
}