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