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