[15496] | 1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
---|
| 2 | |
---|
| 3 | /* test-async-cancel.c - Test program for the GNOME Virtual File System. |
---|
| 4 | |
---|
| 5 | Copyright (C) 1999 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: Darin Adler <darin@eazel.com> |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #include <config.h> |
---|
| 26 | |
---|
| 27 | #include <glib.h> |
---|
| 28 | #include <signal.h> |
---|
| 29 | #include <stdlib.h> |
---|
| 30 | #include <stdio.h> |
---|
| 31 | #include <fcntl.h> |
---|
| 32 | #include <libgnorba/gnorba.h> |
---|
| 33 | #include <unistd.h> |
---|
| 34 | |
---|
| 35 | #include "gnome-vfs.h" |
---|
| 36 | #include "gnome-vfs-backend.h" |
---|
| 37 | |
---|
| 38 | #define TEST_ASSERT(expression, message) \ |
---|
| 39 | G_STMT_START { if (!(expression)) test_failed message; } G_STMT_END |
---|
| 40 | |
---|
| 41 | static GnomeVFSAsyncHandle *test_handle; |
---|
| 42 | static gpointer test_callback_data; |
---|
| 43 | static gboolean test_done; |
---|
| 44 | |
---|
| 45 | #define MAX_THREAD_WAIT 100 |
---|
| 46 | |
---|
| 47 | static void |
---|
| 48 | stop_after_log (const char *domain, GLogLevelFlags level, |
---|
| 49 | const char *message, gpointer data) |
---|
| 50 | { |
---|
| 51 | void (* saved_handler)(int); |
---|
| 52 | |
---|
| 53 | g_log_default_handler(domain, level, message, data); |
---|
| 54 | |
---|
| 55 | saved_handler = signal (SIGINT, SIG_IGN); |
---|
| 56 | raise(SIGINT); |
---|
| 57 | signal(SIGINT, saved_handler); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | static void |
---|
| 61 | make_asserts_break (const char *domain) |
---|
| 62 | { |
---|
[15594] | 63 | g_log_set_handler (domain, |
---|
| 64 | (GLogLevelFlags) (G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING), |
---|
[15496] | 65 | stop_after_log, NULL); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | static int |
---|
| 69 | get_free_file_descriptor_count (void) |
---|
| 70 | { |
---|
| 71 | int count; |
---|
| 72 | GList *list, *p; |
---|
| 73 | int fd; |
---|
| 74 | |
---|
| 75 | list = NULL; |
---|
| 76 | for (count = 0; ; count++) { |
---|
| 77 | fd = open ("/dev/null", O_RDONLY); |
---|
| 78 | if (fd == -1) { |
---|
| 79 | break; |
---|
| 80 | } |
---|
| 81 | list = g_list_prepend (list, GINT_TO_POINTER (fd)); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | for (p = list; p != NULL; p = p->next) { |
---|
| 85 | close (GPOINTER_TO_INT (p->data)); |
---|
| 86 | } |
---|
| 87 | g_list_free (list); |
---|
| 88 | |
---|
| 89 | return count; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | static int free_at_start; |
---|
| 93 | |
---|
| 94 | static int |
---|
| 95 | get_used_file_descriptor_count (void) |
---|
| 96 | { |
---|
| 97 | return free_at_start - get_free_file_descriptor_count (); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | static gboolean |
---|
| 101 | wait_for_boolean (gboolean *wait_for_it) |
---|
| 102 | { |
---|
| 103 | int i; |
---|
| 104 | |
---|
| 105 | if (*wait_for_it) { |
---|
| 106 | return TRUE; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | for (i = 0; i < MAX_THREAD_WAIT; i++) { |
---|
| 110 | usleep (1); |
---|
| 111 | gtk_main_iteration_do (FALSE); |
---|
| 112 | if (*wait_for_it) { |
---|
| 113 | return TRUE; |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | return FALSE; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | static gboolean |
---|
| 120 | wait_until_vfs_threads_gone (void) |
---|
| 121 | { |
---|
| 122 | int i; |
---|
| 123 | |
---|
| 124 | if (gnome_vfs_backend_get_job_count () == 0) { |
---|
| 125 | return TRUE; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | for (i = 0; i < MAX_THREAD_WAIT; i++) { |
---|
| 129 | usleep (1); |
---|
| 130 | gtk_main_iteration_do (FALSE); |
---|
| 131 | if (gnome_vfs_backend_get_job_count () == 0) { |
---|
| 132 | return TRUE; |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | return FALSE; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | static gboolean |
---|
| 139 | wait_until_vfs_threads_gone_no_main (void) |
---|
| 140 | { |
---|
| 141 | int i; |
---|
| 142 | |
---|
| 143 | if (gnome_vfs_backend_get_job_count () == 0) { |
---|
| 144 | return TRUE; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | for (i = 0; i < MAX_THREAD_WAIT; i++) { |
---|
| 148 | usleep (1); |
---|
| 149 | if (gnome_vfs_backend_get_job_count () == 0) { |
---|
| 150 | return TRUE; |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | return FALSE; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | static gboolean |
---|
| 157 | wait_until_file_descriptors_gone (void) |
---|
| 158 | { |
---|
| 159 | int i; |
---|
| 160 | |
---|
| 161 | if (get_used_file_descriptor_count () == 0) { |
---|
| 162 | return TRUE; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | for (i = 0; i < MAX_THREAD_WAIT; i++) { |
---|
| 166 | usleep (1); |
---|
| 167 | gtk_main_iteration_do (FALSE); |
---|
| 168 | if (get_used_file_descriptor_count () == 0) { |
---|
| 169 | return TRUE; |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | return FALSE; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | static gboolean at_least_one_test_failed = FALSE; |
---|
| 176 | |
---|
| 177 | static void |
---|
| 178 | test_failed (const char *format, ...) |
---|
| 179 | { |
---|
| 180 | va_list arguments; |
---|
| 181 | char *message; |
---|
| 182 | |
---|
| 183 | va_start (arguments, format); |
---|
| 184 | message = g_strdup_vprintf (format, arguments); |
---|
| 185 | va_end (arguments); |
---|
| 186 | |
---|
| 187 | g_message ("test failed: %s", message); |
---|
| 188 | at_least_one_test_failed = TRUE; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | static void |
---|
| 192 | get_file_info_callback (GnomeVFSAsyncHandle *handle, |
---|
| 193 | GList *results, |
---|
| 194 | gpointer callback_data) |
---|
| 195 | { |
---|
| 196 | TEST_ASSERT (handle == test_handle, ("get_file_info, bad handle")); |
---|
| 197 | TEST_ASSERT (g_list_length (results) == 1, ("get_file_info, bad list length")); |
---|
| 198 | TEST_ASSERT (callback_data == test_callback_data, ("get_file_info, bad handle")); |
---|
| 199 | |
---|
| 200 | test_handle = NULL; |
---|
| 201 | g_free (callback_data); |
---|
| 202 | |
---|
| 203 | test_done = TRUE; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | static void |
---|
| 207 | first_get_file_info (void) |
---|
| 208 | { |
---|
| 209 | GList *uri_list; |
---|
| 210 | |
---|
| 211 | /* Start a get_file_info call. */ |
---|
| 212 | test_done = FALSE; |
---|
| 213 | test_callback_data = g_malloc (1); |
---|
| 214 | uri_list = g_list_prepend (NULL, gnome_vfs_uri_new ("file:///dev/null")); |
---|
| 215 | gnome_vfs_async_get_file_info (&test_handle, |
---|
| 216 | uri_list, |
---|
| 217 | GNOME_VFS_FILE_INFO_DEFAULT, |
---|
| 218 | get_file_info_callback, |
---|
| 219 | test_callback_data); |
---|
| 220 | g_list_free (uri_list); |
---|
| 221 | |
---|
| 222 | /* Wait until it is done. */ |
---|
| 223 | TEST_ASSERT (wait_for_boolean (&test_done), ("first_get_file_info: callback was not called")); |
---|
| 224 | TEST_ASSERT (wait_until_vfs_threads_gone (), ("first_get_file_info: thread never went away")); |
---|
| 225 | |
---|
| 226 | /* For some reason, this consumes file descriptors. |
---|
| 227 | * I don't know why. |
---|
| 228 | */ |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | static void |
---|
| 232 | test_get_file_info (void) |
---|
| 233 | { |
---|
| 234 | GList *uri_list; |
---|
| 235 | |
---|
| 236 | /* Start a get_file_info call. */ |
---|
| 237 | test_done = FALSE; |
---|
| 238 | test_callback_data = g_malloc (1); |
---|
| 239 | uri_list = g_list_prepend (NULL, gnome_vfs_uri_new ("file:///dev/null")); |
---|
| 240 | gnome_vfs_async_get_file_info (&test_handle, |
---|
| 241 | uri_list, |
---|
| 242 | GNOME_VFS_FILE_INFO_DEFAULT, |
---|
| 243 | get_file_info_callback, |
---|
| 244 | test_callback_data); |
---|
| 245 | g_list_free (uri_list); |
---|
| 246 | |
---|
| 247 | /* Wait until it is done. */ |
---|
| 248 | TEST_ASSERT (wait_for_boolean (&test_done), ("get_file_info 1: callback was not called")); |
---|
| 249 | TEST_ASSERT (wait_until_vfs_threads_gone (), ("get_file_info 1: thread never went away")); |
---|
| 250 | TEST_ASSERT (get_used_file_descriptor_count () == 0, |
---|
| 251 | ("get_file_info 1: %d file descriptors leaked", get_used_file_descriptor_count ())); |
---|
| 252 | free_at_start = get_free_file_descriptor_count (); |
---|
| 253 | |
---|
| 254 | /* Cancel one right after starting it. */ |
---|
| 255 | test_done = FALSE; |
---|
| 256 | test_callback_data = g_malloc (1); |
---|
| 257 | uri_list = g_list_prepend (NULL, gnome_vfs_uri_new ("file:///dev/null")); |
---|
| 258 | gnome_vfs_async_get_file_info (&test_handle, |
---|
| 259 | uri_list, |
---|
| 260 | GNOME_VFS_FILE_INFO_DEFAULT, |
---|
| 261 | get_file_info_callback, |
---|
| 262 | test_callback_data); |
---|
| 263 | g_list_free (uri_list); |
---|
| 264 | gnome_vfs_async_cancel (test_handle); |
---|
| 265 | g_free (test_callback_data); |
---|
| 266 | |
---|
| 267 | /* Wait until it is done. */ |
---|
| 268 | TEST_ASSERT (wait_until_vfs_threads_gone (), ("get_file_info 2: thread never went away")); |
---|
| 269 | TEST_ASSERT (get_used_file_descriptor_count () == 0, |
---|
| 270 | ("get_file_info 2: %d file descriptors leaked", get_used_file_descriptor_count ())); |
---|
| 271 | TEST_ASSERT (test_done == FALSE, ("get_file_info 2: callback was called")); |
---|
| 272 | free_at_start = get_free_file_descriptor_count (); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | static gboolean file_open_flag; |
---|
| 276 | |
---|
| 277 | static void |
---|
| 278 | file_open_callback (GnomeVFSAsyncHandle *handle, |
---|
| 279 | GnomeVFSResult result, |
---|
| 280 | gpointer callback_data) |
---|
| 281 | { |
---|
| 282 | TEST_ASSERT (handle == test_handle, ("open callback, bad handle")); |
---|
| 283 | TEST_ASSERT (result == GNOME_VFS_OK, ("open callback, bad result")); |
---|
| 284 | TEST_ASSERT (callback_data == test_callback_data, ("open callback, bad callback data")); |
---|
| 285 | |
---|
| 286 | file_open_flag = TRUE; |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | static gboolean file_closed_flag; |
---|
| 290 | |
---|
| 291 | static void |
---|
| 292 | file_close_callback (GnomeVFSAsyncHandle *handle, |
---|
| 293 | GnomeVFSResult result, |
---|
| 294 | gpointer callback_data) |
---|
| 295 | { |
---|
| 296 | TEST_ASSERT (handle == test_handle, ("close callback, bad handle")); |
---|
| 297 | TEST_ASSERT (result == GNOME_VFS_OK, ("close callback, bad result")); |
---|
| 298 | TEST_ASSERT (callback_data == test_callback_data, ("close callback, bad callback data")); |
---|
| 299 | |
---|
| 300 | file_closed_flag = TRUE; |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | static gboolean file_read_flag; |
---|
| 304 | char read_buffer[1]; |
---|
| 305 | |
---|
| 306 | static void |
---|
| 307 | file_read_callback (GnomeVFSAsyncHandle *handle, |
---|
| 308 | GnomeVFSResult result, |
---|
| 309 | gpointer buffer, |
---|
| 310 | GnomeVFSFileSize bytes_requested, |
---|
| 311 | GnomeVFSFileSize bytes_read, |
---|
| 312 | gpointer callback_data) |
---|
| 313 | { |
---|
| 314 | TEST_ASSERT (handle == test_handle, ("read callback, bad handle")); |
---|
| 315 | TEST_ASSERT (result == GNOME_VFS_OK, ("read callback, bad result")); |
---|
| 316 | TEST_ASSERT (buffer == read_buffer, ("read callback, bad buffer")); |
---|
| 317 | TEST_ASSERT (bytes_requested == 1, ("read callback, bad bytes_requested")); |
---|
| 318 | TEST_ASSERT (bytes_read == 1, ("read callback, bad bytes_read")); |
---|
| 319 | TEST_ASSERT (callback_data == test_callback_data, ("read callback, bad callback data")); |
---|
| 320 | |
---|
| 321 | file_read_flag = TRUE; |
---|
| 322 | } |
---|
| 323 | |
---|
[15594] | 324 | static gboolean directory_load_flag; |
---|
| 325 | |
---|
[15496] | 326 | static void |
---|
| 327 | directory_load_callback (GnomeVFSAsyncHandle *handle, |
---|
| 328 | GnomeVFSResult result, |
---|
[15857] | 329 | GList *list, |
---|
[15496] | 330 | guint entries_read, |
---|
| 331 | gpointer callback_data) |
---|
| 332 | { |
---|
[15857] | 333 | GList *element; |
---|
| 334 | GnomeVFSFileInfo *info; |
---|
[15594] | 335 | |
---|
[15857] | 336 | for (element = list; element != NULL; element = element->next) { |
---|
| 337 | info = element->data; |
---|
| 338 | gnome_vfs_file_info_ref (info); |
---|
[15594] | 339 | } |
---|
| 340 | |
---|
[15857] | 341 | for (element = list; element != NULL; element = element->next) { |
---|
| 342 | info = element->data; |
---|
| 343 | gnome_vfs_file_info_unref (info); |
---|
[15594] | 344 | } |
---|
| 345 | |
---|
| 346 | directory_load_flag = TRUE; |
---|
[15496] | 347 | } |
---|
| 348 | |
---|
[15594] | 349 | static gboolean directory_load_failed_flag; |
---|
| 350 | |
---|
[15496] | 351 | static void |
---|
[15594] | 352 | directory_load_failed_callback (GnomeVFSAsyncHandle *handle, |
---|
| 353 | GnomeVFSResult result, |
---|
[15857] | 354 | GList *list, |
---|
[15594] | 355 | guint entries_read, |
---|
| 356 | gpointer callback_data) |
---|
| 357 | { |
---|
| 358 | g_assert (result != GNOME_VFS_OK); |
---|
| 359 | directory_load_failed_flag = TRUE; |
---|
| 360 | } |
---|
| 361 | |
---|
| 362 | static void |
---|
| 363 | test_open_read_close (void) |
---|
| 364 | { |
---|
| 365 | file_open_flag = FALSE; |
---|
| 366 | gnome_vfs_async_open (&test_handle, |
---|
| 367 | "file:///etc/passwd", |
---|
| 368 | GNOME_VFS_OPEN_READ, |
---|
| 369 | file_open_callback, |
---|
| 370 | test_callback_data); |
---|
| 371 | TEST_ASSERT (wait_for_boolean (&file_open_flag), ("open: callback was not called")); |
---|
| 372 | |
---|
| 373 | file_read_flag = FALSE; |
---|
| 374 | gnome_vfs_async_read (test_handle, |
---|
| 375 | read_buffer, |
---|
| 376 | 1, |
---|
| 377 | file_read_callback, |
---|
| 378 | test_callback_data); |
---|
| 379 | |
---|
| 380 | TEST_ASSERT (wait_for_boolean (&file_read_flag), ("open read close: read callback was not called")); |
---|
| 381 | file_closed_flag = FALSE; |
---|
| 382 | gnome_vfs_async_close (test_handle, |
---|
| 383 | file_close_callback, |
---|
| 384 | test_callback_data); |
---|
| 385 | |
---|
| 386 | TEST_ASSERT (wait_for_boolean (&file_closed_flag), ("open read close: close callback was not called")); |
---|
| 387 | |
---|
| 388 | TEST_ASSERT (wait_until_vfs_threads_gone (), ("open read cancel close: thread never went away")); |
---|
| 389 | TEST_ASSERT (get_used_file_descriptor_count () == 0, |
---|
| 390 | ("open read cancel close: %d file descriptors leaked", get_used_file_descriptor_count ())); |
---|
| 391 | free_at_start = get_free_file_descriptor_count (); |
---|
| 392 | } |
---|
| 393 | |
---|
| 394 | static void |
---|
[15496] | 395 | test_open_read_cancel_close (void) |
---|
| 396 | { |
---|
| 397 | file_open_flag = FALSE; |
---|
| 398 | gnome_vfs_async_open (&test_handle, |
---|
| 399 | "file:///etc/passwd", |
---|
| 400 | GNOME_VFS_OPEN_READ, |
---|
| 401 | file_open_callback, |
---|
| 402 | test_callback_data); |
---|
| 403 | TEST_ASSERT (wait_for_boolean (&file_open_flag), ("open: callback was not called")); |
---|
| 404 | |
---|
| 405 | file_read_flag = FALSE; |
---|
| 406 | gnome_vfs_async_read (test_handle, |
---|
| 407 | read_buffer, |
---|
| 408 | 1, |
---|
| 409 | file_read_callback, |
---|
| 410 | test_callback_data); |
---|
| 411 | gnome_vfs_async_cancel (test_handle); |
---|
| 412 | |
---|
| 413 | file_closed_flag = FALSE; |
---|
| 414 | gnome_vfs_async_close (test_handle, |
---|
| 415 | file_close_callback, |
---|
| 416 | test_callback_data); |
---|
| 417 | |
---|
| 418 | TEST_ASSERT (wait_for_boolean (&file_closed_flag), ("open read cancel close: callback was not called")); |
---|
| 419 | TEST_ASSERT (!file_read_flag, ("open read cancel close: read callback was called")); |
---|
| 420 | |
---|
| 421 | TEST_ASSERT (wait_until_vfs_threads_gone (), ("open read cancel close: thread never went away")); |
---|
| 422 | TEST_ASSERT (get_used_file_descriptor_count () == 0, |
---|
| 423 | ("open read cancel close: %d file descriptors leaked", get_used_file_descriptor_count ())); |
---|
| 424 | free_at_start = get_free_file_descriptor_count (); |
---|
| 425 | } |
---|
| 426 | |
---|
| 427 | static void |
---|
[15594] | 428 | test_open_close (void) |
---|
| 429 | { |
---|
| 430 | file_open_flag = FALSE; |
---|
| 431 | gnome_vfs_async_open (&test_handle, |
---|
| 432 | "file:///etc/passwd", |
---|
| 433 | GNOME_VFS_OPEN_READ, |
---|
| 434 | file_open_callback, |
---|
| 435 | test_callback_data); |
---|
| 436 | TEST_ASSERT (wait_for_boolean (&file_open_flag), ("open: open callback was not called")); |
---|
| 437 | |
---|
| 438 | file_closed_flag = FALSE; |
---|
| 439 | gnome_vfs_async_close (test_handle, |
---|
| 440 | file_close_callback, |
---|
| 441 | test_callback_data); |
---|
| 442 | |
---|
| 443 | |
---|
| 444 | TEST_ASSERT (wait_for_boolean (&file_closed_flag), ("open close: close callback was not called")); |
---|
| 445 | |
---|
| 446 | TEST_ASSERT (wait_until_vfs_threads_gone (), ("open cancel 1: thread never went away")); |
---|
| 447 | TEST_ASSERT (get_used_file_descriptor_count () == 0, |
---|
| 448 | ("open cancel 1: %d file descriptors leaked", get_used_file_descriptor_count ())); |
---|
| 449 | free_at_start = get_free_file_descriptor_count (); |
---|
| 450 | } |
---|
| 451 | |
---|
| 452 | static void |
---|
[15496] | 453 | test_open_cancel (void) |
---|
| 454 | { |
---|
| 455 | file_open_flag = FALSE; |
---|
| 456 | gnome_vfs_async_open (&test_handle, |
---|
| 457 | "file:///etc/passwd", |
---|
| 458 | GNOME_VFS_OPEN_READ, |
---|
| 459 | file_open_callback, |
---|
| 460 | test_callback_data); |
---|
| 461 | gnome_vfs_async_cancel (test_handle); |
---|
| 462 | |
---|
| 463 | TEST_ASSERT (wait_until_vfs_threads_gone (), ("open cancel 1: thread never went away")); |
---|
| 464 | TEST_ASSERT (!file_open_flag, ("open cancel 1: open callback was called")); |
---|
| 465 | TEST_ASSERT (get_used_file_descriptor_count () == 0, |
---|
| 466 | ("open cancel 1: %d file descriptors leaked", get_used_file_descriptor_count ())); |
---|
| 467 | free_at_start = get_free_file_descriptor_count (); |
---|
| 468 | |
---|
| 469 | file_open_flag = FALSE; |
---|
| 470 | gnome_vfs_async_open (&test_handle, |
---|
| 471 | "file:///etc/passwd", |
---|
| 472 | GNOME_VFS_OPEN_READ, |
---|
| 473 | file_open_callback, |
---|
| 474 | test_callback_data); |
---|
| 475 | wait_until_vfs_threads_gone_no_main (); |
---|
| 476 | gnome_vfs_async_cancel (test_handle); |
---|
| 477 | TEST_ASSERT (wait_until_file_descriptors_gone (), |
---|
| 478 | ("open cancel 2: %d file descriptors leaked", get_used_file_descriptor_count ())); |
---|
| 479 | free_at_start = get_free_file_descriptor_count (); |
---|
| 480 | TEST_ASSERT (wait_until_vfs_threads_gone (), ("open cancel 2: later thread never went away")); |
---|
| 481 | TEST_ASSERT (!file_open_flag, ("open cancel 2: open callback was called")); |
---|
| 482 | } |
---|
| 483 | |
---|
| 484 | static void |
---|
| 485 | file_open_fail_callback (GnomeVFSAsyncHandle *handle, |
---|
| 486 | GnomeVFSResult result, |
---|
| 487 | gpointer callback_data) |
---|
| 488 | { |
---|
| 489 | TEST_ASSERT (handle == test_handle, ("open callback, bad handle")); |
---|
| 490 | TEST_ASSERT (result == GNOME_VFS_ERROR_NOT_FOUND, ("open callback, bad result")); |
---|
| 491 | TEST_ASSERT (callback_data == test_callback_data, ("open callback, bad callback data")); |
---|
| 492 | |
---|
| 493 | file_open_flag = TRUE; |
---|
| 494 | } |
---|
| 495 | |
---|
| 496 | static void |
---|
| 497 | test_open_fail (void) |
---|
| 498 | { |
---|
| 499 | file_open_flag = FALSE; |
---|
| 500 | gnome_vfs_async_open (&test_handle, |
---|
| 501 | "file:///etc/mugwump-xxx", |
---|
| 502 | GNOME_VFS_OPEN_READ, |
---|
| 503 | file_open_fail_callback, |
---|
| 504 | test_callback_data); |
---|
| 505 | TEST_ASSERT (wait_for_boolean (&file_open_flag), ("open fail 1: callback was not called")); |
---|
| 506 | TEST_ASSERT (wait_until_vfs_threads_gone (), ("open fail 1: thread never went away")); |
---|
| 507 | TEST_ASSERT (get_used_file_descriptor_count () == 0, |
---|
| 508 | ("open fail 1: %d file descriptors leaked", get_used_file_descriptor_count ())); |
---|
| 509 | free_at_start = get_free_file_descriptor_count (); |
---|
| 510 | } |
---|
| 511 | |
---|
| 512 | static void |
---|
| 513 | my_yield (int count) |
---|
| 514 | { |
---|
[15594] | 515 | for (; count > 0; count--) { |
---|
| 516 | usleep (100); |
---|
[15496] | 517 | gtk_main_iteration_do (FALSE); |
---|
| 518 | } |
---|
| 519 | } |
---|
| 520 | |
---|
| 521 | static void |
---|
[15594] | 522 | test_load_directory_cancel (int delay_till_cancel, int chunk_count) |
---|
[15496] | 523 | { |
---|
| 524 | GnomeVFSAsyncHandle *handle; |
---|
| 525 | guint num_entries; |
---|
[15594] | 526 | |
---|
| 527 | |
---|
[15496] | 528 | gnome_vfs_async_load_directory (&handle, |
---|
| 529 | "file:///etc", |
---|
| 530 | GNOME_VFS_FILE_INFO_GET_MIME_TYPE |
---|
| 531 | | GNOME_VFS_FILE_INFO_FORCE_FAST_MIME_TYPE |
---|
| 532 | | GNOME_VFS_FILE_INFO_FOLLOW_LINKS, |
---|
| 533 | GNOME_VFS_DIRECTORY_FILTER_NONE, |
---|
| 534 | 0, |
---|
| 535 | NULL, |
---|
[15594] | 536 | chunk_count, |
---|
[15496] | 537 | directory_load_callback, |
---|
| 538 | &num_entries); |
---|
[15594] | 539 | |
---|
| 540 | usleep (delay_till_cancel * 100); |
---|
| 541 | |
---|
| 542 | directory_load_flag = FALSE; |
---|
[15496] | 543 | gnome_vfs_async_cancel (handle); |
---|
| 544 | TEST_ASSERT (wait_until_vfs_threads_gone (), ("open cancel 1: thread never went away")); |
---|
[15594] | 545 | TEST_ASSERT (!directory_load_flag, ("load directory cancel 1: load callback was called")); |
---|
| 546 | |
---|
| 547 | gnome_vfs_async_load_directory (&handle, |
---|
| 548 | "file:///etc", |
---|
| 549 | GNOME_VFS_FILE_INFO_GET_MIME_TYPE |
---|
| 550 | | GNOME_VFS_FILE_INFO_FORCE_FAST_MIME_TYPE |
---|
| 551 | | GNOME_VFS_FILE_INFO_FOLLOW_LINKS, |
---|
| 552 | GNOME_VFS_DIRECTORY_FILTER_NONE, |
---|
| 553 | 0, |
---|
| 554 | NULL, |
---|
| 555 | chunk_count, |
---|
| 556 | directory_load_callback, |
---|
| 557 | &num_entries); |
---|
| 558 | |
---|
| 559 | my_yield (delay_till_cancel); |
---|
| 560 | |
---|
| 561 | directory_load_flag = FALSE; |
---|
| 562 | gnome_vfs_async_cancel (handle); |
---|
| 563 | TEST_ASSERT (wait_until_vfs_threads_gone (), ("open cancel 2: thread never went away")); |
---|
| 564 | TEST_ASSERT (!directory_load_flag, ("load directory cancel 2: load callback was called")); |
---|
[15496] | 565 | } |
---|
| 566 | |
---|
[15594] | 567 | static void |
---|
| 568 | test_load_directory_fail (void) |
---|
| 569 | { |
---|
| 570 | GnomeVFSAsyncHandle *handle; |
---|
| 571 | guint num_entries; |
---|
| 572 | |
---|
| 573 | directory_load_failed_flag = FALSE; |
---|
| 574 | gnome_vfs_async_load_directory (&handle, |
---|
| 575 | "file:///strcprstskrzkrk", |
---|
| 576 | GNOME_VFS_FILE_INFO_GET_MIME_TYPE |
---|
| 577 | | GNOME_VFS_FILE_INFO_FORCE_FAST_MIME_TYPE |
---|
| 578 | | GNOME_VFS_FILE_INFO_FOLLOW_LINKS, |
---|
| 579 | GNOME_VFS_DIRECTORY_FILTER_NONE, |
---|
| 580 | 0, |
---|
| 581 | NULL, |
---|
| 582 | 32, |
---|
| 583 | directory_load_failed_callback, |
---|
| 584 | &num_entries); |
---|
| 585 | |
---|
| 586 | TEST_ASSERT (wait_for_boolean (&directory_load_failed_flag), ("load directory cancel 1: load callback was not called")); |
---|
| 587 | TEST_ASSERT (wait_until_vfs_threads_gone (), ("open cancel 1: thread never went away")); |
---|
| 588 | } |
---|
| 589 | |
---|
| 590 | static gboolean find_directory_flag; |
---|
| 591 | |
---|
| 592 | static void |
---|
| 593 | test_find_directory_callback (GnomeVFSAsyncHandle *handle, |
---|
| 594 | GList *results, |
---|
| 595 | gpointer callback_data) |
---|
| 596 | { |
---|
| 597 | GList *element; |
---|
| 598 | |
---|
| 599 | find_directory_flag = TRUE; |
---|
| 600 | |
---|
| 601 | for (element = results; element != NULL; element = element->next) { |
---|
| 602 | GnomeVFSFindDirectoryResult *result_item = (GnomeVFSFindDirectoryResult *)element->data; |
---|
| 603 | |
---|
| 604 | if (result_item->result == GNOME_VFS_OK) { |
---|
| 605 | gnome_vfs_uri_ref (result_item->uri); |
---|
| 606 | gnome_vfs_uri_unref (result_item->uri); |
---|
| 607 | } |
---|
| 608 | } |
---|
| 609 | |
---|
| 610 | g_assert (callback_data == &find_directory_flag); |
---|
| 611 | } |
---|
| 612 | |
---|
| 613 | static void |
---|
| 614 | test_find_directory (int delay_till_cancel) |
---|
| 615 | { |
---|
| 616 | GnomeVFSAsyncHandle *handle; |
---|
| 617 | GList *vfs_uri_as_list; |
---|
| 618 | |
---|
| 619 | |
---|
| 620 | vfs_uri_as_list = g_list_append (NULL, gnome_vfs_uri_new ("file://~")); |
---|
| 621 | vfs_uri_as_list = g_list_append (vfs_uri_as_list, gnome_vfs_uri_new ("file:///ace_of_spades")); |
---|
| 622 | |
---|
| 623 | find_directory_flag = FALSE; |
---|
| 624 | |
---|
| 625 | gnome_vfs_async_find_directory (&handle, vfs_uri_as_list, |
---|
| 626 | GNOME_VFS_DIRECTORY_KIND_TRASH, FALSE, TRUE, 0777, |
---|
| 627 | test_find_directory_callback, &find_directory_flag); |
---|
| 628 | |
---|
| 629 | TEST_ASSERT (wait_for_boolean (&find_directory_flag), ("find directory cancel 1: callback was not called")); |
---|
| 630 | |
---|
| 631 | find_directory_flag = FALSE; |
---|
| 632 | |
---|
| 633 | gnome_vfs_async_find_directory (&handle, vfs_uri_as_list, |
---|
| 634 | GNOME_VFS_DIRECTORY_KIND_TRASH, FALSE, TRUE, 0777, |
---|
| 635 | test_find_directory_callback, &find_directory_flag); |
---|
| 636 | |
---|
| 637 | usleep (delay_till_cancel * 100); |
---|
| 638 | |
---|
| 639 | gnome_vfs_async_cancel (handle); |
---|
| 640 | TEST_ASSERT (wait_until_vfs_threads_gone (), ("open cancel 2: thread never went away")); |
---|
| 641 | TEST_ASSERT (!find_directory_flag, ("find directory cancel 2: callback was called")); |
---|
| 642 | |
---|
| 643 | |
---|
| 644 | gnome_vfs_async_find_directory (&handle, vfs_uri_as_list, |
---|
| 645 | GNOME_VFS_DIRECTORY_KIND_TRASH, FALSE, TRUE, 0777, |
---|
| 646 | test_find_directory_callback, &find_directory_flag); |
---|
| 647 | |
---|
| 648 | my_yield (delay_till_cancel); |
---|
| 649 | |
---|
| 650 | find_directory_flag = FALSE; |
---|
| 651 | gnome_vfs_async_cancel (handle); |
---|
| 652 | TEST_ASSERT (wait_until_vfs_threads_gone (), ("open cancel 3: thread never went away")); |
---|
| 653 | TEST_ASSERT (!find_directory_flag, ("find directory cancel 3: callback was called")); |
---|
| 654 | |
---|
| 655 | gnome_vfs_uri_list_free (vfs_uri_as_list); |
---|
| 656 | } |
---|
| 657 | |
---|
[15496] | 658 | int |
---|
| 659 | main (int argc, char **argv) |
---|
| 660 | { |
---|
| 661 | CORBA_Environment ev; |
---|
| 662 | |
---|
| 663 | make_asserts_break("GnomeVFS"); |
---|
| 664 | /* Initialize the libraries we use. */ |
---|
| 665 | g_thread_init (NULL); |
---|
| 666 | CORBA_exception_init (&ev); |
---|
| 667 | gnome_CORBA_init ("test-async-cancel", "0.0", &argc, argv, 0, &ev); |
---|
| 668 | gnome_vfs_init (); |
---|
| 669 | |
---|
[15594] | 670 | |
---|
[15496] | 671 | /* Initialize our own stuff. */ |
---|
| 672 | free_at_start = get_free_file_descriptor_count (); |
---|
| 673 | |
---|
| 674 | /* Do the basic tests of our own tools. */ |
---|
| 675 | TEST_ASSERT (get_used_file_descriptor_count () == 0, ("file descriptor count")); |
---|
| 676 | TEST_ASSERT (gnome_vfs_backend_get_job_count () == 0, ("VFS thread count")); |
---|
| 677 | |
---|
| 678 | /* Spend those first few file descriptors. */ |
---|
| 679 | first_get_file_info (); |
---|
| 680 | free_at_start = get_free_file_descriptor_count (); |
---|
| 681 | |
---|
| 682 | /* Test to see that a simple async. call works without leaking or anything. */ |
---|
| 683 | test_get_file_info (); |
---|
| 684 | test_get_file_info (); |
---|
[15594] | 685 | test_open_close (); |
---|
| 686 | test_open_close (); |
---|
| 687 | test_open_read_close (); |
---|
| 688 | test_open_read_close (); |
---|
[15496] | 689 | test_open_cancel (); |
---|
| 690 | test_open_cancel (); |
---|
[15594] | 691 | |
---|
[15496] | 692 | test_open_fail (); |
---|
| 693 | test_open_fail (); |
---|
| 694 | test_open_read_cancel_close (); |
---|
| 695 | test_open_read_cancel_close (); |
---|
| 696 | |
---|
[15594] | 697 | test_load_directory_fail (); |
---|
| 698 | test_load_directory_cancel (0, 1); |
---|
| 699 | test_load_directory_cancel (1, 1); |
---|
| 700 | test_load_directory_cancel (10, 1); |
---|
| 701 | test_load_directory_cancel (100, 1); |
---|
| 702 | test_load_directory_cancel (0, 1); |
---|
| 703 | test_load_directory_cancel (1, 1); |
---|
| 704 | test_load_directory_cancel (10, 1); |
---|
| 705 | test_load_directory_cancel (100, 1); |
---|
[15496] | 706 | |
---|
[15594] | 707 | test_load_directory_cancel (0, 32); |
---|
| 708 | test_load_directory_cancel (1, 32); |
---|
| 709 | test_load_directory_cancel (10, 32); |
---|
| 710 | test_load_directory_cancel (100, 32); |
---|
| 711 | test_load_directory_cancel (0, 32); |
---|
| 712 | test_load_directory_cancel (1, 32); |
---|
| 713 | test_load_directory_cancel (10, 32); |
---|
| 714 | test_load_directory_cancel (100, 32); |
---|
| 715 | |
---|
| 716 | test_find_directory (0); |
---|
| 717 | test_find_directory (0); |
---|
| 718 | test_find_directory (1); |
---|
| 719 | test_find_directory (1); |
---|
| 720 | test_find_directory (10); |
---|
| 721 | test_find_directory (10); |
---|
| 722 | test_find_directory (100); |
---|
| 723 | test_find_directory (100); |
---|
| 724 | |
---|
[15496] | 725 | gnome_vfs_shutdown (); |
---|
| 726 | /* Report to "make check" on whether it all worked or not. */ |
---|
| 727 | return at_least_one_test_failed ? EXIT_FAILURE : EXIT_SUCCESS; |
---|
| 728 | } |
---|