[tracker/async-queries] Added unit test for TrackerDBInterfacePool.



commit 8f6655178bd15b450a995ec615846ecd468fefe0
Author: Carlos Garnacho <carlosg gnome org>
Date:   Wed Apr 14 14:28:00 2010 +0200

    Added unit test for TrackerDBInterfacePool.

 tests/libtracker-db/Makefile.am                    |   14 ++
 .../libtracker-db/tracker-db-interface-pool-test.c |  237 ++++++++++++++++++++
 2 files changed, 251 insertions(+), 0 deletions(-)
---
diff --git a/tests/libtracker-db/Makefile.am b/tests/libtracker-db/Makefile.am
index ca3cd55..afd1de1 100644
--- a/tests/libtracker-db/Makefile.am
+++ b/tests/libtracker-db/Makefile.am
@@ -31,6 +31,7 @@ INCLUDES = 								\
 	$(SQLITE3_CFLAGS)
 
 TEST_PROGS +=								\
+	tracker-db-interface-pool					\
 	tracker-db-journal						\
 	tracker-db-dbus
 
@@ -66,6 +67,19 @@ tracker_db_dbus_LDADD =						\
 	$(GLIB2_LIBS)							\
 	-lz
 
+tracker_db_interface_pool_SOURCES =					\
+	tracker-db-interface-pool-test.c
+
+tracker_db_interface_pool_LDADD =					\
+	$(top_builddir)/src/libtracker-common/libtracker-common.la 	\
+	$(top_builddir)/src/libtracker-db/libtracker-db.la 		\
+	$(top_builddir)/tests/common/libtracker-testcommon.la 		\
+	$(SQLITE3_LIBS)							\
+	$(GMODULE_LIBS)							\
+	$(GTHREAD_LIBS)							\
+	$(GLIB2_LIBS)							\
+	-lz
+
 #
 # tracker_db_manager_attach_SOURCES = 					\
 # 	tracker-db-manager-test-attach.c 				\
