1 8 9 package org.jboss.test.cache.test.standAloneAop; 10 11 import junit.framework.Test; 12 import junit.framework.TestCase; 13 import junit.framework.TestSuite; 14 import org.jboss.cache.CacheException; 15 import org.jboss.cache.PropertyConfigurator; 16 import org.jboss.cache.TreeCache; 17 import org.jboss.cache.aop.TreeCacheAop; 18 import org.jboss.cache.lock.IsolationLevel; 19 import org.jboss.cache.lock.LockStrategyFactory; 20 import org.jboss.cache.transaction.DummyTransactionManager; 21 22 import javax.naming.Context ; 23 import javax.transaction.*; 24 import java.util.ArrayList ; 25 import java.util.Properties ; 26 import java.util.Random ; 27 28 29 36 public class LocalConcurrentTestCase extends TestCase 37 { 38 static TreeCacheAop cache_; 39 int cachingMode_ = TreeCache.LOCAL; 40 final static Properties p_; 41 String oldFactory_ = null; 42 final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; 43 static ArrayList nodeList_; 44 static final int depth_ = 3; 45 static final int children_ = 3; 46 DummyTransactionManager tm_; 47 static final int MAX_LOOP = 100; 48 static final int SLEEP_TIME = 50; 49 static Exception thread_ex=null; 50 51 static 52 { 53 p_ = new Properties (); 54 p_.put(Context.INITIAL_CONTEXT_FACTORY, 55 "org.jboss.cache.transaction.DummyContextFactory"); 56 } 57 58 public LocalConcurrentTestCase(String name) 59 { 60 super(name); 61 } 62 63 public void setUp() throws Exception 64 { 65 super.setUp(); 66 67 oldFactory_ = System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 68 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY); 69 70 DummyTransactionManager.getInstance(); 71 initCaches(TreeCache.LOCAL); 72 nodeList_ = nodeGen(depth_, children_); 73 tm_ = new DummyTransactionManager(); 74 75 log("LocalConcurrentTestCase: cacheMode=TRANSIENT, one cache"); 76 } 77 78 public void tearDown() throws Exception 79 { 80 super.tearDown(); 81 thread_ex=null; 82 DummyTransactionManager.destroy(); 83 destroyCaches(); 84 85 if (oldFactory_ != null) { 86 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, oldFactory_); 87 oldFactory_ = null; 88 } 89 90 } 91 92 void initCaches(int caching_mode) throws Exception 93 { 94 cachingMode_ = caching_mode; 95 cache_ = new TreeCacheAop(); 96 PropertyConfigurator config = new PropertyConfigurator(); 97 config.configure(cache_, "META-INF/local-aop-eviction-service.xml"); cache_.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 99 cache_.createService(); 100 cache_.startService(); 101 } 102 103 void destroyCaches() throws Exception 104 { 105 cache_.stopService(); 106 cache_ = null; 107 } 108 109 protected void setLevelRW() 110 { 111 log("set lock level to RWUpgrade ..."); 112 LockStrategyFactory.setIsolationLevel(IsolationLevel.REPEATABLE_READ); 113 } 114 115 protected void setLevelSerial() 116 { 117 log("set lock level to SimpleLock ..."); 118 LockStrategyFactory.setIsolationLevel(IsolationLevel.SERIALIZABLE); 119 } 120 121 public void testAll_RWLock() throws Exception 122 { 123 setLevelRW(); 124 all(); 125 } 126 127 public void XtestAll_SimpleLock() throws Exception 128 { 129 setLevelSerial(); 130 all(); 131 } 132 133 private void all() throws Exception 134 { 135 RunThread t1 = new RunThread(1); 136 RunThread t2 = new RunThread(2); 137 RunThread t3 = new RunThread(3); 138 RunThread t4 = new RunThread(4); 139 140 t1.start(); 141 t2.start(); 142 t3.start(); 143 t4.start(); 144 145 t1.join(60000); t2.join(60000); t3.join(60000); t4.join(60000); 150 if(thread_ex != null) 151 throw thread_ex; 152 } 153 154 static class RunThread extends Thread 155 { 156 final int seed_; 157 Random random_; 158 Person person_; 159 160 public RunThread(int seed) 161 { 162 seed_ = seed; 163 random_ = new Random (seed); 164 createPerson(); 165 } 166 167 private void createPerson() { 168 person_ = new Person(); 169 person_.setName("Ben"); 170 person_.setAge(18); 171 ArrayList lang = new ArrayList (); 172 lang.add("English"); 173 lang.add("French"); 174 lang.add("Mandarin"); 175 person_.setLanguages(lang); 176 Address addr = new Address(); 177 addr.setZip(95123); 178 addr.setStreet("Almeria"); 179 addr.setCity("San Jose"); 180 person_.setAddress(addr); 181 } 182 183 public void run() { 184 try { 185 _run(); 186 } 187 catch(Exception e) { 188 thread_ex=e; 189 } 190 } 191 192 public void _run() throws HeuristicMixedException, SystemException, 193 NotSupportedException, HeuristicRollbackException, RollbackException, CacheException { 194 for (int loop = 0; loop < MAX_LOOP; loop++) { 195 sleep_(random_.nextInt(50)); 196 log("Executing op1. Loop-" + loop); 197 op1(); 198 sleep_(random_.nextInt(50)); 199 log("Executing op2..."); 200 op2(); 201 sleep_(random_.nextInt(50)); 202 log("Executing op3..."); 203 op3(); 204 } 205 } 206 207 private void op1() throws SystemException, NotSupportedException, CacheException, 209 HeuristicMixedException, HeuristicRollbackException, RollbackException { 210 int i = random_.nextInt(nodeList_.size() - 1); 211 212 String node = (String ) nodeList_.get(i); 213 cache_.getObject(node); 214 sleep_(random_.nextInt(SLEEP_TIME)); cache_.putObject(node, person_); 216 sleep_(random_.nextInt(SLEEP_TIME)); cache_.removeObject(node); 218 } 219 220 private void op2() throws SystemException, NotSupportedException, CacheException, 222 HeuristicMixedException, HeuristicRollbackException, RollbackException { 223 int i = random_.nextInt(nodeList_.size() - 1); 224 225 String node = (String ) nodeList_.get(i); 226 cache_.getObject(node); 227 sleep_(random_.nextInt(SLEEP_TIME)); cache_.getObject(node); 229 sleep_(random_.nextInt(SLEEP_TIME)); } 231 232 private void op3() throws SystemException, NotSupportedException, CacheException, 234 HeuristicMixedException, HeuristicRollbackException, RollbackException { 235 int i = random_.nextInt(nodeList_.size() - 1); 236 237 String node = (String ) nodeList_.get(i); 238 cache_.putObject(node, person_); 239 sleep_(random_.nextInt(SLEEP_TIME)); cache_.removeObject(node); 241 } 242 243 private void sleep_(long msecs) 244 { 245 try { 246 Thread.sleep(msecs); 247 } catch (Exception ex) { 248 } 249 } 250 } 251 252 257 private ArrayList nodeGen(int depth, int children) 258 { 259 ArrayList strList = new ArrayList (); 260 ArrayList oldList = new ArrayList (); 261 ArrayList newList = new ArrayList (); 262 263 oldList.add("/"); 265 newList.add("/"); 266 strList.add("/"); 267 268 while (depth > 0) { 269 newList = new ArrayList (); 271 for (int i = 0; i < oldList.size(); i++) { 272 for (int j = 0; j < children; j++) { 273 String tmp = (String ) oldList.get(i); 274 tmp += Integer.toString(j); 275 if (depth != 1) { 276 tmp += "/"; 277 } 278 279 newList.add(tmp); 280 } 281 } 282 strList.addAll(newList); 283 oldList = newList; 284 depth--; 285 } 286 287 for(int i=0; i < strList.size(); i++) { 289 if( ((String )strList.get(i)).equals("/") ) { 290 strList.remove(i); 291 break; 292 } 293 } 294 log("Nodes generated: " + strList.size()); 295 return strList; 296 } 297 298 public static Test suite() throws Exception 299 { 300 return new TestSuite(LocalConcurrentTestCase.class); 301 } 302 303 private static void log(String str) 304 { 305 System.out.println("Thread: " + Thread.currentThread() + ": " + str); 306 } 308 309 } 310 | Popular Tags |