View Javadoc

1   package net.sf.ehcache.store;
2   
3   import junit.framework.Assert;
4   import net.sf.ehcache.Cache;
5   import net.sf.ehcache.CacheManager;
6   import net.sf.ehcache.Element;
7   import net.sf.ehcache.config.CacheConfiguration;
8   import org.junit.Test;
9   
10  import static junit.framework.Assert.assertNotNull;
11  import static junit.framework.Assert.assertNull;
12  
13  /***
14   * @author Ludovic Orban
15   */
16  public class DiskBackedMemoryStoreTest {
17  
18      @Test
19      public void testElementPinning() throws Exception {
20          CacheManager cm = new CacheManager();
21          Cache cache = new Cache(new CacheConfiguration("myCache", 20).overflowToDisk(true).maxElementsOnDisk(20));
22          cm.addCache(cache);
23  
24          for (int i = 0; i < 200; i++) {
25              Element element = new Element("Ku-" + i, "" + i);
26              cache.put(element);
27          }
28  
29          Thread.sleep(1000);
30  
31          Assert.assertEquals(20, cache.getSize());
32  
33          for (int i = 0; i < 200; i++) {
34              Element element = new Element("Kp-" + i, new Object());
35              element.setTimeToIdle(1);
36              element.setTimeToLive(1);
37              element.setPinned(true);
38              cache.put(element);
39          }
40  
41          for (int i = 0; i < 200; i++) {
42              assertNotNull(cache.get("Kp-" + i));
43          }
44  
45          // wait until all pinned elements expire
46          Thread.sleep(1100);
47  
48          for (int i = 0; i < 200; i++) {
49              assertNull(cache.get("Kp-" + i));
50          }
51  
52          Assert.assertEquals(20, cache.getSize());
53  
54          cm.shutdown();
55      }
56  }