diff --git a/tests/libtracker-db/tracker-db-interface-pool-test.c b/tests/libtracker-db/tracker-db-interface-pool-test.c
new file mode 100644
index 0000000..92a77fb
--- /dev/null
+++ b/tests/libtracker-db/tracker-db-interface-pool-test.c
@@ -0,0 +1,237 @@
+/*
+ * Copyright (C) 2008, Nokia <ivan frade nokia com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+#include "config.h"
+#include <libtracker-db/tracker-db-manager.h>
+#include <unistd.h>
+
+typedef struct TestData TestData;
+
+struct TestData {
+        GMainLoop *main_loop;
+        guint n_callers;
+        guint finished : 1;
+};
+
+static TrackerDBInterfacePool *pool = NULL;
+
+static TestData *
+test_data_inc (TestData *data)
+{
+        data->n_callers++;
+        return data;
+}
+
+static void
+test_data_dec (TestData *data)
+{
+        data->n_callers--;
+
+        if (data->n_callers == 0) {
+                g_main_loop_quit (data->main_loop);
+        }
+}
+
+static void
+query_async_cb (GObject      *object,
+                GAsyncResult *res,
+                gpointer      user_data)
+{
+        TrackerDBResultSet *result_set;
+        TestData *data = user_data;
+        GError *error = NULL;
+
+        result_set = tracker_db_interface_pool_execute_query_finish (TRACKER_DB_INTERFACE_POOL (object),
+                                                                     res, &error);
+
+        g_assert_no_error (error);
+
+        g_assert (result_set != NULL);
+
+        g_print ("Result rows x cols: %d x %d\n",
+                 tracker_db_result_set_get_n_rows (result_set),
+                 tracker_db_result_set_get_n_columns (result_set));
+
+        g_object_unref (result_set);
+
+        test_data_dec (data);
+}
+
+static void
+query_async_finish_cb (GObject      *object,
+                       GAsyncResult *res,
+                       gpointer      user_data)
+{
+        TrackerDBResultSet *result_set;
+        TestData *data = user_data;
+        GError *error = NULL;
+
+        result_set = tracker_db_interface_pool_execute_query_finish (TRACKER_DB_INTERFACE_POOL (object),
+                                                                     res, &error);
+
+        g_assert_no_error (error);
+
+        g_assert (result_set != NULL);
+
+        g_print ("Result rows x cols: %d x %d\n",
+                 tracker_db_result_set_get_n_rows (result_set),
+                 tracker_db_result_set_get_n_columns (result_set));
+
+        g_object_unref (result_set);
+
+        data->finished = TRUE;
+        test_data_dec (data);
+}
+
+static void
+query_async_cancelled_cb (GObject      *object,
+                          GAsyncResult *res,
+                          gpointer      user_data)
+{
+        TrackerDBResultSet *result_set;
+        TestData *data = user_data;
+        GError *error = NULL;
+
+        result_set = tracker_db_interface_pool_execute_query_finish (TRACKER_DB_INTERFACE_POOL (object),
+                                                                     res, &error);
+
+        g_assert_error (error, g_io_error_quark (), G_IO_ERROR_CANCELLED);
+        g_error_free (error);
+
+        test_data_dec (data);
+}
+
+static void
+test_interface_pool_query (void)
+{
+        TestData data = { 0 };
+
+        data.main_loop = g_main_loop_new (NULL, FALSE);
+
+        tracker_db_interface_pool_execute_query_async (pool,
+                                                       10,
+                                                       "SELECT * FROM Resource",
+                                                       NULL,
+                                                       NULL,
+                                                       query_async_cb,
+                                                       test_data_inc (&data));
+
+        g_main_loop_run (data.main_loop);
+
+        g_main_loop_unref (data.main_loop);
+}
+
+static gboolean
+small_query_async_cb (gpointer user_data)
+{
+        TestData *data = user_data;
+
+        if (data->finished) {
+                /* Long query has already finished */
+                return FALSE;
+        }
+
+        tracker_db_interface_pool_execute_query_async (pool,
+                                                       10,
+                                                       "SELECT * FROM Options",
+                                                       NULL,
+                                                       NULL,
+                                                       query_async_cb,
+                                                       test_data_inc (data));
+        return TRUE;
+}
+
+static void
+test_interface_pool_parallel_query (void)
+{
+        TestData data = { 0 };
+        guint timeout_id;
+
+        data.main_loop = g_main_loop_new (NULL, FALSE);
+
+        tracker_db_interface_pool_execute_query_async (pool,
+                                                       10,
+                                                       "SELECT * FROM Resource as A, Resource as B LIMIT 1000000",
+                                                       NULL,
+                                                       NULL,
+                                                       query_async_finish_cb,
+                                                       test_data_inc (&data));
+
+        timeout_id = g_timeout_add (500, small_query_async_cb, &data);
+
+        g_main_loop_run (data.main_loop);
+
+        g_source_remove (timeout_id);
+        g_main_loop_unref (data.main_loop);
+}
+
+static void
+test_interface_pool_query_cancellation (void)
+{
+        TestData data = { 0 };
+        GCancellable *cancellable;
+
+        data.main_loop = g_main_loop_new (NULL, FALSE);
+        cancellable = g_cancellable_new ();
+
+        tracker_db_interface_pool_execute_query_async (pool,
+                                                       10,
+                                                       "SELECT * FROM Resource as A, Resource as B",
+                                                       NULL,
+                                                       cancellable,
+                                                       query_async_cancelled_cb,
+                                                       test_data_inc (&data));
+
+        sleep (1);
+        g_cancellable_cancel (cancellable);
+
+        g_main_loop_run (data.main_loop);
+
+        g_main_loop_unref (data.main_loop);
+        g_object_unref (cancellable);
+}
+
+int
+main (int argc, char **argv) {
+	int result;
+	gboolean first_time;
+
+	g_type_init ();
+	g_thread_init (NULL);
+	g_test_init (&argc, &argv, NULL);
+
+	/* Init */
+	tracker_db_manager_init (0, &first_time, FALSE);
+
+        pool = tracker_db_manager_get_interface_pool ();
+
+	g_test_add_func ("/libtracker-db/tracker-db-interface-pool/query",
+	                 test_interface_pool_query);
+	g_test_add_func ("/libtracker-db/tracker-db-interface-pool/parallel_query",
+	                 test_interface_pool_parallel_query);
+	g_test_add_func ("/libtracker-db/tracker-db-interface-pool/query_cancellation",
+	                 test_interface_pool_query_cancellation);
+
+	result = g_test_run ();
+
+	/* End */
+	tracker_db_manager_shutdown ();
+
+	return result;
+}



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