
com.kolibrifx.common.testing.TestThread Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-testing Show documentation
Show all versions of common-testing Show documentation
Helper classes for unit testing.
The newest version!
/*
* Copyright (c) 2010-2017, KolibriFX AS. Licensed under the Apache License, version 2.0.
*/
package com.kolibrifx.common.testing;
/**
* Helper class for multithreaded tests.
*
* Make sure to run rethrowErrors() after joining a TestThread instance, otherwise errors within runTestCode()
* will be ignored.
*/
public abstract class TestThread extends Thread {
private Error errorException = null;
private Exception otherException = null;
protected abstract void runTestCode() throws Exception;
@Override
public final void run() {
try {
runTestCode();
} catch (Error e) {
errorException = e;
} catch (Exception e) {
otherException = e;
}
}
/**
* Call this from the main test thread after join(), otherwise test failures or errors will not be
* reported.
*/
public void rethrowErrors() throws Exception {
if (errorException != null)
throw errorException;
if (otherException != null)
throw otherException;
}
/**
* Start all threads, join them, and possibly rethrow one exception. If more than one thread got an
* exception, the first one in the iterable will be rethrown.
*/
public static void executeTestThreads(final Iterable extends TestThread> threads) throws Exception {
for (TestThread thread : threads)
thread.start();
for (TestThread thread : threads)
thread.join();
for (TestThread thread : threads)
thread.rethrowErrors();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy