public abstract class TestingThread extends Thread implements Runnable
public void testConcurrency()
{
TestingThread thread = new TestingThread()
{
protected void doRun() throws Throwable
{
// Wait a few seconds for somethingElse to begin
Thread.sleep(3000);
assertTrue(somethingElse.isRunning());
assertEquals(3, somethingElse.counter);
}
};
thread.start();
// This will block the main test thread
runSomething("big long task");
assertEquals("peachy", something.getResult());
assertFalse(something.isOutOfControl());
// Verify that no exceptions occurred meanwhile in the TestingThread
thread.await();
if (thread.getException() != null)
{
fail(thread.getException().getMessage());
}
}
Both the TestingThread and the main thread will run in parallel,
therefore assertions can be made on "somethingElse" while the call
to runSomething() is blocking the main thread of the TestCase.Thread.State, Thread.UncaughtExceptionHandler
Modifier and Type | Field and Description |
---|---|
static long |
AWAIT_TIMEOUT |
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
Constructor and Description |
---|
TestingThread() |
Modifier and Type | Method and Description |
---|---|
void |
await()
Block until the thread completes its doRun() method.
|
protected abstract void |
doRun() |
Throwable |
getException() |
void |
run()
Executes the doRun() method and stores any exception which occurred to be
returned at a later time.
|
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
public static final long AWAIT_TIMEOUT
public final void run()
public void await() throws InterruptedException
InterruptedException
public Throwable getException()
Copyright © 2003–2016 MuleSoft, Inc.. All rights reserved.