View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache;
18  
19  import junit.framework.Assert;
20  import net.sf.ehcache.config.CacheConfiguration;
21  import net.sf.ehcache.config.Configuration;
22  import net.sf.ehcache.config.DiskStoreConfiguration;
23  import net.sf.ehcache.config.MemoryUnit;
24  import net.sf.ehcache.config.PinningConfiguration;
25  import org.junit.After;
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  import static org.junit.Assert.assertNotNull;
30  
31  /***
32   * Tests for Cache pinning with pools
33   *
34   * @author Ludovic Orban
35   */
36  public class CachePoolPinningTest {
37  
38      private static final int ELEMENT_COUNT = 40000;
39  
40      private CacheManager cacheManager;
41  
42      @Before
43      public void setUp() throws Exception {
44          cacheManager = new CacheManager(
45                  new Configuration()
46                          .diskStore(new DiskStoreConfiguration().path(System.getProperty("java.io.tmpdir")))
47                          .maxBytesLocalHeap(100, MemoryUnit.KILOBYTES)
48                          .maxBytesLocalDisk(200, MemoryUnit.KILOBYTES)
49          );
50      }
51  
52      @After
53      public void tearDown() {
54          cacheManager.shutdown();
55          cacheManager = null;
56      }
57  
58      @Test
59      public void testClassicLru() throws Exception {
60          System.setProperty(Cache.NET_SF_EHCACHE_USE_CLASSIC_LRU, "true");
61          try {
62              testMemoryOnly();
63          } finally {
64              System.setProperty(Cache.NET_SF_EHCACHE_USE_CLASSIC_LRU, "false");
65          }
66      }
67  
68      @Test
69      public void testMemoryOnly() throws Exception {
70          cacheManager.addCache(new Cache(
71                  new CacheConfiguration()
72                          .statistics(true)
73                          .name("memoryOnlyCache_onHeap")
74                          .pinning(new PinningConfiguration().store(PinningConfiguration.Store.LOCALHEAP))
75          ));
76          doAssertions(cacheManager.getCache("memoryOnlyCache_onHeap"), ELEMENT_COUNT, 0);
77  
78          cacheManager.addCache(new Cache(
79                  new CacheConfiguration()
80                          .statistics(true)
81                          .name("memoryOnlyCache_inMemory")
82                          .pinning(new PinningConfiguration().store(PinningConfiguration.Store.LOCALMEMORY))
83          ));
84          doAssertions(cacheManager.getCache("memoryOnlyCache_inMemory"), ELEMENT_COUNT, 0);
85  
86          cacheManager.addCache(new Cache(
87                  new CacheConfiguration()
88                          .statistics(true)
89                          .name("memoryOnlyCache_inCache")
90                          .pinning(new PinningConfiguration().store(PinningConfiguration.Store.INCACHE))
91          ));
92          doAssertions(cacheManager.getCache("memoryOnlyCache_inCache"), ELEMENT_COUNT, 0);
93      }
94  
95      @Test
96      public void testOverflowToDisk() throws Exception {
97          cacheManager.addCache(new Cache(
98                  new CacheConfiguration()
99                          .overflowToDisk(true)
100                         .statistics(true)
101                         .name("overflowToDiskCache_onHeap")
102                         .pinning(new PinningConfiguration().store(PinningConfiguration.Store.LOCALHEAP))
103         ));
104         doAssertions(cacheManager.getCache("overflowToDiskCache_onHeap"), ELEMENT_COUNT, 0);
105 
106         cacheManager.addCache(new Cache(
107                 new CacheConfiguration()
108                         .overflowToDisk(true)
109                         .statistics(true)
110                         .name("overflowToDiskCache_inMemory")
111                         .pinning(new PinningConfiguration().store(PinningConfiguration.Store.LOCALMEMORY))
112         ));
113         doAssertions(cacheManager.getCache("overflowToDiskCache_inMemory"), ELEMENT_COUNT, 0);
114 
115         cacheManager.addCache(new Cache(
116                 new CacheConfiguration()
117                         .overflowToDisk(true)
118                         .statistics(true)
119                         .name("overflowToDiskCache_inCache")
120                         .pinning(new PinningConfiguration().store(PinningConfiguration.Store.INCACHE))
121         ));
122         doAssertions(cacheManager.getCache("overflowToDiskCache_inCache"), 0, ELEMENT_COUNT);
123     }
124 
125     @Test
126     public void testDiskPersistent() throws Exception {
127         cacheManager.addCache(new Cache(
128                 new CacheConfiguration()
129                         .overflowToDisk(true)
130                         .diskPersistent(true)
131                         .statistics(true)
132                         .name("diskPersistentCache_onHeap")
133                         .pinning(new PinningConfiguration().store(PinningConfiguration.Store.LOCALHEAP))
134         ));
135         doAssertions(cacheManager.getCache("diskPersistentCache_onHeap"), ELEMENT_COUNT, 0);
136 
137         cacheManager.addCache(new Cache(
138                 new CacheConfiguration()
139                         .overflowToDisk(true)
140                         .diskPersistent(true)
141                         .statistics(true)
142                         .name("diskPersistentCache_inMemory")
143                         .pinning(new PinningConfiguration().store(PinningConfiguration.Store.LOCALMEMORY))
144         ));
145         doAssertions(cacheManager.getCache("diskPersistentCache_inMemory"), ELEMENT_COUNT, 0);
146 
147         cacheManager.addCache(new Cache(
148                 new CacheConfiguration()
149                         .overflowToDisk(true)
150                         .diskPersistent(true)
151                         .statistics(true)
152                         .name("diskPersistentCache_inCache")
153                         .pinning(new PinningConfiguration().store(PinningConfiguration.Store.INCACHE))
154         ));
155         doAssertions(cacheManager.getCache("diskPersistentCache_inCache"), 0, ELEMENT_COUNT);
156     }
157 
158     private void doAssertions(Cache cache, long expectedMemoryHits, long expectedDiskHits) {
159         for (int i = 0; i < ELEMENT_COUNT; i++) {
160             cache.put(new Element(i, i));
161         }
162 
163         Assert.assertEquals(ELEMENT_COUNT, cache.getSize());
164 
165         for (int i = 0; i < ELEMENT_COUNT; i++) {
166             assertNotNull(cache.get(i));
167         }
168 
169         Assert.assertEquals(expectedMemoryHits, cache.getStatistics().getInMemoryHits());
170         Assert.assertEquals(ELEMENT_COUNT - expectedMemoryHits, cache.getStatistics().getInMemoryMisses());
171         Assert.assertEquals(expectedDiskHits, cache.getStatistics().getOnDiskHits());
172         Assert.assertEquals(0, cache.getStatistics().getOnDiskMisses());
173     }
174 
175 }
176