rippled
ManualTimeKeeper.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012-2015 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/jtx/ManualTimeKeeper.h>
21 
22 namespace ripple {
23 namespace test {
24 
25 using namespace std::chrono_literals;
26 
27 ManualTimeKeeper::ManualTimeKeeper() : closeOffset_{}, now_(0s)
28 {
29 }
30 
31 void
33 {
34 }
35 
36 auto
38 {
39  std::lock_guard lock(mutex_);
40  return now_;
41 }
42 
43 auto
45 {
46  std::lock_guard lock(mutex_);
47  return now_ + closeOffset_;
48 }
49 
50 void
52 {
53  // Copied from TimeKeeper::adjustCloseTime
54  using namespace std::chrono;
55  auto const s = amount.count();
56  std::lock_guard lock(mutex_);
57  // Take large offsets, ignore small offsets,
58  // push the close time towards our wall time.
59  if (s > 1)
60  closeOffset_ += seconds((s + 3) / 4);
61  else if (s < -1)
62  closeOffset_ += seconds((s - 3) / 4);
63  else
64  closeOffset_ = (closeOffset_ * 3) / 4;
65 }
66 
69 {
70  return {};
71 }
72 
75 {
76  std::lock_guard lock(mutex_);
77  return closeOffset_;
78 }
79 
80 void
82 {
83  std::lock_guard lock(mutex_);
84  now_ = now;
85 }
86 
87 auto
88 ManualTimeKeeper::adjust(std::chrono::system_clock::time_point when)
89  -> time_point
90 {
91  return time_point(std::chrono::duration_cast<duration>(
92  when.time_since_epoch() - days(10957)));
93 }
94 } // namespace test
95 } // namespace ripple
ripple::test::ManualTimeKeeper::run
void run(std::vector< std::string > const &servers) override
Launch the internal thread.
Definition: ManualTimeKeeper.cpp:32
ripple::test::ManualTimeKeeper::ManualTimeKeeper
ManualTimeKeeper()
Definition: ManualTimeKeeper.cpp:27
std::vector< std::string >
std::chrono::duration< std::int32_t >
std::lock_guard
STL class.
ripple::test::ManualTimeKeeper::closeTime
time_point closeTime() const override
Returns the close time, in network time.
Definition: ManualTimeKeeper.cpp:44
ripple::test::ManualTimeKeeper::nowOffset
std::chrono::duration< std::int32_t > nowOffset() const override
Definition: ManualTimeKeeper.cpp:68
ripple::test::ManualTimeKeeper::closeOffset
std::chrono::duration< std::int32_t > closeOffset() const override
Definition: ManualTimeKeeper.cpp:74
ripple::test::ManualTimeKeeper::adjust
static time_point adjust(std::chrono::system_clock::time_point when)
Definition: ManualTimeKeeper.cpp:88
ripple::test::ManualTimeKeeper::mutex_
std::mutex mutex_
Definition: ManualTimeKeeper.h:60
ripple::test::ManualTimeKeeper::now_
time_point now_
Definition: ManualTimeKeeper.h:62
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::ManualTimeKeeper::adjustCloseTime
void adjustCloseTime(std::chrono::duration< std::int32_t > amount) override
Adjust the close time.
Definition: ManualTimeKeeper.cpp:51
std::chrono::duration::count
T count(T... args)
ripple::test::ManualTimeKeeper::now
time_point now() const override
Returns the estimate of wall time, in network time.
Definition: ManualTimeKeeper.cpp:37
ripple::test::ManualTimeKeeper::set
void set(time_point now)
Definition: ManualTimeKeeper.cpp:81
beast::abstract_clock< NetClock >::time_point
typename NetClock ::time_point time_point
Definition: abstract_clock.h:63
ripple::test::ManualTimeKeeper::closeOffset_
std::chrono::duration< std::int32_t > closeOffset_
Definition: ManualTimeKeeper.h:61
std::chrono