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