[tracker/tracker-1.0] libtracker-data: Improve unit tests calling g_test_add()
- From: Martyn James Russell <mr src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tracker/tracker-1.0] libtracker-data: Improve unit tests calling g_test_add()
- Date: Fri, 22 Aug 2014 11:53:38 +0000 (UTC)
commit 0a9e8127f395439cdfa9a62e138a5974af9adbed
Author: Martyn Russell <martyn lanedo com>
Date: Thu Aug 7 11:29:08 2014 +0100
libtracker-data: Improve unit tests calling g_test_add()
This includes a setup and teardown function to clean up properly.
This was needed because the new framework seems to run tests in parallel and
was randomly failing because the same data dir was being used for all tests.
tests/libtracker-data/tracker-ontology-test.c | 87 +++++++++++++++------
tests/libtracker-data/tracker-sparql-blank-test.c | 59 +++++++++++----
tests/libtracker-data/tracker-sparql-test.c | 64 ++++++++++-----
3 files changed, 150 insertions(+), 60 deletions(-)
---
diff --git a/tests/libtracker-data/tracker-ontology-test.c b/tests/libtracker-data/tracker-ontology-test.c
index 5cc4ed7..614692e 100644
--- a/tests/libtracker-data/tracker-ontology-test.c
+++ b/tests/libtracker-data/tracker-ontology-test.c
@@ -37,6 +37,7 @@ typedef struct _TestInfo TestInfo;
struct _TestInfo {
const gchar *test_name;
const gchar *data;
+ gchar *xdg_location;
};
typedef struct _ChangeInfo ChangeInfo;
@@ -214,18 +215,15 @@ test_ontology_init (void)
}
static void
-test_query (gconstpointer test_data)
+test_query (TestInfo *test_info,
+ gconstpointer context)
{
- const TestInfo *test_info;
- GError *error;
+ GError *error = NULL;
gchar *data_filename;
gchar *query_filename;
gchar *results_filename;
gchar *prefix, *data_prefix, *test_prefix;
- error = NULL;
- test_info = test_data;
-
prefix = g_build_path (G_DIR_SEPARATOR_S, TOP_SRCDIR, "tests", "libtracker-data", NULL);
data_prefix = g_build_filename (prefix, test_info->data, NULL);
@@ -269,27 +267,74 @@ test_query (gconstpointer test_data)
tracker_data_manager_shutdown ();
}
+static inline void
+setup (TestInfo *info,
+ gint i)
+{
+ gchar *basename;
+ gchar *current_dir;
+
+ basename = g_strdup_printf ("%d-%d", i, g_test_rand_int_range (0, G_MAXINT));
+ current_dir = g_get_current_dir ();
+ info->xdg_location = g_build_path (G_DIR_SEPARATOR_S, current_dir, "test-data", basename, NULL);
+ g_free (current_dir);
+ g_free (basename);
+
+ g_setenv ("XDG_DATA_HOME", info->xdg_location, TRUE);
+ g_setenv ("XDG_CACHE_HOME", info->xdg_location, TRUE);
+ g_setenv ("TRACKER_DB_ONTOLOGIES_DIR", TOP_SRCDIR "/data/ontologies/", TRUE);
+}
+
+
+static void
+setup_nie (TestInfo *info,
+ gconstpointer context)
+{
+ gint i = GPOINTER_TO_INT (context);
+
+ *info = nie_tests[i];
+ setup (info, i);
+}
+
+static void
+setup_nmo (TestInfo *info,
+ gconstpointer context)
+{
+ gint i = GPOINTER_TO_INT (context);
+
+ *info = nmo_tests[i];
+ setup (info, i);
+}
+
+static void
+teardown (TestInfo *info,
+ gconstpointer context)
+{
+ gchar *cleanup_command;
+
+ /* clean up */
+ g_print ("Removing temporary data (%s)\n", info->xdg_location);
+
+ cleanup_command = g_strdup_printf ("rm -Rf %s/", info->xdg_location);
+ g_spawn_command_line_sync (cleanup_command, NULL, NULL, NULL, NULL);
+ g_free (cleanup_command);
+
+ g_free (info->xdg_location);
+}
+
int
main (int argc, char **argv)
{
gint result;
gint i;
- gchar *data_dir;
-
- g_test_init (&argc, &argv, NULL);
-
- data_dir = g_build_filename (g_get_current_dir (), "test-cache", NULL);
/* Warning warning!!! We need to impose a proper LC_COLLATE here, so
* that the expected order in the test results is always the same! */
setlocale (LC_COLLATE, "en_US.utf8");
- g_setenv ("XDG_DATA_HOME", data_dir, TRUE);
- g_setenv ("XDG_CACHE_HOME", data_dir, TRUE);
- g_setenv ("TRACKER_DB_ONTOLOGIES_DIR", TOP_SRCDIR "/data/ontologies/", TRUE);
+ g_test_init (&argc, &argv, NULL);
/* add test cases */
-
g_test_add_func ("/libtracker-data/ontology-init", test_ontology_init);
for (i = 0; nie_tests[i].test_name; i++) {
@@ -298,7 +343,7 @@ main (int argc, char **argv)
g_message ("%d", i);
testpath = g_strconcat ("/libtracker-data/nie/", nie_tests[i].test_name, NULL);
- g_test_add_data_func (testpath, &nie_tests[i], test_query);
+ g_test_add (testpath, TestInfo, GINT_TO_POINTER(i), setup_nie, test_query, teardown);
g_free (testpath);
}
@@ -308,20 +353,12 @@ main (int argc, char **argv)
g_message ("%d", i);
testpath = g_strconcat ("/libtracker-data/nmo/", nmo_tests[i].test_name, NULL);
- g_test_add_data_func (testpath, &nmo_tests[i], test_query);
+ g_test_add (testpath, TestInfo, GINT_TO_POINTER(i), setup_nmo, test_query, teardown);
g_free (testpath);
}
/* run tests */
-
result = g_test_run ();
- /* clean up */
- g_print ("Removing temporary data\n");
- g_spawn_command_line_sync ("rm -R tracker/", NULL, NULL, NULL, NULL);
- g_spawn_command_line_sync ("rm -R test-cache/", NULL, NULL, NULL, NULL);
-
- g_free (data_dir);
-
return result;
}
diff --git a/tests/libtracker-data/tracker-sparql-blank-test.c
b/tests/libtracker-data/tracker-sparql-blank-test.c
index 12c5673..f056ae7 100644
--- a/tests/libtracker-data/tracker-sparql-blank-test.c
+++ b/tests/libtracker-data/tracker-sparql-blank-test.c
@@ -20,6 +20,7 @@
#include "config.h"
#include <string.h>
+#include <locale.h>
#include <glib.h>
#include <gio/gio.h>
@@ -30,8 +31,13 @@
#include <libtracker-data/tracker-data.h>
#include <libtracker-data/tracker-sparql-query.h>
+typedef struct {
+ gchar *xdg_location;
+} TestInfo;
+
static void
-test_blank (void)
+test_blank (TestInfo *info,
+ gconstpointer context)
{
GError *error;
GVariant *updates;
@@ -119,29 +125,54 @@ test_blank (void)
tracker_data_manager_shutdown ();
}
-int
-main (int argc, char **argv)
+static void
+setup (TestInfo *info,
+ gconstpointer context)
{
- gint result;
- gchar *current_dir;
+ gchar *current_dir, *basename;
+ gint i;
- g_test_init (&argc, &argv, NULL);
+ i = GPOINTER_TO_INT (context);
+ basename = g_strdup_printf ("%d-%d", i, g_test_rand_int_range (0, G_MAXINT));
current_dir = g_get_current_dir ();
+ info->xdg_location = g_build_path (G_DIR_SEPARATOR_S, current_dir, "test-data", basename, NULL);
+ g_free (current_dir);
+ g_free (basename);
- g_setenv ("XDG_DATA_HOME", current_dir, TRUE);
- g_setenv ("XDG_CACHE_HOME", current_dir, TRUE);
+ g_setenv ("XDG_DATA_HOME", info->xdg_location, TRUE);
+ g_setenv ("XDG_CACHE_HOME", info->xdg_location, TRUE);
g_setenv ("TRACKER_DB_ONTOLOGIES_DIR", TOP_SRCDIR "/data/ontologies/", TRUE);
+}
- g_test_add_func ("/libtracker-data/sparql-blank", test_blank);
+static void
+teardown (TestInfo *info,
+ gconstpointer context)
+{
+ gchar *cleanup_command;
- /* run tests */
+ /* clean up */
+ g_print ("Removing temporary data (%s)\n", info->xdg_location);
- result = g_test_run ();
+ cleanup_command = g_strdup_printf ("rm -Rf %s/", info->xdg_location);
+ g_spawn_command_line_sync (cleanup_command, NULL, NULL, NULL, NULL);
+ g_free (cleanup_command);
- /* clean up */
- g_print ("Removing temporary data\n");
- g_spawn_command_line_sync ("rm -R tracker/", NULL, NULL, NULL, NULL);
+ g_free (info->xdg_location);
+}
+
+int
+main (int argc, char **argv)
+{
+ gint result;
+
+ setlocale (LC_COLLATE, "en_US.utf8");
+
+ g_test_init (&argc, &argv, NULL);
+ g_test_add ("/libtracker-data/sparql-blank", TestInfo, GINT_TO_POINTER(0), setup, test_blank,
teardown);
+
+ /* run tests */
+ result = g_test_run ();
return result;
}
diff --git a/tests/libtracker-data/tracker-sparql-test.c b/tests/libtracker-data/tracker-sparql-test.c
index 9cebc52..3674e0e 100644
--- a/tests/libtracker-data/tracker-sparql-test.c
+++ b/tests/libtracker-data/tracker-sparql-test.c
@@ -40,6 +40,7 @@ struct _TestInfo {
const gchar *data;
gboolean expect_query_error;
gboolean expect_update_error;
+ gchar *xdg_location;
};
const TestInfo tests[] = {
@@ -216,20 +217,17 @@ check_result (TrackerDBCursor *cursor,
}
static void
-test_sparql_query (gconstpointer test_data)
+test_sparql_query (TestInfo *test_info,
+ gconstpointer context)
{
TrackerDBCursor *cursor;
- const TestInfo *test_info;
- GError *error;
+ GError *error = NULL;
gchar *data_filename;
gchar *query, *query_filename;
gchar *results_filename;
gchar *prefix, *data_prefix, *test_prefix;
const gchar *test_schemas[2] = { NULL, NULL };
- error = NULL;
- test_info = test_data;
-
/* initialization */
prefix = g_build_path (G_DIR_SEPARATOR_S, TOP_SRCDIR, "tests", "libtracker-data", NULL);
data_prefix = g_build_filename (prefix, test_info->data, NULL);
@@ -317,24 +315,52 @@ test_sparql_query (gconstpointer test_data)
tracker_data_manager_shutdown ();
}
-int
-main (int argc, char **argv)
+static void
+setup (TestInfo *info,
+ gconstpointer context)
{
- gint result;
+ gchar *current_dir, *basename;
gint i;
- gchar *current_dir;
-
- g_test_init (&argc, &argv, NULL);
- setlocale (LC_COLLATE, "en_US.utf8");
+ i = GPOINTER_TO_INT (context);
+ *info = tests[i];
+ basename = g_strdup_printf ("%d-%d", i, g_test_rand_int_range (0, G_MAXINT));
current_dir = g_get_current_dir ();
+ info->xdg_location = g_build_path (G_DIR_SEPARATOR_S, current_dir, "test-data", basename, NULL);
+ g_free (current_dir);
+ g_free (basename);
- g_setenv ("XDG_DATA_HOME", current_dir, TRUE);
- g_setenv ("XDG_CACHE_HOME", current_dir, TRUE);
+ g_setenv ("XDG_DATA_HOME", info->xdg_location, TRUE);
+ g_setenv ("XDG_CACHE_HOME", info->xdg_location, TRUE);
g_setenv ("TRACKER_DB_ONTOLOGIES_DIR", TOP_SRCDIR "/data/ontologies/", TRUE);
+}
- g_free (current_dir);
+static void
+teardown (TestInfo *info,
+ gconstpointer context)
+{
+ gchar *cleanup_command;
+
+ /* clean up */
+ g_print ("Removing temporary data (%s)\n", info->xdg_location);
+
+ cleanup_command = g_strdup_printf ("rm -Rf %s/", info->xdg_location);
+ g_spawn_command_line_sync (cleanup_command, NULL, NULL, NULL, NULL);
+ g_free (cleanup_command);
+
+ g_free (info->xdg_location);
+}
+
+int
+main (int argc, char **argv)
+{
+ gint result;
+ gint i;
+
+ setlocale (LC_COLLATE, "en_US.utf8");
+
+ g_test_init (&argc, &argv, NULL);
/* add test cases */
for (i = 0; tests[i].test_name; i++) {
@@ -352,16 +378,12 @@ main (int argc, char **argv)
#endif
testpath = g_strconcat ("/libtracker-data/sparql/", tests[i].test_name, NULL);
- g_test_add_data_func (testpath, &tests[i], test_sparql_query);
+ g_test_add (testpath, TestInfo, GINT_TO_POINTER(i), setup, test_sparql_query, teardown);
g_free (testpath);
}
/* run tests */
result = g_test_run ();
- /* clean up */
- g_print ("Removing temporary data\n");
- g_spawn_command_line_sync ("rm -R tracker/", NULL, NULL, NULL, NULL);
-
return result;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]