Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
time_shield::detail::FakeNtpRunner Class Reference

Fake runner for tests without network access. More...

#include <ntp_time_service.hpp>

Public Member Functions

 FakeNtpRunner ()=default
 Construct fake runner.
 
 FakeNtpRunner (NtpClientPool)
 Construct fake runner with an unused pool.
 
 FakeNtpRunner (const FakeNtpRunner &)=delete
 
FakeNtpRunneroperator= (const FakeNtpRunner &)=delete
 
 ~FakeNtpRunner ()
 Stop background thread on destruction.
 
bool start (std::chrono::milliseconds interval=std::chrono::seconds(30), bool measure_immediately=true)
 Start fake measurements on a background thread.
 
bool start (int interval_ms, bool measure_immediately=true)
 Start fake measurements using milliseconds.
 
void stop ()
 Stop background measurements.
 
bool running () const noexcept
 Return true when background thread is running.
 
bool force_measure ()
 Wake the worker thread and request a measurement.
 
bool measure_now ()
 Perform one measurement immediately.
 
int64_t offset_us () const noexcept
 Return last estimated offset in microseconds.
 
int64_t utc_time_us () const noexcept
 Return current UTC time in microseconds based on offset.
 
int64_t utc_time_ms () const noexcept
 Return current UTC time in milliseconds based on offset.
 
int64_t utc_time_sec () const noexcept
 Return current UTC time in seconds based on offset.
 
bool last_measure_ok () const noexcept
 Return whether last measurement updated the offset.
 
uint64_t measure_count () const noexcept
 Return total number of measurement attempts.
 
uint64_t fail_count () const noexcept
 Return number of failed measurement attempts.
 
int64_t last_update_realtime_us () const noexcept
 Return realtime timestamp of last measurement attempt.
 
int64_t last_success_realtime_us () const noexcept
 Return realtime timestamp of last successful measurement.
 
std::vector< NtpSamplelast_samples () const
 Return copy of most recent samples.
 

Private Member Functions

void run_loop ()
 Background loop for fake measurements.
 
bool do_measure ()
 Update fake offset and stats.
 

Private Attributes

std::chrono::milliseconds m_interval {std::chrono::seconds(30)}
 
bool m_measure_immediately {true}
 
std::thread m_thread
 
std::condition_variable m_cv
 
std::mutex m_cv_mtx
 
std::atomic< bool > m_is_running {false}
 
std::atomic< bool > m_is_stop_requested {false}
 
std::atomic< bool > m_is_force_requested {false}
 
std::atomic< bool > m_last_measure_ok {false}
 
std::atomic< uint64_t > m_measure_count {0}
 
std::atomic< uint64_t > m_fail_count {0}
 
std::atomic< int64_t > m_last_update_realtime_us {0}
 
std::atomic< int64_t > m_last_success_realtime_us {0}
 
std::atomic< int64_t > m_offset_us {0}
 

Detailed Description

Fake runner for tests without network access.

Definition at line 32 of file ntp_time_service.hpp.

Constructor & Destructor Documentation

◆ FakeNtpRunner() [1/3]

time_shield::detail::FakeNtpRunner::FakeNtpRunner ( )
default

Construct fake runner.

◆ FakeNtpRunner() [2/3]

time_shield::detail::FakeNtpRunner::FakeNtpRunner ( NtpClientPool )
inlineexplicit

Construct fake runner with an unused pool.

Definition at line 37 of file ntp_time_service.hpp.

◆ FakeNtpRunner() [3/3]

time_shield::detail::FakeNtpRunner::FakeNtpRunner ( const FakeNtpRunner & )
delete

◆ ~FakeNtpRunner()

time_shield::detail::FakeNtpRunner::~FakeNtpRunner ( )
inline

Stop background thread on destruction.

Definition at line 43 of file ntp_time_service.hpp.

Member Function Documentation

◆ do_measure()

bool time_shield::detail::FakeNtpRunner::do_measure ( )
inlineprivate

Update fake offset and stats.

Definition at line 151 of file ntp_time_service.hpp.

◆ fail_count()

uint64_t time_shield::detail::FakeNtpRunner::fail_count ( ) const
inlinenoexcept

Return number of failed measurement attempts.

Definition at line 117 of file ntp_time_service.hpp.

◆ force_measure()

bool time_shield::detail::FakeNtpRunner::force_measure ( )
inline

Wake the worker thread and request a measurement.

Definition at line 89 of file ntp_time_service.hpp.

◆ last_measure_ok()

bool time_shield::detail::FakeNtpRunner::last_measure_ok ( ) const
inlinenoexcept

Return whether last measurement updated the offset.

Definition at line 113 of file ntp_time_service.hpp.

◆ last_samples()

std::vector< NtpSample > time_shield::detail::FakeNtpRunner::last_samples ( ) const
inline

Return copy of most recent samples.

Definition at line 124 of file ntp_time_service.hpp.

◆ last_success_realtime_us()

int64_t time_shield::detail::FakeNtpRunner::last_success_realtime_us ( ) const
inlinenoexcept

Return realtime timestamp of last successful measurement.

Definition at line 121 of file ntp_time_service.hpp.

◆ last_update_realtime_us()

int64_t time_shield::detail::FakeNtpRunner::last_update_realtime_us ( ) const
inlinenoexcept

