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.util.Jdk14Logger; 38 import org.apache.commons.transaction.util.LoggerFacade; 39 import org.apache.commons.transaction.util.RendezvousBarrier; 40 41 46 public class OptimisticMapWrapperTest extends MapWrapperTest { 47 48 private static final Logger logger = Logger.getLogger(OptimisticMapWrapperTest.class.getName()); 49 private static final LoggerFacade sLogger = new Jdk14Logger(logger); 50 51 public static Test suite() { 52 TestSuite suite = new TestSuite(OptimisticMapWrapperTest.class); 53 return suite; 54 } 55 56 public static void main(java.lang.String [] args) { 57 junit.textui.TestRunner.run(suite()); 58 } 59 60 public OptimisticMapWrapperTest(String testName) { 61 super(testName); 62 } 63 64 protected TransactionalMapWrapper getNewWrapper(Map map) { 65 return new OptimisticMapWrapper(map); 66 } 67 68 public void testBasic() throws Throwable { 70 super.testBasic(); 71 } 72 73 public void testComplex() throws Throwable { 74 super.testComplex(); 75 } 76 77 public void testSets() throws Throwable { 78 super.testSets(); 79 } 80 81 public void testMulti() throws Throwable { 82 logger.info("Checking concurrent transaction features"); 83 84 final Map map1 = new HashMap (); 85 86 final OptimisticMapWrapper txMap1 = (OptimisticMapWrapper) getNewWrapper(map1); 87 88 final RendezvousBarrier beforeCommitBarrier = 89 new RendezvousBarrier("Before Commit", 2, BARRIER_TIMEOUT, sLogger); 90 91 final RendezvousBarrier afterCommitBarrier = new RendezvousBarrier("After Commit", 2, BARRIER_TIMEOUT, sLogger); 92 93 Thread thread1 = new Thread (new Runnable () { 94 public void run() { 95 txMap1.startTransaction(); 96 try { 97 beforeCommitBarrier.meet(); 98 txMap1.put("key1", "value2"); 99 txMap1.commitTransaction(); 100 afterCommitBarrier.call(); 101 } catch (InterruptedException e) { 102 logger.log(Level.WARNING, "Thread interrupted", e); 103 afterCommitBarrier.reset(); 104 beforeCommitBarrier.reset(); 105 } 106 } 107 }, "Thread1"); 108 109 txMap1.put("key1", "value1"); 110 111 txMap1.startTransaction(); 112 thread1.start(); 113 114 report("value1", (String ) txMap1.get("key1")); 115 beforeCommitBarrier.call(); 116 afterCommitBarrier.meet(); 117 report("value1", (String ) txMap1.get("key1")); 119 120 txMap1.put("key1", "value3"); 122 report("value3", (String ) txMap1.get("key1")); 123 124 txMap1.rollbackTransaction(); 126 report("value2", (String ) txMap1.get("key1")); 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 OptimisticMapWrapper txMap1 = (OptimisticMapWrapper) getNewWrapper(map1); 135 136 final RendezvousBarrier beforeCommitBarrier = 137 new RendezvousBarrier("Before Commit", 2, BARRIER_TIMEOUT, sLogger); 138 139 final RendezvousBarrier afterCommitBarrier = new RendezvousBarrier("After Commit", 2, BARRIER_TIMEOUT, sLogger); 140 141 Thread thread1 = new Thread (new Runnable () { 142 public void run() { 143 txMap1.startTransaction(); 144 try { 145 beforeCommitBarrier.meet(); 146 txMap1.put("key1", "value2"); 147 txMap1.commitTransaction(); 148 afterCommitBarrier.call(); 149 } catch (InterruptedException e) { 150 logger.log(Level.WARNING, "Thread interrupted", e); 151 afterCommitBarrier.reset(); 152 beforeCommitBarrier.reset(); 153 } 154 } 155 }, "Thread1"); 156 157 txMap1.put("key1", "value1"); 158 159 txMap1.startTransaction(); 160 thread1.start(); 161 162 report("value1", (String ) txMap1.get("key1")); 163 beforeCommitBarrier.call(); 164 afterCommitBarrier.meet(); 165 report("value1", (String ) txMap1.get("key1")); 167 168 txMap1.put("key1", "value3"); 170 report("value3", (String ) txMap1.get("key1")); 171 172 boolean conflict = false; 173 174 try { 175 txMap1.commitTransaction(); 176 } catch (ConflictException ce) { 177 conflict = true; 178 } 179 assertTrue(conflict); 180 report("value2", (String ) map1.get("key1")); 182 183 txMap1.commitTransaction(true); 185 report("value3", (String ) txMap1.get("key1")); 187 report("value3", (String ) map1.get("key1")); 188 } 189 190 public void testTxControl() throws Throwable { 191 super.testTxControl(); 192 } 193 194 } 195 | Popular Tags |