From e39316c866d546cc4f6a8bf9390d022fca65cf8a Mon Sep 17 00:00:00 2001 From: Dennis Klein Date: Tue, 9 Oct 2018 20:46:23 +0200 Subject: [PATCH] Test exceptions thrown in user code --- fairmq/DeviceRunner.cxx | 6 +- fairmq/runFairMQDevice.h | 4 +- test/CMakeLists.txt | 2 + test/device/_exceptions.cxx | 71 ++++++++++++++++++++++ test/helper/devices/TestExceptions.cxx | 84 ++++++++++++++++++++++++++ test/helper/runTestDevice.cxx | 5 ++ 6 files changed, 166 insertions(+), 6 deletions(-) create mode 100644 test/device/_exceptions.cxx create mode 100644 test/helper/devices/TestExceptions.cxx diff --git a/fairmq/DeviceRunner.cxx b/fairmq/DeviceRunner.cxx index a2fea031..95ad9712 100644 --- a/fairmq/DeviceRunner.cxx +++ b/fairmq/DeviceRunner.cxx @@ -111,12 +111,10 @@ auto DeviceRunner::RunWithExceptionHandlers() -> int try { return Run(); } catch (std::exception& e) { - LOG(error) << "Unhandled exception reached the top of main: " << e.what() - << ", application will now exit"; + LOG(error) << "Uncaught exception reached the top of DeviceRunner: " << e.what(); return 1; } catch (...) { - LOG(error) << "Non-exception instance being thrown. Please make sure you use " - "std::runtime_exception() instead. Application will now exit."; + LOG(error) << "Uncaught exception reached the top of DeviceRunner."; return 1; } } diff --git a/fairmq/runFairMQDevice.h b/fairmq/runFairMQDevice.h index 152edc1b..7e811a64 100644 --- a/fairmq/runFairMQDevice.h +++ b/fairmq/runFairMQDevice.h @@ -56,12 +56,12 @@ int main(int argc, char* argv[]) } catch (std::exception& e) { - LOG(error) << "Unhandled exception reached the top of main: " << e.what() << ", application will now exit"; + LOG(error) << "Uncaught exception reached the top of main: " << e.what(); return 1; } catch (...) { - LOG(error) << "Non-exception instance being thrown. Please make sure you use std::runtime_exception() instead. Application will now exit."; + LOG(error) << "Uncaught exception reached the top of main."; return 1; } } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 188ea556..ecd655bb 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -27,6 +27,7 @@ add_testhelper(runTestDevice helper/devices/TestSub.cxx helper/devices/TestTransferTimeout.cxx helper/devices/TestWaitFor.cxx + helper/devices/TestExceptions.cxx LINKS FairMQ ) @@ -95,6 +96,7 @@ add_testsuite(FairMQ.Device device/_device_version.cxx device/_device_config.cxx device/_waitfor.cxx + device/_exceptions.cxx LINKS FairMQ DEPENDS testhelper_runTestDevice diff --git a/test/device/_exceptions.cxx b/test/device/_exceptions.cxx new file mode 100644 index 00000000..93607ca4 --- /dev/null +++ b/test/device/_exceptions.cxx @@ -0,0 +1,71 @@ +/******************************************************************************** + * Copyright (C) 2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * * + * This software is distributed under the terms of the * + * GNU Lesser General Public Licence (LGPL) version 3, * + * copied verbatim in the file "LICENSE" * + ********************************************************************************/ + +#include "runner.h" + +#include +#include +#include + +#include +#include +#include + +namespace +{ + +using namespace std; +using namespace fair::mq::test; +using namespace fair::mq::tools; + +void RunExceptionIn(const std::string& state) +{ + size_t session{fair::mq::tools::UuidHash()}; + + execute_result result{"", 100}; + thread device_thread([&]() { + stringstream cmd; + cmd << runTestDevice + << " --id exceptions_" << state << "_" + << " --control static" + << " --session " << session + << " --color false"; + result = execute(cmd.str(), "[EXCEPTION IN " + state + "]"); + }); + + device_thread.join(); + + ASSERT_NE(std::string::npos, result.console_out.find("exception in " + state + "()")); + + exit(result.exit_code); +} + +TEST(Exceptions, InInit) { EXPECT_EXIT(RunExceptionIn("Init"), ::testing::ExitedWithCode(1), ""); } +TEST(Exceptions, InInitTask) +{ + EXPECT_EXIT(RunExceptionIn("InitTask"), ::testing::ExitedWithCode(1), ""); +} +TEST(Exceptions, InPreRun) +{ + EXPECT_EXIT(RunExceptionIn("PreRun"), ::testing::ExitedWithCode(1), ""); +} +TEST(Exceptions, InRun) { EXPECT_EXIT(RunExceptionIn("Run"), ::testing::ExitedWithCode(1), ""); } +TEST(Exceptions, InPostRun) +{ + EXPECT_EXIT(RunExceptionIn("PostRun"), ::testing::ExitedWithCode(1), ""); +} +TEST(Exceptions, InResetTask) +{ + EXPECT_EXIT(RunExceptionIn("ResetTask"), ::testing::ExitedWithCode(1), ""); +} +TEST(Exceptions, InReset) +{ + EXPECT_EXIT(RunExceptionIn("Reset"), ::testing::ExitedWithCode(1), ""); +} + +} // namespace diff --git a/test/helper/devices/TestExceptions.cxx b/test/helper/devices/TestExceptions.cxx new file mode 100644 index 00000000..cbc51e22 --- /dev/null +++ b/test/helper/devices/TestExceptions.cxx @@ -0,0 +1,84 @@ +/******************************************************************************** + * Copyright (C) 2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * * + * This software is distributed under the terms of the * + * GNU Lesser General Public Licence (LGPL) version 3, * + * copied verbatim in the file "LICENSE" * + ********************************************************************************/ + +#include +#include + +#include +#include + +namespace fair +{ +namespace mq +{ +namespace test +{ + +class TestExceptions : public FairMQDevice +{ + public: + auto Init() -> void override + { + std::string state("Init"); + if (std::string::npos != GetId().find("_" + state + "_")) { + throw std::runtime_error("exception in " + state + "()"); + } + } + + auto InitTask() -> void override + { + std::string state("InitTask"); + if (std::string::npos != GetId().find("_" + state + "_")) { + throw std::runtime_error("exception in " + state + "()"); + } + } + + auto PreRun() -> void override + { + std::string state("PreRun"); + if (std::string::npos != GetId().find("_" + state + "_")) { + throw std::runtime_error("exception in " + state + "()"); + } + } + + auto Run() -> void override + { + std::string state("Run"); + if (std::string::npos != GetId().find("_" + state + "_")) { + throw std::runtime_error("exception in " + state + "()"); + } + } + + auto PostRun() -> void override + { + std::string state("PostRun"); + if (std::string::npos != GetId().find("_" + state + "_")) { + throw std::runtime_error("exception in " + state + "()"); + } + } + + auto ResetTask() -> void override + { + std::string state("ResetTask"); + if (std::string::npos != GetId().find("_" + state + "_")) { + throw std::runtime_error("exception in " + state + "()"); + } + } + + auto Reset() -> void override + { + std::string state("Reset"); + if (std::string::npos != GetId().find("_" + state + "_")) { + throw std::runtime_error("exception in " + state + "()"); + } + } +}; + +} // namespace test +} // namespace mq +} // namespace fair diff --git a/test/helper/runTestDevice.cxx b/test/helper/runTestDevice.cxx index 9be10098..4ca6b0b3 100644 --- a/test/helper/runTestDevice.cxx +++ b/test/helper/runTestDevice.cxx @@ -18,6 +18,7 @@ #include "devices/TestSub.cxx" #include "devices/TestTransferTimeout.cxx" #include "devices/TestWaitFor.cxx" +#include "devices/TestExceptions.cxx" #include @@ -87,6 +88,10 @@ auto getDevice(const FairMQProgOptions& config) -> FairMQDevicePtr { return new TestWaitFor; } + else if (0 == id.find("exceptions_")) + { + return new TestExceptions; + } else { cerr << "Don't know id '" << id << "'" << endl;