Return realtime timestamp of last measurement attempt.

Definition at line 119 of file ntp_time_service.hpp.

◆ measure_count()

uint64_t time_shield::detail::FakeNtpRunner::measure_count ( ) const
inlinenoexcept

Return total number of measurement attempts.

Definition at line 115 of file ntp_time_service.hpp.

◆ measure_now()

bool time_shield::detail::FakeNtpRunner::measure_now ( )
inline

Perform one measurement immediately.

Definition at line 99 of file ntp_time_service.hpp.

◆ offset_us()

int64_t time_shield::detail::FakeNtpRunner::offset_us ( ) const
inlinenoexcept

Return last estimated offset in microseconds.

Definition at line 104 of file ntp_time_service.hpp.

◆ operator=()

FakeNtpRunner & time_shield::detail::FakeNtpRunner::operator= ( const FakeNtpRunner & )
delete

◆ run_loop()

void time_shield::detail::FakeNtpRunner::run_loop ( )
inlineprivate

Background loop for fake measurements.

Definition at line 128 of file ntp_time_service.hpp.

◆ running()

bool time_shield::detail::FakeNtpRunner::running ( ) const
inlinenoexcept

Return true when background thread is running.

Definition at line 86 of file ntp_time_service.hpp.

◆ start() [1/2]

bool time_shield::detail::FakeNtpRunner::start ( int interval_ms,
bool measure_immediately = true )
inline

Start fake measurements using milliseconds.

Definition at line 71 of file ntp_time_service.hpp.

◆ start() [2/2]

bool time_shield::detail::FakeNtpRunner::start ( std::chrono::milliseconds interval = std::chrono::seconds(30),
bool measure_immediately = true )
inline

Start fake measurements on a background thread.

Definition at line 48 of file ntp_time_service.hpp.

◆ stop()

void time_shield::detail::FakeNtpRunner::stop ( )
inline

Stop background measurements.

Definition at line 76 of file ntp_time_service.hpp.

◆ utc_time_ms()

int64_t time_shield::detail::FakeNtpRunner::utc_time_ms ( ) const
inlinenoexcept

Return current UTC time in milliseconds based on offset.

Definition at line 108 of file ntp_time_service.hpp.

◆ utc_time_sec()

int64_t time_shield::detail::FakeNtpRunner::utc_time_sec ( ) const
inlinenoexcept

Return current UTC time in seconds based on offset.

Definition at line 110 of file ntp_time_service.hpp.

◆ utc_time_us()

int64_t time_shield::detail::FakeNtpRunner::utc_time_us ( ) const
inlinenoexcept

Return current UTC time in microseconds based on offset.

Definition at line 106 of file ntp_time_service.hpp.

Member Data Documentation

◆ m_cv

std::condition_variable time_shield::detail::FakeNtpRunner::m_cv
private

Definition at line 166 of file ntp_time_service.hpp.

◆ m_cv_mtx

std::mutex time_shield::detail::FakeNtpRunner::m_cv_mtx
private

Definition at line 167 of file ntp_time_service.hpp.

◆ m_fail_count

std::atomic<uint64_t> time_shield::detail::FakeNtpRunner::m_fail_count {0}
private

Definition at line 175 of file ntp_time_service.hpp.

◆ m_interval

std::chrono::milliseconds time_shield::detail::FakeNtpRunner::m_interval {std::chrono::seconds(30)}
private

Definition at line 162 of file ntp_time_service.hpp.

◆ m_is_force_requested

std::atomic<bool> time_shield::detail::FakeNtpRunner::m_is_force_requested {false}
private

Definition at line 171 of file ntp_time_service.hpp.

◆ m_is_running

std::atomic<bool> time_shield::detail::FakeNtpRunner::m_is_running {false}
private

Definition at line 169 of file ntp_time_service.hpp.

◆ m_is_stop_requested

std::atomic<bool> time_shield::detail::FakeNtpRunner::m_is_stop_requested {false}
private

Definition at line 170 of file ntp_time_service.hpp.

◆ m_last_measure_ok

std::atomic<bool> time_shield::detail::FakeNtpRunner::m_last_measure_ok {false}
private

Definition at line 173 of file ntp_time_service.hpp.

◆ m_last_success_realtime_us

std::atomic<int64_t> time_shield::detail::FakeNtpRunner::m_last_success_realtime_us {0}
private

Definition at line 177 of file ntp_time_service.hpp.

◆ m_last_update_realtime_us

std::atomic<int64_t> time_shield::detail::FakeNtpRunner::m_last_update_realtime_us {0}
private

Definition at line 176 of file ntp_time_service.hpp.

◆ m_measure_count

std::atomic<uint64_t> time_shield::detail::FakeNtpRunner::m_measure_count {0}
private

Definition at line 174 of file ntp_time_service.hpp.

◆ m_measure_immediately

bool time_shield::detail::FakeNtpRunner::m_measure_immediately {true}
private

Definition at line 163 of file ntp_time_service.hpp.

◆ m_offset_us

std::atomic<int64_t> time_shield::detail::FakeNtpRunner::m_offset_us {0}
private

Definition at line 178 of file ntp_time_service.hpp.

◆ m_thread

std::thread time_shield::detail::FakeNtpRunner::m_thread
private

Definition at line 165 of file ntp_time_service.hpp.


The documentation for this class was generated from the following file: