-
Notifications
You must be signed in to change notification settings - Fork 2
KF-30 add tests for ConditionVariable #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Kellesi
wants to merge
2
commits into
main
Choose a base branch
from
KF-30-test-ConditionVariable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,292 @@ | ||
| #include "pch.h" | ||
| #include <kf/ConditionVariable.h> | ||
| #include <kf/Thread.h> | ||
| #include <kf/EResource.h> | ||
|
|
||
| namespace | ||
| { | ||
| struct Context | ||
| { | ||
| kf::EResource* resource = nullptr; | ||
| kf::ConditionVariable* cv = nullptr; | ||
| LONG counter = 0; | ||
| }; | ||
|
|
||
| void delay() | ||
| { | ||
| LARGE_INTEGER interval; | ||
| interval.QuadPart = -10'000; // 1ms | ||
| KeDelayExecutionThread(KernelMode, FALSE, &interval); | ||
| } | ||
| } | ||
|
|
||
| SCENARIO("kf::ConditionVariable") | ||
| { | ||
| GIVEN("ConditionVariable without timeout and 1 thread") | ||
| { | ||
| WHEN("waitFor: timeout is nullptr and cv is notified") | ||
| { | ||
| kf::EResource resource; | ||
| kf::ConditionVariable cv; | ||
| kf::Thread thread; | ||
| thread.start([](void* context) { | ||
| auto cv = static_cast<kf::ConditionVariable*>(context); | ||
| delay(); | ||
| cv->notifyOne(); | ||
| }, &cv); | ||
|
|
||
| auto status = cv.waitFor(resource, nullptr); | ||
| THEN("waitFor should return success") | ||
| { | ||
| REQUIRE(status == kf::ConditionVariable::Status::Success); | ||
| } | ||
| } | ||
|
|
||
| WHEN("waitFor: timeout is set to 0") | ||
| { | ||
| kf::EResource resource; | ||
| kf::ConditionVariable cv; | ||
| LARGE_INTEGER timeout{ 0 }; | ||
| auto status = cv.waitFor(resource, &timeout); | ||
| THEN("waitFor should return Status::Timeout") | ||
| { | ||
| REQUIRE(status == kf::ConditionVariable::Status::Timeout); | ||
| } | ||
| } | ||
|
|
||
| WHEN("waitFor: timeout is set to 1ms") | ||
| { | ||
| kf::EResource resource; | ||
| kf::ConditionVariable cv; | ||
| LARGE_INTEGER timeout; | ||
| timeout.QuadPart = -10'000; // 1ms | ||
| auto status = cv.waitFor(resource, &timeout); | ||
| THEN("waitFor should return Status::Timeout") | ||
| { | ||
| REQUIRE(status == kf::ConditionVariable::Status::Timeout); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| GIVEN("ConditionVariable wait with predicate immediately true") | ||
| { | ||
| kf::EResource resource; | ||
| kf::ConditionVariable cv; | ||
| LONG counter = 1; | ||
|
|
||
| cv.wait(resource, [&]() { return counter > 0; }); | ||
| THEN("Wait should return immediately") | ||
| { | ||
| REQUIRE(counter == 1); | ||
| } | ||
| } | ||
|
|
||
| GIVEN("ConditionVariable without timeout and 2 Threads waiting for condition variable") | ||
| { | ||
| kf::EResource resource; | ||
| kf::ConditionVariable cv; | ||
| Context ctx{ &resource, &cv }; | ||
| kf::Thread thread1; | ||
| kf::Thread thread2; | ||
|
|
||
| thread1.start([](void* context) { | ||
| auto ctx = static_cast<Context*>(context); | ||
| ctx->cv->wait(*ctx->resource, [&]() { | ||
| return ctx->counter > 0; }); | ||
| ctx->counter++; | ||
| ctx->resource->release(); | ||
| }, &ctx); | ||
|
|
||
| thread2.start([](void* context) { | ||
| auto ctx = static_cast<Context*>(context); | ||
| ctx->cv->wait(*ctx->resource, [&]() { | ||
| return ctx->counter > 0; }); | ||
| ctx->counter++; | ||
| ctx->resource->release(); | ||
| }, &ctx); | ||
| delay(); | ||
|
|
||
| WHEN("Predicate is true and called notifyAll") | ||
| { | ||
| ctx.counter++; | ||
| cv.notifyAll(); | ||
| thread1.join(); | ||
| thread2.join(); | ||
|
|
||
| THEN("Both threads should wake up and increment counter") | ||
| { | ||
| REQUIRE(ctx.counter == 3); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| GIVEN("ConditionVariable without timeout and 1 thread waiting for predicate") | ||
| { | ||
| kf::EResource resource; | ||
| kf::ConditionVariable cv; | ||
| Context ctx{ &resource, &cv }; | ||
| kf::Thread thread; | ||
|
|
||
| WHEN("Thread is notified") | ||
| { | ||
| thread.start([](void* context) { | ||
| auto ctx = static_cast<Context*>(context); | ||
| ctx->cv->wait(*ctx->resource, [&]() { return ctx->counter > 0; }); | ||
| ctx->counter++; | ||
| ctx->resource->release(); | ||
| }, &ctx); | ||
|
|
||
| delay(); | ||
|
|
||
| THEN("Thread should be waiting for predicate") | ||
| { | ||
| REQUIRE(ctx.counter == 0); | ||
| } | ||
|
|
||
| ctx.counter++; | ||
| cv.notifyOne(); | ||
| thread.join(); | ||
|
|
||
| THEN("Predicate become true and thread should wake up") | ||
| { | ||
| REQUIRE(ctx.counter == 2); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| GIVEN("ConditionVariable with 2 waiting threads and notifyOne is called") | ||
| { | ||
| kf::EResource resource; | ||
| kf::ConditionVariable cv; | ||
| Context ctx{ &resource, &cv }; | ||
| kf::Thread thread1; | ||
| kf::Thread thread2; | ||
|
|
||
| thread1.start([](void* context) { | ||
| auto ctx = static_cast<Context*>(context); | ||
| ctx->cv->wait(*ctx->resource, [&]() { return ctx->counter > 0; }); | ||
| ctx->counter++; | ||
| ctx->resource->release(); | ||
| }, &ctx); | ||
|
|
||
| thread2.start([](void* context) { | ||
| auto ctx = static_cast<Context*>(context); | ||
| ctx->cv->wait(*ctx->resource, [&]() { return ctx->counter > 0; }); | ||
| ctx->counter++; | ||
| ctx->resource->release(); | ||
| }, &ctx); | ||
|
|
||
| delay(); | ||
| ctx.counter++; | ||
| cv.notifyOne(); | ||
| delay(); | ||
|
|
||
| THEN("Only one thread should wake up and increment counter") | ||
| { | ||
| REQUIRE(ctx.counter == 2); | ||
| } | ||
|
|
||
| cv.notifyOne(); | ||
| thread1.join(); | ||
| thread2.join(); | ||
|
|
||
| THEN("After second notifyOne both threads should finish") | ||
| { | ||
| REQUIRE(ctx.counter == 3); | ||
| } | ||
| } | ||
|
|
||
| GIVEN("ConditionVariable::waitFor with predicate and timeout") | ||
| { | ||
| WHEN("Timeout is nullptr and predicate is true") | ||
| { | ||
| kf::EResource resource; | ||
| kf::ConditionVariable cv; | ||
| kf::Thread thread; | ||
| int counter = 1; | ||
| thread.start([](void* context) { | ||
| auto cv = static_cast<kf::ConditionVariable*>(context); | ||
| delay(); | ||
| cv->notifyOne(); | ||
| }, &cv); | ||
|
|
||
| THEN("waitFor should return true") | ||
| { | ||
| REQUIRE(cv.waitFor(resource, nullptr, [&]() { return counter > 0; })); | ||
| } | ||
| } | ||
|
|
||
| WHEN("Timeout is set to 0 and predicate is false") | ||
| { | ||
| kf::EResource resource; | ||
| kf::ConditionVariable cv; | ||
| LARGE_INTEGER timeout{ 0 }; | ||
| int counter = 0; | ||
| THEN("waitFor should return false") | ||
| { | ||
| REQUIRE(!cv.waitFor(resource, &timeout, [&]() { return counter > 0; })); | ||
| } | ||
| } | ||
|
|
||
| WHEN("Timeout is set to 1ms and predicate is false") | ||
| { | ||
| kf::EResource resource; | ||
| kf::ConditionVariable cv; | ||
| LARGE_INTEGER timeout; | ||
| timeout.QuadPart = -10'000; // 1ms | ||
| int counter = 0; | ||
| THEN("waitFor should return false") | ||
| { | ||
| REQUIRE(!cv.waitFor(resource, &timeout, [&]() { return counter > 0; })); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| GIVEN("ConditionVariable with no waiters") | ||
| { | ||
| kf::EResource resource; | ||
| kf::ConditionVariable cv; | ||
|
|
||
| WHEN("notifyAll is called without waiters") | ||
| { | ||
| cv.notifyAll(); | ||
| THEN("Nothing should happen") | ||
| { | ||
| } | ||
| } | ||
|
|
||
| WHEN("notifyOne is called without waiters") | ||
| { | ||
| cv.notifyOne(); | ||
| THEN("Nothing should happen") | ||
| { | ||
| } | ||
| } | ||
| } | ||
|
|
||
| GIVEN("ConditionVariable with 1 thread and multiple notifyOne calls") | ||
| { | ||
| kf::EResource resource; | ||
| kf::ConditionVariable cv; | ||
| Context ctx{ &resource, &cv }; | ||
| kf::Thread thread; | ||
|
|
||
| thread.start([](void* context) { | ||
| auto ctx = static_cast<Context*>(context); | ||
| ctx->cv->wait(*ctx->resource, [&]() { return ctx->counter > 0; }); | ||
| ctx->counter++; | ||
| ctx->resource->release(); | ||
| }, &ctx); | ||
|
|
||
| delay(); | ||
| ctx.counter++; | ||
| cv.notifyOne(); | ||
| cv.notifyOne(); | ||
| thread.join(); | ||
|
|
||
| THEN("Thread should wake only once and increment counter") | ||
| { | ||
| REQUIRE(ctx.counter == 2); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition logic is redundant. If NT_SUCCESS(status) is true, then status cannot be STATUS_TIMEOUT since STATUS_TIMEOUT is not a success code. Consider simplifying to: auto result = NT_SUCCESS(status) ? Status::Success : Status::Timeout;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
STATUS_TIMEOUT is evaluated as success by NT_SUCCESS(status), but here it should be Status::Timeout