File: MultiThreadedTestRunner.java

package info (click to toggle)
libgroboutils-java 5-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 9,396 kB
  • ctags: 11,186
  • sloc: java: 59,748; xml: 12,762; sh: 377; perl: 104; makefile: 20
file content (573 lines) | stat: -rw-r--r-- 19,498 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/*
 * @(#)MultiThreadedTestRunner.java
 *
 * The basics are taken from an article by Andy Schneider
 * andrew.schneider@javaworld.com
 * The article is "JUnit Best Practices"
 * http://www.javaworld.com/javaworld/jw-12-2000/jw-1221-junit_p.html
 *
 * Part of the GroboUtils package at:
 * http://groboutils.sourceforge.net
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a
 *  copy of this software and associated documentation files (the "Software"),
 *  to deal in the Software without restriction, including without limitation
 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
 *  and/or sell copies of the Software, and to permit persons to whom the
 *  Software is furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 *  DEALINGS IN THE SOFTWARE.
 */

package net.sourceforge.groboutils.junit.v1;

import org.apache.log4j.Logger;
import junit.framework.TestCase;
import junit.framework.TestResult;
import junit.framework.AssertionFailedError;
import junit.framework.Assert;

import java.lang.reflect.Method;


/**
 * A framework which allows for an array of tests to be
 * run asynchronously.  TestCases should reference this class in a test
 * method.
 * <P>
 * <B>Update for July 9, 2003:</B> now, you can also register
 * <tt>TestRunner</tt> instances as monitors (request 771008); these run
 * parallel with the standard <tt>TestRunner</tt> instances, but they only quit
 * when all of the standard <tt>TestRunner</tt> instances end.
 * <P>
 * Fixed bugs 771000 and 771001: spawned threads are now Daemon threads,
 * and all "wild threads" (threads that just won't stop) are
 * <tt>Thread.stop()</tt>ed.
 * <P>
 * All these changes have made this class rather fragile, as there are
 * many threaded timing issues to deal with.  Expect future refactoring
 * with backwards compatibility.
 *
 * @author    Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
 * @since     Jan 14, 2002
 * @version   $Date: 2003/10/03 14:26:45 $
 */
public class MultiThreadedTestRunner
{
    private static final Class THIS_CLASS = MultiThreadedTestRunner.class;
    private static final String THIS_CLASS_NAME = THIS_CLASS.getName();
    private static final Logger LOG = Logger.getLogger( THIS_CLASS );
    
    private static final long DEFAULT_MAX_FINAL_JOIN_TIME = 30l * 1000l;
    private static final long DEFAULT_MAX_WAIT_TIME = 24l * 60l * 60l * 1000l;
    private static final long MIN_WAIT_TIME = 10l;
    
