Skip to content

Commit

Permalink
TimerReset functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mattheweshleman committed Oct 16, 2024
1 parent 935cc64 commit b53d757
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
11 changes: 11 additions & 0 deletions include/FakeTimers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,17 @@ class FakeTimers
return true;
}

/**
* re-starts a timer that was previously created with TimerCreate() API function.
* Modeled after FreeRTOS xTimerReset.
* @param handle
* @return
*/
bool TimerReset(TimerHandle handle)
{
return TimerStart(handle);
}

/**
* Similar to FreeRTOS pvTimerGetTimerID
* @param handle
Expand Down
44 changes: 44 additions & 0 deletions tests/FakeTimersTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,47 @@ TEST(FakeTimersTests, timer_stop_will_stop_the_timer)
mUnderTest->MoveTimeForward(TEST_PERIOD);
mock().checkExpectations();
}

TEST(FakeTimersTests, timer_reset_will_restart_a_singleshot_timer)
{
const auto TEST_PERIOD = DEFAULT_TIMER_PERIOD;
using namespace cms::test;
auto handle = CreateAndStartSingleShot(TEST_PERIOD);
CHECK_TRUE(mUnderTest->IsTimerActive(handle));

mock().expectOneCall("testCallback").withParameter("handle", handle);
mUnderTest->MoveTimeForward(TEST_PERIOD);
mock().checkExpectations();

//move a bit forward
mUnderTest->Tick();

//reset (i.e. re-activate the single shot timer)
CHECK_TRUE(mUnderTest->TimerReset(handle));

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

TEST(FakeTimersTests, timer_reset_will_restart_a_repeating_timer)
{
const auto TEST_PERIOD = DEFAULT_TIMER_PERIOD;
using namespace cms::test;
auto handle = CreateAndStartAutoReload(TEST_PERIOD);
CHECK_TRUE(mUnderTest->IsTimerActive(handle));

//move time a bit forward
mUnderTest->Tick();

//reset (i.e. re-activate the timer)
CHECK_TRUE(mUnderTest->TimerReset(handle));

mock().expectNoCall("testCallback");
mUnderTest->MoveTimeForward(TEST_PERIOD - DEFAULT_SYS_TICK_PERIOD);
mock().checkExpectations();

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

0 comments on commit b53d757

Please sign in to comment.