001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.core.util;
017:
018: import junit.framework.TestCase;
019:
020: import org.columba.core.base.Semaphore;
021: import org.columba.core.base.StopWatch;
022:
023: /**
024: * @author redsolo
025: */
026: public class SemaphoreTest extends TestCase {
027:
028: /**
029: * Tests the constructors.
030: */
031: public void testConstructors() {
032: Semaphore semaphore = new Semaphore();
033: assertTrue("The semaphore is not holding.", semaphore
034: .isHolding());
035:
036: semaphore.hold();
037: assertTrue("The semaphore is not holding.", semaphore
038: .isHolding());
039:
040: semaphore.release();
041: assertTrue("The semaphore has not been released", !semaphore
042: .isHolding());
043:
044: semaphore = new Semaphore(false);
045: assertTrue("The semaphore is holding.", !semaphore.isHolding());
046:
047: semaphore.hold();
048: assertTrue("The semaphore is not holding.", semaphore
049: .isHolding());
050:
051: semaphore.release();
052: assertTrue("The semaphore has not been released", !semaphore
053: .isHolding());
054: }
055:
056: /**
057: * Test the hold() and release() methods.
058: */
059: public void testHold() {
060: Semaphore semaphore = new Semaphore();
061: assertTrue("The semaphore is not holding.", semaphore
062: .isHolding());
063:
064: semaphore.hold();
065: assertTrue("The semaphore is not holding.", semaphore
066: .isHolding());
067:
068: semaphore.release();
069: assertTrue("The semaphore has not been released", !semaphore
070: .isHolding());
071:
072: semaphore.hold();
073: assertTrue("The semaphore is not holding.", semaphore
074: .isHolding());
075: }
076:
077: /**
078: * Test a single thread wait on the semaphore.
079: * @throws InterruptedException thrown for any good reason if the test failed.
080: */
081: public void testSingleWait() throws InterruptedException {
082: Semaphore semaphore = new Semaphore();
083:
084: StopWatch timer = new StopWatch();
085: semaphore.waitUntilReleased(50);
086: if (timer.getTiming() < 25) {
087: fail("Single thread did not wait for semaphore.");
088: }
089:
090: semaphore.hold();
091: timer.start();
092: semaphore.waitUntilReleased(50);
093: if (timer.getTiming() < 25) {
094: fail("Single thread did not wait for semaphore.");
095: }
096:
097: semaphore.release();
098: timer.start();
099: semaphore.waitUntilReleased(100);
100: if (timer.getTiming() > 25) {
101: fail("Single thread did wait for semaphore.");
102: }
103: }
104: }
|