mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-03-21 05:00:10 +01:00
fuzz: make it possible to mock (fuzz) CThreadInterrupt
* Make the methods of `CThreadInterrupt` virtual and store a pointer to it in `CConnman`, thus making it possible to override with a mocked instance. * Initialize `CConnman::m_interrupt_net` from the constructor, making it possible for callers to supply mocked version. * Introduce `FuzzedThreadInterrupt` and `ConsumeThreadInterrupt()` and use them in `src/test/fuzz/connman.cpp` and `src/test/fuzz/i2p.cpp`. This improves the CPU utilization of the `connman` fuzz test. As a nice side effect, the `std::shared_ptr` used for `CConnman::m_interrupt_net` resolves the possible lifetime issues with it (see the removed comment for that variable).
This commit is contained in:
@@ -9,11 +9,16 @@
|
||||
|
||||
CThreadInterrupt::CThreadInterrupt() : flag(false) {}
|
||||
|
||||
CThreadInterrupt::operator bool() const
|
||||
bool CThreadInterrupt::interrupted() const
|
||||
{
|
||||
return flag.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
CThreadInterrupt::operator bool() const
|
||||
{
|
||||
return interrupted();
|
||||
}
|
||||
|
||||
void CThreadInterrupt::reset()
|
||||
{
|
||||
flag.store(false, std::memory_order_release);
|
||||
|
||||
@@ -27,11 +27,27 @@ class CThreadInterrupt
|
||||
{
|
||||
public:
|
||||
using Clock = std::chrono::steady_clock;
|
||||
|
||||
CThreadInterrupt();
|
||||
explicit operator bool() const;
|
||||
void operator()() EXCLUSIVE_LOCKS_REQUIRED(!mut);
|
||||
void reset();
|
||||
bool sleep_for(Clock::duration rel_time) EXCLUSIVE_LOCKS_REQUIRED(!mut);
|
||||
|
||||
virtual ~CThreadInterrupt() = default;
|
||||
|
||||
/// Return true if `operator()()` has been called.
|
||||
virtual bool interrupted() const;
|
||||
|
||||
/// An alias for `interrupted()`.
|
||||
virtual explicit operator bool() const;
|
||||
|
||||
/// Interrupt any sleeps. After this `interrupted()` will return `true`.
|
||||
virtual void operator()() EXCLUSIVE_LOCKS_REQUIRED(!mut);
|
||||
|
||||
/// Reset to an non-interrupted state.
|
||||
virtual void reset();
|
||||
|
||||
/// Sleep for the given duration.
|
||||
/// @retval true The time passed.
|
||||
/// @retval false The sleep was interrupted.
|
||||
virtual bool sleep_for(Clock::duration rel_time) EXCLUSIVE_LOCKS_REQUIRED(!mut);
|
||||
|
||||
private:
|
||||
std::condition_variable cond;
|
||||
|
||||
Reference in New Issue
Block a user