1 28 29 package org.apache.commons.transaction.memory; 30 31 import junit.framework.*; 32 33 import java.util.HashMap ; 34 import java.util.Map ; 35 import java.util.logging.*; 36 37 import org.apache.commons.transaction.locking.LockException; 38 import org.apache.commons.transaction.util.Jdk14Logger; 39 import org.apache.commons.transaction.util.LoggerFacade; 40 import org.apache.commons.transaction.util.RendezvousBarrier; 41 42 47 public class PessimisticMapWrapperTest extends MapWrapperTest { 48 49 private static final Logger logger = Logger.getLogger(PessimisticMapWrapperTest.class.getName()); 50 private static final LoggerFacade sLogger = new Jdk14Logger(logger); 51 52 protected static final long TIMEOUT = Long.MAX_VALUE; 53 54 private static int deadlockCnt = 0; 55 56 public static Test suite() { 57 TestSuite suite = new TestSuite(PessimisticMapWrapperTest.class); 58 return suite; 59 } 60 61 public static void main(java.lang.String [] args) { 62 junit.textui.TestRunner.run(suite()); 63 } 64 65 public PessimisticMapWrapperTest(String testName) { 66 super(testName); 67 } 68 69 protected TransactionalMapWrapper getNewWrapper(Map map) { 70 return new PessimisticMapWrapper(map, sLogger); 71 } 72 73 public void testBasic() throws Throwable { 75 super.testBasic(); 76 } 77 78 public void testComplex() throws Throwable { 79 super.testComplex(); 80 } 81 82 public void testSets() throws Throwable { 83 super.testSets(); 84 } 85 86 public void testMulti() throws Throwable { 87 logger.info("Checking concurrent transaction features"); 88 89 final Map map1 = new HashMap (); 90 91 final PessimisticMapWrapper txMap1 = (PessimisticMapWrapper) getNewWrapper(map1); 92 93 final RendezvousBarrier beforeCommitBarrier = 94 new RendezvousBarrier("Before Commit", 2, BARRIER_TIMEOUT, sLogger); 95 96 final RendezvousBarrier afterCommitBarrier = new RendezvousBarrier("After Commit", 2, BARRIER_TIMEOUT, sLogger); 97 98 Thread thread1 = new Thread (new Runnable () { 99 public void run() { 100 txMap1.startTransaction(); 101 txMap1.put("key1", "value2"); 102 synchronized (txMap1) { 103 txMap1.commitTransaction(); 104 report("value2", (String ) txMap1.get("key1")); 105 } 106 } 107 }, "Thread1"); 108 109 txMap1.put("key1", "value1"); 110 111 txMap1.startTransaction(); 112 113 report("value1", (String ) txMap1.get("key1")); 114 115 thread1.start(); 116 117 report("value1", (String ) txMap1.get("key1")); 119 120 txMap1.put("key1", "value3"); 121 122 synchronized (txMap1) { 124 txMap1.commitTransaction(); 125 report("value3", (String ) txMap1.get("key1")); 126 } 127 } 128 129 public void testConflict() throws Throwable { 130 logger.info("Checking concurrent transaction features"); 131 132 final Map map1 = new HashMap (); 133 134 final PessimisticMapWrapper txMap1 = (PessimisticMapWrapper) getNewWrapper(map1); 135 136 final RendezvousBarrier restart = new RendezvousBarrier("restart", 137 TIMEOUT, sLogger); 138 139 for (int i = 0; i < 25; i++) { 140 141 final RendezvousBarrier deadlockBarrier1 = new RendezvousBarrier("deadlock" + i, 142 TIMEOUT, sLogger); 143 144 Thread thread1 = new Thread (new Runnable () { 145 public void run() { 146 txMap1.startTransaction(); 147 try { 148 txMap1.put("key2", "value2"); 150 synchronized (deadlockBarrier1) { 151 deadlockBarrier1.meet(); 152 deadlockBarrier1.reset(); 153 } 154 txMap1.put("key1", "value2"); 157 txMap1.commitTransaction(); 158 } catch (LockException le) { 159 assertEquals(le.getCode(), LockException.CODE_DEADLOCK_VICTIM); 160 deadlockCnt++; 161 txMap1.rollbackTransaction(); 162 } catch (InterruptedException ie) { 163 } finally { 164 try { 165 synchronized (restart) { 166 restart.meet(); 167 restart.reset(); 168 } 169 } catch (InterruptedException ie) {} 170 171 } 172 } 173 }, "Thread1"); 174 175 thread1.start(); 176 177 txMap1.startTransaction(); 178 try { 179 txMap1.get("key1"); 181 synchronized (deadlockBarrier1) { 182 deadlockBarrier1.meet(); 183 deadlockBarrier1.reset(); 184 } 185 txMap1.get("key2"); 188 txMap1.commitTransaction(); 189 } catch (LockException le) { 190 assertEquals(le.getCode(), LockException.CODE_DEADLOCK_VICTIM); 191 deadlockCnt++; 192 txMap1.rollbackTransaction(); 193 } finally { 194 try { 195 synchronized (restart) { 196 restart.meet(); 197 restart.reset(); 198 } 199 } catch (InterruptedException ie) {} 200 201 } 202 203 if (deadlockCnt != 1) { 206 sLogger.logWarning("More than one thread was deadlock victim!"); 207 } 208 assertTrue(deadlockCnt >= 1); 209 deadlockCnt = 0; 210 } 211 } 212 213 public void testTxControl() throws Throwable { 214 super.testTxControl(); 215 } 216 217 } 218 | Popular Tags |