From 5b335e7fbab7e4b3b2428dd2a0a82a7edb4d75bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Sat, 12 Nov 2022 14:24:26 +0100 Subject: [PATCH] Tests: Update thread tests and make them pass The existing tests have only mildly changed, and there is another test for joining dead non-detached threads. --- Tests/LibThreading/TestThread.cpp | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/Tests/LibThreading/TestThread.cpp b/Tests/LibThreading/TestThread.cpp index 33bb5c3136e..72a73d82d5d 100644 --- a/Tests/LibThreading/TestThread.cpp +++ b/Tests/LibThreading/TestThread.cpp @@ -8,9 +8,6 @@ #include #include -// FIXME: Enable these tests once they work reliably. - -#if 0 TEST_CASE(threads_can_detach) { int should_be_42 = 0; @@ -29,10 +26,32 @@ TEST_CASE(threads_can_detach) TEST_CASE(joining_detached_thread_errors) { - auto thread = Threading::Thread::construct([]() { return 0; }); + Atomic should_exit { false }; + auto thread = Threading::Thread::construct([&]() { + while (!should_exit.load()) + usleep(10 * 1000); + return 0; + }); thread->start(); thread->detach(); - EXPECT(thread->join().is_error()); + // Because of how the crash test forks and removes the thread, we can't use that to verify that join() crashes. Instead, we check the join crash condition ourselves. + EXPECT(!thread->needs_to_be_joined()); + + // FIXME: Dropping a running thread crashes because of the Function destructor. For now, force the detached thread to exit. + should_exit.store(true); + usleep(20 * 1000); +} + +TEST_CASE(join_dead_thread) +{ + auto thread = Threading::Thread::construct([&]() { return 0 /*nullptr*/; }); + thread->start(); + // The thread should have exited by then. + usleep(40 * 1000); + + auto join_result = thread->join(); + + EXPECT(!join_result.is_error()); + EXPECT_EQ(join_result.value(), static_cast(0)); } -#endif