001: /*
002: * All content copyright (c) 2003-2006 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 EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
008:
009: import com.tc.object.bytecode.ManagerUtil;
010: import com.tc.object.config.ConfigVisitor;
011: import com.tc.object.config.DSOClientConfigHelper;
012: import com.tc.object.config.TransparencyClassSpec;
013: import com.tc.simulator.app.ApplicationConfig;
014: import com.tc.simulator.listener.ListenerProvider;
015: import com.tc.util.Assert;
016: import com.tc.util.DebugUtil;
017: import com.tc.util.TIMUtil;
018: import com.tcclient.ehcache.TimeExpiryMap;
019: import com.tctest.runner.AbstractTransparentApp;
020:
021: public class TimeExpiryMapTestApp extends AbstractTransparentApp {
022: private final CyclicBarrier barrier;
023: private DataRoot dataRoot = null;
024:
025: public TimeExpiryMapTestApp(String appId, ApplicationConfig cfg,
026: ListenerProvider listenerProvider) {
027: super (appId, cfg, listenerProvider);
028: this .barrier = new CyclicBarrier(getParticipantCount());
029: }
030:
031: public void run() {
032: try {
033: int index = barrier.barrier();
034:
035: basicMapTTLTest(index);
036: basicMapTest(index);
037: expiredItemsTest(index);
038:
039: } catch (Throwable t) {
040: notifyError(t);
041: }
042: }
043:
044: private void basicMapTTLTest(int index) throws Exception {
045: DebugUtil.DEBUG = true;
046: if (index == 0) {
047: dataRoot = new DataRoot();
048: dataRoot.setMap(new MockTimeExpiryMap(3, 50, 5));
049: }
050:
051: barrier.barrier();
052:
053: long st = System.currentTimeMillis();
054: if (index == 0) {
055: dataRoot.put("key1", "val1");
056: dataRoot.put("key2", "val2");
057: dataRoot.put("key3", "val3");
058: }
059:
060: barrier.barrier();
061:
062: long en = System.currentTimeMillis();
063: if (DebugUtil.DEBUG) {
064: System.err.println("Client " + ManagerUtil.getClientID()
065: + ", index: " + index + " time for sleep: "
066: + (en - st));
067: }
068: Assert.assertEquals(3, dataRoot.size());
069: Assert.assertFalse(dataRoot.isExpired("key1"));
070: Assert.assertFalse(dataRoot.isExpired("key2"));
071: Assert.assertFalse(dataRoot.isExpired("key3"));
072: Assert.assertEquals("val1", dataRoot.get("key1"));
073: Assert.assertEquals("val2", dataRoot.get("key2"));
074: Assert.assertEquals("val3", dataRoot.get("key3"));
075:
076: barrier.barrier();
077:
078: Thread.sleep(10000);
079:
080: barrier.barrier();
081:
082: Assert.assertTrue(dataRoot.isExpired("key1"));
083: Assert.assertTrue(dataRoot.isExpired("key2"));
084: Assert.assertTrue(dataRoot.isExpired("key3"));
085: Assert.assertEquals(0, dataRoot.size());
086: Assert.assertEquals(3, dataRoot.getNumOfExpired());
087:
088: barrier.barrier();
089: DebugUtil.DEBUG = false;
090: }
091:
092: private void basicMapTest(int index) throws Exception {
093: DebugUtil.DEBUG = true;
094:
095: if (index == 0) {
096: dataRoot.setMap(new MockTimeExpiryMap(1, 5, 10));
097: }
098:
099: barrier.barrier();
100:
101: if (index == 0) {
102: dataRoot.put("key1", "val1");
103: dataRoot.put("key2", "val2");
104: dataRoot.put("key3", "val3");
105: }
106:
107: barrier.barrier();
108:
109: Assert.assertEquals(3, dataRoot.size());
110: Assert.assertEquals("val1", dataRoot.get("key1"));
111: Assert.assertEquals("val2", dataRoot.get("key2"));
112: Assert.assertEquals("val3", dataRoot.get("key3"));
113:
114: barrier.barrier();
115:
116: Thread.sleep(10000);
117:
118: barrier.barrier();
119:
120: Assert.assertEquals(0, dataRoot.size());
121:
122: barrier.barrier();
123:
124: Assert.assertEquals(3, dataRoot.getNumOfExpired());
125:
126: barrier.barrier();
127:
128: DebugUtil.DEBUG = false;
129: }
130:
131: private void expiredItemsTest(int index) throws Exception {
132: DebugUtil.DEBUG = true;
133: if (index == 0) {
134: dataRoot.setMap(new MockTimeExpiryMap(1, 5, 10));
135: }
136:
137: barrier.barrier();
138:
139: if (index == 1) {
140: dataRoot.put("key1", "val1");
141: dataRoot.put("key2", "val2");
142: dataRoot.put("key3", "val3");
143: }
144:
145: barrier.barrier();
146:
147: Assert.assertEquals(3, dataRoot.size());
148: Assert.assertEquals("val1", dataRoot.get("key1"));
149: Assert.assertEquals("val2", dataRoot.get("key2"));
150: Assert.assertEquals("val3", dataRoot.get("key3"));
151:
152: barrier.barrier();
153:
154: Thread.sleep(3000);
155:
156: if (index == 0) {
157: Assert.assertEquals("val3", dataRoot.get("key3"));
158:
159: Thread.sleep(4000);
160: }
161:
162: barrier.barrier();
163:
164: if (index == 0) {
165: Assert.assertEquals(2, dataRoot.getNumOfExpired());
166: Assert.assertEquals(null, dataRoot.get("key1"));
167: Assert.assertEquals(null, dataRoot.get("key2"));
168: }
169:
170: barrier.barrier();
171: DebugUtil.DEBUG = false;
172: }
173:
174: public static void visitL1DSOConfig(ConfigVisitor visitor,
175: DSOClientConfigHelper config) {
176: TransparencyClassSpec spec = config
177: .getOrCreateSpec(CyclicBarrier.class.getName());
178: config.addWriteAutolock("* " + CyclicBarrier.class.getName()
179: + "*.*(..)");
180:
181: String testClass = TimeExpiryMapTestApp.class.getName();
182: spec = config.getOrCreateSpec(testClass);
183: config.addIncludePattern(testClass + "$DataRoot");
184: config.addIncludePattern(testClass + "$MockTimeExpiryMap");
185: String methodExpression = "* " + testClass + "*.*(..)";
186: config.addWriteAutolock(methodExpression);
187:
188: spec.addRoot("barrier", "barrier");
189: spec.addRoot("dataRoot", "dataRoot");
190:
191: config.addModule(TIMUtil.EHCACHE_1_2_4, TIMUtil
192: .getVersion(TIMUtil.EHCACHE_1_2_4)); // this is just a quick way
193: // to add TimeExpiryMap to
194: // the instrumentation list
195: }
196:
197: private static class DataRoot {
198: private MockTimeExpiryMap map;
199:
200: public DataRoot() {
201: super ();
202: }
203:
204: public synchronized void put(Object key, Object val) {
205: map.put(key, val);
206: }
207:
208: public synchronized Object get(Object key) {
209: return map.get(key);
210: }
211:
212: public synchronized int size() {
213: return map.size();
214: }
215:
216: public synchronized int getNumOfExpired() {
217: return map.getNumOfExpired();
218: }
219:
220: public synchronized void setMap(MockTimeExpiryMap map) {
221: if (this .map != null) {
222: this .map.stopTimeMonitoring();
223: }
224: this .map = map;
225: this .map.initialize();
226: }
227:
228: public synchronized boolean isExpired(Object key) {
229: return map.isExpired(key);
230: }
231: }
232:
233: private static class MockTimeExpiryMap extends TimeExpiryMap {
234: private int numOfExpired = 0;
235:
236: public MockTimeExpiryMap(int invalidatorSleepSeconds,
237: int maxIdleTimeoutSeconds, int maxTTLSeconds) {
238: super (invalidatorSleepSeconds, maxIdleTimeoutSeconds,
239: maxTTLSeconds, "MockCache");
240: }
241:
242: protected final synchronized void processExpired(Object key) {
243: numOfExpired++;
244: if (DebugUtil.DEBUG) {
245: System.err.println("Client "
246: + ManagerUtil.getClientID()
247: + " expiring ... key: " + key
248: + ", numOfExpired: " + numOfExpired);
249: }
250: }
251:
252: public synchronized int getNumOfExpired() {
253: return this.numOfExpired;
254: }
255: }
256:
257: }
|