rippled
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 <ripple/basics/ByteUtilities.h>
21 #include <ripple/beast/unit_test.h>
22 #include <test/csf/Scheduler.h>
23 
24 #include <set>
25 
26 namespace ripple {
27 namespace test {
28 
29 class Scheduler_test : public beast::unit_test::suite
30 {
31 public:
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 
86 BEAST_DEFINE_TESTSUITE(Scheduler, test, ripple);
87 
88 } // namespace test
89 } // namespace ripple
ripple::test::csf::Scheduler::cancel
void cancel(cancel_token const &token)
Cancel a timer.
Definition: test/csf/Scheduler.h:385
ripple::test::csf::Scheduler::step_while
bool step_while(Function &&func)
Run the scheduler while a condition is true.
Definition: test/csf/Scheduler.h:415
ripple::test::csf::Scheduler
Simulated discrete-event scheduler.
Definition: test/csf/Scheduler.h:47
ripple::test::csf::Scheduler::step_one
bool step_one()
Run the scheduler for up to one event.
Definition: test/csf/Scheduler.h:391
ripple::test::csf::Scheduler::in
cancel_token in(duration const &delay, Function &&f)
Schedule an event after a specified duration passes.
ripple::test::Scheduler_test
Definition: Scheduler_test.cpp:29
ripple::test::csf::Scheduler::step_until
bool step_until(time_point const &until)
Run the scheduler until the specified time.
Definition: test/csf/Scheduler.h:424
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::csf::Scheduler::at
cancel_token at(time_point const &when, Function &&f)
Schedule an event at a specific time.
ripple::test::csf::Scheduler::step
bool step()
Run the scheduler until no events remain.
Definition: test/csf/Scheduler.h:403
std::set::insert
T insert(T... args)
ripple::test::Scheduler_test::run
void run() override
Definition: Scheduler_test.cpp:33
std::set::empty
T empty(T... args)
ripple::test::csf::Scheduler::now
time_point now() const
Return the current network time.
Definition: test/csf/Scheduler.h:365
ripple::test::csf::Scheduler::step_for
bool step_for(std::chrono::duration< Period, Rep > const &amount)
Run the scheduler until time has elapsed.
Definition: test/csf/Scheduler.h:449
set
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(DeliverMin, app, ripple)