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 net.sf.ehcache.config.CacheConfiguration;
20  import net.sf.ehcache.config.Configuration;
21  import net.sf.ehcache.config.DiskStoreConfiguration;
22  import net.sf.ehcache.config.MemoryUnit;
23  import org.junit.After;
24  import org.junit.Before;
25  import org.junit.Test;
26  
27  import static junit.framework.Assert.assertTrue;
28  
29  /***
30   * Tests for Cache copy on r/w with pools
31   *
32   * @author Ludovic Orban
33   */
34  public class CacheCopyOnRwPoolTest {
35  
36      private CacheManager cacheManager;
37  
38      @Before
39      public void setUp() throws Exception {
40          cacheManager = new CacheManager(
41                  new Configuration()
42                          .diskStore(new DiskStoreConfiguration().path(System.getProperty("java.io.tmpdir")))
43                          .maxBytesLocalHeap(50, MemoryUnit.KILOBYTES)
44                          .maxBytesLocalDisk(200, MemoryUnit.KILOBYTES)
45          );
46      }
47  
48      @After
49      public void tearDown() {
50          cacheManager.shutdown();
51          cacheManager = null;
52      }
53  
54      @Test
55      public void testMemoryOnly() throws Exception {
56          cacheManager.addCache(new Cache(
57                  new CacheConfiguration()
58                          .statistics(true)
59                          .name("memoryOnlyCache")
60          ));
61          cacheManager.addCache(new Cache(
62                  new CacheConfiguration()
63                          .statistics(true)
64                          .name("memoryOnlyCache_copy")
65                          .copyOnRead(true)
66                          .copyOnWrite(true)
67          ));
68  
69          Cache cache = cacheManager.getCache("memoryOnlyCache");
70          Cache copyCache = cacheManager.getCache("memoryOnlyCache_copy");
71  
72          cache.put(new Element(1000, 1000));
73          copyCache.put(new Element(1000, 1000));
74  
75          long cacheSize = cache.calculateInMemorySize();
76          System.out.println("cache size : " + cacheSize);
77          assertTrue(cacheSize != 0);
78          long copyCacheSize = copyCache.calculateInMemorySize();
79          System.out.println("copyCache size : " + copyCacheSize);
80          assertTrue(copyCacheSize != 0);
81          assertTrue(cacheSize != copyCacheSize);
82      }
83  
84      @Test
85      public void testOverflowToDisk() throws Exception {
86          cacheManager.addCache(new Cache(
87                  new CacheConfiguration()
88                          .overflowToDisk(true)
89                          .statistics(true)
90                          .name("overflowToDiskCache")
91          ));
92          cacheManager.addCache(new Cache(
93                  new CacheConfiguration()
94                          .overflowToDisk(true)
95                          .statistics(true)
96                          .name("overflowToDiskCache_copy")
97                          .copyOnRead(true)
98                          .copyOnWrite(true)
99          ));
100 
101         Cache cache = cacheManager.getCache("overflowToDiskCache");
102         Cache copyCache = cacheManager.getCache("overflowToDiskCache_copy");
103 
104         cache.put(new Element(1000, 1000));
105         copyCache.put(new Element(1000, 1000));
106 
107         Thread.sleep(1000);
108 
109         long cacheSize = cache.calculateInMemorySize();
110         System.out.println("cache size : " + cacheSize);
111         assertTrue(cacheSize != 0);
112         long copyCacheSize = copyCache.calculateInMemorySize();
113         System.out.println("copyCache size : " + copyCacheSize);
114         assertTrue(copyCacheSize != 0);
115         assertTrue(cacheSize != copyCacheSize);
116 
117         long cacheDiskSize = cache.calculateOnDiskSize();
118         System.out.println("cache disk size : " + cacheDiskSize);
119         assertTrue(cacheDiskSize != 0);
120         long copyCacheDiskSize = copyCache.calculateOnDiskSize();
121         System.out.println("copyCache disk size : " + copyCacheDiskSize);
122         assertTrue(copyCacheDiskSize != 0);
123         assertTrue(cacheDiskSize != copyCacheDiskSize);
124     }
125 
126     @Test
127     public void testDiskPersistent() throws Exception {
128         cacheManager.addCache(new Cache(
129                 new CacheConfiguration()
130                         .overflowToDisk(true)
131                         .diskPersistent(true)
132                         .statistics(true)
133                         .name("diskPersistentCache")
134         ));
135         cacheManager.addCache(new Cache(
136                 new CacheConfiguration()
137                         .overflowToDisk(true)
138                         .diskPersistent(true)
139                         .statistics(true)
140                         .name("diskPersistentCache_copy")
141                         .copyOnRead(true)
142                         .copyOnWrite(true)
143         ));
144 
145         Cache cache = cacheManager.getCache("diskPersistentCache");
146         Cache copyCache = cacheManager.getCache("diskPersistentCache_copy");
147 
148         cache.put(new Element(1000, 1000));
149         copyCache.put(new Element(1000, 1000));
150 
151         Thread.sleep(1000);
152 
153         long cacheSize = cache.calculateInMemorySize();
154         System.out.println("cache size : " + cacheSize);
155         assertTrue(cacheSize != 0);
156         long copyCacheSize = copyCache.calculateInMemorySize();
157         System.out.println("copyCache size : " + copyCacheSize);
158         assertTrue(copyCacheSize != 0);
159         assertTrue(cacheSize != copyCacheSize);
160 
161         long cacheDiskSize = cache.calculateOnDiskSize();
162         System.out.println("cache disk size : " + cacheDiskSize);
163         assertTrue(cacheDiskSize != 0);
164         long copyCacheDiskSize = copyCache.calculateOnDiskSize();
165         System.out.println("copyCache disk size : " + copyCacheDiskSize);
166         assertTrue(copyCacheDiskSize != 0);
167         assertTrue(cacheDiskSize != copyCacheDiskSize);
168     }
169 
170 }
171