1 23 24 package org.apache.commons.transaction.locking; 25 26 import java.io.PrintWriter ; 27 28 import junit.framework.TestCase; 29 30 import org.apache.commons.transaction.util.LoggerFacade; 31 import org.apache.commons.transaction.util.PrintWriterLogger; 32 33 39 public class LockTestRepeatableReads extends TestCase 40 { 41 private static final LoggerFacade logFacade = new PrintWriterLogger(new PrintWriter (System.out), 42 LockTestRepeatableReads.class.getName(), false); 43 44 protected static final long TIMEOUT = 1000000; 45 46 public static void main(String [] args) 47 { 48 String [] arr = {LockTestRepeatableReads.class.getName()}; 49 junit.textui.TestRunner.main(arr); 50 } 51 52 public LockTestRepeatableReads(String name) 53 { 54 super(name); 55 } 56 57 Object tx1; 58 Object tx2; 59 Object obj; 60 ReadWriteUpgradeLockManager lockManager; 61 62 public void setUp() throws Exception 63 { 64 super.setUp(); 65 66 lockManager = new ReadWriteUpgradeLockManager(logFacade, TIMEOUT); 67 68 tx2 = new Object (); 70 tx1 = new Object (); 71 obj = new Object (); 72 } 73 74 public void tearDown() throws Exception 75 { 76 try 77 { 78 lockManager.release(tx1, obj); 79 lockManager.release(tx2, obj); 80 } 81 finally 82 { 83 super.tearDown(); 84 } 85 } 86 87 90 public void testWriteReleaseCheckRead() 91 { 92 assertTrue(lockManager.tryWriteLock(tx2, obj)); 93 assertTrue(lockManager.checkReadLock(tx2, obj)); 94 assertTrue(lockManager.release(tx2, obj)); 95 assertFalse(lockManager.hasReadLock(tx2, obj)); 96 } 97 98 101 public void testReadWriteReleaseCheckRead() 102 { 103 assertTrue(lockManager.tryReadLock(tx2, obj)); 104 assertTrue(lockManager.tryWriteLock(tx2, obj)); 105 assertTrue(lockManager.checkReadLock(tx2, obj)); 106 assertTrue(lockManager.release(tx2, obj)); 107 assertFalse(lockManager.hasReadLock(tx2, obj)); 108 } 109 110 113 public void testSingleReadLock() 114 { 115 assertTrue(lockManager.tryReadLock(tx1, obj)); 116 } 117 118 121 public void testUpgradeReadLock() 122 { 123 assertTrue(lockManager.tryReadLock(tx1, obj)); 124 assertTrue(lockManager.tryUpgradeLock(tx1, obj)); 125 } 126 127 130 public void testReadThenWrite() 131 { 132 assertTrue(lockManager.tryReadLock(tx1, obj)); 133 assertTrue(lockManager.tryWriteLock(tx1, obj)); 134 } 135 136 139 public void testSingleWriteLock() 140 { 141 assertTrue(lockManager.tryWriteLock(tx1, obj)); 142 } 143 144 147 public void testWriteThenRead() 148 { 149 assertTrue(lockManager.tryWriteLock(tx1, obj)); 150 assertTrue(lockManager.tryReadLock(tx1, obj)); 151 } 152 153 156 public void testMultipleReadLock() 157 { 158 assertTrue(lockManager.tryReadLock(tx1, obj)); 159 assertTrue(lockManager.tryReadLock(tx2, obj)); 160 } 161 162 165 public void testUpgradeWithExistingReader() 166 { 167 assertTrue(lockManager.tryReadLock(tx1, obj)); 168 assertTrue(lockManager.tryUpgradeLock(tx2, obj)); 169 } 170 171 174 public void testWriteWithExistingReader() 175 { 176 assertTrue(lockManager.tryReadLock(tx1, obj)); 177 assertFalse(lockManager.tryWriteLock(tx2, obj)); 178 } 179 180 183 public void testUpgradeWithMultipleReaders() 184 { 185 assertTrue(lockManager.tryReadLock(tx1, obj)); 186 assertTrue(lockManager.tryReadLock(tx2, obj)); 187 assertTrue(lockManager.tryUpgradeLock(tx2, obj)); 188 } 189 190 193 public void testWriteWithMultipleReaders() 194 { 195 assertTrue(lockManager.tryReadLock(tx1, obj)); 196 assertTrue(lockManager.tryReadLock(tx2, obj)); 197 assertTrue(!lockManager.tryWriteLock(tx2, obj)); 198 } 199 200 203 public void testUpgradeWithMultipleReadersOn1() 204 { 205 assertTrue(lockManager.tryReadLock(tx1, obj)); 206 assertTrue(lockManager.tryReadLock(tx2, obj)); 207 assertTrue(lockManager.tryUpgradeLock(tx1, obj)); 208 } 209 210 213 public void testWriteWithMultipleReadersOn1() 214 { 215 assertTrue(lockManager.tryReadLock(tx1, obj)); 216 assertTrue(lockManager.tryReadLock(tx2, obj)); 217 assertFalse(lockManager.tryWriteLock(tx1, obj)); 218 } 219 220 223 public void testReadWithExistingWriter() 224 { 225 assertTrue(lockManager.tryWriteLock(tx1, obj)); 226 assertFalse(lockManager.tryReadLock(tx2, obj)); 227 } 228 229 232 public void testMultipleWriteLock() 233 { 234 assertTrue(lockManager.tryWriteLock(tx1, obj)); 235 assertFalse(lockManager.tryWriteLock(tx2, obj)); 236 } 237 238 241 public void testReleaseReadLock() 242 { 243 assertTrue(lockManager.tryReadLock(tx1, obj)); 244 assertTrue(lockManager.release(tx1, obj)); 245 assertTrue(lockManager.tryWriteLock(tx2, obj)); 246 } 247 248 251 public void testReleaseUpgradeLock() 252 { 253 assertTrue(lockManager.tryUpgradeLock(tx1, obj)); 254 assertTrue(lockManager.release(tx1, obj)); 255 assertTrue(lockManager.tryWriteLock(tx2, obj)); 256 } 257 258 261 public void testReleaseWriteLock() 262 { 263 assertTrue(lockManager.tryWriteLock(tx1, obj)); 264 assertTrue(lockManager.release(tx1, obj)); 265 assertTrue(lockManager.tryWriteLock(tx2, obj)); 266 } 267 268 271 public void testReadThenRead() 272 { 273 assertTrue(lockManager.tryReadLock(tx1, obj)); 274 assertTrue(lockManager.tryReadLock(tx1, obj)); 275 } 276 } | Popular Tags |