KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cache > stress > ReadWriteLockWithUpgradeStressTestCase


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

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 /**
17  * Stress test for RWLock with upgrade.
18  */

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 JavaDoc name)
29    {
30       super(name);
31    }
32
33
34    public static void main(String JavaDoc[] args) throws Exception JavaDoc
35    {
36       log("\nBeginning ReadWriteLockWithUpgrade automated testing ...\n");
37
38       junit.textui.TestRunner.run(suite());
39    }
40
41    // Needed for JUnit.
42
public static Test suite()
43    {
44       TestSuite suite = new TestSuite();
45       // Adding test cases here ...
46
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 JavaDoc readAttemptThread(String JavaDoc name, final long msecs)
62    {
63       return new Thread JavaDoc(name)
64       {
65          public void run()
66          {
67             java.util.Random JavaDoc rand = new java.util.Random JavaDoc(++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 // fail("Read lock attempt failed.");
77
counter++;
78                      continue;
79                   }
80                   log("acquired read lock");
81                   _sleep(duration);
82                   log("released read lock");
83                } catch (Exception JavaDoc ex) {
84                }
85                finally {
86                   rlock.release();
87                }
88             }
89          }
90       };
91    }
92
93    protected Thread JavaDoc writeAttemptThread(String JavaDoc name, final long msecs)
94    {
95       return new Thread JavaDoc(name)
96       {
97          public void run()
98          {
99             java.util.Random JavaDoc rand = new java.util.Random JavaDoc(++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 // fail("Write lock attempt failed.");
109
counter++;
110                      continue;
111                   }
112                   log("acquired write lock");
113                   _sleep(duration);
114                   log("released write lock");
115                } catch (Exception JavaDoc ex) {
116                }
117                finally {
118                   wlock.release();
119                }
120             }
121          }
122       };
123    }
124
125    protected Thread JavaDoc upgradeAttemptThread(String JavaDoc name, final long msecs)
126    {
127       return new Thread JavaDoc(name)
128       {
129          public void run()
130          {
131             java.util.Random JavaDoc rand = new java.util.Random JavaDoc(++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 // fail("Read lock attempt failed.");
142
counter++;
143                      continue;
144                   }
145                   log("Acquired read lock for upgrade later");
146                   _sleep(duration / 2);
147                   // upgrade lock. Note that read lock will be released
148
// and write lock be acquired automatically.
149
wlock = lock_.upgradeLockAttempt(msecs);
150                   if (wlock == null) {
151                      log("upgrade lock attempt failed");
152 // fail("upgrade lock attempt failed");
153
rlock.release();
154                      counter++;
155                   }
156
157                   log("Upgraded write lock");
158                   _sleep(duration);
159                   log("released write lock");
160                } catch (Exception JavaDoc 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 JavaDoc e) {
176          e.printStackTrace();
177       }
178    }
179
180    public void testWriteWriteAttempt() throws Exception JavaDoc
181    {
182       log("testWriteWriteAttempt() ...");
183       final long msecs = 1000;
184       Thread JavaDoc t1 = writeAttemptThread("t1-write", msecs);
185       Thread JavaDoc t2 = writeAttemptThread("t2-write", msecs);
186       Thread JavaDoc t3 = writeAttemptThread("t3-write", msecs);
187       Thread JavaDoc 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 JavaDoc
202    {
203       log("testReadWriteAttemp1() ...");
204       final long msecs = 2000;
205       Thread JavaDoc t1 = readAttemptThread("t1-read", msecs);
206       Thread JavaDoc t2 = readAttemptThread("t2-read", msecs);
207       Thread JavaDoc t3 = writeAttemptThread("t3-write", msecs);
208       Thread JavaDoc t4 = writeAttemptThread("t4-write", msecs);
209       Thread JavaDoc t5 = upgradeAttemptThread("t5-upgrade", msecs);
210       Thread JavaDoc 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 JavaDoc
229    {
230       log("**************");
231       log("testReadWriteAttemp2() ...");
232       log("**************");
233       SEED++;
234       final long msecs = 1000;
235       Thread JavaDoc t1 = readAttemptThread("t1-read", msecs);
236 // Thread t2 = readAttemptThread("t2-read", msecs);
237
Thread JavaDoc t3 = writeAttemptThread("t3-write", msecs);
238 // Thread t4 = writeAttemptThread("t4-write", msecs);
239
Thread JavaDoc t5 = upgradeAttemptThread("t5-upgrade", msecs);
240       Thread JavaDoc t6 = upgradeAttemptThread("t6-upgrade", msecs);
241
242       t1.start();
243 // t2.start();
244
t3.start();
245 // t4.start();
246
t5.start();
247       t6.start();
248       long timeout = 0;
249       t1.join(timeout);
250 // t2.join(timeout);
251
t3.join(timeout);
252 // t4.join(timeout);
253
t5.join(timeout);
254       t6.join(timeout);
255       checkCounter();
256    }
257
258    public void testReadWriteAttempt3() throws Exception JavaDoc
259    {
260       log("**************");
261       log("testReadWriteAttemp3() ...");
262       log("**************");
263       SEED++;
264       final long msecs = 1000;
265       Thread JavaDoc t1 = readAttemptThread("t1-read", msecs);
266       Thread JavaDoc t2 = readAttemptThread("t2-read", msecs);
267       Thread JavaDoc t3 = writeAttemptThread("t3-write", msecs);
268       Thread JavaDoc t4 = writeAttemptThread("t4-write", msecs);
269 // Thread t5 = upgradeAttemptThread("t5-upgrade", msecs);
270
Thread JavaDoc t6 = upgradeAttemptThread("t6-upgrade", msecs);
271
272       t1.start();
273       t2.start();
274       t3.start();
275       t4.start();
276 // t5.start();
277
t6.start();
278       long timeout = 0;
279       t1.join(timeout);
280       t2.join(timeout);
281       t3.join(timeout);
282       t4.join(timeout);
283 // t5.join(timeout);
284
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 JavaDoc str)
294    {
295 // System.out.println(Thread.currentThread() + ": " + " StressTestCase " + " : "
296
// +" : "+java.util.Calendar.getInstance().getTime() + " : " +str);
297
log_.debug(Thread.currentThread() + ": "
298             + " : " + str);
299    }
300 }
301
302
Popular Tags