Skip to content

Commit 0c45d73

Browse files
committed
Merge bitcoin/bitcoin#29872: test: Add missing Assert(mock_time_in >= 0s) to SetMockTime
fae0db5 refactor: Use chrono type for g_mock_time (MarcoFalke) fa382d3 test: Add missing Assert(mock_time_in >= 0s) to SetMockTime (MarcoFalke) Pull request description: Seems odd to have the assert in the *deprecated* function, but not in the other. Fix this by adding it to the other, and by inlining the deprecated one. Also, use chrono type for the global mocktime variable. ACKs for top commit: davidgumberg: crACK bitcoin/bitcoin@fae0db5 stickies-v: ACK fae0db5 Tree-SHA512: 630c2917422ff2a7fa307114f95f22ad3c205429ffe36e67f0b2650733e40c876289c1aecebe882a9123d3106db7606bd6eff067ed6e2ecb95765984d3fe8612
2 parents 4d2d91a + fae0db5 commit 0c45d73

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

src/util/time.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
1818

19-
static std::atomic<int64_t> nMockTime(0); //!< For testing
19+
static std::atomic<std::chrono::seconds> g_mock_time{}; //!< For testing
2020

2121
NodeClock::time_point NodeClock::now() noexcept
2222
{
23-
const std::chrono::seconds mocktime{nMockTime.load(std::memory_order_relaxed)};
23+
const auto mocktime{g_mock_time.load(std::memory_order_relaxed)};
2424
const auto ret{
2525
mocktime.count() ?
2626
mocktime :
@@ -29,20 +29,16 @@ NodeClock::time_point NodeClock::now() noexcept
2929
return time_point{ret};
3030
};
3131

32-
void SetMockTime(int64_t nMockTimeIn)
33-
{
34-
Assert(nMockTimeIn >= 0);
35-
nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
36-
}
37-
32+
void SetMockTime(int64_t nMockTimeIn) { SetMockTime(std::chrono::seconds{nMockTimeIn}); }
3833
void SetMockTime(std::chrono::seconds mock_time_in)
3934
{
40-
nMockTime.store(mock_time_in.count(), std::memory_order_relaxed);
35+
Assert(mock_time_in >= 0s);
36+
g_mock_time.store(mock_time_in, std::memory_order_relaxed);
4137
}
4238

4339
std::chrono::seconds GetMockTime()
4440
{
45-
return std::chrono::seconds(nMockTime.load(std::memory_order_relaxed));
41+
return g_mock_time.load(std::memory_order_relaxed);
4642
}
4743

4844
int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); }

0 commit comments

Comments
 (0)