001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.test;
028:
029: import com.sun.cldc.util.Semaphore;
030: import com.sun.midp.i3test.*;
031:
032: public class TestSemaphore extends TestCase {
033:
034: Semaphore sema;
035:
036: // local methods and classes
037:
038: void sleep(int s) {
039: try {
040: Thread.sleep(s);
041: } catch (InterruptedException ignore) {
042: }
043: }
044:
045: class Blocker extends Thread {
046: Semaphore sema;
047: boolean isBlocked = false;
048:
049: Blocker(Semaphore newSema) {
050: sema = newSema;
051: }
052:
053: public void run() {
054: isBlocked = true;
055: sema.acquire();
056: isBlocked = false;
057: }
058: }
059:
060: // the tests
061:
062: void testOne() {
063: Semaphore sema = new Semaphore(1);
064: sema.acquire();
065: assertTrue(true);
066: }
067:
068: void testTwo() {
069: Semaphore sema = new Semaphore(0);
070: sema.release();
071: sema.acquire();
072: assertTrue(true);
073: }
074:
075: void testBlock() {
076: sema = new Semaphore(0);
077:
078: Blocker t1 = new Blocker(sema);
079:
080: assertTrue(!t1.isBlocked);
081: t1.start();
082: sleep(100);
083: assertTrue(t1.isBlocked);
084: sema.release();
085: try {
086: t1.join();
087: } catch (InterruptedException ignore) {
088: }
089: assertTrue(!t1.isBlocked);
090:
091: sema = null;
092: }
093:
094: void testManyBlock() {
095: sema = new Semaphore(0);
096: final int NTHREADS = 10;
097:
098: Blocker ta[] = new Blocker[NTHREADS];
099: for (int i = 0; i < NTHREADS; i++) {
100: ta[i] = new Blocker(sema);
101: }
102:
103: for (int i = 0; i < NTHREADS; i++) {
104: assertTrue("blocked initially", !ta[i].isBlocked);
105: }
106:
107: for (int i = 0; i < NTHREADS; i++) {
108: ta[i].start();
109: }
110:
111: sleep(100);
112:
113: for (int i = 0; i < NTHREADS; i++) {
114: assertTrue("not blocked after start", ta[i].isBlocked);
115: }
116:
117: sema.release();
118: sema.release();
119: sema.release();
120:
121: sleep(100);
122:
123: int count = 0;
124: for (int i = 0; i < NTHREADS; i++) {
125: if (ta[i].isBlocked)
126: ++count;
127: }
128:
129: assertEquals("blocked " + count + "instead of 7", 7, count);
130:
131: sema.release();
132: sema.release();
133: sema.release();
134: sema.release();
135: sema.release();
136: sema.release();
137: sema.release();
138:
139: sleep(100);
140:
141: count = 0;
142: for (int i = 0; i < NTHREADS; i++) {
143: if (ta[i].isBlocked)
144: ++count;
145: }
146:
147: assertEquals("all not unblocked", 0, count);
148:
149: for (int i = 0; i < NTHREADS; i++) {
150: try {
151: ta[i].join();
152: } catch (InterruptedException ignore) {
153: }
154: }
155: }
156:
157: public void runTests() {
158: declare("testOne");
159: testOne();
160: declare("testTwo");
161: testTwo();
162: declare("testBlock");
163: testBlock();
164: declare("testManyBlock");
165: testManyBlock();
166: }
167:
168: }
|