001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest;
006:
007: import net.sf.ehcache.Cache;
008: import net.sf.ehcache.CacheManager;
009: import net.sf.ehcache.Element;
010: import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
011:
012: import com.tc.simulator.app.ApplicationConfig;
013: import com.tc.simulator.listener.ListenerProvider;
014: import com.tc.util.Assert;
015: import com.tctest.runner.AbstractErrorCatchingTransparentApp;
016:
017: import java.util.Arrays;
018:
019: public abstract class CacheEvictorTestApp extends
020: AbstractErrorCatchingTransparentApp {
021:
022: public CacheEvictorTestApp(String appId, ApplicationConfig cfg,
023: ListenerProvider listenerProvider) {
024: super (appId, cfg, listenerProvider);
025: }
026:
027: protected void runTest() throws Throwable {
028: System.out.println("Cache names: "
029: + Arrays.asList(getCacheManger().getCacheNames()));
030:
031: try {
032: testIsElementInMemory();
033: testIsKeyInCache();
034: testIsValueInCache();
035: testElementExpired();
036: testRemove();
037:
038: testIdleAndTimeToLive("cache1", 5, 10, 20);
039: testIdleAndTimeToLive("cache2", 10, 0, 20);
040: testIdleAndTimeToLive("cache3", 0, 10, 20);
041: testIdleAndTimeToLive("cache4", 0, 0, 20);
042:
043: barrier();
044: } finally {
045: getCacheManger().shutdown();
046: }
047:
048: }
049:
050: protected abstract int barrier() throws Exception;
051:
052: protected abstract CacheManager getCacheManger();
053:
054: private void testIsElementInMemory() throws Exception {
055: Cache cache = getCacheManger().getCache("sampleCache1");
056: populateCache(cache);
057: barrier();
058:
059: Assert.assertTrue(cache.isElementInMemory("k1"));
060: Assert.assertTrue(cache.isElementInMemory("k2"));
061: Assert.assertTrue(cache.isElementInMemory("k3"));
062: }
063:
064: private void populateCache(Cache cache) throws Exception {
065: if (barrier() == 0) {
066: cache.removeAll();
067: cache.put(new Element("k1", "v1"));
068: cache.put(new Element("k2", "v2"));
069: cache.put(new Element("k3", "v3"));
070: }
071: }
072:
073: private void testIsKeyInCache() throws Exception {
074: Cache cache = getCacheManger().getCache("sampleCache1");
075: populateCache(cache);
076: barrier();
077:
078: Assert.assertTrue(cache.isKeyInCache("k1"));
079: Assert.assertTrue(cache.isKeyInCache("k2"));
080: Assert.assertTrue(cache.isKeyInCache("k3"));
081: }
082:
083: private void testIsValueInCache() throws Exception {
084: Cache cache = getCacheManger().getCache("sampleCache1");
085: populateCache(cache);
086: barrier();
087:
088: Assert.assertTrue(cache.isValueInCache("v1"));
089: Assert.assertTrue(cache.isValueInCache("v2"));
090: Assert.assertTrue(cache.isValueInCache("v3"));
091: }
092:
093: private void testRemove() throws Exception {
094: Cache cache = getCacheManger().getCache("sampleCache1");
095: populateCache(cache);
096:
097: if (barrier() == 0) {
098: Assert.assertTrue(cache.remove("k1"));
099: Assert.assertTrue(cache.remove("k2"));
100: }
101: barrier();
102:
103: Assert.assertFalse(cache.isElementInMemory("k1"));
104: Assert.assertFalse(cache.isElementInMemory("k2"));
105: Assert.assertTrue(cache.isElementInMemory("k3"));
106:
107: Assert.assertFalse(cache.remove("k1"));
108: Assert.assertFalse(cache.remove("k2"));
109:
110: }
111:
112: private void testElementExpired() throws Exception {
113: Cache cache = getCacheManger().getCache("sampleCache1");
114: populateCache(cache);
115: barrier();
116:
117: Element e1 = cache.get("k1");
118: Element e2 = cache.get("k2");
119: Element e3 = cache.get("k3");
120: long timeout = System.currentTimeMillis() + (40 * 1000);
121: while (System.currentTimeMillis() < timeout) {
122: cache.get("k1");
123: cache.get("k2");
124: Thread.sleep(100);
125: }
126:
127: // k3,v3 should expire due to timeToIdleSeconds=15
128: System.out.println("cache: " + cache);
129: System.out.println("get k1: " + cache.get("k1"));
130: System.out.println("get k2: " + cache.get("k2"));
131: System.out.println("get k3: " + cache.get("k3"));
132:
133: assertExpired(cache, e3);
134: assertInCache(cache, e1);
135: assertInCache(cache, e2);
136:
137: timeout = System.currentTimeMillis() + (80 * 1000);
138: while (System.currentTimeMillis() < timeout) {
139: cache.get("k1");
140: Thread.sleep(100);
141: }
142:
143: System.out.println("get k1: " + cache.get("k1"));
144: System.out.println("get k2: " + cache.get("k2"));
145: System.out.println("get k3: " + cache.get("k3"));
146:
147: // both (k1,v2) and (k2,v2) should expired due to timeToLiveSeconds=60
148: assertExpired(cache, e1);
149: assertExpired(cache, e2);
150: assertExpired(cache, e3);
151: }
152:
153: private void testIdleAndTimeToLive(String cacheName, int ttl,
154: int idle, int sleep) throws Exception {
155: if (barrier() == 0) {
156: getCacheManger().removalAll();
157: Cache newCache = new Cache(cacheName, 3,
158: MemoryStoreEvictionPolicy.LFU, false, ".", false,
159: ttl, idle, false, 120, null, null, 100);
160: getCacheManger().addCache(newCache);
161: System.out.println("newCache: " + newCache);
162: }
163:
164: barrier();
165:
166: Cache cache = getCacheManger().getCache(cacheName);
167: populateCache(cache);
168: barrier();
169:
170: System.out.println("get k1: " + cache.get("k1"));
171: System.out.println("get k2: " + cache.get("k2"));
172: System.out.println("get k3: " + cache.get("k3"));
173:
174: Element e1 = cache.get("k1");
175: Element e2 = cache.get("k2");
176: Element e3 = cache.get("k3");
177:
178: assertInCache(cache, e1);
179: assertInCache(cache, e2);
180: assertInCache(cache, e3);
181:
182: Thread.sleep(sleep * 1000);
183:
184: if (idle == 0 && ttl == 0) { // nothing expired ever
185: Assert.assertNotNull(cache.get("k1"));
186: Assert.assertNotNull(cache.get("k2"));
187: Assert.assertNotNull(cache.get("k3"));
188: } else {
189: Assert.assertNull(cache.get("k1"));
190: Assert.assertNull(cache.get("k2"));
191: Assert.assertNull(cache.get("k3"));
192: }
193: }
194:
195: private void assertInCache(Cache cache, Element e) throws Exception {
196: Assert.assertTrue("Should be in cache", cache.isKeyInCache(e
197: .getKey()));
198: Assert.assertTrue("Should be in memory", cache
199: .isElementInMemory(e.getKey()));
200: Assert.assertFalse("Should not expire", cache.isExpired(e));
201: }
202:
203: private void assertExpired(Cache cache, Element e) throws Exception {
204: Assert.assertFalse("Should not be in cache", cache
205: .isKeyInCache(e.getKey()));
206: Assert.assertFalse("Should not be in memory", cache
207: .isElementInMemory(e.getKey()));
208: Assert.assertTrue("Should expired", cache.isExpired(e));
209: }
210: }
|