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