1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
3 /* test-queue.c - Test program for the GNOME Virtual File System.
5 Copyright (C) 2001 Free Software Foundation
7 The Gnome Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The Gnome Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the Gnome Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Author: László Péter <laca@ireland.sun.com>
25 /* This is a simple test program for the job queue.
26 It tests some basic features but to see what's really going on on the
27 inside you should change
31 in libgnomevfs/gnome-vfs-job-queue.c and rebuild gnome-vfs.
36 #include <glib/glist.h>
37 #include <glib/gmain.h>
38 #include <libgnomevfs/gnome-vfs-async-ops.h>
39 #include <libgnomevfs/gnome-vfs-job-limit.h>
40 #include <libgnomevfs/gnome-vfs-init.h>
41 #include <libgnomevfs/gnome-vfs-job.h>
51 static GnomeVFSAsyncHandle *test_handle;
52 static gpointer test_callback_data;
53 static int jobs_started;
54 static int jobs_finished;
55 static int finished_7, finished_0;
57 static gboolean verbose = FALSE;
59 static gboolean at_least_one_test_failed = FALSE;
61 static int prio_db[100];
66 for (; count > 0; count--) {
67 if (!g_main_context_iteration (NULL, FALSE)) {
75 wait_until_vfs_jobs_gone (void)
79 if (gnome_vfs_job_get_count () == 0) {
83 for (i = 0; i < 2000; i++) {
85 g_main_context_iteration (NULL, FALSE);
86 if (gnome_vfs_job_get_count () == 0) {
94 get_file_info_callback (GnomeVFSAsyncHandle *handle,
96 gpointer callback_data)
98 int this_test_no = *(int *)callback_data;
100 if (prio_db[this_test_no] == 7) {
101 finished_7 = jobs_finished;
103 if (prio_db[this_test_no] == 0) {
104 finished_0 = jobs_finished;
107 printf ("finished test #%d: get_file_info, priority = %d\n", this_test_no, prio_db[this_test_no]);
109 g_free (callback_data);
114 test_get_file_info (const char *uri, int priority)
118 /* Start a get_file_info call. */
119 test_callback_data = g_malloc (sizeof (int));
120 *(int *)test_callback_data = ++test_no;
121 prio_db[test_no] = priority;
123 uri_list = g_list_prepend (NULL, gnome_vfs_uri_new (uri));
128 printf ("starting test #%d: get_file_info, priority = %d\n", test_no, priority);
132 gnome_vfs_async_get_file_info (&test_handle,
134 GNOME_VFS_FILE_INFO_DEFAULT,
136 get_file_info_callback,
138 if (test_handle == NULL) {
143 g_list_free (uri_list);
150 test_find_directory_callback (GnomeVFSAsyncHandle *handle,
152 gpointer callback_data)
154 int this_test_no = *(int *)callback_data;
157 printf ("finished test #%d: find_directory, priority = %d\n", this_test_no, prio_db[this_test_no]);
160 g_free (callback_data);
165 test_find_directory (const char *uri, int priority)
167 GnomeVFSAsyncHandle *handle;
168 GList *vfs_uri_as_list;
170 test_callback_data = g_malloc (sizeof (int));
171 *(int *)test_callback_data = ++test_no;
172 prio_db[test_no] = priority;
174 vfs_uri_as_list = g_list_append (NULL, gnome_vfs_uri_new (uri));
178 printf ("starting test #%d: find_directory, priority = %d\n", test_no, priority);
181 gnome_vfs_async_find_directory (&handle, vfs_uri_as_list,
182 GNOME_VFS_DIRECTORY_KIND_TRASH, FALSE, TRUE, 0777, priority,
183 test_find_directory_callback, test_callback_data);
188 static int usage (void)
190 printf ("Usage: test-queue [-h|--help] [-v|--verbose]\n");
191 printf ("\t-h, --help display usage information\n");
192 printf ("\t-v, --verbose verbose mode\n\n");
197 main (int argc, char **argv)
200 int finished_before_7;
201 int finished_before_0;
204 for (i = 1; i < argc; i++) {
205 if (!strcmp (argv[i], "-h") ||
206 !strcmp (argv[i], "--help")) {
210 if (!strcmp (argv[i], "-v") ||
211 !strcmp (argv[i], "--verbose")) {
215 printf ("Unrecognized option: %s\n\n", argv[i]);
221 limit = gnome_vfs_async_get_job_limit ();
223 printf ("Current job limit: %d\n", limit);
224 printf ("Trying to set the limit to 1; should result in a warning\n");
226 printf ("You should see a warning and 2 critical errors below.\n");
229 gnome_vfs_async_set_job_limit (1);
230 if (limit != gnome_vfs_async_get_job_limit ()) {
231 at_least_one_test_failed = TRUE;
232 g_warning ("Current job limit changed to %d\n",
233 gnome_vfs_async_get_job_limit ());
236 printf ("Trying to start a job with priority = -11; should result in a critical error\n");
237 if (test_get_file_info ("file:///dev/null", -11)) {
238 at_least_one_test_failed = TRUE;
241 printf ("Trying to start a job with priority = 11; should result in a critical error\n");
242 if (test_get_file_info ("file:///dev/null", 11)) {
243 at_least_one_test_failed = TRUE;
247 printf ("No warning or error messages are expected beyond this point\n");
251 printf ("Setting job limit to %d\n", TEST_LIMIT);
254 gnome_vfs_async_set_job_limit (TEST_LIMIT);
255 limit = gnome_vfs_async_get_job_limit ();
256 if (limit != TEST_LIMIT) {
257 at_least_one_test_failed = TRUE;
258 g_warning ("Limit is %d, not %d as expected", limit, TEST_LIMIT);
261 for (i = 0; i < 10; i++) {
262 test_find_directory ("test:///usr", 8);
265 finished_before_7 = jobs_finished;
266 test_find_directory ("test:///usr", 7);
267 finished_before_0 = jobs_finished;
268 test_get_file_info ("test:///etc/passwd", 0);
270 wait_until_vfs_jobs_gone ();
272 /* Do some random tests with different priorities */
273 test_find_directory ("file:///", 10);
274 test_find_directory ("file:///usr", 6);
275 test_find_directory ("file:///usr/local", -1);
276 test_find_directory ("file:///home", 2);
277 test_find_directory ("file:///etc", 5);
278 test_find_directory ("file:///tmp", 5);
279 test_find_directory ("file:///opt", 9);
280 test_find_directory ("file:///var", 10);
281 test_find_directory ("file:///", 10);
282 test_find_directory ("file:///home", 10);
283 test_get_file_info ("file:///dev/null", -1);
284 test_get_file_info ("file:///etc/passwd", -5);
285 test_find_directory ("file:///", -8);
286 test_find_directory ("file:///tmp", -10);
287 test_find_directory ("file:///etc", -5);
288 test_find_directory ("file:///usr", -1);
290 wait_until_vfs_jobs_gone ();
292 if (jobs_started != 0) {
293 printf ("%d jobs appear to be still running.\n", jobs_started - 2);
294 at_least_one_test_failed = TRUE;
297 /* at most TEST_LIMIT low priority jobs + the 0 priority job may have
298 finished before this one */
299 if ((finished_7 > 0) && (finished_7 - finished_before_7 > TEST_LIMIT + 1)) {
300 printf ("Scheduling error: %d lower priority jobs finished before "
301 "the 7 priority job when the job limit is %d\n",
302 finished_7 - finished_before_7,
304 at_least_one_test_failed = TRUE;
307 /* at most TEST_LIMIT low priority jobs may have finished before this one */
308 if ((finished_0 > 0) && (finished_0 - finished_before_0 > TEST_LIMIT + 1)) {
309 printf ("Scheduling error: %d lower priority jobs finished before "
310 "the 0 priority job when the job limit is %d\n",
311 finished_0 - finished_before_0,
313 at_least_one_test_failed = TRUE;
317 printf ("Shutting down\n");
320 gnome_vfs_shutdown ();
322 /* Report to "make check" on whether it all worked or not. */
323 return at_least_one_test_failed ? EXIT_FAILURE : EXIT_SUCCESS;