[18316] | 1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
---|
| 2 | |
---|
| 3 | /* test-queue.c - Test program for the GNOME Virtual File System. |
---|
| 4 | |
---|
| 5 | Copyright (C) 2001 Free Software Foundation |
---|
| 6 | |
---|
| 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. |
---|
| 11 | |
---|
| 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. |
---|
| 16 | |
---|
| 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. |
---|
| 21 | |
---|
| 22 | Author: László Péter <laca@ireland.sun.com> |
---|
| 23 | */ |
---|
| 24 | |
---|
| 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 |
---|
| 28 | #undef QUEUE_DEBUG |
---|
| 29 | to |
---|
| 30 | #define QUEUE_DEBUG 1 |
---|
| 31 | in libgnomevfs/gnome-vfs-job-queue.c and rebuild gnome-vfs. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #include <config.h> |
---|
| 35 | |
---|
| 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> |
---|
| 42 | #include <fcntl.h> |
---|
| 43 | #include <signal.h> |
---|
| 44 | #include <stdio.h> |
---|
| 45 | #include <stdlib.h> |
---|
| 46 | #include <unistd.h> |
---|
| 47 | #include <string.h> |
---|
| 48 | |
---|
| 49 | #define TEST_LIMIT 3 |
---|
| 50 | |
---|
| 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; |
---|
| 56 | static int test_no; |
---|
| 57 | static gboolean verbose = FALSE; |
---|
| 58 | |
---|
| 59 | static gboolean at_least_one_test_failed = FALSE; |
---|
| 60 | |
---|
| 61 | static int prio_db[100]; |
---|
| 62 | |
---|
| 63 | static void |
---|
| 64 | my_yield (int count) |
---|
| 65 | { |
---|
| 66 | for (; count > 0; count--) { |
---|
| 67 | if (!g_main_context_iteration (NULL, FALSE)) { |
---|
| 68 | break; |
---|
| 69 | } |
---|
| 70 | usleep (10); |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | static gboolean |
---|
| 75 | wait_until_vfs_jobs_gone (void) |
---|
| 76 | { |
---|
| 77 | int i; |
---|
| 78 | |
---|
| 79 | if (gnome_vfs_job_get_count () == 0) { |
---|
| 80 | return TRUE; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | for (i = 0; i < 2000; i++) { |
---|
| 84 | usleep (1000); |
---|
| 85 | g_main_context_iteration (NULL, FALSE); |
---|
| 86 | if (gnome_vfs_job_get_count () == 0) { |
---|
| 87 | return TRUE; |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | return FALSE; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | static void |
---|
| 94 | get_file_info_callback (GnomeVFSAsyncHandle *handle, |
---|
| 95 | GList *results, |
---|
| 96 | gpointer callback_data) |
---|
| 97 | { |
---|
| 98 | int this_test_no = *(int *)callback_data; |
---|
| 99 | jobs_finished ++; |
---|
| 100 | if (prio_db[this_test_no] == 7) { |
---|
| 101 | finished_7 = jobs_finished; |
---|
| 102 | } |
---|
| 103 | if (prio_db[this_test_no] == 0) { |
---|
| 104 | finished_0 = jobs_finished; |
---|
| 105 | } |
---|
| 106 | if (verbose) { |
---|
| 107 | printf ("finished test #%d: get_file_info, priority = %d\n", this_test_no, prio_db[this_test_no]); |
---|
| 108 | } |
---|
| 109 | g_free (callback_data); |
---|
| 110 | jobs_started --; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | static gboolean |
---|
| 114 | test_get_file_info (const char *uri, int priority) |
---|
| 115 | { |
---|
| 116 | GList *uri_list; |
---|
| 117 | |
---|
| 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; |
---|
| 122 | |
---|
| 123 | uri_list = g_list_prepend (NULL, gnome_vfs_uri_new (uri)); |
---|
| 124 | |
---|
| 125 | jobs_started ++; |
---|
| 126 | |
---|
| 127 | if (verbose) { |
---|
| 128 | printf ("starting test #%d: get_file_info, priority = %d\n", test_no, priority); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | test_handle = NULL; |
---|
| 132 | gnome_vfs_async_get_file_info (&test_handle, |
---|
| 133 | uri_list, |
---|
| 134 | GNOME_VFS_FILE_INFO_DEFAULT, |
---|
| 135 | priority, |
---|
| 136 | get_file_info_callback, |
---|
| 137 | test_callback_data); |
---|
| 138 | if (test_handle == NULL) { |
---|
| 139 | jobs_started --; |
---|
| 140 | return FALSE; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | g_list_free (uri_list); |
---|
| 144 | my_yield (20); |
---|
| 145 | |
---|
| 146 | return TRUE; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | static void |
---|
| 150 | test_find_directory_callback (GnomeVFSAsyncHandle *handle, |
---|
| 151 | GList *results, |
---|
| 152 | gpointer callback_data) |
---|
| 153 | { |
---|
| 154 | int this_test_no = *(int *)callback_data; |
---|
| 155 | jobs_finished++; |
---|
| 156 | if (verbose) { |
---|
| 157 | printf ("finished test #%d: find_directory, priority = %d\n", this_test_no, prio_db[this_test_no]); |
---|
| 158 | } |
---|
| 159 | fflush (stdout); |
---|
| 160 | g_free (callback_data); |
---|
| 161 | jobs_started --; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | static void |
---|
| 165 | test_find_directory (const char *uri, int priority) |
---|
| 166 | { |
---|
| 167 | GnomeVFSAsyncHandle *handle; |
---|
| 168 | GList *vfs_uri_as_list; |
---|
| 169 | |
---|
| 170 | test_callback_data = g_malloc (sizeof (int)); |
---|
| 171 | *(int *)test_callback_data = ++test_no; |
---|
| 172 | prio_db[test_no] = priority; |
---|
| 173 | |
---|
| 174 | vfs_uri_as_list = g_list_append (NULL, gnome_vfs_uri_new (uri)); |
---|
| 175 | jobs_started++; |
---|
| 176 | |
---|
| 177 | if (verbose) { |
---|
| 178 | printf ("starting test #%d: find_directory, priority = %d\n", test_no, priority); |
---|
| 179 | } |
---|
| 180 | |
---|
| 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); |
---|
| 184 | my_yield (20); |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | |
---|
| 188 | static int usage (void) |
---|
| 189 | { |
---|
| 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"); |
---|
| 193 | exit (1); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | int |
---|
| 197 | main (int argc, char **argv) |
---|
| 198 | { |
---|
| 199 | int limit; |
---|
| 200 | int finished_before_7; |
---|
| 201 | int finished_before_0; |
---|
| 202 | int i; |
---|
| 203 | |
---|
| 204 | for (i = 1; i < argc; i++) { |
---|
| 205 | if (!strcmp (argv[i], "-h") || |
---|
| 206 | !strcmp (argv[i], "--help")) { |
---|
| 207 | usage (); |
---|
| 208 | continue; |
---|
| 209 | } |
---|
| 210 | if (!strcmp (argv[i], "-v") || |
---|
| 211 | !strcmp (argv[i], "--verbose")) { |
---|
| 212 | verbose = TRUE; |
---|
| 213 | continue; |
---|
| 214 | } |
---|
| 215 | printf ("Unrecognized option: %s\n\n", argv[i]); |
---|
| 216 | usage (); |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | gnome_vfs_init (); |
---|
| 220 | |
---|
| 221 | limit = gnome_vfs_async_get_job_limit (); |
---|
| 222 | if (verbose) { |
---|
| 223 | printf ("Current job limit: %d\n", limit); |
---|
| 224 | printf ("Trying to set the limit to 1; should result in a warning\n"); |
---|
| 225 | } else { |
---|
| 226 | printf ("You should see a warning and 2 critical errors below.\n"); |
---|
| 227 | } |
---|
| 228 | |
---|
| 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 ()); |
---|
| 234 | } |
---|
| 235 | |
---|
| 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; |
---|
| 239 | } |
---|
| 240 | |
---|
| 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; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | if (!verbose) { |
---|
| 247 | printf ("No warning or error messages are expected beyond this point\n"); |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | if (verbose) { |
---|
| 251 | printf ("Setting job limit to %d\n", TEST_LIMIT); |
---|
| 252 | } |
---|
| 253 | |
---|
| 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); |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | for (i = 0; i < 10; i++) { |
---|
| 262 | test_find_directory ("test:///usr", 8); |
---|
| 263 | } |
---|
| 264 | |
---|
| 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); |
---|
| 269 | |
---|
| 270 | wait_until_vfs_jobs_gone (); |
---|
| 271 | |
---|
| 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); |
---|
| 289 | |
---|
| 290 | wait_until_vfs_jobs_gone (); |
---|
| 291 | |
---|
| 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; |
---|
| 295 | } |
---|
| 296 | |
---|
| 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, |
---|
| 303 | TEST_LIMIT); |
---|
| 304 | at_least_one_test_failed = TRUE; |
---|
| 305 | } |
---|
| 306 | |
---|
| 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, |
---|
| 312 | TEST_LIMIT); |
---|
| 313 | at_least_one_test_failed = TRUE; |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | if (verbose) { |
---|
| 317 | printf ("Shutting down\n"); |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | gnome_vfs_shutdown (); |
---|
| 321 | |
---|
| 322 | /* Report to "make check" on whether it all worked or not. */ |
---|
| 323 | return at_least_one_test_failed ? EXIT_FAILURE : EXIT_SUCCESS; |
---|
| 324 | } |
---|