From dd3996e20755d1a970963ab899b58f7f4696c270 Mon Sep 17 00:00:00 2001 From: Spencer Dixon Date: Fri, 2 Jul 2021 08:06:48 -0400 Subject: [PATCH] Tests+LibThreading: Add new tests for LibThreading for detach() --- Tests/LibThreading/CMakeLists.txt | 4 ++++ Tests/LibThreading/TestThread.cpp | 34 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 Tests/LibThreading/CMakeLists.txt create mode 100644 Tests/LibThreading/TestThread.cpp diff --git a/Tests/LibThreading/CMakeLists.txt b/Tests/LibThreading/CMakeLists.txt new file mode 100644 index 00000000000..40021e9d4a5 --- /dev/null +++ b/Tests/LibThreading/CMakeLists.txt @@ -0,0 +1,4 @@ +file(GLOB TEST_SOURCES CONFIGURE_DEPENDS "*.cpp") +foreach(source ${TEST_SOURCES}) + serenity_test(${source} LibThreading LIBS LibThreading LibPthread) +endforeach() diff --git a/Tests/LibThreading/TestThread.cpp b/Tests/LibThreading/TestThread.cpp new file mode 100644 index 00000000000..8d5c7eae480 --- /dev/null +++ b/Tests/LibThreading/TestThread.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2021, Spencer Dixon + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include + +TEST_CASE(threads_can_detach) +{ + int should_be_42 = 0; + + auto thread = Threading::Thread::construct([&should_be_42]() { + usleep(10 * 1000); + should_be_42 = 42; + return 0; + }); + thread->start(); + thread->detach(); + usleep(20 * 1000); + + EXPECT(should_be_42 == 42); +} + +TEST_CASE(joining_detached_thread_errors) +{ + auto thread = Threading::Thread::construct([]() { return 0; }); + thread->start(); + thread->detach(); + + EXPECT(thread->join().is_error()); +}