Newer
Older
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source
// & Institut Laue - Langevin
// SPDX - License - Identifier: GPL - 3.0 +
#ifndef MANTID_KERNEL_THREADPOOLRUNNABLETEST_H_
#define MANTID_KERNEL_THREADPOOLRUNNABLETEST_H_
#include <MantidKernel/System.h>
#include <MantidKernel/Timer.h>
#include <cxxtest/TestSuite.h>
#include <MantidKernel/ThreadPoolRunnable.h>
Janik Zikovsky
committed
#include <MantidKernel/ThreadScheduler.h>
using namespace Mantid::Kernel;
Janik Zikovsky
committed
int ThreadPoolRunnableTest_value;
class ThreadPoolRunnableTest : public CxxTest::TestSuite {
TS_ASSERT_THROWS(new ThreadPoolRunnable(0, nullptr), std::invalid_argument);
Janik Zikovsky
committed
//=======================================================================================
void run() override { ThreadPoolRunnableTest_value = 1234; }
Janik Zikovsky
committed
};
void test_run() {
ThreadPoolRunnable *tpr;
ThreadScheduler *sc = new ThreadSchedulerFIFO();
Janik Zikovsky
committed
tpr = new ThreadPoolRunnable(0, sc);
sc->push(new SimpleTask());
Janik Zikovsky
committed
// Run it
ThreadPoolRunnableTest_value = 0;
tpr->run();
// The task worked
TS_ASSERT_EQUALS(ThreadPoolRunnableTest_value, 1234);
Janik Zikovsky
committed
// Nothing more in the queue.
delete tpr;
delete sc;
Janik Zikovsky
committed
}
//=======================================================================================
/** Class that throws an exception */
class TaskThatThrows : public Task {
Janik Zikovsky
committed
ThreadPoolRunnableTest_value += 1;
throw Mantid::Kernel::Exception::NotImplementedError(
"Test exception from TaskThatThrows.");
Janik Zikovsky
committed
}
};
void test_run_throws() {
ThreadPoolRunnable *tpr;
ThreadScheduler *sc = new ThreadSchedulerFIFO();
Janik Zikovsky
committed
tpr = new ThreadPoolRunnable(0, sc);
// Put 10 tasks in
Janik Zikovsky
committed
sc->push(new TaskThatThrows());
// The task throws but the runnable just aborts instead
ThreadPoolRunnableTest_value = 0;
TS_ASSERT_THROWS_NOTHING(tpr->run());
// Nothing more in the queue.
Janik Zikovsky
committed
// Yet only one task actually ran.
TS_ASSERT_EQUALS(ThreadPoolRunnableTest_value, 1);
Janik Zikovsky
committed
Janik Zikovsky
committed
// Get the reason for aborting
std::runtime_error e = sc->getAbortException();
std::string what = e.what();
TS_ASSERT_EQUALS(what, "Test exception from TaskThatThrows.");
delete tpr;
delete sc;
Janik Zikovsky
committed
}
};
#endif /* MANTID_KERNEL_THREADPOOLRUNNABLETEST_H_ */