1 8 9 package org.jboss.cache.pojo; 10 11 import junit.framework.Test; 12 import junit.framework.TestCase; 13 import junit.framework.TestSuite; 14 import org.jboss.cache.config.Configuration; 15 import org.jboss.cache.lock.UpgradeException; 16 import org.jboss.cache.pojo.test.Address; 17 import org.jboss.cache.pojo.test.Person; 18 import org.jboss.cache.transaction.DummyTransactionManager; 19 20 import javax.naming.Context ; 21 import javax.naming.InitialContext ; 22 import javax.transaction.UserTransaction ; 23 import java.util.ArrayList ; 24 import java.util.Properties ; 25 import java.util.Random ; 26 27 28 35 public class LocalConcurrentTest extends TestCase 36 { 37 static PojoCache cache_; 38 Configuration.CacheMode cachingMode_ = Configuration.CacheMode.LOCAL; 39 Properties p_; 40 String oldFactory_ = null; 41 final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; 42 static ArrayList nodeList_; 43 static final int depth_ = 2; 44 static final int children_ = 2; 45 static final int MAX_LOOP = 100; 46 static final int SLEEP_TIME = 50; 47 static Exception thread_ex = null; 48 UserTransaction tx_ = null; 49 50 public LocalConcurrentTest(String name) 51 { 52 super(name); 53 } 54 55 public void setUp() throws Exception 56 { 57 super.setUp(); 58 oldFactory_ = System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 59 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY); 60 DummyTransactionManager.getInstance(); 61 if (p_ == null) 62 { 63 p_ = new Properties (); 64 p_.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory"); 65 } 66 67 tx_ = (UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 68 69 initCaches(Configuration.CacheMode.LOCAL); 70 nodeList_ = nodeGen(depth_, children_); 71 72 log("LocalConcurrentTestCase: cacheMode=TRANSIENT, one cache"); 73 } 74 75 public void tearDown() throws Exception 76 { 77 super.tearDown(); 78 thread_ex = null; 79 DummyTransactionManager.destroy(); 80 destroyCaches(); 81 82 if (oldFactory_ != null) 83 { 84 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, oldFactory_); 85 oldFactory_ = null; 86 } 87 88 } 89 90 void initCaches(Configuration.CacheMode caching_mode) throws Exception 91 { 92 cachingMode_ = caching_mode; 93 boolean toStart = false; 94 cache_ = PojoCacheFactory.createCache("META-INF/local-service.xml", toStart); 95 cache_.start(); 96 } 97 98 void destroyCaches() throws Exception 99 { 100 cache_.stop(); 101 cache_ = null; 102 } 103 104 public void testAll_RWLock() throws Exception 105 { 106 try 107 { 108 all(); 109 } 110 catch (UpgradeException ue) 111 { 112 log("Upgrade exception. Can ingore for repeatable read. " + ue); 113 } 114 catch (Exception ex) 115 { 116 log("Exception: " + ex); 117 throw ex; 118 } 119 } 120 121 private void all() throws Exception 122 { 123 RunThread t1 = new RunThread(1); 124 RunThread t2 = new RunThread(2); 125 RunThread t3 = new RunThread(3); 126 RunThread t4 = new RunThread(4); 127 128 t1.start(); 129 TestingUtil.sleepThread(100); 130 t2.start(); 131 TestingUtil.sleepThread(100); 132 t3.start(); 133 TestingUtil.sleepThread(100); 134 t4.start(); 135 136 t1.join(60000); t2.join(60000); t3.join(60000); t4.join(60000); 141 if (thread_ex != null) 142 { 143 throw thread_ex; 144 } 145 } 146 147 class RunThread extends Thread 148 { 149 final int seed_; 150 Random random_; 151 Person person_; 152 153 public RunThread(int seed) 154 { 155 seed_ = seed; 156 random_ = new Random (seed); 157 } 158 159 private void createPerson() 160 { 161 person_ = new Person(); 162 person_.setName("Ben"); 163 person_.setAge(18); 164 ArrayList <String > lang = new ArrayList <String >(); 165 lang.add("English"); 166 lang.add("French"); 167 lang.add("Mandarin"); 168 person_.setLanguages(lang); 169 Address addr = new Address(); 170 addr.setZip(95123); 171 addr.setStreet("Almeria"); 172 addr.setCity("San Jose"); 173 person_.setAddress(addr); 174 } 175 176 public void run() 177 { 178 try 179 { 180 _run(); 181 } 182 catch (Exception e) 183 { 184 thread_ex = e; 185 } 186 } 187 188 190 public void _run() throws Exception 191 { 192 for (int loop = 0; loop < MAX_LOOP; loop++) 193 { 194 createPerson(); TestingUtil.sleepThread(random_.nextInt(50)); 196 op1(); 197 } 198 } 199 200 private void op1() 202 { 203 int i = random_.nextInt(nodeList_.size() - 1); 204 if (i == 0) return; String node = nodeList_.get(i) + "/aop"; 206 cache_.attach(node, person_); 207 TestingUtil.sleepThread(random_.nextInt(SLEEP_TIME)); TestingUtil.sleepThread(random_.nextInt(SLEEP_TIME)); cache_.detach(node); 210 } 211 } 212 213 218 private ArrayList nodeGen(int depth, int children) 219 { 220 ArrayList <String > strList = new ArrayList <String >(); 221 ArrayList <String > oldList = new ArrayList <String >(); 222 ArrayList <String > newList = new ArrayList <String >(); 223 224 oldList.add("/"); 226 newList.add("/"); 227 strList.add("/"); 228 229 while (depth > 0) 230 { 231 newList = new ArrayList <String >(); 233 for (int i = 0; i < oldList.size(); i++) 234 { 235 for (int j = 0; j < children; j++) 236 { 237 String tmp = oldList.get(i); 238 tmp += Integer.toString(j); 239 if (depth != 1) 240 { 241 tmp += "/"; 242 } 243 244 newList.add(tmp); 245 } 246 } 247 strList.addAll(newList); 248 oldList = newList; 249 depth--; 250 } 251 252 for (int i = 0; i < strList.size(); i++) 254 { 255 if (strList.get(i).equals("/")) 256 { 257 strList.remove(i); 258 break; 259 } 260 } 261 log("Nodes generated: " + strList.size()); 262 return strList; 263 } 264 265 public static Test suite() throws Exception 266 { 267 return new TestSuite(LocalConcurrentTest.class); 268 } 269 270 private static void log(String str) 271 { 272 System.out.println("Thread: " + Thread.currentThread() + ": " + str); 273 } 275 276 } 277 | Popular Tags |