[glib/glib-2-46] GDateTime test: fix occasional failures



commit d46166e6e9019d3a4456fb258fc95ee6202452a3
Author: Ryan Lortie <desrt desrt ca>
Date:   Thu Oct 15 15:14:36 2015 +0100

    GDateTime test: fix occasional failures
    
    We were using the time() library call to get the current time from the
    system in order to compare it to the time returned by
    g_date_time_new_now().
    
    Of course, we took care to ensure that the time (in seconds) didn't
    change in the middle of this process by checking the before and after
    value of the system time.
    
    Unfortunately, the system time as measured by time() was being taken
    from a less-accurate clock source than the time used by GDateTime.  As a
    result, we could have GDateTime already into the next second while the
    "seconds" value of the time returned by time() was still in the last
    one, even when checked "after".
    
    Avoid the problem by using the same ultimate source for time --
    g_get_real_time().
    
    This is based on a similar patch from Iain Lane, but it uses
    g_get_real_time() instead of g_get_current_time().
    
    https://bugzilla.gnome.org/show_bug.cgi?id=754994

 glib/tests/gdatetime.c |   14 +++++++-------
 1 files changed, 7 insertions(+), 7 deletions(-)
---
diff --git a/glib/tests/gdatetime.c b/glib/tests/gdatetime.c
index 967cc05..16a163c 100644
--- a/glib/tests/gdatetime.c
+++ b/glib/tests/gdatetime.c
@@ -79,14 +79,14 @@ test_GDateTime_now (void)
    * to guard against that. */
   do
     {
-      before = time (NULL);
+      before = g_get_real_time () / G_TIME_SPAN_SECOND;
 
       memset (&tm, 0, sizeof (tm));
       get_localtime_tm (before, &tm);
 
       dt = g_date_time_new_now_local ();
 
-      after = time (NULL);
+      after = g_get_real_time () / G_TIME_SPAN_SECOND;
     }
   while (before != after);
 
@@ -672,7 +672,7 @@ test_GDateTime_now_utc (void)
    * to guard against that. */
   do
     {
-      t = time (NULL);
+      t = g_get_real_time () / G_TIME_SPAN_SECOND;
 #ifdef HAVE_GMTIME_R
       gmtime_r (&t, &tm);
 #else
@@ -687,7 +687,7 @@ test_GDateTime_now_utc (void)
 #endif
       dt = g_date_time_new_now_utc ();
 
-      after = time (NULL);
+      after = g_get_real_time () / G_TIME_SPAN_SECOND;
     }
   while (t != after);
 
@@ -733,7 +733,7 @@ test_GDateTime_get_utc_offset (void)
   struct tm tm;
 
   memset (&tm, 0, sizeof (tm));
-  get_localtime_tm (time (NULL), &tm);
+  get_localtime_tm (g_get_real_time () / G_TIME_SPAN_SECOND, &tm);
 
   dt = g_date_time_new_now_local ();
   ts = g_date_time_get_utc_offset (dt);
@@ -776,12 +776,12 @@ test_GDateTime_to_local (void)
    * to guard against that. */
   do
     {
-      before = time (NULL);
+      before = g_get_real_time () / G_TIME_SPAN_SECOND;
       g_clear_pointer (&utc, g_date_time_unref);
       g_clear_pointer (&now, g_date_time_unref);
       utc = g_date_time_new_now_utc ();
       now = g_date_time_new_now_local ();
-      after = time (NULL);
+      after = g_get_real_time () / G_TIME_SPAN_SECOND;
     }
   while (before != after);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]