diff --git a/lib/base/utility.cpp b/lib/base/utility.cpp index 10491d9c4..ae2b9f6d2 100644 --- a/lib/base/utility.cpp +++ b/lib/base/utility.cpp @@ -64,6 +64,10 @@ using namespace icinga; boost::thread_specific_ptr Utility::m_ThreadName; boost::thread_specific_ptr Utility::m_RandSeed; +#ifdef I2_DEBUG +double Utility::m_DebugTime = -1; +#endif /* I2_DEBUG */ + /** * Demangles a symbol name. * @@ -332,6 +336,29 @@ void Utility::NullDeleter(void *) /* Nothing to do here. */ } +#ifdef I2_DEBUG +/** + * (DEBUG / TESTING ONLY) Sets the current system time to a static value, + * that will be be retrieved by any component of Icinga, when using GetTime(). + * + * This should be only used for testing purposes, e.g. unit tests and debugging of certain functionalities. + */ +void Utility::SetTime(double time) +{ + m_DebugTime = time; +} + +/** + * (DEBUG / TESTING ONLY) Increases the set debug system time by X seconds. + * + * This should be only used for testing purposes, e.g. unit tests and debugging of certain functionalities. + */ +void Utility::IncrementTime(double diff) +{ + m_DebugTime += diff; +} +#endif /* I2_DEBUG */ + /** * Returns the current UNIX timestamp including fractions of seconds. * @@ -339,6 +366,12 @@ void Utility::NullDeleter(void *) */ double Utility::GetTime(void) { +#ifdef I2_DEBUG + if (m_DebugTime >= 0) { + // (DEBUG / TESTING ONLY) this will return a *STATIC* system time, if the value has been set! + return m_DebugTime; + } +#endif /* I2_DEBUG */ #ifdef _WIN32 FILETIME cft; GetSystemTimeAsFileTime(&cft); diff --git a/lib/base/utility.hpp b/lib/base/utility.hpp index c1c50cc53..ea5bf3f12 100644 --- a/lib/base/utility.hpp +++ b/lib/base/utility.hpp @@ -148,6 +148,11 @@ public: static String GetIcingaDataPath(void); #endif /* _WIN32 */ +#ifdef I2_DEBUG + static void SetTime(double); + static void IncrementTime(double); +#endif /* I2_DEBUG */ + private: Utility(void); static void CollectPaths(const String& path, std::vector& paths); @@ -156,6 +161,10 @@ private: static int MksTemp (char *tmpl); #endif /* _WIN32 */ +#ifdef I2_DEBUG + static double m_DebugTime; +#endif /* I2_DEBUG */ + static boost::thread_specific_ptr m_ThreadName; static boost::thread_specific_ptr m_RandSeed; };