nmtst: add nmtst_main_loop_run()
authorThomas Haller <thaller@redhat.com>
Mon, 21 Dec 2015 13:16:35 +0000 (14:16 +0100)
committerThomas Haller <thaller@redhat.com>
Fri, 25 Dec 2015 19:36:43 +0000 (20:36 +0100)
shared/nm-test-utils.h

index 28ae56f..27a8d16 100644 (file)
@@ -733,6 +733,45 @@ nmtst_rand_perm (GRand *rand, void *dst, const void *src, gsize elmt_size, gsize
        return dst;
 }
 
+/*****************************************************************************/
+
+inline static gboolean
+_nmtst_main_loop_run_timeout (gpointer user_data)
+{
+       GMainLoop **p_loop = user_data;
+
+       g_assert (p_loop);
+       g_assert (*p_loop);
+
+       g_main_loop_quit (*p_loop);
+       *p_loop = NULL;
+
+       return G_SOURCE_REMOVE;
+}
+
+inline static gboolean
+nmtst_main_loop_run (GMainLoop *loop, int timeout_ms)
+{
+       GSource *source = NULL;
+       guint id = 0;
+       GMainLoop *loopx = loop;
+
+       if (timeout_ms > 0) {
+               source = g_timeout_source_new (timeout_ms);
+               g_source_set_callback (source, _nmtst_main_loop_run_timeout, &loopx, NULL);
+               id = g_source_attach (source, g_main_loop_get_context (loop));
+               g_assert (id);
+               g_source_unref (source);
+       }
+
+       g_main_loop_run (loop);
+
+       /* if the timeout was reached, return FALSE. */
+       return loopx != NULL;
+}
+
+/*****************************************************************************/
+
 inline static const char *
 nmtst_get_sudo_cmd (void)
 {