lib1501.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "test.h"
  23. #include <fcntl.h>
  24. #include "testutil.h"
  25. #include "warnless.h"
  26. #include "memdebug.h"
  27. #define TEST_HANG_TIMEOUT 30 * 1000
  28. /* 500 milliseconds allowed. An extreme number but lets be really conservative
  29. to allow old and slow machines to run this test too */
  30. #define MAX_BLOCKED_TIME_US 500000
  31. /* return the number of microseconds between two time stamps */
  32. static int elapsed(struct timeval *before,
  33. struct timeval *after)
  34. {
  35. ssize_t result;
  36. result = (after->tv_sec - before->tv_sec) * 1000000 +
  37. after->tv_usec - before->tv_usec;
  38. if (result < 0)
  39. result = 0;
  40. return curlx_sztosi(result);
  41. }
  42. int test(char *URL)
  43. {
  44. CURL *handle = NULL;
  45. CURLM *mhandle = NULL;
  46. int res = 0;
  47. int still_running = 0;
  48. start_test_timing();
  49. global_init(CURL_GLOBAL_ALL);
  50. easy_init(handle);
  51. easy_setopt(handle, CURLOPT_URL, URL);
  52. easy_setopt(handle, CURLOPT_VERBOSE, 1L);
  53. multi_init(mhandle);
  54. multi_add_handle(mhandle, handle);
  55. multi_perform(mhandle, &still_running);
  56. abort_on_test_timeout();
  57. while(still_running) {
  58. struct timeval timeout;
  59. fd_set fdread;
  60. fd_set fdwrite;
  61. fd_set fdexcep;
  62. int maxfd = -99;
  63. struct timeval before;
  64. struct timeval after;
  65. int e;
  66. timeout.tv_sec = 0;
  67. timeout.tv_usec = 100000L; /* 100 ms */
  68. FD_ZERO(&fdread);
  69. FD_ZERO(&fdwrite);
  70. FD_ZERO(&fdexcep);
  71. multi_fdset(mhandle, &fdread, &fdwrite, &fdexcep, &maxfd);
  72. /* At this point, maxfd is guaranteed to be greater or equal than -1. */
  73. select_test(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
  74. abort_on_test_timeout();
  75. fprintf(stderr, "ping\n");
  76. before = tutil_tvnow();
  77. multi_perform(mhandle, &still_running);
  78. abort_on_test_timeout();
  79. after = tutil_tvnow();
  80. e = elapsed(&before, &after);
  81. fprintf(stderr, "pong = %d\n", e);
  82. if(e > MAX_BLOCKED_TIME_US) {
  83. res = 100;
  84. break;
  85. }
  86. }
  87. test_cleanup:
  88. /* undocumented cleanup sequence - type UA */
  89. curl_multi_cleanup(mhandle);
  90. curl_easy_cleanup(handle);
  91. curl_global_cleanup();
  92. return res;
  93. }