1 7 package org.jboss.cache.tests; 8 9 import EDU.oswego.cs.dl.util.concurrent.ReadWriteLock; 10 import EDU.oswego.cs.dl.util.concurrent.ReentrantWriterPreferenceReadWriteLock; 11 import junit.framework.Test; 12 import junit.framework.TestCase; 13 import junit.framework.TestSuite; 14 15 20 public class ReadWriteLockTest extends TestCase { 21 ReadWriteLock lock; 22 Exception ex=null; 23 24 25 protected void setUp() throws Exception { 26 super.setUp(); 27 ex=null; 28 } 29 30 protected void tearDown() throws Exception { 31 super.tearDown(); 32 lock=null; 33 if(ex != null) 34 throw ex; 35 } 36 37 38 public void testMoreWriteReleasesThanAcquisitions() throws InterruptedException { 39 lock=new ReentrantWriterPreferenceReadWriteLock(); 40 lock.writeLock().acquire(); 41 lock.writeLock().release(); 42 lock.writeLock().release(); 43 } 44 45 public void testMoreReadReleasesThanAcquisitions() throws InterruptedException { 46 lock=new ReentrantWriterPreferenceReadWriteLock(); 47 lock.readLock().acquire(); 48 lock.readLock().release(); 49 try { 50 lock.readLock().release(); 51 fail("read locks cannot be released more than acquired"); 52 } 53 catch(IllegalStateException illegalStateEx) { 54 55 } 56 } 57 58 public void testSimple() throws InterruptedException { 59 lock=new ReentrantWriterPreferenceReadWriteLock(); 60 lock.readLock().acquire(); 61 lock.readLock().acquire(); 62 lock.writeLock().acquire(); 63 lock.writeLock().acquire(); 64 } 65 66 67 public void testOneWriterMultipleReaders() throws InterruptedException { 68 lock=new ReentrantWriterPreferenceReadWriteLock(); 69 70 Writer writer=new Writer("writer"); 71 Reader reader1=new Reader("reader1"); 72 Reader reader2=new Reader("reader2"); 73 74 writer.start(); 75 reader1.start(); 76 reader2.start(); 77 78 writer.join(); 79 reader1.join(); 80 reader2.join(); 81 } 82 83 class Writer extends Thread { 84 85 public Writer(String name) { 86 super(name); 87 } 88 89 public void run() { 90 try { 91 log("acquiring WL"); 92 lock.writeLock().acquire(); 93 log("acquired WL successfully"); 94 sleep(1000); 95 } 96 catch(InterruptedException e) { 97 ex=e; 98 } 99 finally { 100 log("releasing WL"); 101 lock.writeLock().release(); 102 } 103 } 104 } 105 106 107 class Reader extends Thread { 108 109 public Reader(String name) { 110 super(name); 111 } 112 113 114 public void run() { 115 try { 116 log("acquiring RL"); 117 lock.readLock().acquire(); 118 log("acquired RL successfully"); 119 sleep(500); 120 } 121 catch(InterruptedException e) { 122 ex=e; 123 } 124 finally { 125 log("releasing RL"); 126 lock.readLock().release(); 127 } 128 } 129 } 130 131 132 static void sleep(long timeout) { 133 try { 134 Thread.sleep(timeout); 135 } 136 catch(InterruptedException e) { 137 } 138 } 139 140 static void log(String msg) { 141 System.out.println(Thread.currentThread().getName() + ": " + msg); 142 } 143 144 public static Test suite() { 145 return new TestSuite(ReadWriteLockTest.class); 146 } 147 148 public static void main(String [] args) { 149 junit.textui.TestRunner.run(suite()); 150 } 151 152 } 153 | Popular Tags |