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:
011: import org.apache.commons.io.FileUtils;
012: import org.apache.log4j.ConsoleAppender;
013: import org.apache.log4j.Logger;
014: import org.apache.log4j.PatternLayout;
015:
016: import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
017:
018: import com.tc.objectserver.control.ExtraL1ProcessControl;
019: import com.tc.simulator.app.ApplicationConfig;
020: import com.tc.simulator.listener.ListenerProvider;
021: import com.tc.util.Assert;
022: import com.tc.util.DebugUtil;
023:
024: import java.io.File;
025: import java.util.ArrayList;
026: import java.util.Collections;
027: import java.util.List;
028:
029: public abstract class EhcacheGlobalEvictionTestApp extends
030: ServerCrashingAppBase {
031: private final static int NUM_OF_L1 = 2;
032:
033: public EhcacheGlobalEvictionTestApp(String appId,
034: ApplicationConfig cfg, ListenerProvider listenerProvider) {
035: super (appId, cfg, listenerProvider);
036: }
037:
038: public void runTest() throws Throwable {
039: basicGlobalEvictionTest();
040: }
041:
042: private void basicGlobalEvictionTest() throws Exception {
043: DebugUtil.DEBUG = true;
044:
045: final List jvmArgs = new ArrayList();
046: final List errorList = Collections
047: .synchronizedList(new ArrayList());
048: final L1ClientWrapper l1Wrapper = new L1ClientWrapper(
049: getHostName(), getPort(), new File(getConfigFilePath()));
050:
051: addTestTcPropertiesFile(jvmArgs);
052:
053: Thread t1 = new Thread(new Runnable() {
054: public void run() {
055: try {
056: l1Wrapper.spawn("0", L1Client.class,
057: new String[] { "0" }, jvmArgs);
058: } catch (Exception e) {
059: errorList.add(e);
060: }
061: }
062: });
063:
064: Thread t2 = new Thread(new Runnable() {
065: public void run() {
066: try {
067: l1Wrapper.spawn("1", L1Client.class,
068: new String[] { "1" }, jvmArgs);
069: } catch (Exception e) {
070: errorList.add(e);
071: }
072: }
073: });
074:
075: t1.start();
076: t2.start();
077:
078: Thread.sleep(60000L);
079:
080: t1.join();
081: t2.join();
082:
083: if (errorList.size() > 0) {
084: throw (Exception) errorList.get(0);
085: }
086:
087: DebugUtil.DEBUG = false;
088: }
089:
090: public static class L1Client {
091: private CyclicBarrier barrier = new CyclicBarrier(NUM_OF_L1); // root
092: private CacheManager cacheManager; // root
093: private int index;
094:
095: public L1Client(int index) {
096: this .index = index;
097: if (index == 0) {
098: cacheManager = getCacheManager();
099: }
100: }
101:
102: public static void main(String args[]) throws Exception {
103: Logger rootLogger = Logger.getRootLogger();
104: if (!rootLogger.getAllAppenders().hasMoreElements()) {
105: rootLogger.setLevel(org.apache.log4j.Level.DEBUG);
106: rootLogger.addAppender(new ConsoleAppender(
107: new PatternLayout("%-5p [%t]: %m%n")));
108: }
109:
110: DebugUtil.DEBUG = true;
111:
112: int index = Integer.parseInt(args[0]);
113: L1Client l1 = new L1Client(index);
114: l1.execute();
115:
116: DebugUtil.DEBUG = false;
117: }
118:
119: public void execute() throws Exception {
120: barrier.barrier();
121: Cache cache = cacheManager.getCache("sampleCache1");
122: populateCache(cache, index, 1);
123: barrier.barrier();
124:
125: Thread.sleep(1000);
126: Assert.assertEquals(new Element("key01", "val01"), cache
127: .get("key01"));
128: Assert.assertEquals(new Element("key02", "val02"), cache
129: .get("key02"));
130: Assert.assertEquals(new Element("key03", "val03"), cache
131: .get("key03"));
132: Assert.assertEquals(new Element("key11", "val11"), cache
133: .get("key11"));
134: Assert.assertEquals(new Element("key12", "val12"), cache
135: .get("key12"));
136: Assert.assertEquals(new Element("key13", "val13"), cache
137: .get("key13"));
138: Assert.assertEquals(6, cache.getSize());
139:
140: Thread.sleep(30000);
141: Assert.assertTrue(cache.isExpired(new Element("key01",
142: "val01")));
143: Assert.assertTrue(cache.isExpired(new Element("key02",
144: "val02")));
145: Assert.assertTrue(cache.isExpired(new Element("key03",
146: "val03")));
147: Assert.assertTrue(cache.isExpired(new Element("key11",
148: "val11")));
149: Assert.assertTrue(cache.isExpired(new Element("key12",
150: "val12")));
151: Assert.assertTrue(cache.isExpired(new Element("key13",
152: "val13")));
153: Assert.assertEquals(0, cache.getSize());
154:
155: barrier.barrier();
156:
157: populateCache(cache, index, 4);
158: barrier.barrier();
159:
160: Assert.assertEquals(new Element("key04", "val04"), cache
161: .get("key04"));
162: Assert.assertEquals(new Element("key05", "val05"), cache
163: .get("key05"));
164: Assert.assertEquals(new Element("key06", "val06"), cache
165: .get("key06"));
166: Assert.assertEquals(new Element("key14", "val14"), cache
167: .get("key14"));
168: Assert.assertEquals(new Element("key15", "val15"), cache
169: .get("key15"));
170: Assert.assertEquals(new Element("key16", "val16"), cache
171: .get("key16"));
172: Assert.assertEquals(6, cache.getSize());
173:
174: barrier.barrier();
175:
176: if (index == 0) {
177: Thread.sleep(30000);
178:
179: Assert.assertTrue(cache.isExpired(new Element("key04",
180: "val04")));
181: Assert.assertTrue(cache.isExpired(new Element("key05",
182: "val05")));
183: Assert.assertTrue(cache.isExpired(new Element("key06",
184: "val06")));
185: Assert.assertTrue(cache.isExpired(new Element("key14",
186: "val14")));
187: Assert.assertTrue(cache.isExpired(new Element("key15",
188: "val15")));
189: Assert.assertTrue(cache.isExpired(new Element("key16",
190: "val16")));
191:
192: System.out.println("Cache content: " + cache);
193:
194: Assert.assertEquals(0, cache.getSize());
195: }
196: }
197:
198: private CacheManager getCacheManager() {
199: return CacheManager.create(getClass().getResource(
200: "cache-global-evictor-test.xml"));
201: }
202:
203: private void populateCache(Cache cache, int index,
204: int startValue) throws Exception {
205: cache.put(new Element("key" + index + startValue, "val"
206: + index + startValue));
207: cache.put(new Element("key" + index + (startValue + 1),
208: "val" + index + (startValue + 1)));
209: cache.put(new Element("key" + index + (startValue + 2),
210: "val" + index + (startValue + 2)));
211: }
212: }
213:
214: private static class L1ClientWrapper {
215: private String hostname;
216: private int port;
217: private File configFile;
218:
219: public L1ClientWrapper(String hostname, int port,
220: File configFile) {
221: this .hostname = hostname;
222: this .port = port;
223: this .configFile = configFile;
224: }
225:
226: public void spawn(String clientId, Class clientClass,
227: String[] mainArgs, List jvmArgs) throws Exception {
228: ExtraL1ProcessControl client = spawnNewClient(clientId,
229: clientClass, mainArgs, jvmArgs);
230: if (client.waitFor() != 0) {
231: throw new Exception(clientClass.getName()
232: + " exited with non zero code");
233: }
234: }
235:
236: protected ExtraL1ProcessControl spawnNewClient(String clientId,
237: Class clientClass, String[] mainArgs, List jvmArgs)
238: throws Exception {
239:
240: File workingDir = new File(configFile.getParentFile(),
241: "client-" + clientId);
242: FileUtils.forceMkdir(workingDir);
243:
244: ExtraL1ProcessControl client = new ExtraL1ProcessControl(
245: hostname, port, clientClass, configFile
246: .getAbsolutePath(), mainArgs, workingDir,
247: jvmArgs);
248: client.start();
249: System.err.println("\n### Started New Client");
250:
251: client.mergeSTDERR();
252: client.mergeSTDOUT();
253:
254: return client;
255: }
256: }
257:
258: }
|