Allow debugging of Utility::GetTime

So we can run unit tests that expect a certain behavior based on time.

When Icinga 2 is compiled with I2_DEBUG one can use Utility::SetTime to
override the current system time, and lock it to this value.

fixes #11875
This commit is contained in:
Markus Frosch 2016-05-31 17:09:22 +02:00 committed by Gunnar Beutner
parent 2e8c8809ea
commit a5abe1a972
2 changed files with 42 additions and 0 deletions

View File

@ -64,6 +64,10 @@ using namespace icinga;
boost::thread_specific_ptr<String> Utility::m_ThreadName;
boost::thread_specific_ptr<unsigned int> 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);

View File

@ -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<String>& 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<String> m_ThreadName;
static boost::thread_specific_ptr<unsigned int> m_RandSeed;
};