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

« back to all changes in this revision

Viewing changes to src/test/java/net/sf/ehcache/MemoryStoreTester.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:
1
1
/**
2
 
 *  Copyright 2003-2010 Terracotta, Inc.
 
2
 *  Copyright Terracotta, Inc.
3
3
 *
4
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
5
 *  you may not use this file except in compliance with the License.
23
23
import net.sf.ehcache.store.MemoryStore;
24
24
import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
25
25
import net.sf.ehcache.store.Store;
 
26
 
26
27
import org.junit.After;
27
28
import org.junit.Assume;
28
29
import org.junit.Before;
35
36
import java.util.ArrayList;
36
37
import java.util.Date;
37
38
import java.util.List;
 
39
import java.util.Map.Entry;
38
40
import java.util.Random;
39
41
 
40
42
import net.sf.ehcache.config.CacheConfiguration;
 
43
import net.sf.ehcache.config.Configuration;
41
44
import static org.hamcrest.Matchers.is;
 
45
import static org.hamcrest.number.OrderingComparison.greaterThan;
 
46
import static org.hamcrest.number.OrderingComparison.lessThan;
42
47
import static org.junit.Assert.*;
43
48
 
44
49
/**
45
50
 * Other than policy differences, the Store implementations should work identically
46
51
 *
47
52
 * @author Greg Luck
48
 
 * @version $Id: MemoryStoreTester.java 4842 2011-10-13 13:42:15Z cdennis $
 
53
 * @version $Id: MemoryStoreTester.java 7369 2013-04-10 17:49:29Z cdennis $
49
54
 */
50
55
@Ignore
51
56
public class MemoryStoreTester extends AbstractCacheTest {
401
406
     */
402
407
    @Test
403
408
    public void testMemoryLeak() throws Exception {
404
 
        long differenceMemoryCache = thrashCache();
405
 
        LOG.info("Difference is : " + differenceMemoryCache);
406
 
        //Sometimes this can be higher but a three hour run confirms no memory leak. Consider increasing.
407
 
        assertTrue("Memory difference was expected to be less than 500000, but was " + differenceMemoryCache, differenceMemoryCache < 500000);
 
409
        try {
 
410
            //Sometimes this can be higher but a three hour run confirms no memory leak. Consider increasing.
 
411
            assertThat(thrashCache(), lessThan(500000L));
 
412
        } catch (AssertionError e) {
 
413
            for (Entry<Thread, StackTraceElement[]> entry : Thread.getAllStackTraces().entrySet()) {
 
414
                new ThreadDumpException(entry.getKey(), entry.getValue()).printStackTrace(System.out);
 
415
            }
 
416
            throw e;
 
417
        }
408
418
    }
409
419
 
410
420
 
621
631
        Assert.assertEquals(20, store.getSize());
622
632
    }
623
633
 
 
634
    /**
 
635
     * Tests adding an entry.
 
636
     */
 
637
    @Test
 
638
    public void testPreSizedMemoryStore() throws Exception {
 
639
        System.setProperty(MemoryStore.class.getName() + ".presize", "true");
 
640
        try {
 
641
            CacheManager manager = new CacheManager(new Configuration().name("testPreSizedMemoryStore"));
 
642
            Cache cache = new Cache(new CacheConfiguration().name("testPreSizedMemoryStore").maxEntriesLocalHeap(1000));
 
643
            manager.addCache(cache);
 
644
            
 
645
            for (int i = 0; i == cache.getSize(); i++) {
 
646
              cache.put(new Element(i, i));
 
647
            }
 
648
            
 
649
            assertThat(cache.getSize(), greaterThan(0));
 
650
        } finally {
 
651
            System.clearProperty(MemoryStore.class.getName() + ".presize");
 
652
        }
 
653
    }
 
654
 
 
655
    static class ThreadDumpException extends Throwable {
 
656
 
 
657
        private final Thread thread;
 
658
 
 
659
        ThreadDumpException(Thread t, StackTraceElement[] trace) {
 
660
            thread = t;
 
661
            setStackTrace(trace);
 
662
        }
 
663
 
 
664
        @Override
 
665
        public String toString() {
 
666
            return thread.toString();
 
667
        }
 
668
    }
624
669
}