[icinga-checkins] icinga.org: icinga2/fix/api-sync-repl-11684: Allow debugging of Utility:: GetTime

git at icinga.org git at icinga.org
Wed Jun 15 17:50:18 CEST 2016


Module: icinga2
Branch: fix/api-sync-repl-11684
Commit: a5abe1a972e8e29cbbbea2d52b94b32703c332db
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=a5abe1a972e8e29cbbbea2d52b94b32703c332db

Author: Markus Frosch <lazyfrosch at icinga.org>
Date:   Tue May 31 17:09:22 2016 +0200

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

---

 lib/base/utility.cpp |   33 +++++++++++++++++++++++++++++++++
 lib/base/utility.hpp |    9 +++++++++
 2 files changed, 42 insertions(+)

diff --git a/lib/base/utility.cpp b/lib/base/utility.cpp
index 10491d9..ae2b9f6 100644
--- a/lib/base/utility.cpp
+++ b/lib/base/utility.cpp
@@ -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);
diff --git a/lib/base/utility.hpp b/lib/base/utility.hpp
index c1c50cc..ea5bf3f 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<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;
 };



More information about the icinga-checkins mailing list