From a9c9d8e8143e738d9569af74eb8c1ca44109436a Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Fri, 30 Jun 2023 11:42:49 -0600 Subject: [PATCH] Add a test. --- tests/multithreading/task_01_exception_03.cc | 69 +++++++++++++++++++ .../task_01_exception_03.output | 3 + 2 files changed, 72 insertions(+) create mode 100644 tests/multithreading/task_01_exception_03.cc create mode 100644 tests/multithreading/task_01_exception_03.output diff --git a/tests/multithreading/task_01_exception_03.cc b/tests/multithreading/task_01_exception_03.cc new file mode 100644 index 0000000000..124ee8f314 --- /dev/null +++ b/tests/multithreading/task_01_exception_03.cc @@ -0,0 +1,69 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2009 - 2020 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + +// Like the _01 test, but check that if the function called on the +// task throws an exception, that Task::wait() continues to work but +// Task::return_value() throws the exception. +// +// While Task::return_value() throws an exception the first time +// around, this won't happen the second time around because the stored +// exception is already gone. But the return value has not been +// initialized in that case, and so you just aren't allowed to call +// return_value() a second time. This test checks that. + +#include + +#include "../tests.h" + + +int +test() +{ + std::this_thread::sleep_for(std::chrono::seconds(3)); + + throw 42; +} + + +int +main() +{ + initlog(); + + Threads::Task t = Threads::new_task(test); + + // Here, an exception should be triggered: + try + { + t.return_value(); + } + catch (int i) + { + Assert(i == 42, ExcInternalError()); + deallog << "OK" << std::endl; + } + + // This must fail: + deal_II_exceptions::disable_abort_on_exception(); + try + { + t.return_value(); + } + catch (const ExcMessage &) + { + deallog << "Expected exception." << std::endl; + } +} diff --git a/tests/multithreading/task_01_exception_03.output b/tests/multithreading/task_01_exception_03.output new file mode 100644 index 0000000000..24f870fa7d --- /dev/null +++ b/tests/multithreading/task_01_exception_03.output @@ -0,0 +1,3 @@ + +DEAL::OK +DEAL::Expected exception. -- 2.39.5