-
Notifications
You must be signed in to change notification settings - Fork 2
KF-30 add tests for Event #67
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f38af63
add tests for Event
ea74c1a
Update test/EventTest.cpp
Kellesi dfce764
Merge branch 'main' into KF-30-test-Event
belyshevdenis 95a3bbe
refactoring
6bd3916
refactoring
52dc801
fix test header
c8a3027
refactoring
cb2cdb8
refactoring
9c182e0
Refactor, add const for timeout value
SergiusTheBest 4ccff0a
Merge branch 'main' into KF-30-test-Event
SergiusTheBest 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| #include "pch.h" | ||
| #include <kf/Event.h> | ||
| #include <kf/Thread.h> | ||
|
|
||
| SCENARIO("kf::Event") | ||
| { | ||
| constexpr auto kOneSecond = 10'000'000; // 1 second in 100-nanosecond intervals | ||
|
|
||
| GIVEN("NotificationEvent with true state") | ||
| { | ||
| kf::Event ev(NotificationEvent, true); | ||
|
|
||
| THEN("The event is initially set") | ||
| { | ||
| REQUIRE(ev.isSet()); | ||
| } | ||
|
|
||
| WHEN("The event is cleared") | ||
| { | ||
| ev.clear(); | ||
|
|
||
| THEN("The event is no longer set") | ||
| { | ||
| REQUIRE(!ev.isSet()); | ||
| } | ||
| } | ||
|
|
||
| WHEN("The event is cleared multiple times") | ||
| { | ||
| ev.clear(); | ||
| ev.clear(); | ||
| ev.clear(); | ||
|
|
||
| THEN("The event remains unsignaled") | ||
| { | ||
| REQUIRE(!ev.isSet()); | ||
| } | ||
| } | ||
|
|
||
| WHEN("The event is set") | ||
| { | ||
| ev.set(); | ||
|
|
||
| THEN("The event is in a signaled state") | ||
| { | ||
| REQUIRE(ev.isSet()); | ||
| } | ||
| } | ||
|
|
||
| WHEN("The event is set multiple times") | ||
| { | ||
| ev.set(); | ||
| ev.set(); | ||
|
|
||
| THEN("The event remains in a signaled state") | ||
| { | ||
| REQUIRE(ev.isSet()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| GIVEN("SynchronizationEvent with false state") | ||
| { | ||
| kf::Event ev(SynchronizationEvent, false); | ||
|
|
||
| THEN("The event is initially not set") | ||
| { | ||
| REQUIRE(!ev.isSet()); | ||
| } | ||
|
|
||
| WHEN("The event is set") | ||
| { | ||
| ev.set(); | ||
|
|
||
| THEN("The event is in a signaled state") | ||
| { | ||
| REQUIRE(ev.isSet()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| GIVEN("NotificationEvent with false state") | ||
| { | ||
| WHEN("A wait is performed with no timeout and the event is not set") | ||
| { | ||
| kf::Event ev(NotificationEvent, false); | ||
| kf::Thread thread; | ||
| thread.start([](void* context) { | ||
SergiusTheBest marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| auto e = static_cast<kf::Event*>(context); | ||
| LARGE_INTEGER delay; | ||
| delay.QuadPart = -kOneSecond; | ||
| KeDelayExecutionThread(KernelMode, false, &delay); | ||
belyshevdenis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| e->set(); | ||
| }, &ev); | ||
|
|
||
| THEN("Event is not set immediately") | ||
| { | ||
| REQUIRE(!ev.isSet()); | ||
| } | ||
|
|
||
| auto status = ev.wait(); | ||
|
|
||
| THEN("The wait succeeds when the event is set") | ||
| { | ||
| REQUIRE(status == STATUS_SUCCESS); | ||
| REQUIRE(ev.isSet()); | ||
| } | ||
| } | ||
|
|
||
| WHEN("A wait is performed with a timeout and the event is not set") | ||
| { | ||
| kf::Event ev(NotificationEvent, false); | ||
| LARGE_INTEGER timeout; | ||
| timeout.QuadPart = -kOneSecond; | ||
|
|
||
| auto status = ev.wait(&timeout); | ||
|
|
||
| THEN("The wait times out") | ||
belyshevdenis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| REQUIRE(status == STATUS_TIMEOUT); | ||
| REQUIRE(!ev.isSet()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| GIVEN("NotificationEvent with multiple waiters") | ||
| { | ||
| WHEN("A wait is performed with no timeout and the event is not set") | ||
| { | ||
| kf::Event ev(NotificationEvent, false); | ||
| kf::Thread thread1; | ||
| kf::Thread thread2; | ||
| bool firstWoken = false; | ||
| bool secondWoken = false; | ||
|
|
||
| std::pair firstContext{ &ev, &firstWoken }; | ||
| std::pair secondContext{ &ev, &secondWoken }; | ||
|
|
||
| thread1.start([](void* context) { | ||
| auto e = static_cast<std::pair<kf::Event*, bool*>*>(context); | ||
| e->first->wait(); | ||
| *e->second = true; | ||
| }, &firstContext); | ||
|
|
||
| thread2.start([](void* context) { | ||
| auto e = static_cast<std::pair<kf::Event*, bool*>*>(context); | ||
| e->first->wait(); | ||
| *e->second = true; | ||
| }, &secondContext); | ||
|
|
||
| THEN("Both threads are waiting") | ||
| { | ||
| REQUIRE(!firstWoken); | ||
| REQUIRE(!secondWoken); | ||
| } | ||
|
|
||
| ev.set(); | ||
|
|
||
| // Give some time for threads to wake up | ||
| LARGE_INTEGER delay; | ||
| delay.QuadPart = -kOneSecond / 2; | ||
| KeDelayExecutionThread(KernelMode, false, &delay); | ||
SergiusTheBest marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
SergiusTheBest marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| THEN("Both threads are released from waiting") | ||
| { | ||
| REQUIRE(firstWoken); | ||
| REQUIRE(secondWoken); | ||
| } | ||
| } | ||
| } | ||
| GIVEN("SynchronizationEvent with multiple waiters") | ||
| { | ||
| kf::Event ev(SynchronizationEvent, false); | ||
| kf::Thread thread1; | ||
| kf::Thread thread2; | ||
|
|
||
| bool firstWoken = false; | ||
| bool secondWoken = false; | ||
| std::pair firstContext{ &ev, &firstWoken }; | ||
| std::pair secondContext{ &ev, &secondWoken }; | ||
|
|
||
| thread1.start([](void* context) { | ||
| auto data = static_cast<std::pair<kf::Event*, bool*>*>(context); | ||
| data->first->wait(); | ||
| *data->second = true; | ||
| }, &firstContext); | ||
|
|
||
| thread2.start([](void* context) { | ||
| auto data = static_cast<std::pair<kf::Event*, bool*>*>(context); | ||
| data->first->wait(); | ||
| *data->second = true; | ||
| }, &secondContext); | ||
|
|
||
| THEN("Both threads are waiting") | ||
| { | ||
| REQUIRE(!firstWoken); | ||
| REQUIRE(!secondWoken); | ||
| } | ||
|
|
||
| ev.set(); | ||
| // Give some time for threads to wake up | ||
| LARGE_INTEGER delay; | ||
| delay.QuadPart = -kOneSecond / 2; | ||
| KeDelayExecutionThread(KernelMode, false, &delay); | ||
|
|
||
| THEN("Only one thread is released and event resets") | ||
| { | ||
| REQUIRE(firstWoken ^ secondWoken); // Exactly one is true | ||
| REQUIRE(!ev.isSet()); | ||
| } | ||
|
|
||
| ev.set(); | ||
| } | ||
| } | ||
SergiusTheBest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.