[glib: 6/7] gmain: test far-future ready_time



commit 4544dcb78897ccad7d7fdd2a49ac9653c3349b08
Author: Will Thompson <will willthompson co uk>
Date:   Mon Nov 26 21:07:20 2018 +0000

    gmain: test far-future ready_time

 glib/tests/timeout.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
---
diff --git a/glib/tests/timeout.c b/glib/tests/timeout.c
index cbbab7ff2..8f44e7552 100644
--- a/glib/tests/timeout.c
+++ b/glib/tests/timeout.c
@@ -91,6 +91,53 @@ test_weeks_overflow (void)
   g_source_remove (id);
 }
 
+/* The ready_time for a GSource is stored as a gint64, as an absolute monotonic
+ * time in microseconds. To call poll(), this must be converted to a relative
+ * timeout, in milliseconds, as a gint. If the ready_time is sufficiently far
+ * in the future, the timeout will not fit. Previously, it would be narrowed in
+ * an implementation-defined way; if this gave a negative result, poll() would
+ * block forever.
+ *
+ * This test creates a GSource with the largest possible ready_time (a little
+ * over 292 millennia, assuming g_get_monotonic_time() starts from near 0 when
+ * the system boots), adds it to a GMainContext, queries it for the parameters
+ * to pass to poll() -- essentially the first half of
+ * g_main_context_iteration() -- and checks that the timeout is a large
+ * positive number.
+ */
+static void
+test_far_future_ready_time (void)
+{
+  GSourceFuncs source_funcs = { 0 };
+  GMainContext *context = g_main_context_new ();
+  GSource *source = g_source_new (&source_funcs, sizeof (GSource));
+  gboolean acquired, ready;
+  gint priority, timeout_, n_fds;
+
+  g_source_set_ready_time (source, G_MAXINT64);
+  g_source_attach (source, context);
+
+  acquired = g_main_context_acquire (context);
+  g_assert_true (acquired);
+
+  ready = g_main_context_prepare (context, &priority);
+  g_assert_false (ready);
+
+  n_fds = 0;
+  n_fds = g_main_context_query (context, priority, &timeout_, NULL, n_fds);
+
+  /* The true timeout in milliseconds doesn't fit into a gint. We definitely
+   * don't want poll() to block forever:
+   */
+  g_assert_cmpint (timeout_, >=, 0);
+  /* Instead, we want it to block for as long as possible: */
+  g_assert_cmpint (timeout_, ==, G_MAXINT);
+
+  g_main_context_release (context);
+  g_main_context_unref (context);
+  g_source_unref (source);
+}
+
 static gint64 last_time;
 static gint count;
 
@@ -145,6 +192,7 @@ main (int argc, char *argv[])
 
   g_test_add_func ("/timeout/seconds", test_seconds);
   g_test_add_func ("/timeout/weeks-overflow", test_weeks_overflow);
+  g_test_add_func ("/timeout/far-future-ready-time", test_far_future_ready_time);
   g_test_add_func ("/timeout/rounding", test_rounding);
 
   return g_test_run ();


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