1 7 package org.jboss.test.cache.stress; 8 9 import EDU.oswego.cs.dl.util.concurrent.Sync; 10 import junit.framework.Test; 11 import junit.framework.TestCase; 12 import junit.framework.TestSuite; 13 import org.jboss.cache.lock.ReadWriteLockWithUpgrade; 14 import org.jboss.logging.Logger; 15 16 19 public class ReadWriteLockWithUpgradeStressTestCase extends TestCase 20 { 21 static final ReadWriteLockWithUpgrade lock_ = new ReadWriteLockWithUpgrade(); 22 static final long SLEEP_MSECS = 50; 23 static final int LOOPS = 200; 24 static int SEED = 1; 25 static Logger log_ = Logger.getLogger("ReadWriteLockWithUpgrade"); 26 int counter; 27 28 public ReadWriteLockWithUpgradeStressTestCase(String name) 29 { 30 super(name); 31 } 32 33 34 public static void main(String [] args) throws Exception 35 { 36 log("\nBeginning ReadWriteLockWithUpgrade automated testing ...\n"); 37 38 junit.textui.TestRunner.run(suite()); 39 } 40 41 public static Test suite() 43 { 44 TestSuite suite = new TestSuite(); 45 suite.addTestSuite(ReadWriteLockWithUpgradeStressTestCase.class); 47 return suite; 48 } 49 50 public void setUp() 51 { 52 log("Setting up stress test case ..."); 53 counter = 0; 54 } 55 56 public void tearDown() 57 { 58 log("Tearing down stress test case ..."); 59 } 60 61 protected Thread readAttemptThread(String name, final long msecs) 62 { 63 return new Thread (name) 64 { 65 public void run() 66 { 67 java.util.Random rand = new java.util.Random (++SEED); 68 for (int i = 0; i < LOOPS; i++) { 69 int duration = rand.nextInt((int) SLEEP_MSECS); 70 _sleep(SLEEP_MSECS); 71 Sync rlock = null; 72 try { 73 rlock = lock_.readLock(); 74 if (!rlock.attempt(msecs)) { 75 log("Read lock attempt failed."); 76 counter++; 78 continue; 79 } 80 log("acquired read lock"); 81 _sleep(duration); 82 log("released read lock"); 83 } catch (Exception ex) { 84 } 85 finally { 86 rlock.release(); 87 } 88 } 89 } 90 }; 91 } 92 93 protected Thread writeAttemptThread(String name, final long msecs) 94 { 95 return new Thread (name) 96 { 97 public void run() 98 { 99 java.util.Random rand = new java.util.Random (++SEED); 100 for (int i = 0; i < LOOPS; i++) { 101 int duration = rand.nextInt((int) SLEEP_MSECS); 102 _sleep(SLEEP_MSECS + duration); 103 Sync wlock = null; 104 try { 105 wlock = lock_.writeLock(); 106 if (!wlock.attempt(msecs)) { 107 log("Write lock attempt failed."); 108 counter++; 110 continue; 111 } 112 log("acquired write lock"); 113 _sleep(duration); 114 log("released write lock"); 115 } catch (Exception ex) { 116 } 117 finally { 118 wlock.release(); 119 } 120 } 121 } 122 }; 123 } 124 125 protected Thread upgradeAttemptThread(String name, final long msecs) 126 { 127 return new Thread (name) 128 { 129 public void run() 130 { 131 java.util.Random rand = new java.util.Random (++SEED); 132 for (int i = 0; i < LOOPS; i++) { 133 int duration = rand.nextInt((int) SLEEP_MSECS); 134 _sleep(SLEEP_MSECS); 135 Sync rlock = null; 136 Sync wlock = null; 137 try { 138 rlock = lock_.readLock(); 139 if (!wlock.attempt(msecs)) { 140 log("Read lock attempt failed."); 141 counter++; 143 continue; 144 } 145 log("Acquired read lock for upgrade later"); 146 _sleep(duration / 2); 147 wlock = lock_.upgradeLockAttempt(msecs); 150 if (wlock == null) { 151 log("upgrade lock attempt failed"); 152 rlock.release(); 154 counter++; 155 } 156 157 log("Upgraded write lock"); 158 _sleep(duration); 159 log("released write lock"); 160 } catch (Exception ex) { 161 } 162 finally { 163 if(wlock != null) 164 wlock.release(); 165 } 166 } 167 } 168 }; 169 } 170 171 private void _sleep(long l) { 172 try { 173 Thread.sleep(l); 174 } 175 catch(InterruptedException e) { 176 e.printStackTrace(); 177 } 178 } 179 180 public void testWriteWriteAttempt() throws Exception 181 { 182 log("testWriteWriteAttempt() ..."); 183 final long msecs = 1000; 184 Thread t1 = writeAttemptThread("t1-write", msecs); 185 Thread t2 = writeAttemptThread("t2-write", msecs); 186 Thread t3 = writeAttemptThread("t3-write", msecs); 187 Thread t4 = writeAttemptThread("t4-write", msecs); 188 189 t1.start(); 190 t2.start(); 191 t3.start(); 192 t4.start(); 193 long timeout = 0; 194 t1.join(timeout); 195 t2.join(timeout); 196 t3.join(timeout); 197 t4.join(timeout); 198 checkCounter(); 199 } 200 201 public void testReadWriteAttempt1() throws Exception 202 { 203 log("testReadWriteAttemp1() ..."); 204 final long msecs = 2000; 205 Thread t1 = readAttemptThread("t1-read", msecs); 206 Thread t2 = readAttemptThread("t2-read", msecs); 207 Thread t3 = writeAttemptThread("t3-write", msecs); 208 Thread t4 = writeAttemptThread("t4-write", msecs); 209 Thread t5 = upgradeAttemptThread("t5-upgrade", msecs); 210 Thread t6 = upgradeAttemptThread("t6-upgrade", msecs); 211 212 t1.start(); 213 t2.start(); 214 t3.start(); 215 t4.start(); 216 t5.start(); 217 t6.start(); 218 long timeout = 0; 219 t1.join(timeout); 220 t2.join(timeout); 221 t3.join(timeout); 222 t4.join(timeout); 223 t5.join(timeout); 224 t6.join(timeout); 225 checkCounter(); 226 } 227 228 public void testReadWriteAttempt2() throws Exception 229 { 230 log("**************"); 231 log("testReadWriteAttemp2() ..."); 232 log("**************"); 233 SEED++; 234 final long msecs = 1000; 235 Thread t1 = readAttemptThread("t1-read", msecs); 236 Thread t3 = writeAttemptThread("t3-write", msecs); 238 Thread t5 = upgradeAttemptThread("t5-upgrade", msecs); 240 Thread t6 = upgradeAttemptThread("t6-upgrade", msecs); 241 242 t1.start(); 243 t3.start(); 245 t5.start(); 247 t6.start(); 248 long timeout = 0; 249 t1.join(timeout); 250 t3.join(timeout); 252 t5.join(timeout); 254 t6.join(timeout); 255 checkCounter(); 256 } 257 258 public void testReadWriteAttempt3() throws Exception 259 { 260 log("**************"); 261 log("testReadWriteAttemp3() ..."); 262 log("**************"); 263 SEED++; 264 final long msecs = 1000; 265 Thread t1 = readAttemptThread("t1-read", msecs); 266 Thread t2 = readAttemptThread("t2-read", msecs); 267 Thread t3 = writeAttemptThread("t3-write", msecs); 268 Thread t4 = writeAttemptThread("t4-write", msecs); 269 Thread t6 = upgradeAttemptThread("t6-upgrade", msecs); 271 272 t1.start(); 273 t2.start(); 274 t3.start(); 275 t4.start(); 276 t6.start(); 278 long timeout = 0; 279 t1.join(timeout); 280 t2.join(timeout); 281 t3.join(timeout); 282 t4.join(timeout); 283 t6.join(timeout); 285 checkCounter(); 286 } 287 288 private void checkCounter() { 289 if(counter > (LOOPS) ) 290 fail("Failed lock attempt or upgrade exceeded warning. Counter: "+counter); 291 } 292 293 public static void log(String str) 294 { 295 log_.debug(Thread.currentThread() + ": " 298 + " : " + str); 299 } 300 } 301 302 | Popular Tags |