forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests+LibThreading: Add new tests for LibThreading for detach()
- Loading branch information
1 parent
48731e9
commit dd3996e
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
file(GLOB TEST_SOURCES CONFIGURE_DEPENDS "*.cpp") | ||
foreach(source ${TEST_SOURCES}) | ||
serenity_test(${source} LibThreading LIBS LibThreading LibPthread) | ||
endforeach() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (c) 2021, Spencer Dixon <[email protected]> | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
||
#include <LibTest/TestCase.h> | ||
#include <LibThreading/Thread.h> | ||
#include <unistd.h> | ||
|
||
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()); | ||
} |