1 8 package org.jboss.test.cache.test.local; 9 10 import junit.framework.Test; 11 import junit.framework.TestCase; 12 import junit.framework.TestSuite; 13 import org.jboss.cache.PropertyConfigurator; 14 import org.jboss.cache.TreeCache; 15 import org.jboss.cache.lock.IsolationLevel; 16 import org.jboss.cache.transaction.DummyTransactionManager; 17 import org.jboss.logging.Logger; 18 19 import javax.naming.Context ; 20 import javax.naming.InitialContext ; 21 import javax.naming.NamingException ; 22 import javax.transaction.UserTransaction ; 23 import java.util.*; 24 25 31 public class ConcurrentTransactionalUnitTestCase extends TestCase 32 { 33 TreeCache cache; 34 private Logger logger_ = Logger.getLogger(ConcurrentTransactionalUnitTestCase.class); 35 static Throwable thread_ex=null; 36 final int NUM=10000; 37 static Properties p = null; 38 String old_factory = null; 39 final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; 40 41 public ConcurrentTransactionalUnitTestCase(String name) 42 { 43 super(name); 44 } 45 46 public void setUp() throws Exception 47 { 48 super.setUp(); 49 old_factory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 50 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY); 51 DummyTransactionManager.getInstance(); 52 if (p == null) { 53 p = new Properties(); 54 p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory"); 55 } 56 } 57 58 private void createCache(IsolationLevel level) throws Exception 59 { 60 cache = new TreeCache(); 61 PropertyConfigurator config = new PropertyConfigurator(); 62 config.configure(cache, "META-INF/local-service.xml"); 63 64 cache.setCacheMode(TreeCache.LOCAL); 65 cache.setIsolationLevel(level); 66 cache.start(); 67 cache.put("/a/b/c", null); 68 } 69 70 private UserTransaction getTransaction() { 71 UserTransaction tx = null; 72 try { 73 tx = (UserTransaction ) new InitialContext (p).lookup("UserTransaction"); 74 } catch (NamingException e) { 75 e.printStackTrace(); 76 } 77 78 if(tx == null) 79 throw new RuntimeException ("Tx is null"); 80 81 return tx; 82 } 83 84 public void tearDown() throws Exception 85 { 86 super.tearDown(); 87 cache.stopService(); 88 thread_ex=null; 89 DummyTransactionManager.destroy(); 90 if (old_factory != null) { 91 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory); 92 old_factory = null; 93 } 94 } 95 96 public void testConcurrentAccessWithRWLock() throws Throwable 97 { 98 createCache(IsolationLevel.REPEATABLE_READ); 99 work_(); 100 } 101 102 public void testConcurrentAccessWithExclusiveLock() throws Throwable 103 { 104 createCache(IsolationLevel.SERIALIZABLE); 105 work_(); 106 } 107 108 private void work_() throws Throwable { 109 Updater one, two; 110 try { 111 one = new Updater("Thread one"); 112 two = new Updater("Thread two"); 113 long current = System.currentTimeMillis(); 114 one.start(); 115 two.start(); 116 one.join(); 117 two.join(); 118 if(thread_ex != null) 119 throw thread_ex; 120 121 long now = System.currentTimeMillis(); 122 log("*** Time elapsed: " + (now-current)); 123 124 System.out.println("cache content: " + cache.toString()); 125 Set keys = cache.getKeys("/a/b/c"); 126 System.out.println("number of keys=" + keys.size()); 127 128 if(keys.size() != NUM) { 129 scanForNullValues(keys); 130 131 try { 132 System.out.println("size=" + keys.size()); 133 List l=new LinkedList(keys); 134 Collections.sort(l); 135 System.out.println("keys: " + l); 136 for(int i=0; i < NUM; i++) { 137 if(!l.contains(new Integer (i))) 138 System.out.println("missing: " + i); 139 } 140 141 LinkedList duplicates=new LinkedList(); 142 for(Iterator it=l.iterator(); it.hasNext();) { 143 Integer integer=(Integer )it.next(); 144 if(duplicates.contains(integer)) { 145 System.out.println(integer + " is a duplicate"); 146 } 147 else 148 duplicates.add(integer); 149 } 150 } 151 catch(Exception e1) { 152 e1.printStackTrace(); 153 } 154 } 155 156 assertEquals(NUM, keys.size()); 157 log("lock info:\n" + cache.printLockInfo()); 158 159 } catch (Exception e) { 160 e.printStackTrace(); 161 fail(e.toString()); 162 } 163 } 164 165 private void scanForNullValues(Set keys) { 166 for(Iterator it=keys.iterator(); it.hasNext();) { 167 Object o=it.next(); 168 if(o == null) 169 System.err.println("found a null value in keys"); 170 } 171 } 172 173 174 void sleep(long timeout) 175 { 176 try { 177 Thread.sleep(timeout); 178 } catch (InterruptedException e) { 179 } 180 } 181 182 void log(String msg) 183 { 184 logger_.debug(" [" + Thread.currentThread() + "]: " + msg); 186 } 187 188 189 class Updater extends Thread { 190 String name=null; 191 UserTransaction tx; 192 193 Updater(String name) { 194 this.name=name; 195 } 196 197 public void run() { 198 try { 199 log("adding data"); 200 tx=getTransaction(); 201 for(int i=0; i < NUM; i++) { 202 log("adding data i=" + i); 203 tx.begin(); 204 cache.put("/a/b/c", new Integer (i), name); 205 tx.commit(); 206 yield(); 207 } 208 } 209 catch(Throwable t) { 210 t.printStackTrace(); 211 thread_ex=t; 212 } 213 } 214 } 215 216 public static Test suite() 217 { 218 return new TestSuite(ConcurrentTransactionalUnitTestCase.class); 219 } 220 221 public static void main(String [] args) 222 { 223 junit.textui.TestRunner.run(suite()); 224 } 225 226 227 } 228 | Popular Tags |