Skip to content

Commit

Permalink
[timer] Fix Timeout::wait implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Nov 20, 2024
1 parent 7b571a6 commit 873c97e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/modm/processing/fiber/functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ yield();
modm::fiber::id
get_id();

/// Yields the current fiber while `bool condition()` returns true.
/// Yields the current fiber until `bool condition()` returns true.
/// @warning If `bool condition()` is true on first call, no yield is performed!
template< class Function >
requires requires { std::is_invocable_r_v<bool, Function, void>; }
Expand Down
2 changes: 1 addition & 1 deletion src/modm/processing/timer/timeout_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ template< class Clock, class Duration >
void
modm::GenericTimeout<Clock, Duration>::wait()
{
modm::this_fiber::poll([this]{ return not execute(); });
modm::this_fiber::poll([this]{ return execute(); });
}

// ----------------------------------------------------------------------------
Expand Down
65 changes: 65 additions & 0 deletions test/modm/processing/fiber/fiber_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "fiber_test.hpp"
#include "shared.hpp"

#include <modm/processing/timer.hpp>
#include <modm-test/mock/clock.hpp>

using namespace std::chrono_literals;
Expand Down Expand Up @@ -261,3 +262,67 @@ FiberTest::testStopToken()
});
modm::fiber::Scheduler::run();
}

void
FiberTest::testTimeoutWait()
{
modm::fiber::Task fiber1(stack1, [&]
{
TEST_ASSERT_EQUALS(state++, 0u);
modm::ShortTimeout timeout(20ms);
timeout.wait();
TEST_ASSERT_EQUALS(state++, 5u);

});
modm::fiber::Task fiber2(stack2, [&]
{
TEST_ASSERT_EQUALS(state++, 1u);
modm::this_fiber::yield();
TEST_ASSERT_EQUALS(state++, 2u);
test_clock_ms::increment(1);
modm::this_fiber::yield();
TEST_ASSERT_EQUALS(state++, 3u);
test_clock_ms::increment(10);
modm::this_fiber::yield();
TEST_ASSERT_EQUALS(state++, 4u);
test_clock_ms::increment(30);
modm::this_fiber::yield(); // goto 3

TEST_ASSERT_EQUALS(state++, 6u);
});
modm::fiber::Scheduler::run();
}

void
FiberTest::testPeriodicTimerWait()
{
modm::fiber::Task fiber1(stack1, [&]
{
TEST_ASSERT_EQUALS(state++, 0u);
modm::ShortPeriodicTimer timer(20ms);
timer.wait();
TEST_ASSERT_EQUALS(state++, 3u);
timer.wait(); // goto 4

TEST_ASSERT_EQUALS(state++, 6u);

});
modm::fiber::Task fiber2(stack2, [&]
{
TEST_ASSERT_EQUALS(state++, 1u);
modm::this_fiber::yield();
TEST_ASSERT_EQUALS(state++, 2u);
test_clock_ms::increment(30);
modm::this_fiber::yield(); // goto 3

TEST_ASSERT_EQUALS(state++, 4u);
test_clock_ms::increment(9);
modm::this_fiber::yield();
TEST_ASSERT_EQUALS(state++, 5u);
test_clock_ms::increment(1);
modm::this_fiber::yield(); // goto 6

TEST_ASSERT_EQUALS(state++, 7u);
});
modm::fiber::Scheduler::run();
}
6 changes: 6 additions & 0 deletions test/modm/processing/fiber/fiber_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@ class FiberTest : public unittest::TestSuite

void
testStopToken();

void
testTimeoutWait();

void
testPeriodicTimerWait();
};

0 comments on commit 873c97e

Please sign in to comment.