1 7 package org.jboss.cache.lock; 8 9 import junit.framework.Test; 10 import junit.framework.TestCase; 11 import junit.framework.TestSuite; 12 13 import java.util.concurrent.locks.ReentrantReadWriteLock ; 14 import java.util.concurrent.TimeUnit ; 15 16 21 public class ReentrantWriterPreference2Readers1WriterLockTest extends TestCase { 22 ReentrantReadWriteLock lock; 23 ReentrantReadWriteLock.ReadLock rl; 24 ReentrantReadWriteLock.WriteLock wl; 25 Exception thread_ex=null; 26 27 protected void setUp() throws Exception { 28 super.setUp(); 29 lock=new ReentrantReadWriteLock (); 30 rl=lock.readLock(); 31 wl=lock.writeLock(); 32 thread_ex=null; 33 } 34 35 protected void tearDown() throws Exception { 36 super.tearDown(); 37 lock=null; 38 if(thread_ex != null) 39 throw thread_ex; 40 } 41 42 43 private void log(String msg) { 44 System.out.println(System.currentTimeMillis() + " " + Thread.currentThread() + 45 " [" + Thread.currentThread().getName() + "]: " + msg); 46 } 47 48 49 public void testSimpleUpgradeFromReadLockToWriteLock() { 50 int readers, writers; 51 try { 52 rl.lock(); 53 readers=lock.getReadLockCount(); 54 assertEquals(1, readers); 55 boolean wl_acquired=wl.tryLock(500, TimeUnit.MILLISECONDS); 56 if(!wl_acquired) { 57 fail("write lock could not be acquired"); 58 return; 59 } 60 readers=lock.getReadLockCount(); 61 assertEquals(1, readers); 62 writers=lock.getWriteHoldCount(); 63 assertEquals(1, writers); 64 } 65 catch(InterruptedException e) { 66 67 } 68 finally { 69 rl.unlock(); 70 if(lock.getWriteHoldCount() > 0) 71 wl.unlock(); 72 } 73 } 74 75 public void test2ReadersAnd1Writer() throws InterruptedException { 76 int readers, writers; 77 Upgrader upgrader=new Upgrader("Upgrader"); 78 Reader reader=new Reader("Reader"); 79 reader.start(); 80 sleepThread(500); 81 82 readers=lock.getReadLockCount(); 83 assertEquals(1, readers); 84 85 upgrader.start(); 86 sleepThread(500); 87 88 readers=lock.getReadLockCount(); 89 assertEquals(2, readers); 90 91 synchronized(upgrader) { upgrader.notify(); 93 } 94 sleepThread(500); 95 96 readers=lock.getReadLockCount(); 97 assertEquals(2, readers); 98 writers=lock.getWriteHoldCount(); 99 assertEquals(0, writers); 100 101 synchronized(reader) { reader.notify(); 103 } 104 reader.join(); 105 106 readers=lock.getReadLockCount(); 107 assertEquals(1, readers); 108 writers=lock.getWriteHoldCount(); 109 assertEquals(1, writers); 110 111 synchronized(upgrader) { upgrader.notify(); 113 } 114 sleepThread(500); 115 readers=lock.getReadLockCount(); 116 assertEquals(0, readers); 117 writers=lock.getWriteHoldCount(); 118 assertEquals(0, writers); 119 120 upgrader.join(3000); 121 assertTrue("Known failure. See JBCACHE-461; This is due to a potential bug in ReentrantWriterPreferenceReadWriteLock !", 122 upgrader.wasUpgradeSuccessful()); 123 } 124 125 126 private class Reader extends Thread { 127 128 public Reader(String name) { 129 super(name); 130 } 131 132 public void run() { 133 try { 134 log("acquiring RL"); 135 rl.lock(); 136 log("acquired RL"); 137 synchronized(this) { 138 this.wait(); 139 } 140 } 141 catch(InterruptedException e) { 142 } 143 finally { 144 log("releasing RL"); 145 rl.unlock(); 146 log("released RL"); 147 } 148 } 149 } 150 151 152 private class Upgrader extends Thread { 153 boolean upgradeSuccessful=false; 154 155 public Upgrader(String name) { 156 super(name); 157 } 158 159 public boolean wasUpgradeSuccessful() { 160 return upgradeSuccessful; 161 } 162 163 164 public void run() { 165 try { 166 log("acquiring RL"); 167 rl.lock(); 168 log("acquired RL"); 169 synchronized(this) { 170 this.wait(); 171 } 172 log("attempting to acquire WL"); 173 wl.lock(); 174 upgradeSuccessful=true; 175 log("acquired WL"); 176 177 178 synchronized(this) { 179 this.wait(); 180 } 181 log("releasing WL"); 182 rl.unlock(); 183 log("released WL"); 184 } 185 catch(InterruptedException e) { 186 ; 187 } 188 finally { 189 wl.unlock(); 190 rl.unlock(); 191 } 192 } 193 } 194 195 196 static void sleepThread(long timeout) { 197 try { 198 Thread.sleep(timeout); 199 } 200 catch(InterruptedException e) { 201 } 202 } 203 204 public static Test suite() { 205 return new TestSuite(ReentrantWriterPreference2Readers1WriterLockTest.class); 206 } 207 208 public static void main(String [] args) { 209 junit.textui.TestRunner.run(ReentrantWriterPreference2Readers1WriterLockTest.suite()); 210 } 211 212 } 213 | Popular Tags |