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