-
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 6 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,173 @@ | ||
| #include "pch.h" | ||
| #include <kf/Event.h> | ||
| #include <kf/Thread.h> | ||
|
|
||
| SCENARIO("kf::Event") | ||
| { | ||
| constexpr auto kOneMillisecond = 10'000; // 1ms | ||
| LARGE_INTEGER kZeroTimeout{0}; | ||
|
|
||
| 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("2 NotificationEvents for triggering and compition with false state") | ||
| { | ||
| kf::Event triggerEvent(NotificationEvent, false); | ||
| kf::Event completionEvent(NotificationEvent, false); | ||
| std::pair<kf::Event*, kf::Event*> events{ &triggerEvent, &completionEvent }; | ||
| kf::Thread thread; | ||
|
|
||
| WHEN("A wait is performed with no timeout and the event is not set") | ||
| { | ||
| thread.start([](void* context) { | ||
| const auto events = static_cast<std::pair<kf::Event*, kf::Event*>*>(context); | ||
| events->first->wait(); | ||
| events->second->set(); | ||
| }, &events); | ||
|
|
||
| THEN("Event is not set immediately") | ||
| { | ||
| REQUIRE(!completionEvent.isSet()); | ||
| } | ||
|
|
||
| triggerEvent.set(); | ||
|
|
||
| THEN("After trigger event is set, completionEvent from thread is set too") | ||
| { | ||
| REQUIRE(completionEvent.wait() == STATUS_SUCCESS); | ||
| REQUIRE(completionEvent.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 = -kOneMillisecond; | ||
|
|
||
| const 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 wait() calls") | ||
| { | ||
| kf::Event event(NotificationEvent, false); | ||
|
|
||
| WHEN("A wait is performed with 0 and the event is not set") | ||
| { | ||
| THEN("Both waits are timed out") | ||
| { | ||
| REQUIRE(STATUS_TIMEOUT == event.wait(&kZeroTimeout)); | ||
| REQUIRE(STATUS_TIMEOUT == event.wait(&kZeroTimeout)); | ||
| } | ||
|
|
||
| event.set(); | ||
|
|
||
| THEN("After event is set, both waits are successed") | ||
| { | ||
| REQUIRE(STATUS_SUCCESS == event.wait(&kZeroTimeout)); | ||
| REQUIRE(STATUS_SUCCESS == event.wait(&kZeroTimeout)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| GIVEN("SynchronizationEvent with multiple wait() calls") | ||
| { | ||
| kf::Event event(SynchronizationEvent, false); | ||
|
|
||
| WHEN("A wait is performed with 0 and the event is not set") | ||
| { | ||
| THEN("Both waits are timed out") | ||
| { | ||
| REQUIRE(STATUS_TIMEOUT == event.wait(&kZeroTimeout)); | ||
| REQUIRE(STATUS_TIMEOUT == event.wait(&kZeroTimeout)); | ||
| } | ||
|
|
||
| event.set(); | ||
|
|
||
| THEN("Only one wait is successed and event resets") | ||
| { | ||
| auto status1 = event.wait(&kZeroTimeout) == STATUS_SUCCESS ? 1 : 0; | ||
SergiusTheBest marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| auto status2 = event.wait(&kZeroTimeout) == STATUS_SUCCESS ? 1 : 0; | ||
| REQUIRE(status1 ^ status2); | ||
| REQUIRE(!event.isSet()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.