001: /*
002: * CoadunationUtil: The coaduntion utility library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * ObjectLockFactoryTest.java
020: */
021:
022: package com.rift.coad.util.lock;
023:
024: import junit.framework.*;
025: import java.util.Map;
026: import java.util.concurrent.ConcurrentHashMap;
027: import org.apache.log4j.Logger;
028: import org.apache.log4j.BasicConfigurator;
029:
030: /**
031: *
032: *
033: * @author Brett Chaldecott
034: */
035: public class ObjectLockFactoryTest extends TestCase {
036:
037: private String lockObject = "lock object";
038: private String lockObject2 = "lock object2";
039: private boolean hasLock = false;
040: private long lockCount = 0;
041:
042: public ObjectLockFactoryTest(String testName) {
043: super (testName);
044: BasicConfigurator.configure();
045: }
046:
047: protected void setUp() throws Exception {
048: }
049:
050: protected void tearDown() throws Exception {
051: }
052:
053: /**
054: * Test of of class com.rift.coad.util.lock.ObjectLockFactory.
055: */
056: public void testObjectLockFactory() throws Exception {
057: System.out.println("getInstance");
058:
059: try {
060: ObjectLockFactory.getInstance();
061: fail("Could retrieve an reference without calling init");
062: } catch (LockException ex) {
063: System.out.println(ex.getMessage());
064: }
065:
066: ObjectLockFactory.init();
067:
068: ObjectLockFactory expResult = ObjectLockFactory.getInstance();
069: ObjectLockFactory result = ObjectLockFactory.getInstance();
070: assertEquals(expResult, result);
071:
072: System.out.println("Aquire write lock");
073: LockRef ref = result.acquireWriteLock(lockObject);
074: System.out.println("Test lock");
075: assertEquals(ref.getKey(), lockObject);
076: System.out.println("Test thread id");
077: assertEquals(ref.getThreadId(), Thread.currentThread().getId());
078:
079: System.out.println("Aquire lock");
080: ref = result.acquireWriteLock(lockObject, "test");
081:
082: System.out.println("Test the lock");
083: assertEquals(ref.getKey(), lockObject);
084: assertEquals(ref.getThreadId(), Thread.currentThread().getId());
085: assertEquals(ref.getLockName(), "test");
086:
087: Thread testThread = new Thread(new Runnable() {
088: public void run() {
089: try {
090: LockRef ref2 = ObjectLockFactory.getInstance()
091: .acquireWriteLock(lockObject, "test2");
092: hasLock = true;
093: ref2.release();
094: } catch (Exception ex) {
095: System.out.println("Failed to aquire the lock : "
096: + ex.getMessage());
097: ex.printStackTrace(System.out);
098: }
099: }
100: });
101: testThread.start();
102:
103: System.out.println("Test a seperate thread");
104: Thread.sleep(1000);
105: if (hasLock == true) {
106: fail("Was able to get lock");
107: }
108:
109: System.out.println("Attempt to release");
110: ref.release();
111: ref.release();
112:
113: Thread.sleep(500);
114: if (hasLock == false) {
115: fail("Was not able to get lock");
116: }
117:
118: System.out.println("Aquire new lock");
119: ref = result.acquireWriteLock(lockObject, "test2");
120: hasLock = false;
121: System.out.println("Create second thread");
122: testThread = new Thread(new Runnable() {
123: public void run() {
124: try {
125: LockRef ref2 = ObjectLockFactory.getInstance()
126: .acquireWriteLock(lockObject,
127: ObjectLockFactory.WAIT_ON_THREAD);
128: hasLock = true;
129: ref2.release();
130: } catch (Exception ex) {
131: System.out.println("Failed to aquire the lock : "
132: + ex.getMessage());
133: ex.printStackTrace(System.out);
134: }
135: }
136: });
137: testThread.start();
138: System.out.println("Sleep");
139: Thread.sleep(1000);
140: if (hasLock == true) {
141: fail("Was able to get lock");
142: }
143:
144: System.out.println("Release");
145: ref.release();
146:
147: Thread.sleep(500);
148: if (hasLock == true) {
149: fail("Was able to get lock");
150: }
151:
152: // wait for a named lock
153: ref = result.acquireWriteLock(lockObject, "test2");
154: hasLock = false;
155: testThread = new Thread(new Runnable() {
156: public void run() {
157: try {
158: LockRef ref2 = ObjectLockFactory.getInstance()
159: .acquireWriteLock(lockObject, "test2",
160: ObjectLockFactory.WAIT_ON_NAMED);
161: hasLock = true;
162: ref2.release();
163: } catch (Exception ex) {
164: System.out.println("Failed to aquire the lock : "
165: + ex.getMessage());
166: ex.printStackTrace(System.out);
167: }
168: }
169: });
170: testThread.start();
171:
172: Thread.sleep(1000);
173: if (hasLock == false) {
174: fail("Was not able to acquire the lock");
175: }
176:
177: ref.release();
178:
179: // attempt to aquire a lock on two different objects
180: System.out.println("Aquire two different locks");
181: ref = result.acquireWriteLock(lockObject, "test2");
182: hasLock = false;
183: testThread = new Thread(new Runnable() {
184: public void run() {
185: try {
186: LockRef ref2 = ObjectLockFactory.getInstance()
187: .acquireWriteLock(lockObject2, "test2",
188: ObjectLockFactory.WAIT_ON_NAMED);
189: hasLock = true;
190: ref2.release();
191: } catch (Exception ex) {
192: System.out.println("Failed to aquire the lock : "
193: + ex.getMessage());
194: ex.printStackTrace(System.out);
195: }
196: }
197: });
198: testThread.start();
199:
200: Thread.sleep(1000);
201: if (hasLock == false) {
202: fail("Was not able to acquire the lock");
203: }
204:
205: ref.release();
206:
207: // test the read
208: Thread readThread1 = new Thread(new Runnable() {
209: public void run() {
210: try {
211: LockRef ref2 = ObjectLockFactory.getInstance()
212: .acquireReadLock(lockObject);
213: lockCount++;
214: Thread.sleep(1000);
215: System.out.println("Read1: Release lock");
216: ref2.release();
217: System.out.println("Read1: Released lock");
218: } catch (Exception ex) {
219: System.out.println("Failed to aquire the lock : "
220: + ex.getMessage());
221: ex.printStackTrace(System.out);
222: }
223: }
224: });
225:
226: Thread readThread2 = new Thread(new Runnable() {
227: public void run() {
228: try {
229: LockRef ref2 = ObjectLockFactory.getInstance()
230: .acquireReadLock(lockObject);
231: lockCount++;
232: Thread.sleep(1000);
233: System.out.println("Read2: Release lock");
234: ref2.release();
235: System.out.println("Read2: Released lock");
236: } catch (Exception ex) {
237: System.out.println("Failed to aquire the lock : "
238: + ex.getMessage());
239: ex.printStackTrace(System.out);
240: }
241: }
242: });
243:
244: readThread1.start();
245: readThread2.start();
246:
247: Thread.sleep(300);
248: if (lockCount != 2) {
249: fail("Aquire the lock");
250: }
251:
252: System.out.println("Wait for lock");
253: ref = result.acquireWriteLock(lockObject);
254: System.out.println("1: Acquired lock");
255:
256: // test the read
257: readThread1 = new Thread(new Runnable() {
258: public void run() {
259: try {
260: System.out.println("Read1: Acquired lock");
261: LockRef ref2 = ObjectLockFactory.getInstance()
262: .acquireReadLock(lockObject);
263: System.out.println("Read1: Acquired lock");
264: lockCount++;
265: Thread.sleep(1000);
266: ref2.release();
267: System.out.println("Released lock");
268: } catch (Exception ex) {
269: System.out.println("Failed to aquire the lock : "
270: + ex.getMessage());
271: ex.printStackTrace(System.out);
272: }
273: }
274: });
275:
276: readThread2 = new Thread(new Runnable() {
277: public void run() {
278: try {
279: System.out.println("Read2: Acquire lock");
280: LockRef ref2 = ObjectLockFactory.getInstance()
281: .acquireReadLock(lockObject);
282: System.out.println("Read2: Acquired lock");
283: lockCount++;
284: Thread.sleep(1000);
285: ref2.release();
286: System.out.println("Released lock");
287: } catch (Exception ex) {
288: System.out.println("Failed to aquire the lock : "
289: + ex.getMessage());
290: ex.printStackTrace(System.out);
291: }
292: }
293: });
294:
295: lockCount = 0;
296: readThread1.start();
297: readThread2.start();
298:
299: Thread.sleep(300);
300: if (lockCount != 0) {
301: fail("The could aquire the lock");
302: }
303:
304: ref.release();
305:
306: Thread.sleep(2000);
307: if (lockCount != 2) {
308: fail("The could not acquire the lock : " + lockCount);
309: }
310:
311: // test the read
312: readThread1 = new Thread(new Runnable() {
313: public void run() {
314: try {
315: LockRef ref2 = ObjectLockFactory.getInstance()
316: .acquireReadLock(lockObject, "test");
317: lockCount++;
318: Thread.sleep(1000);
319: ref2.release();
320: System.out.println("Released lock");
321: } catch (Exception ex) {
322: System.out.println("Failed to aquire the lock : "
323: + ex.getMessage());
324: ex.printStackTrace(System.out);
325: }
326: }
327: });
328:
329: readThread2 = new Thread(new Runnable() {
330: public void run() {
331: try {
332: LockRef ref2 = ObjectLockFactory.getInstance()
333: .acquireReadLock(lockObject, "test");
334: lockCount++;
335: Thread.sleep(1000);
336: ref2.release();
337: System.out.println("Released lock");
338: } catch (Exception ex) {
339: System.out.println("Failed to aquire the lock : "
340: + ex.getMessage());
341: ex.printStackTrace(System.out);
342: }
343: }
344: });
345:
346: lockCount = 0;
347: readThread1.start();
348: readThread2.start();
349:
350: Thread.sleep(300);
351: if (lockCount != 2) {
352: fail("The could not aquire the lock");
353: }
354:
355: System.out.println("Wait for lock");
356: ref = result.acquireWriteLock(lockObject);
357: System.out.println("Acquired lock");
358:
359: // test the read
360: readThread1 = new Thread(new Runnable() {
361: public void run() {
362: try {
363: LockRef ref2 = ObjectLockFactory.getInstance()
364: .acquireReadLock(lockObject, "test");
365: lockCount++;
366: Thread.sleep(1000);
367: ref2.release();
368: System.out.println("Released lock");
369: } catch (Exception ex) {
370: System.out.println("Failed to aquire the lock : "
371: + ex.getMessage());
372: ex.printStackTrace(System.out);
373: }
374: }
375: });
376:
377: readThread2 = new Thread(new Runnable() {
378: public void run() {
379: try {
380: LockRef ref2 = ObjectLockFactory.getInstance()
381: .acquireReadLock(lockObject, "test");
382: lockCount++;
383: Thread.sleep(1000);
384: ref2.release();
385: System.out.println("Released lock");
386: } catch (Exception ex) {
387: System.out.println("Failed to aquire the lock : "
388: + ex.getMessage());
389: ex.printStackTrace(System.out);
390: }
391: }
392: });
393:
394: lockCount = 0;
395: readThread1.start();
396: readThread2.start();
397:
398: Thread.sleep(300);
399: if (lockCount != 0) {
400: fail("The could aquire the lock");
401: }
402:
403: ref.release();
404:
405: Thread.sleep(2000);
406: if (lockCount != 2) {
407: fail("The could not acquire the lock");
408: }
409:
410: ObjectLockFactory.fin();
411: try {
412: ObjectLockFactory.getInstance();
413: fail("Could retrieve an reference without after calling fin");
414: } catch (LockException ex) {
415: System.out.println(ex.getMessage());
416: }
417: }
418:
419: }
|