1#include <csf/Scheduler.h>
3#include <gtest/gtest.h>
9TEST(SchedulerTest, scheduler)
11 using namespace std::chrono_literals;
15 scheduler.
in(1s, [&] { seen.
insert(1); });
16 scheduler.
in(2s, [&] { seen.
insert(2); });
18 scheduler.
at(scheduler.
now() + 4s, [&] { seen.insert(4); });
19 scheduler.
at(scheduler.
now() + 8s, [&] { seen.insert(8); });
21 auto start = scheduler.
now();
24 EXPECT_TRUE(seen.
empty());
25 EXPECT_TRUE(scheduler.
stepOne());
27 EXPECT_TRUE(scheduler.
now() == (start + 1s));
32 EXPECT_TRUE(scheduler.
now() == (start + 1s));
35 EXPECT_TRUE(scheduler.
stepFor(1s));
37 EXPECT_TRUE(scheduler.
now() == (start + 2s));
41 EXPECT_TRUE(scheduler.
stepFor(1s));
43 EXPECT_TRUE(scheduler.
now() == (start + 3s));
46 EXPECT_TRUE(scheduler.
stepWhile([&]() { return seen.size() < 3; }));
48 EXPECT_TRUE(scheduler.
now() == (start + 4s));
51 EXPECT_TRUE(scheduler.
step());
53 EXPECT_TRUE(scheduler.
now() == (start + 8s));
56 EXPECT_TRUE(!scheduler.
step());
58 EXPECT_TRUE(scheduler.
now() == (start + 8s));
Simulated discrete-event scheduler.
bool step()
Run the scheduler until no events remain.
CancelToken at(time_point const &when, Function &&f)
Schedule an event at a specific time.
time_point now() const
Return the current network time.
bool stepUntil(time_point const &until)
Run the scheduler until the specified time.
void cancel(CancelToken const &token)
Cancel a timer.
CancelToken in(duration const &delay, Function &&f)
Schedule an event after a specified duration passes.
bool stepOne()
Run the scheduler for up to one event.
bool stepWhile(Function &&func)
Run the scheduler while a condition is true.
bool stepFor(std::chrono::duration< Period, Rep > const &amount)
Run the scheduler until time has elapsed.