Skip to content

Commit

Permalink
Add unit tests for _get_type_id (#9)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **Tests**
- Added new assertions to existing test cases to verify the expected
type ID for the `repeat()`, `poll()`, and `defer()` methods in the event
loop testing suite.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
maksimdrachov authored Dec 3, 2024
1 parent d71dcfa commit 9a63e04
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions tests/test_olg_scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ using testing::Ne;
using testing::IsNull;
using testing::IsEmpty;
using testing::ElementsAre;
using testing::ElementsAreArray;

// NOLINTBEGIN(bugprone-unchecked-optional-access)
// NOLINTBEGIN(readability-function-cognitive-complexity, misc-const-correctness)
Expand All @@ -42,6 +43,10 @@ using testing::ElementsAre;
namespace
{

/// Expected type ID for the type returned from repeat(), poll() and defer().
constexpr std::array<std::uint8_t, 16> expected_type_id =
{0xB6, 0x87, 0x48, 0xA6, 0x7A, 0xDB, 0x4D, 0xF1, 0xB3, 0x1D, 0xA9, 0x8D, 0x50, 0xA7, 0x82, 0x47};

/// This clock has to keep global state to implement the TrivialClock trait.
class SteadyClockMock final
{
Expand Down Expand Up @@ -103,6 +108,10 @@ TEST(TestOlgScheduler, EventLoopBasic)
EXPECT_THAT(evl.getTree()[1U], IsNull());
EXPECT_FALSE(evl.isEmpty());

// Check the type ID of return type repeat().
const auto actual_type_id = decltype(evt_a)::_get_type_id_();
EXPECT_THAT(actual_type_id, ElementsAreArray(expected_type_id));

auto evt_b = evl.repeat(100ms, // Smaller deadline goes on the left.
[&](const auto& arg) { b.emplace(arg); });
EXPECT_THAT(evl.getTree()[0U]->getDeadline().value().time_since_epoch(), 10'100ms);
Expand Down Expand Up @@ -264,6 +273,10 @@ TEST(TestOlgScheduler, EventLoopPoll)
});
EXPECT_THAT(evl.getTree()[0U]->getDeadline().value().time_since_epoch(), 110ms);

// Check the type ID of return type poll().
const auto actual_type_id = decltype(evt)::_get_type_id_();
EXPECT_THAT(actual_type_id, ElementsAreArray(expected_type_id));

SteadyClockMock::advance(30ms);
EXPECT_THAT(SteadyClockMock::now().time_since_epoch(), 130ms);
(void) evl.spin();
Expand All @@ -290,6 +303,10 @@ TEST(TestOlgScheduler, EventLoopDefer_single_overdue)

auto evt = evl.defer(SteadyClockMock::now() + 1000ms, [](const auto&) {});

// Check the type ID of return type defer().
const auto actual_type_id = decltype(evt)::_get_type_id_();
EXPECT_THAT(actual_type_id, ElementsAreArray(expected_type_id));

// This is special case - only one deferred event (and no "repeat"-s!), and it is already overdue (by +30ms).
// So, `next_deadline` should be `time_point::max()` b/c there will be nothing left pending after spin.
SteadyClockMock::advance(1000ms + 30ms);
Expand Down

0 comments on commit 9a63e04

Please sign in to comment.