lib1565.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2019, 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.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 "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. sleep(TIME_BETWEEN_START_SECS);
  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. 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 = 0;
  78. struct CURLMsg *message;
  79. start_test_timing();
  80. global_init(CURL_GLOBAL_ALL);
  81. multi_init(multi);
  82. url = URL;
  83. res = pthread_create(&tid, NULL, run_thread, NULL);
  84. if(0 != res) {
  85. fprintf(stderr, "%s:%d Couldn't create thread, errno %d\n",
  86. __FILE__, __LINE__, res);
  87. goto test_cleanup;
  88. }
  89. while(1) {
  90. multi_perform(multi, &still_running);
  91. abort_on_test_timeout();
  92. while((message = curl_multi_info_read(multi, &num)) != NULL) {
  93. if(message->msg == CURLMSG_DONE) {
  94. res = message->data.result;
  95. if(res)
  96. goto test_cleanup;
  97. multi_remove_handle(multi, message->easy_handle);
  98. finished_num++;
  99. }
  100. else {
  101. fprintf(stderr, "%s:%d Got an unexpected message from curl: %i\n",
  102. __FILE__, __LINE__, (int)message->msg);
  103. res = TEST_ERR_MAJOR_BAD;
  104. goto test_cleanup;
  105. }
  106. abort_on_test_timeout();
  107. }
  108. if(CONN_NUM == finished_num)
  109. break;
  110. multi_poll(multi, NULL, 0, TEST_HANG_TIMEOUT, &num);
  111. abort_on_test_timeout();
  112. pthread_mutex_lock(&lock);
  113. while(pending_num > 0) {
  114. res_multi_add_handle(multi, pending_handles[pending_num - 1]);
  115. if(res) {
  116. pthread_mutex_unlock(&lock);
  117. goto test_cleanup;
  118. }
  119. started_handles[started_num] = pending_handles[pending_num - 1];
  120. started_num++;
  121. pending_num--;
  122. }
  123. pthread_mutex_unlock(&lock);
  124. abort_on_test_timeout();
  125. }
  126. if(CONN_NUM != started_num) {
  127. fprintf(stderr, "%s:%d Not all connections started: %d of %d\n",
  128. __FILE__, __LINE__, started_num, CONN_NUM);
  129. goto test_cleanup;
  130. }
  131. if(CONN_NUM != finished_num) {
  132. fprintf(stderr, "%s:%d Not all connections finished: %d of %d\n",
  133. __FILE__, __LINE__, started_num, CONN_NUM);
  134. goto test_cleanup;
  135. }
  136. test_cleanup:
  137. pthread_mutex_lock(&lock);
  138. if(!test_failure)
  139. test_failure = res;
  140. pthread_mutex_unlock(&lock);
  141. if(0 != tid)
  142. pthread_join(tid, NULL);
  143. curl_multi_cleanup(multi);
  144. for(i = 0; i < pending_num; i++)
  145. curl_easy_cleanup(pending_handles[i]);
  146. for(i = 0; i < started_num; i++)
  147. curl_easy_cleanup(started_handles[i]);
  148. curl_global_cleanup();
  149. return test_failure;
  150. }
  151. #else /* without pthread, this test doesn't work */
  152. int test(char *URL)
  153. {
  154. (void)URL;
  155. return 0;
  156. }
  157. #endif