A simple test for polling on a file-like descriptor
authorMikhail Zabaluev <mikhail.zabaluev@gmail.com>
Tue, 17 Sep 2013 06:07:07 +0000 (09:07 +0300)
committerMatthias Clasen <mclasen@redhat.com>
Sat, 28 Jun 2014 17:56:07 +0000 (13:56 -0400)
https://bugzilla.gnome.org/show_bug.cgi?id=699132

glib/tests/mainloop.c

index 124ace5..df1dfb6 100644 (file)
@@ -1462,6 +1462,48 @@ test_source_unix_fd_api (void)
   close (fds_b[1]);
 }
 
+static gboolean
+unixfd_quit_loop (gint         fd,
+                  GIOCondition condition,
+                  gpointer     user_data)
+{
+  GMainLoop *loop = user_data;
+
+  g_main_loop_quit (loop);
+
+  return FALSE;
+}
+
+static void
+test_unix_file_poll (void)
+{
+  gint fd;
+  GSource *source;
+  GMainLoop *loop;
+
+  fd = open ("/dev/null", O_RDONLY);
+  g_assert (fd >= 0);
+
+  loop = g_main_loop_new (NULL, FALSE);
+
+  source = g_unix_fd_source_new (fd, G_IO_IN);
+  g_source_set_callback (source, (GSourceFunc) unixfd_quit_loop, loop, NULL);
+  g_source_attach (source, NULL);
+
+  /* Should not block */
+  g_main_loop_run (loop);
+
+  g_source_destroy (source);
+
+  assert_main_context_state (0);
+
+  g_source_unref (source);
+
+  g_main_loop_unref (loop);
+
+  close (fd);
+}
+
 #endif
 
 static gboolean
@@ -1542,6 +1584,7 @@ main (int argc, char *argv[])
   g_test_add_func ("/mainloop/unix-fd-source", test_unix_fd_source);
   g_test_add_func ("/mainloop/source-unix-fd-api", test_source_unix_fd_api);
   g_test_add_func ("/mainloop/wait", test_mainloop_wait);
+  g_test_add_func ("/mainloop/unix-file-poll", test_unix_file_poll);
 #endif
 
   return g_test_run ();