summaryrefslogtreecommitdiff
path: root/tests/functional-tests/test_ipc/test-bus-query-cancellation.c
blob: 65997b06c78e8bbf6ad8ab597ab39febf9c6bd45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * Copyright (C) 2014, Red Hat, Inc.
 *
 * 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 <locale.h>

#include <gio/gio.h>

#include <libtracker-sparql/tracker-sparql.h>

#define MAX_TRIES 100

static int counter = 0;
static gboolean started = FALSE;

TrackerSparqlConnection *
create_local_connection (GError **error)
{
        TrackerSparqlConnection *conn;
        GFile *store, *ontology;
        gchar *path;

        path = g_build_filename (g_get_tmp_dir (), "libtracker-sparql-test-XXXXXX", NULL);
        g_mkdtemp_full (path, 0700);
        store = g_file_new_for_path (path);
        g_free (path);

        ontology = g_file_new_for_path (TEST_ONTOLOGIES_DIR);

        conn = tracker_sparql_connection_new (0, store, ontology, NULL, error);
        g_object_unref (store);
        g_object_unref (ontology);

        return conn;
}

static gpointer
thread_func (gpointer user_data)
{
	GDBusConnection *dbus_conn = user_data;
	TrackerSparqlConnection *direct;
	TrackerEndpointDBus *endpoint;
	GMainContext *context;
	GMainLoop *main_loop;

	context = g_main_context_new ();
	g_main_context_push_thread_default (context);

	main_loop = g_main_loop_new (context, FALSE);

	direct = create_local_connection (NULL);
	if (!direct)
		return NULL;

	endpoint = tracker_endpoint_dbus_new (direct, dbus_conn, NULL, NULL, NULL);
	if (!endpoint)
		return NULL;

	started = TRUE;
	g_main_loop_run (main_loop);

	return NULL;
}

static TrackerSparqlConnection *
create_dbus_connection (GError **error)
{
	TrackerSparqlConnection *dbus;
	GDBusConnection *dbus_conn;

	dbus_conn = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);
	if (!dbus_conn)
		return NULL;

	g_thread_new (NULL, thread_func, dbus_conn);

	while (!started)
		g_usleep (100);

	dbus = tracker_sparql_connection_bus_new (g_dbus_connection_get_unique_name (dbus_conn),
						  NULL, dbus_conn, error);
	return dbus;
}

static void
query_cb (GObject *source_object, GAsyncResult *res, gpointer user_data)
{
	GCancellable *cancellable;
	GError *error = NULL;
	TrackerSparqlConnection *conn = TRACKER_SPARQL_CONNECTION (source_object);
	TrackerSparqlCursor *cursor = NULL;

	cursor = tracker_sparql_connection_query_finish (conn, res, &error);
	if (error != NULL) {
		g_error_free (error);
	}

	counter++;
	if (counter > MAX_TRIES) {
		g_main_loop_quit (user_data);
		return;
	}

	cancellable = g_cancellable_new ();
	tracker_sparql_connection_query_async (conn,
	                                       "SELECT ?urn WHERE {?urn a rdfs:Resource}",
	                                       cancellable,
	                                       query_cb, user_data);
	g_cancellable_cancel (cancellable);

	g_object_unref (cancellable);
	g_clear_object (&cursor);
}

static gboolean
quit_cb (gpointer user_data)
{
	g_main_loop_quit ((GMainLoop *) user_data);
	return G_SOURCE_REMOVE;
}

static void
test_tracker_sparql_gb737023 (void)
{
	GCancellable *cancellable;
	GError *error = NULL;
	GMainLoop *loop;
	TrackerSparqlConnection *conn;

	g_test_bug_base ("https://bugzilla.gnome.org/show_bug.cgi?id=");
	g_test_bug ("737023");

	conn = create_dbus_connection (&error);
        g_assert_no_error (error);

	loop = g_main_loop_new (NULL, FALSE);

	/* This should be enough. */
	g_timeout_add_seconds_full (G_PRIORITY_DEFAULT,
	                            2,
	                            quit_cb,
	                            loop,
	                            (GDestroyNotify) g_main_loop_unref);

	cancellable = g_cancellable_new ();
	tracker_sparql_connection_query_async (conn,
	                                       "SELECT ?urn WHERE {?urn a rdfs:Resource}",
	                                       cancellable,
	                                       query_cb, loop);
	g_cancellable_cancel (cancellable);
	g_main_loop_run (loop);

	g_object_unref (cancellable);
	g_object_unref (conn);
}

gint
main (gint argc, gchar **argv)
{
	gint result;

	setlocale (LC_ALL, "");

	g_test_init (&argc, &argv, NULL);

	g_test_add_func ("/libtracker-sparql/tracker/gb737023",
	                 test_tracker_sparql_gb737023);

	result = g_test_run ();

	return result;
}