lib1565.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 CURLcode test_failure = CURLE_OK;
  38. static CURLM *multi = NULL;
  39. static const char *url;
  40. static void *run_thread(void *ptr)
  41. {
  42. CURL *easy = NULL;
  43. CURLcode res = CURLE_OK;
  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. CURLcode test(char *URL)
  71. {
  72. int still_running;
  73. int num;
  74. int i;
  75. int result;
  76. CURLcode res = CURLE_OK;
  77. CURL *started_handles[CONN_NUM];
  78. int started_num = 0;
  79. int finished_num = 0;
  80. pthread_t tid;
  81. bool tid_valid = false;
  82. struct CURLMsg *message;
  83. start_test_timing();
  84. global_init(CURL_GLOBAL_ALL);
  85. multi_init(multi);
  86. url = URL;
  87. result = pthread_create(&tid, NULL, run_thread, NULL);
  88. if(!result)
  89. tid_valid = true;
  90. else {
  91. fprintf(stderr, "%s:%d Couldn't create thread, errno %d\n",
  92. __FILE__, __LINE__, result);
  93. goto test_cleanup;
  94. }
  95. while(1) {
  96. multi_perform(multi, &still_running);
  97. abort_on_test_timeout();
  98. while((message = curl_multi_info_read(multi, &num))) {
  99. if(message->msg == CURLMSG_DONE) {
  100. res = message->data.result;
  101. if(res)
  102. goto test_cleanup;
  103. multi_remove_handle(multi, message->easy_handle);
  104. finished_num++;
  105. }
  106. else {
  107. fprintf(stderr, "%s:%d Got an unexpected message from curl: %i\n",
  108. __FILE__, __LINE__, (int)message->msg);
  109. res = TEST_ERR_MAJOR_BAD;
  110. goto test_cleanup;
  111. }
  112. abort_on_test_timeout();
  113. }
  114. if(CONN_NUM == finished_num)
  115. break;
  116. multi_poll(multi, NULL, 0, TEST_HANG_TIMEOUT, &num);
  117. abort_on_test_timeout();
  118. pthread_mutex_lock(&lock);
  119. while(pending_num > 0) {
  120. res_multi_add_handle(multi, pending_handles[pending_num - 1]);
  121. if(res) {
  122. pthread_mutex_unlock(&lock);
  123. goto test_cleanup;
  124. }
  125. started_handles[started_num] = pending_handles[pending_num - 1];
  126. started_num++;
  127. pending_num--;
  128. }
  129. pthread_mutex_unlock(&lock);
  130. abort_on_test_timeout();
  131. }
  132. if(CONN_NUM != started_num) {
  133. fprintf(stderr, "%s:%d Not all connections started: %d of %d\n",
  134. __FILE__, __LINE__, started_num, CONN_NUM);
  135. goto test_cleanup;
  136. }
  137. if(CONN_NUM != finished_num) {
  138. fprintf(stderr, "%s:%d Not all connections finished: %d of %d\n",
  139. __FILE__, __LINE__, started_num, CONN_NUM);
  140. goto test_cleanup;
  141. }
  142. test_cleanup:
  143. pthread_mutex_lock(&lock);
  144. if(!test_failure)
  145. test_failure = res;
  146. pthread_mutex_unlock(&lock);
  147. if(tid_valid)
  148. pthread_join(tid, NULL);
  149. curl_multi_cleanup(multi);
  150. for(i = 0; i < pending_num; i++)
  151. curl_easy_cleanup(pending_handles[i]);
  152. for(i = 0; i < started_num; i++)
  153. curl_easy_cleanup(started_handles[i]);
  154. curl_global_cleanup();
  155. return test_failure;
  156. }
  157. #else /* without pthread, this test doesn't work */
  158. CURLcode test(char *URL)
  159. {
  160. (void)URL;
  161. return 0;
  162. }
  163. #endif