[icinga-checkins] icinga.org: icinga2/fix/wip-mfrosch-testing: WIP: Allow debugging with Utility::GetTime

git at icinga.org git at icinga.org
Wed Jun 1 11:01:31 CEST 2016


Module: icinga2
Branch: fix/wip-mfrosch-testing
Commit: 4f0c8d49649695240fcdd94e7c090064cb30507c
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=4f0c8d49649695240fcdd94e7c090064cb30507c

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

WIP: Allow debugging with Utility::GetTime

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

---

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

diff --git a/lib/base/utility.cpp b/lib/base/utility.cpp
index 10491d9..96668e0 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,18 @@ void Utility::NullDeleter(void *)
 	/* Nothing to do here. */
 }
 
+#ifdef I2_DEBUG
+void Utility::SetTime(double time)
+{
+	m_DebugTime = time;
+}
+
+void Utility::IncrementTime(double diff)
+{
+	m_DebugTime += diff;
+}
+#endif /* I2_DEBUG */
+
 /**
  * Returns the current UNIX timestamp including fractions of seconds.
  *
@@ -339,6 +355,11 @@ void Utility::NullDeleter(void *)
  */
 double Utility::GetTime(void)
 {
+#ifdef I2_DEBUG
+	if (m_DebugTime >= 0) {
+		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