2
0

lib1565.c 4.7 KB

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