Back to index

unity  6.0.0
test_utils.h
Go to the documentation of this file.
00001 #ifndef TEST_UTILS_H
00002 #define TEST_UTILS_H
00003 
00004 #include <UnityCore/Model.h>
00005 
00006 namespace
00007 {
00008 
00009 using unity::dash::Model;
00010 
00011 class Utils
00012 {
00013 public:
00014   template <typename Adaptor>
00015   static void WaitForModelSynchronize(Model<Adaptor>& model, unsigned int n_rows)
00016   {
00017     bool timeout_reached = false;
00018 
00019     auto timeout_cb = [](gpointer data) -> gboolean
00020     {
00021       *(bool*)data = true;
00022       return FALSE;
00023     };
00024 
00025     guint32 timeout_id = g_timeout_add(10000, timeout_cb, &timeout_reached);
00026 
00027     while (model.count != n_rows && !timeout_reached)
00028     {
00029       g_main_context_iteration(g_main_context_get_thread_default(), TRUE);
00030     }
00031     if (model.count == n_rows)
00032       g_source_remove(timeout_id);
00033   }
00034 
00035   static void WaitUntil(bool& success, unsigned int max_wait = 10)
00036   {
00037     bool timeout_reached = false;
00038     guint32 timeout_id = ScheduleTimeout(&timeout_reached, max_wait * 1000);
00039 
00040     while (!success && !timeout_reached)
00041       g_main_context_iteration(g_main_context_get_thread_default(), TRUE);
00042 
00043     if (success)
00044       g_source_remove(timeout_id);
00045 
00046     EXPECT_TRUE(success);
00047   }
00048 
00049   static guint32 ScheduleTimeout(bool* timeout_reached, unsigned int timeout_duration = 10)
00050   {
00051     return g_timeout_add(timeout_duration, TimeoutCallback, timeout_reached);
00052   }
00053 
00054   static void WaitForTimeout(unsigned int timeout_duration = 10)
00055   {
00056     WaitForTimeoutMSec(timeout_duration * 1000);
00057   }
00058 
00059   static void WaitForTimeoutMSec(unsigned int timeout_duration = 10)
00060   {
00061     bool timeout_reached = false;
00062     guint32 timeout_id = ScheduleTimeout(&timeout_reached, timeout_duration);
00063 
00064     while (!timeout_reached)
00065       g_main_context_iteration(g_main_context_get_thread_default(), TRUE);
00066 
00067     g_source_remove(timeout_id);
00068   }
00069 
00070 private:
00071   static gboolean TimeoutCallback(gpointer data)
00072   {
00073     *(bool*)data = true;
00074     return FALSE;
00075   };
00076 };
00077 
00078 }
00079 
00080 #endif