1 package org.jboss.cache.transaction; 2 3 import EDU.oswego.cs.dl.util.concurrent.Latch; 4 import junit.framework.AssertionFailedError; 5 import junit.framework.Test; 6 import junit.framework.TestCase; 7 import junit.framework.TestSuite; 8 import org.jboss.cache.CacheImpl; 9 import org.jboss.cache.DummyTransactionManagerLookup; 10 import org.jboss.cache.Fqn; 11 import org.jboss.cache.lock.IsolationLevel; 12 import org.jboss.cache.lock.TimeoutException; 13 14 import javax.transaction.NotSupportedException ; 15 import javax.transaction.SystemException ; 16 import javax.transaction.Transaction ; 17 18 24 25 public class IsolationLevelRepeatableReadTest extends TestCase 26 { 27 28 private CacheImpl cache = null; 29 private final Fqn FQN = Fqn.fromString("/a"); 30 private final String KEY = "key"; 31 private final String VALUE = "value"; 32 33 private volatile boolean writerFailed; 34 private volatile AssertionFailedError writerError; 35 36 protected void setUp() throws Exception 37 { 38 super.setUp(); 39 40 writerFailed = false; 41 writerError = null; 42 43 cache = new CacheImpl(); 44 cache.getConfiguration().setCacheMode("LOCAL"); 45 cache.getConfiguration().setIsolationLevel(IsolationLevel.REPEATABLE_READ); 46 cache.getConfiguration().setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName()); 47 cache.getConfiguration().setLockAcquisitionTimeout(1000); 48 cache.start(); 49 } 50 51 52 protected void tearDown() throws Exception 53 { 54 super.tearDown(); 55 cache.stop(); 56 } 57 58 65 public void testNodeRemoved() throws Exception 66 { 67 final Latch readerCanRead = new Latch(); 68 final Latch readerDone = new Latch(); 69 final Latch writerDone = new Latch(); 70 71 cache.put(FQN, KEY, VALUE); 72 assertEquals(VALUE, cache.get(FQN, KEY)); 73 74 76 Thread writerThread = new Thread (new Runnable () 77 { 78 public void run() 79 { 80 try 81 { 82 Transaction tx = startTransaction(); 83 84 cache.remove(FQN); 86 87 readerCanRead.release(); 89 90 readerDone.acquire(); 91 92 tx.commit(); 93 } 94 catch (AssertionFailedError e) 95 { 96 writerError = e; 97 } 98 catch (Throwable t) 99 { 100 t.printStackTrace(); 101 writerFailed = true; 102 } 103 finally 104 { 105 System.out.println("writer thread exits"); 106 readerCanRead.release(); 107 writerDone.release(); 108 } 109 } 110 }, "WRITER"); 111 writerThread.start(); 112 113 try 114 { 115 readerCanRead.acquire(); 118 119 assertEquals("2nd thread cannot see uncommitted changes", 121 VALUE, cache.get(FQN, KEY)); 122 } 123 catch (TimeoutException t) 124 { 125 } 127 finally 128 { 129 System.out.println("reader thread exits"); 130 readerDone.release(); 131 } 132 133 writerDone.acquire(); 135 136 assertNull("Node was not removed", cache.get(FQN)); 137 138 140 if (writerError != null) 141 { 142 throw writerError; 143 } 144 145 if (writerFailed) 146 { 147 fail("The writer thread exited incorrectly. Watch the log for previous stack traces"); 148 } 149 150 } 151 152 private Transaction startTransaction() throws SystemException , NotSupportedException 153 { 154 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 155 mgr.begin(); 156 return mgr.getTransaction(); 157 } 158 159 public static Test suite() 160 { 161 162 return new TestSuite(IsolationLevelRepeatableReadTest.class); 163 164 } 165 166 } 167 | Popular Tags |