From bdf1c6165e018994e3a1c4bde50e5b3ee5c9658d Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Sun, 21 Jul 2024 17:33:12 -0600 Subject: [PATCH] Add a test. --- tests/multithreading/task_result_05.cc | 100 +++++++++++++++++++++ tests/multithreading/task_result_05.output | 2 + 2 files changed, 102 insertions(+) create mode 100644 tests/multithreading/task_result_05.cc create mode 100644 tests/multithreading/task_result_05.output diff --git a/tests/multithreading/task_result_05.cc b/tests/multithreading/task_result_05.cc new file mode 100644 index 0000000000..ed740cb49e --- /dev/null +++ b/tests/multithreading/task_result_05.cc @@ -0,0 +1,100 @@ +// ------------------------------------------------------------------------ +// +// SPDX-License-Identifier: LGPL-2.1-or-later +// Copyright (C) 2009 - 2024 by the deal.II authors +// +// This file is part of the deal.II library. +// +// Part of the source code is dual licensed under Apache-2.0 WITH +// LLVM-exception OR LGPL-2.1-or-later. Detailed license information +// governing the source code and code contributions can be found in +// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. +// +// ------------------------------------------------------------------------ + + +// A test for basic functionality of the TaskResult function. This +// test checks TaskResult::empty()'s documented functionality. + +#include + +#include "../tests.h" + + + +int +main() +{ + initlog(); + + { + Threads::TaskResult t; + AssertThrow(t.empty() == true, ExcInternalError()); + } + + + { + // Create a background task that sleeps for a while and then + // issues its result. Associate this background task with + // a TaskResult object. + Threads::TaskResult t = Threads::new_task([]() { + std::this_thread::sleep_for(std::chrono::seconds(1)); + return 42; + }); + AssertThrow(t.empty() == false, ExcInternalError()); + + t.join(); + AssertThrow(t.empty() == false, ExcInternalError()); + } + + // Same idea, but move construct the task + { + // Create a background task that sleeps for a while and then + // issues its result. Associate this background task with + // a TaskResult object. + Threads::TaskResult t = Threads::new_task([]() { + std::this_thread::sleep_for(std::chrono::seconds(1)); + return 42; + }); + + Threads::TaskResult tt = std::move(t); + AssertThrow(t.empty() == true, ExcInternalError()); + AssertThrow(tt.empty() == false, ExcInternalError()); + + // Ensure the task has finished before destroying the TaskResult + // object: + tt.join(); + } + + { + // TaskResult::clear() results in an empty object, but we have to + // wait in order to call clear(). We can do this by explicitly + // asking for the resulting value... + Threads::TaskResult t = Threads::new_task([]() { + std::this_thread::sleep_for(std::chrono::seconds(1)); + return 42; + }); + const int value = t.value(); + AssertThrow(value == 42, ExcInternalError()); + AssertThrow(t.empty() == false, ExcInternalError()); + + t.clear(); + AssertThrow(t.empty() == true, ExcInternalError()); + } + + + { + // ...or using the weaker t.join() call. + Threads::TaskResult t = Threads::new_task([]() { + std::this_thread::sleep_for(std::chrono::seconds(1)); + return 42; + }); + t.join(); + AssertThrow(t.empty() == false, ExcInternalError()); + + t.clear(); + AssertThrow(t.empty() == true, ExcInternalError()); + } + + deallog << "OK" << std::endl; +} diff --git a/tests/multithreading/task_result_05.output b/tests/multithreading/task_result_05.output new file mode 100644 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/multithreading/task_result_05.output @@ -0,0 +1,2 @@ + +DEAL::OK -- 2.39.5