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
22#include <xrpl/beast/unit_test.h>
23
24#include <set>
25
26namespace ripple {
27namespace test {
28
30{
31public:
32 void
33 run() override
34 {
35 using namespace std::chrono_literals;
36 csf::Scheduler scheduler;
37 std::set<int> seen;
38
39 scheduler.in(1s, [&] { seen.insert(1); });
40 scheduler.in(2s, [&] { seen.insert(2); });
41 auto token = scheduler.in(3s, [&] { seen.insert(3); });
42 scheduler.at(scheduler.now() + 4s, [&] { seen.insert(4); });
43 scheduler.at(scheduler.now() + 8s, [&] { seen.insert(8); });
44
45 auto start = scheduler.now();
46
47 // Process first event
48 BEAST_EXPECT(seen.empty());
49 BEAST_EXPECT(scheduler.step_one());
50 BEAST_EXPECT(seen == std::set<int>({1}));
51 BEAST_EXPECT(scheduler.now() == (start + 1s));
52
53 // No processing if stepping until current time
54 BEAST_EXPECT(scheduler.step_until(scheduler.now()));
55 BEAST_EXPECT(seen == std::set<int>({1}));
56 BEAST_EXPECT(scheduler.now() == (start + 1s));
57
58 // Process next event
59 BEAST_EXPECT(scheduler.step_for(1s));
60 BEAST_EXPECT(seen == std::set<int>({1, 2}));
61 BEAST_EXPECT(scheduler.now() == (start + 2s));
62
63 // Don't process cancelled event, but advance clock
64 scheduler.cancel(token);
65 BEAST_EXPECT(scheduler.step_for(1s));
66 BEAST_EXPECT(seen == std::set<int>({1, 2}));
67 BEAST_EXPECT(scheduler.now() == (start + 3s));
68
69 // Process until 3 seen ints
70 BEAST_EXPECT(scheduler.step_while([&]() { return seen.size() < 3; }));
71 BEAST_EXPECT(seen == std::set<int>({1, 2, 4}));
72 BEAST_EXPECT(scheduler.now() == (start + 4s));
73
74 // Process the rest
75 BEAST_EXPECT(scheduler.step());
76 BEAST_EXPECT(seen == std::set<int>({1, 2, 4, 8}));
77 BEAST_EXPECT(scheduler.now() == (start + 8s));
78
79 // Process the rest again doesn't advance
80 BEAST_EXPECT(!scheduler.step());
81 BEAST_EXPECT(seen == std::set<int>({1, 2, 4, 8}));
82 BEAST_EXPECT(scheduler.now() == (start + 8s));
83 }
84};
85
86BEAST_DEFINE_TESTSUITE(Scheduler, csf, ripple);
87
88} // namespace test
89} // 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:25