1 8 9 package org.jboss.cache.tests.aop; 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.naming.InitialContext ; 24 import javax.transaction.UserTransaction ; 25 import java.util.ArrayList ; 26 import java.util.Properties ; 27 import java.util.Random ; 28 29 30 37 public class LocalConcurrentTest extends TestCase 38 { 39 static TreeCacheAop cache_; 40 int cachingMode_ = TreeCache.LOCAL; 41 Properties p_; 42 String oldFactory_ = null; 43 final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; 44 static ArrayList nodeList_; 45 static final int depth_ = 3; 46 static final int children_ = 3; 47 static final int MAX_LOOP = 100; 48 static final int SLEEP_TIME = 50; 49 static Exception thread_ex=null; 50 UserTransaction tx_ = null; 51 52 public LocalConcurrentTest(String name) 53 { 54 super(name); 55 } 56 57 public void setUp() throws Exception 58 { 59 super.setUp(); 60 oldFactory_=System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 61 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY); 62 DummyTransactionManager.getInstance(); 63 if(p_ == null) { 64 p_=new Properties (); 65 p_.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory"); 66 } 67 68 tx_ = (UserTransaction )new InitialContext (p_).lookup("UserTransaction"); 69 70 initCaches(TreeCache.LOCAL); 71 nodeList_ = nodeGen(depth_, children_); 72 73 log("LocalConcurrentTestCase: cacheMode=TRANSIENT, one cache"); 74 } 75 76 public void tearDown() throws Exception 77 { 78 super.tearDown(); 79 thread_ex=null; 80 DummyTransactionManager.destroy(); 81 destroyCaches(); 82 83 if (oldFactory_ != null) { 84 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, oldFactory_); 85 oldFactory_ = null; 86 } 87 88 } 89 90 void initCaches(int caching_mode) throws Exception 91 { 92 cachingMode_ = caching_mode; 93 cache_ = new TreeCacheAop(); 94 PropertyConfigurator config = new PropertyConfigurator(); 95 config.configure(cache_, "META-INF/local-aop-eviction-service.xml"); cache_.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 97 cache_.createService(); 98 cache_.startService(); 99 } 100 101 void destroyCaches() throws Exception 102 { 103 cache_.stopService(); 104 cache_ = null; 105 } 106 107 protected void setLevelRW() 108 { 109 log("set lock level to RWUpgrade ..."); 110 LockStrategyFactory.setIsolationLevel(IsolationLevel.REPEATABLE_READ); 111 cache_.setIsolationLevel(IsolationLevel.REPEATABLE_READ); 112 } 113 114 protected void setLevelSerial() 115 { 116 log("set lock level to SimpleLock ..."); 117 LockStrategyFactory.setIsolationLevel(IsolationLevel.SERIALIZABLE); 118 cache_.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 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 } 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 198 public void _run() throws Exception { 199 for (int loop = 0; loop < MAX_LOOP; loop++) { 200 tx_.begin(); 201 createPerson(); 202 sleep_(random_.nextInt(50)); 203 op1(); 204 sleep_(random_.nextInt(50)); 207 op3(); 208 tx_.commit(); 209 } 210 } 211 212 private void op1() throws CacheException { 214 int i = random_.nextInt(nodeList_.size() - 1); 215 216 String node = (String ) nodeList_.get(i) + "/aop"; 217 cache_.putObject(node, person_); 218 sleep_(random_.nextInt(SLEEP_TIME)); cache_.getObject(node); 220 sleep_(random_.nextInt(SLEEP_TIME)); cache_.removeObject(node); 222 } 223 224 private void op2() throws CacheException { 226 int i = random_.nextInt(nodeList_.size() - 1); 227 228 String node = (String ) nodeList_.get(i) + "/aop"; 229 cache_.getObject(node); 230 sleep_(random_.nextInt(SLEEP_TIME)); cache_.getObject(node); 232 sleep_(random_.nextInt(SLEEP_TIME)); } 234 235 private void op3() throws CacheException { 237 int i = random_.nextInt(nodeList_.size() - 1); 238 239 String node = (String ) nodeList_.get(i) + "/aop"; 240 cache_.putObject(node, person_); 241 sleep_(random_.nextInt(SLEEP_TIME)); cache_.removeObject(node); 243 } 244 245 private void sleep_(long msecs) 246 { 247 try { 248 Thread.sleep(msecs); 249 } catch (Exception ex) { 250 } 251 } 252 } 253 254 259 private ArrayList nodeGen(int depth, int children) 260 { 261 ArrayList strList = new ArrayList (); 262 ArrayList oldList = new ArrayList (); 263 ArrayList newList = new ArrayList (); 264 265 oldList.add("/"); 267 newList.add("/"); 268 strList.add("/"); 269 270 while (depth > 0) { 271 newList = new ArrayList (); 273 for (int i = 0; i < oldList.size(); i++) { 274 for (int j = 0; j < children; j++) { 275 String tmp = (String ) oldList.get(i); 276 tmp += Integer.toString(j); 277 if (depth != 1) { 278 tmp += "/"; 279 } 280 281 newList.add(tmp); 282 } 283 } 284 strList.addAll(newList); 285 oldList = newList; 286 depth--; 287 } 288 289 for(int i=0; i < strList.size(); i++) { 291 if( strList.get(i).equals("/") ) { 292 strList.remove(i); 293 break; 294 } 295 } 296 log("Nodes generated: " + strList.size()); 297 return strList; 298 } 299 300 public static Test suite() throws Exception 301 { 302 return new TestSuite(LocalConcurrentTest.class); 303 } 304 305 private static void log(String str) 306 { 307 System.out.println("Thread: " + Thread.currentThread() + ": " + str); 308 } 310 311 } 312 | Popular Tags |