Serenity Operating System
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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.

authored by

kleines Filmröllchen and committed by
Andrew Kaster
5b335e7f 2fcb7130

+25 -6
+25 -6
Tests/LibThreading/TestThread.cpp
··· 8 8 #include <LibThreading/Thread.h> 9 9 #include <unistd.h> 10 10 11 - // FIXME: Enable these tests once they work reliably. 12 - 13 - #if 0 14 11 TEST_CASE(threads_can_detach) 15 12 { 16 13 int should_be_42 = 0; ··· 29 26 30 27 TEST_CASE(joining_detached_thread_errors) 31 28 { 32 - auto thread = Threading::Thread::construct([]() { return 0; }); 29 + Atomic<bool> should_exit { false }; 30 + auto thread = Threading::Thread::construct([&]() { 31 + while (!should_exit.load()) 32 + usleep(10 * 1000); 33 + return 0; 34 + }); 33 35 thread->start(); 34 36 thread->detach(); 35 37 36 - EXPECT(thread->join().is_error()); 38 + // 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. 39 + EXPECT(!thread->needs_to_be_joined()); 40 + 41 + // FIXME: Dropping a running thread crashes because of the Function destructor. For now, force the detached thread to exit. 42 + should_exit.store(true); 43 + usleep(20 * 1000); 44 + } 45 + 46 + TEST_CASE(join_dead_thread) 47 + { 48 + auto thread = Threading::Thread::construct([&]() { return 0 /*nullptr*/; }); 49 + thread->start(); 50 + // The thread should have exited by then. 51 + usleep(40 * 1000); 52 + 53 + auto join_result = thread->join<int*>(); 54 + 55 + EXPECT(!join_result.is_error()); 56 + EXPECT_EQ(join_result.value(), static_cast<int*>(0)); 37 57 } 38 - #endif