Clover coverage report -
Coverage timestamp: Thu Jul 26 2007 06:31:20 GMT+01:00
file stats: LOC: 74   Methods: 5
NCLOC: 41   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ConcurrentTestCase.java 75% 76.9% 80% 77.3%
coverage coverage
 1    /*
 2    * Copyright (c) 2007 Peter Veentjer
 3    *
 4    * This program is made available under the terms of the MIT License.
 5    */
 6    package org.codehaus.prometheus.testsupport;
 7   
 8    import junit.framework.TestCase;
 9   
 10    import java.util.concurrent.TimeUnit;
 11   
 12    /**
 13    * A TestCase that contains various concurrency related functions.
 14    *
 15    * @author Peter Veentjer.
 16    */
 17    public abstract class ConcurrentTestCase extends TestCase {
 18   
 19    public static final boolean START_INTERRUPTED = true;
 20    public static final boolean START_UNINTERRUPTED = false;
 21   
 22    public static long DELAY_TINY_MS = 50;
 23    public static long DELAY_SMALL_MS = DELAY_TINY_MS * 5;//250 ms
 24    public static long DELAY_MEDIUM_MS = DELAY_TINY_MS * 10;//500 ms
 25    public static long DELAY_LONG_MS = DELAY_TINY_MS * 50;//2500 msec
 26    public static long DELAY_EON_MS = 100000000000L;
 27   
 28    public volatile Stopwatch stopwatch;
 29   
 30  461 public ConcurrentTestCase() {
 31    }
 32   
 33  0 public ConcurrentTestCase(String fixture) {
 34  0 super(fixture);
 35    }
 36   
 37  461 @Override
 38    public void setUp() throws Exception {
 39  461 stopwatch = new Stopwatch();
 40    }
 41   
 42    /**
 43    * Joins on all threads. If join times out, the testcase fails, so this call
 44    * won't block indefinetely, so it is also a way to detect deadlocks (waiting
 45    * on an event that never occurs).
 46    * <p/>
 47    * This method should only be called from the thread that executes this
 48    * testcase.
 49    *
 50    * @param threads the threads to join on.
 51    */
 52  864 public void joinAll(Thread... threads) {
 53  864 joinAll(2 * DELAY_LONG_MS,threads);
 54    }
 55   
 56  864 public void joinAll(long delayMs, Thread... threads) {
 57  864 for (int k = 0; k < threads.length; k++) {
 58  920 Thread t = threads[k];
 59  920 try {
 60  920 long startNs = System.nanoTime();
 61  920 t.join(delayMs);
 62  920 long endNs = System.nanoTime();
 63  920 delayMs -= TimeUnit.NANOSECONDS.toMillis(endNs - startNs);
 64   
 65  920 if (t.isAlive()) {
 66  0 fail(String.format("thread #%s is still running", k));
 67    }
 68    } catch (InterruptedException e) {
 69  0 fail(String.format("join on thread #%s was interrupted", k));
 70    }
 71    }
 72    }
 73    }
 74