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

« back to all changes in this revision

Viewing changes to src/test/java/net/sf/ehcache/pool/TwinCachesTest.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:
9
9
import net.sf.ehcache.CacheManager;
10
10
import net.sf.ehcache.Ehcache;
11
11
import net.sf.ehcache.Element;
 
12
import net.sf.ehcache.Status;
12
13
import net.sf.ehcache.config.CacheConfiguration;
13
14
import net.sf.ehcache.config.Configuration;
14
15
import net.sf.ehcache.config.MemoryUnit;
15
16
 
 
17
import org.junit.After;
16
18
import org.junit.Test;
17
19
 
18
20
public class TwinCachesTest {
19
21
 
 
22
    private CacheManager manager;
 
23
 
20
24
    @Test
21
25
    public void testParallelLoadTwinCaches() {
22
 
        CacheManager manager = new CacheManager(new Configuration().maxBytesLocalHeap(16, MemoryUnit.MEGABYTES).defaultCache(new CacheConfiguration("default", 0).eternal(true)));
 
26
        manager = new CacheManager(new Configuration().maxBytesLocalHeap(16, MemoryUnit.MEGABYTES).defaultCache(new CacheConfiguration("default", 0).eternal(true)));
23
27
 
24
28
        Ehcache one = manager.addCacheIfAbsent("one");
25
29
        Ehcache two = manager.addCacheIfAbsent("two");
51
55
        System.err.println("[2] Measured : " + (((float) two.getSize()) / total) + " [" + two.getSize() + "]");
52
56
 
53
57
        Assert.assertEquals(ratio, ((float) one.getSize()) / total, 0.1f);
54
 
        Assert.assertEquals(1f - ratio, ((float) two.getSize()) / total, 0.1f);
55
 
        manager.shutdown();
 
58
        Assert.assertEquals(1f - ratio, ((float)two.getSize()) / total, 0.1f);
56
59
    }
57
60
 
58
61
    @Test
59
62
    public void testSerialLoadTwinCaches() {
60
 
        CacheManager manager = new CacheManager(new Configuration().maxBytesLocalHeap(16, MemoryUnit.MEGABYTES).defaultCache(new CacheConfiguration("default", 0).eternal(true)));
 
63
        manager = new CacheManager(new Configuration().maxBytesLocalHeap(16, MemoryUnit.MEGABYTES).defaultCache(new CacheConfiguration("default", 0).eternal(true)));
61
64
 
62
65
        Ehcache one = manager.addCacheIfAbsent("one");
63
66
        Ehcache two = manager.addCacheIfAbsent("two");
91
94
        System.err.println("[2] Measured : " + (((float) two.getSize()) / total) + " [" + two.getSize() + "]");
92
95
 
93
96
        Assert.assertEquals(ratio, ((float) one.getSize()) / total, 0.1f);
94
 
        Assert.assertEquals(1f - ratio, ((float) two.getSize()) / total, 0.1f);
95
 
        manager.shutdown();
 
97
        Assert.assertEquals(1f - ratio, ((float)two.getSize()) / total, 0.1f);
96
98
    }
97
99
 
98
100
    @Test
99
101
    public void testRandomAccessTwinCaches() {
100
 
        CacheManager manager = new CacheManager(new Configuration().maxBytesLocalHeap(1, MemoryUnit.MEGABYTES).defaultCache(new CacheConfiguration("default", 0).eternal(true)));
 
102
        manager = new CacheManager(new Configuration().maxBytesLocalHeap(1, MemoryUnit.MEGABYTES).defaultCache(new CacheConfiguration("default", 0).eternal(true)));
101
103
 
102
104
        Ehcache one = manager.addCacheIfAbsent("one");
103
105
        Ehcache two = manager.addCacheIfAbsent("two");
126
128
        System.err.println("[2] Measured : " + (((float) two.getSize()) / total) + " [" + two.getSize() + "]");
127
129
 
128
130
        Assert.assertEquals(ratio, ((float) one.getSize()) / total, 0.1f);
129
 
        Assert.assertEquals(1f - ratio, ((float) two.getSize()) / total, 0.1f);
130
 
        manager.shutdown();
 
131
        Assert.assertEquals(1f - ratio, ((float)two.getSize()) / total, 0.1f);
131
132
    }
132
133
 
133
134
    @Test
134
135
    public void testRandomAccessTripletCaches() {
135
 
        CacheManager manager = new CacheManager(new Configuration().maxBytesLocalHeap(1, MemoryUnit.MEGABYTES).defaultCache(new CacheConfiguration("default", 0).eternal(true)));
 
136
        manager = new CacheManager(new Configuration().maxBytesLocalHeap(1, MemoryUnit.MEGABYTES).defaultCache(new CacheConfiguration("default", 0).eternal(true)));
136
137
 
137
138
        Ehcache one = manager.addCacheIfAbsent("one");
138
139
        Ehcache two = manager.addCacheIfAbsent("two");
173
174
 
174
175
        Assert.assertEquals(ratioOne, ((float) one.getSize()) / total, 0.1f);
175
176
        Assert.assertEquals(ratioTwo - ratioOne, ((float) two.getSize()) / total, 0.1f);
176
 
        Assert.assertEquals(1 - ratioTwo, ((float) three.getSize()) / total, 0.1f);
177
 
        manager.shutdown();
 
177
        Assert.assertEquals(1 - ratioTwo, ((float)three.getSize()) / total, 0.1f);
178
178
    }
179
179
 
180
180
    @Test
181
181
    public void testIntroducedRandomAccessTwinCache() throws IOException {
182
 
        CacheManager manager = new CacheManager(new Configuration().maxBytesLocalHeap(2, MemoryUnit.MEGABYTES).defaultCache(new CacheConfiguration("default", 0).eternal(true)));
 
182
        manager = new CacheManager(new Configuration().maxBytesLocalHeap(2, MemoryUnit.MEGABYTES).defaultCache(new CacheConfiguration("default", 0).eternal(true)));
183
183
 
184
184
        Ehcache one = manager.addCacheIfAbsent("one");
185
185
 
218
218
        System.err.println("[2] Measured : " + (((float) two.getSize()) / total) + " [" + two.getSize() + "]");
219
219
 
220
220
        Assert.assertEquals(ratio, ((float) one.getSize()) / total, 0.1f);
221
 
        Assert.assertEquals(1f - ratio, ((float) two.getSize()) / total, 0.1f);
222
 
        manager.shutdown();
 
221
        Assert.assertEquals(1f - ratio, ((float)two.getSize()) / total, 0.1f);
223
222
    }
224
223
 
225
224
    @Test
226
225
    public void testIntroducedRandomAccessTripletCache() throws IOException {
227
 
        CacheManager manager = new CacheManager(new Configuration().maxBytesLocalHeap(2, MemoryUnit.MEGABYTES).defaultCache(new CacheConfiguration("default", 0).eternal(true)));
 
226
        manager = new CacheManager(new Configuration().maxBytesLocalHeap(2, MemoryUnit.MEGABYTES).defaultCache(new CacheConfiguration("default", 0).eternal(true)));
228
227
 
229
228
        Ehcache one = manager.addCacheIfAbsent("one");
230
229
 
299
298
 
300
299
        Assert.assertEquals(ratioOne, ((float) one.getSize()) / totalThree, 0.1f);
301
300
        Assert.assertEquals(ratioTwo - ratioOne, ((float) two.getSize()) / totalThree, 0.1f);
302
 
        Assert.assertEquals(1 - ratioTwo, ((float) three.getSize()) / totalThree, 0.1f);
303
 
        manager.shutdown();
 
301
        Assert.assertEquals(1 - ratioTwo, ((float)three.getSize()) / totalThree, 0.1f);
304
302
    }
305
303
 
306
304
    @Test
307
305
    public void testIntroducedRandomAccessDoubledCache() throws IOException {
308
 
        CacheManager manager = new CacheManager(new Configuration().maxBytesLocalHeap(2, MemoryUnit.MEGABYTES).defaultCache(new CacheConfiguration("default", 0).eternal(true)));
 
306
        doTestIntroducedAccessDoubledCache(System.nanoTime());
 
307
    }
 
308
 
 
309
    @Test
 
310
    public void testIntroducedFixedAccessDoubledCache() throws IOException {
 
311
        // See MNK-3643
 
312
        doTestIntroducedAccessDoubledCache(944752613893346L);
 
313
    }
 
314
 
 
315
    private void doTestIntroducedAccessDoubledCache(final long seed) {
 
316
        manager = new CacheManager(new Configuration().maxBytesLocalHeap(2, MemoryUnit.MEGABYTES).defaultCache(new CacheConfiguration("default", 0).eternal(true)));
309
317
 
310
318
        Ehcache one = manager.addCacheIfAbsent("one");
311
319
 
312
 
        long seed = System.nanoTime();
313
320
        System.err.println("TwinCachesTest.testIntroducedRandomAccessDoubledCache seed=" + seed);
314
321
        Random rndm = new Random(seed);
315
322
        float ratio = rndm.nextFloat();
349
356
        System.err.println("[2] Count Ratio    : " + (1 - ratio));
350
357
        System.err.println("[2] Count Measured : " + (((float) two.getSize()) / totalCount) + " [" + two.getSize() + "]");
351
358
 
352
 
        Assert.assertEquals(ratio, ((float) one.getSize()) / totalCount, 0.1f);
 
359
        Assert.assertEquals(ratio, ((float)one.getSize()) / totalCount, 0.1f);
353
360
        Assert.assertEquals(1f - ratio, ((float) two.getSize()) / totalCount, 0.1f);
354
361
 
355
362
        long totalBytes = one.calculateInMemorySize() + two.calculateInMemorySize();
363
370
                + two.calculateInMemorySize() + "]");
364
371
 
365
372
        Assert.assertEquals(bytesRatio, ((float) one.calculateInMemorySize()) / totalBytes, 0.1f);
366
 
        Assert.assertEquals(1f - bytesRatio, ((float) two.calculateInMemorySize()) / totalBytes, 0.1f);
367
 
        manager.shutdown();
 
373
        Assert.assertEquals(1f - bytesRatio, ((float)two.calculateInMemorySize()) / totalBytes, 0.1f);
368
374
    }
369
375
 
370
376
    private static int getRandomKey(Random rndm, int max) {
374
380
        } while (key < 0 || key >= max);
375
381
        return key;
376
382
    }
 
383
 
 
384
    @After
 
385
    public void tearDown() {
 
386
        if(manager != null && manager.getStatus() == Status.STATUS_ALIVE) {
 
387
            manager.shutdown();
 
388
        }
 
389
    }
377
390
}