1 package org.jboss.cache.tests.lock; 2 3 import EDU.oswego.cs.dl.util.concurrent.Sync; 4 import junit.framework.Test; 5 import junit.framework.TestCase; 6 import junit.framework.TestSuite; 7 import org.jboss.cache.lock.SimpleReadWriteLock; 8 9 14 public class ReentrantWriterPreferenceReadWriteLockTest extends TestCase { 15 SimpleReadWriteLock lock; 17 Sync rl, wl; 18 Exception thread_ex=null; 19 20 protected void setUp() throws Exception { 21 super.setUp(); 22 lock=new SimpleReadWriteLock(); 24 rl=lock.readLock(); 25 wl=lock.writeLock(); 26 thread_ex=null; 27 } 28 29 protected void tearDown() throws Exception { 30 super.tearDown(); 31 lock=null; 32 if(thread_ex != null) 33 throw thread_ex; 34 } 35 36 public void testMultipleReadLockAcquisitions() throws InterruptedException { 37 rl.acquire(); 38 rl.acquire(); 39 } 40 41 public void testInterruptedLockAcquisition() { 42 Thread.currentThread().interrupt(); 43 try { 44 rl.acquire(); 45 fail("thread should be in interrupted status"); 46 } 47 catch(InterruptedException e) { 48 } 49 finally { 50 try { 51 rl.release(); 52 fail("release() should throw an IllegalStateException"); 53 } 54 catch(IllegalStateException illegalStateEx) { 55 assertTrue(true); 56 } 57 } 58 } 59 60 public void testMultipleWriteLockAcquisitions() throws InterruptedException { 61 wl.acquire(); 62 wl.acquire(); 63 } 64 65 public void testMultipleReadLockReleases() throws InterruptedException { 66 rl.acquire(); 67 rl.release(); 68 try { 69 rl.release(); 70 fail("we should not get here, cannot acquire RL once but release twice"); 71 } 72 catch(IllegalStateException illegalState) { 73 } 75 } 76 77 78 public void acquireReadAndWriteLocks() throws InterruptedException { 79 rl.acquire(); 80 rl.acquire(); 81 boolean fl=wl.attempt(4000); 82 assertTrue(fl); 83 } 84 85 86 public void acquireWriteThenReadLock() throws InterruptedException { 87 wl.acquire(); 88 rl.acquire(); 89 wl.release(); 90 rl.release(); 91 } 92 93 public void testMultipleWriteLockReleases() throws InterruptedException { 94 wl.acquire(); 95 wl.release(); 96 wl.release(); 97 } 98 99 public void testAcquireWriteLockAfterReadLock() throws InterruptedException { 100 rl.acquire(); 101 wl.acquire(); 102 } 103 104 105 public void testAcquiringReadLockedLockWithRead() throws InterruptedException { 106 new Thread () { 107 public void run() { 108 try {rl.acquire();} 109 catch(InterruptedException e) {} 110 } 111 }.start(); 112 113 sleep(500); 114 115 117 boolean flag=rl.attempt(3000); 118 assertTrue(flag); 119 flag=wl.attempt(3000); 120 assertFalse(flag); 121 } 122 123 public void testAcquiringReadLockedLock() throws InterruptedException { 124 new Thread () { 125 public void run() { 126 try {rl.acquire();} 127 catch(InterruptedException e) {} 128 } 129 }.start(); 130 131 sleep(500); 132 133 boolean flag=wl.attempt(3000); 135 assertFalse(flag); 136 } 137 138 public void test2ReadersAnd1Writer() throws InterruptedException { 139 Upgrader upgrader=new Upgrader("Upgrader"); 140 Reader reader=new Reader("Reader"); 141 upgrader.start(); 142 reader.start(); 143 144 sleep(500); 145 synchronized(upgrader) { 146 upgrader.notify(); 147 } 148 sleep(500); 149 synchronized(reader) { 150 reader.notify(); 151 } 152 reader.join(); 153 upgrader.join(3000); 154 assertTrue("This is due to a bug in ReentrantWriterPreferenceReadWriteLock !", upgrader.wasUpgradeSuccessful()); 155 } 156 157 158 public void testWriteThenReadByDifferentTx() throws InterruptedException { 159 Writer writer=new Writer("Writer"); 160 Reader reader=new Reader("Reader"); 161 writer.start(); 162 sleep(500); 163 reader.start(); 164 sleep(1000); 165 166 synchronized(writer) { 167 log("terminating Writer"); 168 writer.notify(); 169 } 170 sleep(500); 171 synchronized(reader) { 172 reader.notify(); 173 } 174 writer.join(); 175 reader.join(); 176 } 177 178 public void testReadThenWriteByDifferentTx() throws InterruptedException { 179 Writer writer=new Writer("Writer"); 180 Reader reader=new Reader("Reader"); 181 182 reader.start(); 183 sleep(500); 184 writer.start(); 185 sleep(1000); 186 187 synchronized(reader) { 188 log("terminating Reader"); 189 reader.notify(); 190 } 191 192 sleep(500); 193 synchronized(writer) { 194 writer.notify(); 195 } 196 writer.join(); 197 reader.join(); 198 } 199 200 201 202 private static void log(String msg) { 203 System.out.println(System.currentTimeMillis() + " " + Thread.currentThread() + 204 " [" + Thread.currentThread().getName() + "]: " + msg); 205 } 206 207 private static void sleep(long timeout) { 208 try { 209 Thread.sleep(timeout); 210 } 211 catch(InterruptedException e) { 212 } 213 } 214 215 216 class Reader extends Thread { 217 218 public Reader(String name) { 219 super(name); 220 } 221 222 public void run() { 223 try { 224 log("acquiring RL"); 225 rl.acquire(); 226 log("acquired RL"); 227 synchronized(this) { 228 this.wait(); 229 } 230 log("releasing RL"); 231 rl.release(); 232 log("released RL"); 233 } 234 catch(InterruptedException e) { 235 ; 236 } 237 } 238 } 239 240 241 class Writer extends Thread { 242 243 public Writer(String name) { 244 super(name); 245 } 246 247 public void run() { 248 try { 249 log("acquiring WL"); 250 wl.acquire(); 251 log("acquired WL"); 252 synchronized(this) { 253 this.wait(); 254 } 255 log("releasing WL"); 256 wl.release(); 257 log("released WL"); 258 } 259 catch(InterruptedException e) { 260 ; 261 } 262 } 263 } 264 265 266 class Upgrader extends Thread { 267 boolean upgradeSuccessful=false; 268 public Upgrader(String name) { 269 super(name); 270 } 271 272 public boolean wasUpgradeSuccessful() { 273 return upgradeSuccessful; 274 } 275 276 277 public void run() { 278 try { 279 log("acquiring RL"); 280 rl.acquire(); 281 log("acquired RL"); 282 synchronized(this) { 283 this.wait(); 284 } 285 log("attempting to acquire WL"); 286 wl.acquire(); 288 upgradeSuccessful=true; 289 log("acquired WL"); 290 log("releasing WL/RL"); 291 wl.release(); 292 log("released WL/RL"); 293 } 294 catch(InterruptedException e) { 295 ; 296 } 297 } 298 } 299 300 301 302 public static Test suite() { 303 return new TestSuite(ReentrantWriterPreferenceReadWriteLockTest.class); 304 } 305 306 public static void main(String [] args) { 307 junit.textui.TestRunner.run(suite()); 308 } 309 310 } 311 | Popular Tags |