lib1565.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, Daniel Stenberg, <[email protected]>, 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 https://curl.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 "testutil.h"
  24. #include "warnless.h"
  25. #include "memdebug.h"
  26. #ifdef HAVE_PTHREAD_H
  27. #include <pthread.h>
  28. #include <unistd.h>
  29. #define TEST_HANG_TIMEOUT 60 * 1000
  30. #define CONN_NUM 3
  31. #define TIME_BETWEEN_START_SECS 2
  32. static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
  33. static CURL *pending_handles[CONN_NUM];
  34. static int pending_num = 0;
  35. static int test_failure = 0;
  36. static CURLM *multi = NULL;
  37. static const char *url;
  38. static void *run_thread(void *ptr)
  39. {
  40. CURL *easy = NULL;
  41. int res = 0;
  42. int i;
  43. (void)ptr;
  44. for(i = 0; i < CONN_NUM; i++) {
  45. wait_ms(TIME_BETWEEN_START_SECS * 1000);
  46. easy_init(easy);
  47. easy_setopt(easy, CURLOPT_URL, url);
  48. easy_setopt(easy, CURLOPT_VERBOSE, 0L);
  49. pthread_mutex_lock(&lock);
  50. if(test_failure) {
  51. pthread_mutex_unlock(&lock);
  52. goto test_cleanup;
  53. }
  54. pending_handles[pending_num] = easy;
  55. pending_num++;
  56. easy = NULL;
  57. pthread_mutex_unlock(&lock);
  58. res_multi_wakeup(multi);
  59. }
  60. test_cleanup:
  61. curl_easy_cleanup(easy);
  62. pthread_mutex_lock(&lock);
  63. if(!test_failure)
  64. test_failure = res;
  65. pthread_mutex_unlock(&lock);
  66. return NULL;
  67. }
  68. int test(char *URL)
  69. {
  70. int still_running;
  71. int num;
  72. int i;
  73. int res = 0;
  74. CURL *started_handles[CONN_NUM];
  75. int started_num = 0;
  76. int finished_num = 0;
  77. pthread_t tid;
  78. bool tid_valid = false;
  79. struct CURLMsg *message;
  80. start_test_timing();
  81. global_init(CURL_GLOBAL_ALL);
  82. multi_init(multi);
  83. url = URL;
  84. res = pthread_create(&tid, NULL, run_thread, NULL);
  85. if(!res)
  86. tid_valid = true;
  87. else {
  88. fprintf(stderr, "%s:%d Couldn't create thread, errno %d\n",
  89. __FILE__, __LINE__, res);
  90. goto test_cleanup;
  91. }
  92. while(1) {
  93. multi_perform(multi, &still_running);
  94. abort_on_test_timeout();
  95. while((message = curl_multi_info_read(multi, &num)) != NULL) {
  96. if(message->msg == CURLMSG_DONE) {
  97. res = message->data.result;
  98. if(res)
  99. goto test_cleanup;
  100. multi_remove_handle(multi, message->easy_handle);
  101. finished_num++;
  102. }
  103. else {
  104. fprintf(stderr, "%s:%d Got an unexpected message from curl: %i\n",
  105. __FILE__, __LINE__, (int)message->msg);
  106. res = TEST_ERR_MAJOR_BAD;
  107. goto test_cleanup;
  108. }
  109. abort_on_test_timeout();
  110. }
  111. if(CONN_NUM == finished_num)
  112. break;
  113. multi_poll(multi, NULL, 0, TEST_HANG_TIMEOUT, &num);
  114. abort_on_test_timeout();
  115. pthread_mutex_lock(&lock);
  116. while(pending_num > 0) {
  117. res_multi_add_handle(multi, pending_handles[pending_num - 1]);
  118. if(res) {
  119. pthread_mutex_unlock(&lock);
  120. goto test_cleanup;
  121. }
  122. started_handles[started_num] = pending_handles[pending_num - 1];
  123. started_num++;
  124. pending_num--;
  125. }
  126. pthread_mutex_unlock(&lock);
  127. abort_on_test_timeout();
  128. }
  129. if(CONN_NUM != started_num) {
  130. fprintf(stderr, "%s:%d Not all connections started: %d of %d\n",
  131. __FILE__, __LINE__, started_num, CONN_NUM);
  132. goto test_cleanup;
  133. }
  134. if(CONN_NUM != finished_num) {
  135. fprintf(stderr, "%s:%d Not all connections finished: %d of %d\n",
  136. __FILE__, __LINE__, started_num, CONN_NUM);
  137. goto test_cleanup;
  138. }
  139. test_cleanup:
  140. pthread_mutex_lock(&lock);
  141. if(!test_failure)
  142. test_failure = res;
  143. pthread_mutex_unlock(&lock);
  144. if(tid_valid)
  145. pthread_join(tid, NULL);
  146. curl_multi_cleanup(multi);
  147. for(i = 0; i < pending_num; i++)
  148. curl_easy_cleanup(pending_handles[i]);
  149. for(i = 0; i < started_num; i++)
  150. curl_easy_cleanup(started_handles[i]);
  151. curl_global_cleanup();
  152. return test_failure;
  153. }
  154. #else /* without pthread, this test doesn't work */
  155. int test(char *URL)
  156. {
  157. (void)URL;
  158. return 0;
  159. }
  160. #endif