25 * questions.
26 */
27 package com.sun.javatest;
28
29 import java.io.PrintWriter;
30 import java.util.HashSet;
31 import java.util.Iterator;
32 import java.util.Set;
33
34 import com.sun.javatest.util.BackupPolicy;
35 import com.sun.javatest.util.I18NResourceBundle;
36
37 /**
38 * Traditional implementation of the test execution engine which has been
39 * used throughout the JT Harness 2.x harness. It supplies all the basic
40 * for creating threads for each test, running the <code>Script</code>,
41 * and handling timeouts.
42 */
43 public class DefaultTestRunner extends TestRunner
44 {
45 public synchronized boolean runTests(Iterator testIter)
46 throws InterruptedException
47 {
48 this.testIter = testIter;
49
50 Thread[] threads = new Thread[getConcurrency()];
51 activeThreads = new HashSet<>();
52 allPassed = true;
53
54 try {
55 int n = 0;
56 while (!stopping) {
57 for (int i = 0; i < threads.length; i++) {
58 Thread t = threads[i];
59 if (t == null || !activeThreads.contains(t)) {
60 int prio = Math.max(Thread.MIN_PRIORITY, Thread.currentThread().getPriority() - 1);
61 t = new Thread() {
62 public void run() {
63 try {
64 TestDescription td;
65 while ((td = nextTest()) != null) {
84 wait();
85 }
86 // Wait for all the threads to finish so they don't get nuked by the
87 // finally code. Order is not important so just wait for them one at a time.
88 // Note we can't simply join with the thread because that gives a deadlock
89 // on our lock.
90 for (int i = 0; i < threads.length; i++) {
91 if (threads[i] != null) {
92 while (activeThreads.contains(threads[i]))
93 wait();
94 threads[i] = null;
95 }
96 }
97 }
98 catch (InterruptedException ex) {
99 // The thread has been interrupted
100
101 stopping = true; // stop workers from starting any new tests
102
103 // interrupt the worker threads
104 for (Iterator iter = activeThreads.iterator() ; iter.hasNext(); ) {
105 Thread t = (Thread) (iter.next());
106 t.interrupt();
107 }
108
109 // while a short while (a couple of seconds) for tests to clean up
110 // before we nuke them
111 long now = System.currentTimeMillis();
112 try {
113 while (activeThreads.size() > 0 && (System.currentTimeMillis() - now < 2000)) {
114 wait(100);
115 }
116 }
117 catch (InterruptedException e) {
118 }
119
120 // rethrow the original exception so the caller knows what's happened
121 throw ex;
122 }
123 finally {
124 // ensure all child threads killed
125 for (int i = 0; i < threads.length; i++) {
126 if (threads[i] != null)
127 Deprecated.invokeThreadStop(threads[i]);
128 }
129 }
130
131 return allPassed;
132 }
133
134 private synchronized void threadExiting(Thread t) {
135 activeThreads.remove(t);
136 notifyAll();
137 }
138
139 private synchronized TestDescription nextTest() {
140 if (stopping)
141 return null;
142
143 if (testIter.hasNext())
144 return (TestDescription) (testIter.next());
145 else {
146 stopping = true;
147 return null;
148 }
149 }
150
151 private boolean runTest(TestDescription td) {
152 WorkDirectory workDir = getWorkDirectory();
153 TestResult result = null;
154
155 boolean scriptUsesNotifier = false;
156
157 try {
158 TestSuite testSuite = getTestSuite();
159 TestEnvironment env = getEnvironment();
160 BackupPolicy backupPolicy = getBackupPolicy();
161
162 String[] exclTestCases = getExcludedTestCases(td);
163 Script s = testSuite.createScript(td, exclTestCases, env.copy(), workDir, backupPolicy);
164
237 new Object[] {td.getRootRelativeURL(), e, EXCEPTION });
238 }
239 return tr;
240 }
241
242 private Integer classifyThrowable(Throwable t) {
243 if (t instanceof Exception)
244 return EXCEPTION;
245 else if (t instanceof Error)
246 return ERROR;
247 else
248 return THROWABLE;
249 }
250
251 // constants used by classifyThrowable and i18n key unexpectedThrowable
252 private static final Integer EXCEPTION = new Integer(0);
253 private static final Integer ERROR = new Integer(1);
254 private static final Integer THROWABLE = new Integer(2);
255
256
257 private Iterator testIter;
258 private Set<Thread> activeThreads;
259 private boolean allPassed;
260 private boolean stopping;
261
262 private static I18NResourceBundle i18n = I18NResourceBundle.getBundleForClass(DefaultTestRunner.class);
263 }
|
25 * questions.
26 */
27 package com.sun.javatest;
28
29 import java.io.PrintWriter;
30 import java.util.HashSet;
31 import java.util.Iterator;
32 import java.util.Set;
33
34 import com.sun.javatest.util.BackupPolicy;
35 import com.sun.javatest.util.I18NResourceBundle;
36
37 /**
38 * Traditional implementation of the test execution engine which has been
39 * used throughout the JT Harness 2.x harness. It supplies all the basic
40 * for creating threads for each test, running the <code>Script</code>,
41 * and handling timeouts.
42 */
43 public class DefaultTestRunner extends TestRunner
44 {
45 public synchronized boolean runTests(Iterator<TestDescription> testIter)
46 throws InterruptedException
47 {
48 this.testIter = testIter;
49
50 Thread[] threads = new Thread[getConcurrency()];
51 activeThreads = new HashSet<>();
52 allPassed = true;
53
54 try {
55 int n = 0;
56 while (!stopping) {
57 for (int i = 0; i < threads.length; i++) {
58 Thread t = threads[i];
59 if (t == null || !activeThreads.contains(t)) {
60 int prio = Math.max(Thread.MIN_PRIORITY, Thread.currentThread().getPriority() - 1);
61 t = new Thread() {
62 public void run() {
63 try {
64 TestDescription td;
65 while ((td = nextTest()) != null) {
84 wait();
85 }
86 // Wait for all the threads to finish so they don't get nuked by the
87 // finally code. Order is not important so just wait for them one at a time.
88 // Note we can't simply join with the thread because that gives a deadlock
89 // on our lock.
90 for (int i = 0; i < threads.length; i++) {
91 if (threads[i] != null) {
92 while (activeThreads.contains(threads[i]))
93 wait();
94 threads[i] = null;
95 }
96 }
97 }
98 catch (InterruptedException ex) {
99 // The thread has been interrupted
100
101 stopping = true; // stop workers from starting any new tests
102
103 // interrupt the worker threads
104 for (Thread t : activeThreads) {
105 t.interrupt();
106 }
107
108 // while a short while (a couple of seconds) for tests to clean up
109 // before we nuke them
110 long now = System.currentTimeMillis();
111 try {
112 while (activeThreads.size() > 0 && (System.currentTimeMillis() - now < 2000)) {
113 wait(100);
114 }
115 }
116 catch (InterruptedException e) {
117 }
118
119 // rethrow the original exception so the caller knows what's happened
120 throw ex;
121 }
122 finally {
123 // ensure all child threads killed
124 for (int i = 0; i < threads.length; i++) {
125 if (threads[i] != null)
126 Deprecated.invokeThreadStop(threads[i]);
127 }
128 }
129
130 return allPassed;
131 }
132
133 private synchronized void threadExiting(Thread t) {
134 activeThreads.remove(t);
135 notifyAll();
136 }
137
138 private synchronized TestDescription nextTest() {
139 if (stopping)
140 return null;
141
142 if (testIter.hasNext())
143 return (testIter.next());
144 else {
145 stopping = true;
146 return null;
147 }
148 }
149
150 private boolean runTest(TestDescription td) {
151 WorkDirectory workDir = getWorkDirectory();
152 TestResult result = null;
153
154 boolean scriptUsesNotifier = false;
155
156 try {
157 TestSuite testSuite = getTestSuite();
158 TestEnvironment env = getEnvironment();
159 BackupPolicy backupPolicy = getBackupPolicy();
160
161 String[] exclTestCases = getExcludedTestCases(td);
162 Script s = testSuite.createScript(td, exclTestCases, env.copy(), workDir, backupPolicy);
163
236 new Object[] {td.getRootRelativeURL(), e, EXCEPTION });
237 }
238 return tr;
239 }
240
241 private Integer classifyThrowable(Throwable t) {
242 if (t instanceof Exception)
243 return EXCEPTION;
244 else if (t instanceof Error)
245 return ERROR;
246 else
247 return THROWABLE;
248 }
249
250 // constants used by classifyThrowable and i18n key unexpectedThrowable
251 private static final Integer EXCEPTION = new Integer(0);
252 private static final Integer ERROR = new Integer(1);
253 private static final Integer THROWABLE = new Integer(2);
254
255
256 private Iterator<TestDescription> testIter;
257 private Set<Thread> activeThreads;
258 private boolean allPassed;
259 private boolean stopping;
260
261 private static I18NResourceBundle i18n = I18NResourceBundle.getBundleForClass(DefaultTestRunner.class);
262 }
|