xrpld
Loading...
Searching...
No Matches
Scheduler_test.cpp
1#include <test/csf/Scheduler.h>
2
3#include <xrpl/beast/unit_test/suite.h>
4
5#include <set>
6
7namespace xrpl::test {
8
10{
11public:
12 void
13 run() override
14 {
15 using namespace std::chrono_literals;
16 csf::Scheduler scheduler;
17 std::set<int> seen;
18
19 scheduler.in(1s, [&] { seen.insert(1); });
20 scheduler.in(2s, [&] { seen.insert(2); });
21 auto token = scheduler.in(3s, [&] { seen.insert(3); });
22 scheduler.at(scheduler.now() + 4s, [&] { seen.insert(4); });
23 scheduler.at(scheduler.now() + 8s, [&] { seen.insert(8); });
24
25 auto start = scheduler.now();
26
27 // Process first event
28 BEAST_EXPECT(seen.empty());
29 BEAST_EXPECT(scheduler.stepOne());
30 BEAST_EXPECT(seen == std::set<int>({1}));
31 BEAST_EXPECT(scheduler.now() == (start + 1s));
32
33 // No processing if stepping until current time
34 BEAST_EXPECT(scheduler.stepUntil(scheduler.now()));
35 BEAST_EXPECT(seen == std::set<int>({1}));
36 BEAST_EXPECT(scheduler.now() == (start + 1s));
37
38 // Process next event
39 BEAST_EXPECT(scheduler.stepFor(1s));
40 BEAST_EXPECT(seen == std::set<int>({1, 2}));
41 BEAST_EXPECT(scheduler.now() == (start + 2s));
42
43 // Don't process cancelled event, but advance clock
44 scheduler.cancel(token);
45 BEAST_EXPECT(scheduler.stepFor(1s));
46 BEAST_EXPECT(seen == std::set<int>({1, 2}));
47 BEAST_EXPECT(scheduler.now() == (start + 3s));
48
49 // Process until 3 seen ints
50 BEAST_EXPECT(scheduler.stepWhile([&]() { return seen.size() < 3; }));
51 BEAST_EXPECT(seen == std::set<int>({1, 2, 4}));
52 BEAST_EXPECT(scheduler.now() == (start + 4s));
53
54 // Process the rest
55 BEAST_EXPECT(scheduler.step());
56 BEAST_EXPECT(seen == std::set<int>({1, 2, 4, 8}));
57 BEAST_EXPECT(scheduler.now() == (start + 8s));
58
59 // Process the rest again doesn't advance
60 BEAST_EXPECT(!scheduler.step());
61 BEAST_EXPECT(seen == std::set<int>({1, 2, 4, 8}));
62 BEAST_EXPECT(scheduler.now() == (start + 8s));
63 }
64};
65
67
68} // namespace xrpl::test
A testsuite class.
Definition suite.h:50
void run() override
Runs the suite.
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.
T empty(T... args)
T insert(T... args)
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5