001: //
002: // Copyright (C) 2005 United States Government as represented by the
003: // Administrator of the National Aeronautics and Space Administration
004: // (NASA). All Rights Reserved.
005: //
006: // This software is distributed under the NASA Open Source Agreement
007: // (NOSA), version 1.3. The NOSA has been approved by the Open Source
008: // Initiative. See the file NOSA-1.3-JPF at the top of the distribution
009: // directory tree for the complete NOSA document.
010: //
011: // THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
012: // KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
013: // LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
014: // SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
015: // A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
016: // THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
017: // DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
018: //
019: package gov.nasa.jpf.jvm;
020:
021: /**
022: * threading test
023: */
024: public class TestThread {
025: static String didRunThread = null;
026:
027: public static void main(String[] args) {
028: TestThread t = new TestThread();
029:
030: if (args.length > 0) {
031: // just run the specified tests
032: for (int i = 0; i < args.length; i++) {
033: String func = args[i];
034:
035: // note that we don't use reflection here because this would
036: // blow up execution/test scope under JPF
037: if ("testMain".equals(func)) {
038: t.testMain();
039: } else if ("testName".equals(func)) {
040: t.testName();
041: } else if ("testPriority".equals(func)) {
042: t.testPriority();
043: } else if ("testDaemon".equals(func)) {
044: t.testDaemon();
045: } else if ("testSingleYield".equals(func)) {
046: t.testSingleYield();
047: } else if ("testYield".equals(func)) {
048: t.testYield();
049: } else {
050: throw new IllegalArgumentException(
051: "unknown test function");
052: }
053: }
054: } else {
055: // that's mainly for our standalone test verification
056: t.testMain();
057: t.testName();
058: t.testPriority();
059: t.testDaemon();
060: t.testSingleYield();
061: t.testYield();
062: }
063: }
064:
065: public void testDaemon() {
066: // we should also test the correct Daemon behavior at some point
067: // (running daemons not keeping the system alive)
068: Runnable r = new Runnable() {
069: public void run() {
070: Thread t = Thread.currentThread();
071:
072: if (!t.isDaemon()) {
073: throw new RuntimeException("isDaemon failed");
074: }
075:
076: didRunThread = t.getName();
077: }
078: };
079:
080: didRunThread = null;
081:
082: Thread t = new Thread(r);
083: t.setDaemon(true);
084: t.start();
085:
086: try {
087: t.join();
088: } catch (InterruptedException ix) {
089: throw new RuntimeException("thread was interrupted");
090: }
091:
092: String tname = Thread.currentThread().getName();
093: if ((didRunThread == null) || (didRunThread.equals(tname))) {
094: throw new RuntimeException("thread did not execute: "
095: + didRunThread);
096: }
097: }
098:
099: public void testMain() {
100: Thread t = Thread.currentThread();
101: String refName = "main";
102:
103: String name = t.getName();
104:
105: if (!name.equals(refName)) {
106: throw new RuntimeException("wrong main thread name, is: "
107: + name + ", expected: " + refName);
108: }
109:
110: refName = "my-main-thread";
111: t.setName(refName);
112: name = t.getName();
113:
114: if (!name.equals(refName)) {
115: throw new RuntimeException("Thread.setName() failed, is: "
116: + name + ", expected: " + refName);
117: }
118:
119: int refPrio = Thread.NORM_PRIORITY;
120: int prio = t.getPriority();
121:
122: if (prio != refPrio) {
123: throw new RuntimeException(
124: "main thread has no NORM_PRIORITY: " + prio);
125: }
126:
127: refPrio++;
128: t.setPriority(refPrio);
129: prio = t.getPriority();
130:
131: if (prio != refPrio) {
132: throw new RuntimeException(
133: "main thread setPriority failed: " + prio);
134: }
135:
136: if (t.isDaemon()) {
137: throw new RuntimeException("main thread is daemon");
138: }
139: }
140:
141: public void testName() {
142: Runnable r = new Runnable() {
143: public void run() {
144: Thread t = Thread.currentThread();
145: String name = t.getName();
146:
147: if (!name.equals("my-thread")) {
148: throw new RuntimeException("wrong Thread name: "
149: + name);
150: }
151:
152: didRunThread = name;
153: }
154: };
155:
156: didRunThread = null;
157:
158: Thread t = new Thread(r, "my-thread");
159:
160: //Thread t = new Thread(r);
161: t.start();
162:
163: try {
164: t.join();
165: } catch (InterruptedException ix) {
166: throw new RuntimeException("thread was interrupted");
167: }
168:
169: String tname = Thread.currentThread().getName();
170: if ((didRunThread == null) || (didRunThread.equals(tname))) {
171: throw new RuntimeException("thread did not execute: "
172: + didRunThread);
173: }
174: }
175:
176: public void testSingleYield() {
177: Thread.yield();
178: }
179:
180: public void testYield() {
181: Runnable r = new Runnable() {
182: public void run() {
183: Thread t = Thread.currentThread();
184:
185: while (!didRunThread.equals("blah")) {
186: Thread.yield();
187: }
188:
189: didRunThread = t.getName();
190: }
191: };
192:
193: didRunThread = "blah";
194:
195: Thread t = new Thread(r);
196: t.start();
197:
198: while (didRunThread.equals("blah")) {
199: Thread.yield();
200: }
201:
202: String tname = Thread.currentThread().getName();
203: if ((didRunThread == null) || (didRunThread.equals(tname))) {
204: throw new RuntimeException("thread did not execute: "
205: + didRunThread);
206: }
207: }
208:
209: public void testPriority() {
210: Runnable r = new Runnable() {
211: public void run() {
212: Thread t = Thread.currentThread();
213: int prio = t.getPriority();
214:
215: if (prio != (Thread.MIN_PRIORITY + 2)) {
216: throw new RuntimeException(
217: "wrong Thread priority: " + prio);
218: }
219:
220: didRunThread = t.getName();
221: }
222: };
223:
224: didRunThread = null;
225:
226: Thread t = new Thread(r);
227: t.setPriority(Thread.MIN_PRIORITY + 2);
228: t.start();
229:
230: try {
231: t.join();
232: } catch (InterruptedException ix) {
233: throw new RuntimeException("thread was interrupted");
234: }
235:
236: String tname = Thread.currentThread().getName();
237: if ((didRunThread == null) || (didRunThread.equals(tname))) {
238: throw new RuntimeException("thread did not execute: "
239: + didRunThread);
240: }
241: }
242: }
|