rippled
Loading...
Searching...
No Matches
Scheduler_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2017 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <test/csf/Scheduler.h>
21#include <xrpl/beast/unit_test.h>
22
23#include <set>
24
25namespace ripple {
26namespace test {
27
29{
30public:
31 void
32 run() override
33 {
34 using namespace std::chrono_literals;
35 csf::Scheduler scheduler;
36 std::set<int> seen;
37
38 scheduler.in(1s, [&] { seen.insert(1); });
39 scheduler.in(2s, [&] { seen.insert(2); });
40 auto token = scheduler.in(3s, [&] { seen.insert(3); });
41 scheduler.at(scheduler.now() + 4s, [&] { seen.insert(4); });
42 scheduler.at(scheduler.now() + 8s, [&] { seen.insert(8); });
43
44 auto start = scheduler.now();
45
46 // Process first event
47 BEAST_EXPECT(seen.empty());
48 BEAST_EXPECT(scheduler.step_one());
49 BEAST_EXPECT(seen == std::set<int>({1}));
50 BEAST_EXPECT(scheduler.now() == (start + 1s));
51
52 // No processing if stepping until current time
53 BEAST_EXPECT(scheduler.step_until(scheduler.now()));
54 BEAST_EXPECT(seen == std::set<int>({1}));
55 BEAST_EXPECT(scheduler.now() == (start + 1s));
56
57 // Process next event
58 BEAST_EXPECT(scheduler.step_for(1s));
59 BEAST_EXPECT(seen == std::set<int>({1, 2}));
60 BEAST_EXPECT(scheduler.now() == (start + 2s));
61
62 // Don't process cancelled event, but advance clock
63 scheduler.cancel(token);
64 BEAST_EXPECT(scheduler.step_for(1s));
65 BEAST_EXPECT(seen == std::set<int>({1, 2}));
66 BEAST_EXPECT(scheduler.now() == (start + 3s));
67
68 // Process until 3 seen ints
69 BEAST_EXPECT(scheduler.step_while([&]() { return seen.size() < 3; }));
70 BEAST_EXPECT(seen == std::set<int>({1, 2, 4}));
71 BEAST_EXPECT(scheduler.now() == (start + 4s));
72
73 // Process the rest
74 BEAST_EXPECT(scheduler.step());
75 BEAST_EXPECT(seen == std::set<int>({1, 2, 4, 8}));
76 BEAST_EXPECT(scheduler.now() == (start + 8s));
77
78 // Process the rest again doesn't advance
79 BEAST_EXPECT(!scheduler.step());
80 BEAST_EXPECT(seen == std::set<int>({1, 2, 4, 8}));
81 BEAST_EXPECT(scheduler.now() == (start + 8s));
82 }
83};
84
85BEAST_DEFINE_TESTSUITE(Scheduler, test, ripple);
86
87} // namespace test
88} // namespace ripple
A testsuite class.
Definition: suite.h:55
void run() override
Runs the suite.
Simulated discrete-event scheduler.
bool step_one()
Run the scheduler for up to one event.
bool step_while(Function &&func)
Run the scheduler while a condition is true.
cancel_token at(time_point const &when, Function &&f)
Schedule an event at a specific time.
bool step_for(std::chrono::duration< Period, Rep > const &amount)
Run the scheduler until time has elapsed.
time_point now() const
Return the current network time.
bool step_until(time_point const &until)
Run the scheduler until the specified time.
void cancel(cancel_token const &token)
Cancel a timer.
bool step()
Run the scheduler until no events remain.
cancel_token in(duration const &delay, Function &&f)
Schedule an event after a specified duration passes.
T empty(T... args)
T insert(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26