Skip to content

Commit

Permalink
GetTimerContext implemented. Also convenience Tick() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattheweshleman committed Oct 15, 2024
1 parent f1c8bd9 commit c597616
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
14 changes: 14 additions & 0 deletions include/FakeTimers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ class FakeTimers
return true;
}

void * GetTimerContext(TimerHandle handle)
{
Timer& timer = mTimers.at(handle - 1);
assert(timer.handle == handle);
assert(timer.allocated);

return timer.context;
}

/**
* Move time forward. Timers only have an opportunity
* to fire based on sys tick period.
Expand All @@ -165,6 +174,11 @@ class FakeTimers
}
}

void Tick()
{
MoveTimeForward(mSysTickPeriod);
}

private:
struct Timer
{
Expand Down
21 changes: 20 additions & 1 deletion tests/FakeTimersTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ using namespace cms::test;

static const std::chrono::milliseconds DEFAULT_SYS_TICK_PERIOD = 10ms;
static const std::chrono::milliseconds DEFAULT_TIMER_PERIOD = DEFAULT_SYS_TICK_PERIOD * 10;
static int TestContextObject = 1;

TEST_GROUP(FakeTimersTests) {

Expand All @@ -58,7 +59,7 @@ TEST_GROUP(FakeTimersTests) {
{
auto handle = mUnderTest->TimerCreate(
"TEST", period, behavior,
nullptr, testCallback);
&TestContextObject, testCallback);
return handle;
}

Expand Down Expand Up @@ -179,6 +180,17 @@ TEST(FakeTimersTests, singleshot_timer_only_fires_once)
mock().checkExpectations();
}

TEST(FakeTimersTests, tick_convenience_method_moves_time_forward_as_expected)
{
const auto TEST_PERIOD = DEFAULT_SYS_TICK_PERIOD;
using namespace cms::test;
auto handle = CreateAndStartSingleShot(TEST_PERIOD);

mock().expectOneCall("testCallback").withParameter("handle", handle);
mUnderTest->Tick();
mock().checkExpectations();
}

TEST(FakeTimersTests, auto_reload_timer_fires_after_one_period_of_time)
{
const auto TEST_PERIOD = DEFAULT_TIMER_PERIOD;
Expand Down Expand Up @@ -207,4 +219,11 @@ TEST(FakeTimersTests, auto_reload_timer_fires_multiple_times)
mock().expectNCalls(RELOADS, "testCallback").withParameter("handle", handle);
mUnderTest->MoveTimeForward(TEST_PERIOD * RELOADS);
mock().checkExpectations();
}

TEST(FakeTimersTests, access_user_context_via_handle)
{
auto handle = Create();
auto context = mUnderTest->GetTimerContext(handle);
CHECK_TRUE(context == &TestContextObject);
}

0 comments on commit c597616

Please sign in to comment.