~ubuntu-branches/ubuntu/trusty/ehcache/trusty

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2013-05-06 14:53:07 UTC
  • mfrom: (1.1.7) (2.1.8 sid)
  • Revision ID: package-import@ubuntu.com-20130506145307-v5bhw5yu70re00l3
Tags: 2.6.7-1
* Team upload.
* New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
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
 
12
13
import org.junit.After;
13
14
import org.junit.Test;
14
15
 
15
 
import java.util.concurrent.Callable;
 
16
import java.util.concurrent.BrokenBarrierException;
 
17
import java.util.concurrent.CyclicBarrier;
16
18
 
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;
20
25
 
21
26
/**
24
29
public class DiskStoreBootstrapCacheLoaderFactoryTest {
25
30
 
26
31
    private static final int ELEMENTS_ON_DISK = 500;
27
 
    private static final int LOADER_DELAY = 500;
28
32
    private CacheManager manager;
29
33
    private Cache cacheElementCountBound;
30
34
    private Cache cacheSizeBound;
31
 
    private DiskStoreBootstrapCacheLoader cacheElementCountBoundBootstrapCacheLoader;
32
 
    private DiskStoreBootstrapCacheLoader cacheSizeBoundBootstrapCacheLoader;
 
35
    private TestDiskStoreBootstrapCacheLoader cacheElementCountBoundBootstrapCacheLoader;
 
36
    private TestDiskStoreBootstrapCacheLoader cacheSizeBoundBootstrapCacheLoader;
33
37
 
34
38
    public void setUp(CacheUT cut) {
35
39
        initCacheManager(cut);
52
56
    }
53
57
 
54
58
    @Test
55
 
    public void testLoadsFromDiskWithMaxElementsInMemorySet() throws InterruptedException {
 
59
    public void testLoadsFromDiskWithMaxElementsInMemorySet() throws Exception {
56
60
        setUp(CacheUT.elementBased);
57
 
        int waitCycles = 0;
58
 
        while (cacheElementCountBound.getDiskStoreSize() != ELEMENTS_ON_DISK && waitCycles < 15) {
59
 
            System.err.println("Not all entries have been spooled to disk, waiting a bit ... ");
60
 
            Thread.sleep(250);
61
 
            waitCycles++;
62
 
        }
63
 
        waitForBootstrapLoader(cacheElementCountBoundBootstrapCacheLoader);
64
 
        RetryAssert.assertBy(10, SECONDS, new Callable<Integer>() {
65
 
                public Integer call() throws Exception {
66
 
                    return cacheElementCountBound.getDiskStoreSize();
67
 
                }
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();
75
 
                }
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();
82
 
                }
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));
86
72
    }
87
73
 
88
74
    @Test
89
 
    public void testLoadsFromDiskWithMaxBytesOnHeapSet() throws InterruptedException {
 
75
    public void testLoadsFromDiskWithMaxBytesOnHeapSet() throws Exception {
90
76
        setUp(CacheUT.sizeBased);
91
 
        int waitCycles = 0;
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");
94
 
            Thread.sleep(250);
95
 
            waitCycles++;
96
 
        }
97
 
        waitForBootstrapLoader(cacheSizeBoundBootstrapCacheLoader);
98
 
        RetryAssert.assertBy(10, SECONDS, new Callable<Integer>() {
99
 
                public Integer call() throws Exception {
100
 
                    return cacheSizeBound.getDiskStoreSize();
101
 
                }
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();
109
 
                }
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();
116
 
                }
117
 
            }, Is.is(ELEMENTS_ON_DISK));
118
 
        assertThat(cacheSizeBound.getLiveCacheStatistics().getLocalHeapSizeInBytes() <= MemoryUnit.KILOBYTES.toBytes(220), is(true));
119
 
    }
120
 
 
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);
126
 
        }
127
 
        return bootstrapCacheLoader.getLoadedElements();
 
87
        cacheSizeBoundBootstrapCacheLoader.triggerLoad();
 
88
        assertThat(cacheSizeBound.getMemoryStoreSize(), greaterThan(0L));
 
89
        assertThat(cacheSizeBound.getLiveCacheStatistics().getLocalHeapSizeInBytes(), lessThanOrEqualTo(KILOBYTES.toBytes(220L)));
128
90
    }
129
91
 
130
92
    private void initCacheManager(CacheUT cut) {
131
 
        manager = new CacheManager(new Configuration());
132
93
        switch (cut) {
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)
136
98
                    .eternal(true)
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);
142
103
                break;
143
104
            case sizeBased:
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)
146
108
                    .eternal(true)
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);
154
115
                break;
165
126
    private static enum CacheUT {
166
127
        elementBased, sizeBased
167
128
    }
 
129
 
 
130
    static class TestDiskStoreBootstrapCacheLoader extends DiskStoreBootstrapCacheLoader {
 
131
 
 
132
        private final CyclicBarrier before = new CyclicBarrier(2);
 
133
        private final CyclicBarrier after = new CyclicBarrier(2);
 
134
 
 
135
        public TestDiskStoreBootstrapCacheLoader() {
 
136
            super(true);
 
137
        }
 
138
 
 
139
        @Override
 
140
        protected void doLoad(Ehcache cache) {
 
141
            try {
 
142
                before.await();
 
143
                try {
 
144
                    super.doLoad(cache);
 
145
                } finally {
 
146
                    after.await();
 
147
                }
 
148
            } catch (InterruptedException e) {
 
149
                throw new AssertionError(e);
 
150
            } catch (BrokenBarrierException e) {
 
151
                throw new AssertionError(e);
 
152
            }
 
153
        }
 
154
 
 
155
        public void triggerLoad() {
 
156
            try {
 
157
                before.await();
 
158
                after.await();
 
159
            } catch (InterruptedException e) {
 
160
                throw new AssertionError(e);
 
161
            } catch (BrokenBarrierException e) {
 
162
                throw new AssertionError(e);
 
163
            }
 
164
        }
 
165
    }
168
166
}