[libvirt-glib 2/3] glib: Add unit test for libvirt/glib mainloop integration

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

 



It's currently only testing removal of disabled timer/watches
---
 tests/Makefile.am   |   6 +-
 tests/test-events.c | 172 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 176 insertions(+), 2 deletions(-)
 create mode 100644 tests/test-events.c

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 3ad0819..56887ce 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -4,13 +4,15 @@ AM_CFLAGS = \
 	$(COVERAGE_CFLAGS) \
 	-I$(top_srcdir) \
 	$(GOBJECT2_CFLAGS) \
+	$(LIBVIRT_CFLAGS) \
 	$(LIBXML2_CFLAGS) \
 	$(WARN_CFLAGS)
 
 LDADD = \
-	$(top_builddir)/libvirt-gconfig/libvirt-gconfig-1.0.la
+	$(top_builddir)/libvirt-gconfig/libvirt-gconfig-1.0.la \
+	$(top_builddir)/libvirt-glib/libvirt-glib-1.0.la
 
-test_programs = test-gconfig
+test_programs = test-gconfig test-events
 
 dist_test_data = \
 	xml/gconfig-domain.xml \
diff --git a/tests/test-events.c b/tests/test-events.c
new file mode 100644
index 0000000..ce80719
--- /dev/null
+++ b/tests/test-events.c
@@ -0,0 +1,172 @@
+/*
+ * test-events.c: unit tests for libvirt/glib mainloop integration
+ *
+ * Copyright (C) 2014 Red Hat, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * The Software is provided "as is", without warranty of any kind, express
+ * or implied, including but not limited to the warranties of
+ * merchantability, fitness for a particular purpose and noninfringement.
+ * In no event shall the authors or copyright holders be liable for any
+ * claim, damages or other liability, whether in an action of contract,
+ * tort or otherwise, arising from, out of or in connection with the
+ * software or the use or other dealings in the Software.
+ *
+ * Author: Christophe Fergeau <cfergeau@xxxxxxxxxx>
+ */
+#include <libvirt-glib/libvirt-glib.h>
+
+#include <libvirt/libvirt.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+static GMainLoop *main_loop;
+static int watch_fd;
+static int watch_id;
+static int timeout_id;
+
+static void watch_destroyed(void *opaque)
+{
+    gint *watch = opaque;
+
+    g_assert_cmpint(*watch, >=, 0);
+    g_assert_cmpint(*watch, ==, watch_id);
+    g_message("destroyed watch %d", *watch);
+    *watch = -1;
+}
+
+
+static gboolean check_destroyed(gpointer user_data)
+{
+    gint *id = user_data;
+
+    g_assert_cmpint(*id, ==, -1);
+
+    return G_SOURCE_REMOVE;
+}
+
+
+static void watch_cb(int watch, int fd, int events, void *opaque)
+{
+    g_assert_cmpint(watch_id, !=, -1);
+    g_assert_cmpint(watch, ==, watch_id);
+    g_assert_cmpint(fd, ==, watch_fd);
+
+    g_message("got event(s) %x on fd %d (watch %d)", events, fd, watch);
+}
+
+
+static gboolean test_watch(gpointer user_data)
+{
+    int removal_status;
+
+    watch_id = virEventAddHandle(watch_fd,
+                                 VIR_EVENT_HANDLE_READABLE | VIR_EVENT_HANDLE_WRITABLE,
+                                 watch_cb, &watch_id, watch_destroyed);
+    virEventUpdateHandle(watch_id, 0);
+    removal_status = virEventRemoveHandle(watch_id);
+    g_assert_cmpint(removal_status, ==, 0);
+    virEventUpdateHandle(watch_id, VIR_EVENT_HANDLE_READABLE);
+    removal_status = virEventRemoveHandle(watch_id);
+    g_assert_cmpint(removal_status, ==, -1);
+    g_idle_add_full(G_PRIORITY_LOW, check_destroyed, &watch_id, NULL);
+    g_idle_add_full(G_PRIORITY_LOW,
+                    (GSourceFunc)g_main_loop_quit,
+                    main_loop,
+                    NULL);
+
+    return G_SOURCE_REMOVE;
+}
+
+
+static void test_remove_disabled_watch(void)
+{
+    main_loop = g_main_loop_new(NULL, FALSE);
+    watch_fd = open("/bin/true", O_RDONLY);
+    g_idle_add(test_watch, NULL);
+    g_main_loop_run(main_loop);
+    g_main_loop_unref(main_loop);
+    close(watch_fd);
+    watch_fd = -1;
+    g_assert_cmpint(watch_id, ==, -1);
+}
+
+
+static void timeout_destroyed(void *opaque)
+{
+    gint *timeout = opaque;
+
+    g_assert_cmpint(*timeout, !=, -1);
+    g_assert_cmpint(*timeout, ==, timeout_id);
+    g_message("destroyed timeout %d", *timeout);
+    *timeout = -1;
+}
+
+
+static void timeout_cb(int timer, void *opaque)
+{
+    g_assert_cmpint(timeout_id, !=, -1);
+    g_assert_cmpint(timer, ==, timeout_id);
+
+    g_message("timer %d fired", timer);
+}
+
+
+static gboolean test_timeout(gpointer user_data)
+{
+    int removal_status;
+
+    timeout_id = virEventAddTimeout(10000, timeout_cb, &timeout_id, timeout_destroyed);
+    virEventUpdateTimeout(timeout_id, -1);
+    removal_status = virEventRemoveTimeout(timeout_id);
+    g_assert_cmpint(removal_status, ==, 0);
+    virEventUpdateTimeout(timeout_id, 20000);
+    removal_status = virEventRemoveTimeout(timeout_id);
+    g_assert_cmpint(removal_status, ==, -1);
+
+    g_idle_add_full(G_PRIORITY_LOW, check_destroyed, &timeout_id, NULL);
+    g_idle_add_full(G_PRIORITY_LOW,
+                    (GSourceFunc)g_main_loop_quit,
+                    main_loop,
+                    NULL);
+
+    return G_SOURCE_REMOVE;
+}
+
+
+static void test_remove_disabled_timeout(void)
+{
+    main_loop = g_main_loop_new(NULL, FALSE);
+    g_idle_add(test_timeout, NULL);
+    g_main_loop_run(main_loop);
+    g_main_loop_unref(main_loop);
+    g_assert_cmpint(timeout_id, ==, -1);
+}
+
+
+int main(int argc, char **argv)
+{
+
+    gvir_init(&argc, &argv);
+    g_test_init(&argc, &argv, NULL);
+    gvir_event_register();
+
+    g_test_add_func("/libvirt-glib/remove-disabled-watch",
+                    test_remove_disabled_watch);
+    g_test_add_func("/libvirt-glib/remove-disabled-timeout",
+                    test_remove_disabled_timeout);
+
+    return g_test_run();
+}
-- 
1.8.5.3

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]