    private Object synch = new Object();
    private boolean threadsFinished = false;
    private ThreadGroup threadGroup;
    private Thread coreThread;
    private Throwable exception;
    private TestRunnable runners[];
    private TestRunnable monitors[];
    private long maxFinalJoinTime = DEFAULT_MAX_FINAL_JOIN_TIME;
    private long maxWaitTime = DEFAULT_MAX_WAIT_TIME;
    private boolean performKills = true;
    
    
    /**
     * Create a new utility instance with the given set of parallel runners.
     * All runners passed into this method must end on their own, else it's
     * an error.
     */
    public MultiThreadedTestRunner( TestRunnable tr[] )
    {
        this( tr, null );
    }
    
    
    /**
     * Create a new utility instance with the given set of parallel runners
     * and a set of monitors.  The runners must end on their own, but the
     * monitors can run until told to stop.
     *
     * @param runners a non-null, non-empty collection of test runners.
     * @param monitors a list of monitor runners, which may be <tt>null</tt> or
     *      empty.
     */
    public MultiThreadedTestRunner( TestRunnable runners[],
            TestRunnable monitors[] )
    {
        if (runners == null)
        {
            throw new IllegalArgumentException("no null runners");
        }
        int len = runners.length;
        if (len <= 0)
        {
            throw new IllegalArgumentException(
                "must have at least one runnable");
        }
        this.runners = new TestRunnable[ len ];
        System.arraycopy( runners, 0, this.runners, 0, len );
        
        if (monitors != null)
        {
            len = monitors.length;
            this.monitors = new TestRunnable[ len ];
            System.arraycopy( monitors, 0, this.monitors, 0, len );
        }
        else
        {
            this.monitors = new TestRunnable[ 0 ];
        }
    }
    
    
    /**
     * Run each test given in a separate thread. Wait for each thread
     * to finish running, then return.
     * <P>
     * As of July 9, 2003, this method will not wait forever, but rather
     * will wait for the internal maximum run time, which is by default
     * 24 hours; for most unit testing scenarios, this is more than
     * sufficient.
     *
     * @exception Throwable thrown on a test run if a threaded task
     *      throws an exception.
     */
    public void runTestRunnables()
            throws Throwable
    {
        runTestRunnables( -1 );
    }
    
    
    /**
     * Runs each test given in a separate thread. Waits for each thread
     * to finish running (possibly killing them), then returns.
     *
     * @param runnables the list of TestCaseRunnable objects to run
     *      asynchronously
     * @param maxTime the maximum amount of milliseconds to wait for
     *      the tests to run. If the time is &lt;= 0, then the tests
     *      will run until they are complete. Otherwise, any threads that
     *      don't complete by the given number of milliseconds will be killed,
     *      and a failure will be thrown.
     * @exception Throwable thrown from the underlying tests if they happen
     *      to cause an error.
     */
    public void runTestRunnables( long maxTime )
            throws Throwable
    {
        // Ensure we aren't interrupted.
        // This can happen from one test execution to the next, if an
        // interrupt was poorly timed on the core thread.  Calling
        // Thread.interrupted() will clear the interrupted status flag.
        Thread.interrupted();
        
        // initialize the data.
        this.exception = null;
        this.coreThread = Thread.currentThread();
        this.threadGroup = new ThreadGroup( THIS_CLASS_NAME );
        this.threadsFinished = false;
        
        // start the monitors before the runners
        Thread monitorThreads[] = setupThreads(
            this.threadGroup, this.monitors );
        Thread runnerThreads[] = setupThreads(
            this.threadGroup, this.runners );
        
        // catch the IE exception outside the loop so that an exception
        // thrown in a thread will kill all the other threads.
        boolean threadsStillRunning;
        try
        {
            threadsStillRunning = joinThreads( runnerThreads, maxTime );
        }
        catch (InterruptedException ie)
        {
            // Thread join interrupted: some runner or monitor caused an
            // exception.  Note that this is NOT a timeout!
            threadsStillRunning = true;
        }
        finally
        {
            synchronized (this.synch)
            {
                if (!this.threadsFinished)
                {
                    interruptThreads();
                }
                else
                {
                    LOG.debug( "All threads finished within timeframe." );
                }
            }
        }
        
        if (threadsStillRunning)
        {
            LOG.debug( "Halting the test threads." );
            
            // threads are still running.  If no exception was generated,
            // then set a timeout error to indicate some threads didn't
            // end in time.
            setTimeoutError( maxTime );
            
            // kill any remaining threads
            try
            {
                // but give them one last chance!
                joinThreads( runnerThreads,
                    maxFinalJoinTime );
            }
            catch (InterruptedException ie)
            {
                // someone caused a real exception.  This is NOT a timeout!
            }
            int killCount = killThreads( runnerThreads );
            if (killCount > 0)
            {
                LOG.fatal( killCount+" thread(s) did not stop themselves." );
                setTimeoutError( maxFinalJoinTime );
            }
        }
        
        // Stop the monitor threads - they have a time limit!
        LOG.debug("Halting the monitor threads.");
        try
        {
            joinThreads( monitorThreads, maxFinalJoinTime );
        }
        catch (InterruptedException ex)
        {
            // don't cause a timeout error with monitor threads.
        }
        killThreads( monitorThreads );
        
        
        if (this.exception != null)
        {
            // an exception/error occurred during the test, so throw
            // the exception so it is reported by the owning test
            // correctly.
            LOG.debug( "Exception occurred during testing.", this.exception );
            throw this.exception;
        }
        LOG.debug( "No exceptions caused during execution." );
    }
    
    
    /**
     * Handles an exception by sending them to the test results.  Called by
     * runner or monitor threads.
     */
    void handleException( Throwable t )
    {
        LOG.warn( "A test thread caused an exception.", t );
        synchronized( this.synch )
        {
            if (this.exception == null)
            {
                LOG.debug("Setting the exception to:",t);
                this.exception = t;
            }
            
            if (!this.threadsFinished)
            {
                interruptThreads();
            }
        }
        
        if (t instanceof ThreadDeath)
        {
            // rethrow ThreadDeath after they have been registered
            // and the threads have been signaled to halt.
            throw (ThreadDeath)t;
        }
    }
    
    
    /**
     * Stops all running test threads.  Called by runner or monitor threads.
     */
    void interruptThreads()
    {
        LOG.debug("Forcing all test threads to stop.");
        synchronized (this.synch)
        {
            // interrupt the core thread (that might be doing a join)
            // first, so that it doesn't accidentally do a join on
            // other threads that were interrupted.
            if (Thread.currentThread() != this.coreThread)
            {
                this.coreThread.interrupt();
            }
            
            this.threadsFinished = true;
            
            int count = this.threadGroup.activeCount();
            Thread t[] = new Thread[ count ];
            this.threadGroup.enumerate( t );
            for (int i = t.length; --i >= 0;)
            {
                if (t[i] != null && t[i].isAlive())
                {
                    t[i].interrupt();
                }
            }
        }
    }
    
    
    /**
     * Used by the TestRunnable instances to tell if the parallel execution
     * has stopped or is stopping.
     */
    boolean areThreadsFinished()
    {
        return this.threadsFinished;
    }
    
    
    /**
     * Sets up the threads for the given runnables and starts them.
     */
    private Thread[] setupThreads( ThreadGroup tg, TestRunnable tr[] )
    {
        int len = tr.length;
        Thread threads[] = new Thread[ len ];
        for (int i = 0; i < len; ++i)
        {
            tr[i].setTestRunner( this );
            threads[i] = new Thread( tg, tr[i] );
            threads[i].setDaemon( true );
        }
        for (int i = 0; i < len; ++i)
        {
            threads[i].start();
            
            // wait for the threads to actually start.  If we wait 10
            // times and still no dice, I expect the test already started
            // and finished.
            int count = 0;
            while (!threads[i].isAlive() && count < 10)
            {
                LOG.debug("Waiting for thread at index "+i+" to start.");
                Thread.yield();
                ++count;
            }
            if (count >= 10)
            {
                LOG.debug("Assuming thread at index "+i+" already finished.");
            }
        }
        return threads;
    }
    
    
    /**
     * This joins all the threads together.  If the max time is exceeded,
     * then <tt>true</tt> is returned.  This method is only called by the core
     * thread.  The thread array will be altered at return time to only contain
     * threads which are still active (all other slots will be <tt>null</tt>).
     * <P>
     * This routine allows us to attempt to collect all the halted threads
     * together, while not waiting forever on threads that poorly don't
     * respond to outside stimuli (and thus require a stop() on the
     * thread).
     */
    private boolean joinThreads( Thread t[], long waitTime )
            throws InterruptedException
    {
        // check the arguments
        if (t == null)
        {
            return false;
        }
        int len = t.length;
        if (len <= 0)
        {
            return false;
        }
        if (waitTime < 0 || waitTime > maxWaitTime)
        {
            waitTime = DEFAULT_MAX_WAIT_TIME;
        }
        
        // slowly halt the threads.
        boolean threadsRunning = true;
        InterruptedException iex = null;
        long finalTime = System.currentTimeMillis() + waitTime;
        while (threadsRunning && System.currentTimeMillis() < finalTime &&
            iex == null)
        {
            LOG.debug("Time = "+System.currentTimeMillis()+"; final = "+finalTime);
            threadsRunning = false;
            
            // There might be circumstances where
            // the time between entering the while loop and entering the
            // for loop exceeds the final time, which can cause an incorrect
            // threadsRunning value.  That's why this boolean exists.  Note
            // that since we put in the (len <= 0) test above, we don't
            // have to worry about another edge case where the length prevents
            // the loop from being entered.
            boolean enteredLoop = false;
            
            for (int i = 0;
                i < len && System.currentTimeMillis() < finalTime;
                ++i)
            {
                enteredLoop = true;
                if (t[i] != null)
                {
                    try
                    {
                        // this will yield our time, so we don't
                        // need any explicit yield statement.
                        t[i].join( MIN_WAIT_TIME );
                    }
                    catch (InterruptedException ex)
                    {
                        LOG.debug("Join for thread at index "+i+
                            " was interrupted.");
                        iex = ex;
                    }
                    if (!t[i].isAlive())
                    {
                        LOG.debug("Joined thread at index "+i);
                        t[i] = null;
                    }
                    else
                    {
                        LOG.debug("Thread at index "+i+" still running.");
                        threadsRunning = true;
                    }
                }
            }
            
            // If the threadsRunning is true, it remains true.  If
            // the enteredLoop is false, this will be true.
            threadsRunning = threadsRunning || !enteredLoop;
        }
        if (iex != null)
        {
            throw iex;
        }
        return threadsRunning;
    }
    
    
    /**
     * This will execute a stop() on all non-null, alive threads in the list.
     *
     * @return the number of threads killed
     */
    private int killThreads( Thread[] t )
    {
        int killCount = 0;
        for (int i = 0; i < t.length; ++i)
        {
            if (t[i] != null && t[i].isAlive())
            {
                LOG.debug("Stopping thread at index "+i);
                ++killCount;
                if (this.performKills)
                {
                    // Yes, this is deprecated API, but we give the threads
                    // "sufficient" warning to stop themselves.
                    int count = 0;
                    boolean isAlive = t[i].isAlive();
                    while (isAlive && count < 10)
                    {
                        // send an InterruptedException, as this is handled
                        // specially in the TestRunnable.
                        t[i].stop(
                            new TestDeathException(
                            "Thread "+i+" did not die on its own" ) );
                        LOG.debug( "Waiting for thread at index "+i+
                            " to stop.");
                        Thread.yield();
                        isAlive = t[i].isAlive();
                        
                        if (isAlive)
                        {
                            // it may have been in a sleep state, so
                            // make it shake a leg!
                            t[i].interrupt();
                        }
                        ++count;
                    }
                    if (count >= 10)
                    {
                        LOG.fatal("Thread at index "+i+" did not stop!" );
                    }
                    t[i] = null;
                }
                else
                {
                    LOG.fatal( "Did not stop thread "+t[i] );
                }
            }
        }
        return killCount;
    }
    
    
    private void setTimeoutError( long maxTime )
    {
        Throwable t = createTimeoutError( maxTime );
        synchronized (this.synch)
        {
            if (this.exception == null)
            {
                LOG.debug("Setting the exception to a timeout exception.");
                this.exception = t;
            }
        }
    }
    
    
    private Throwable createTimeoutError( long maxTime )
    {
        Throwable ret = null;
        // need to set the exception to a timeout
        try
        {
            Assert.fail( "Threads did not finish within " +
                maxTime + " milliseconds.");
        }
        catch (ThreadDeath td)
        {
            // never trap these
            throw td;
        }
        catch (Throwable t)
        {
            t.fillInStackTrace();
            ret = t;
        }
        return ret;
    }
    
    
    /**
     * An exception that declares that the test has been stop()ed.
     */
    public static final class TestDeathException extends RuntimeException
    {
        private TestDeathException( String msg )
        {
            super( msg );
        }
    }
}