quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

lib1565.c (4988B)


      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 "first.h"
     25 
     26 #include "memdebug.h"
     27 
     28 #ifdef HAVE_PTHREAD_H
     29 #include <pthread.h>
     30 
     31 #define CONN_NUM 3
     32 #define TIME_BETWEEN_START_SECS 2
     33 
     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 t1565_test_failure = CURLE_OK;
     38 
     39 static CURLM *testmulti = NULL;
     40 static const char *t1565_url;
     41 
     42 static void *t1565_run_thread(void *ptr)
     43 {
     44   CURL *easy = NULL;
     45   CURLcode res = CURLE_OK;
     46   int i;
     47 
     48   (void)ptr;
     49 
     50   for(i = 0; i < CONN_NUM; i++) {
     51     curlx_wait_ms(TIME_BETWEEN_START_SECS * 1000);
     52 
     53     easy_init(easy);
     54 
     55     easy_setopt(easy, CURLOPT_URL, t1565_url);
     56     easy_setopt(easy, CURLOPT_VERBOSE, 0L);
     57 
     58     pthread_mutex_lock(&lock);
     59 
     60     if(t1565_test_failure) {
     61       pthread_mutex_unlock(&lock);
     62       goto test_cleanup;
     63     }
     64 
     65     pending_handles[pending_num] = easy;
     66     pending_num++;
     67     easy = NULL;
     68 
     69     pthread_mutex_unlock(&lock);
     70 
     71     res_multi_wakeup(testmulti);
     72   }
     73 
     74 test_cleanup:
     75 
     76   curl_easy_cleanup(easy);
     77 
     78   pthread_mutex_lock(&lock);
     79 
     80   if(!t1565_test_failure)
     81     t1565_test_failure = res;
     82 
     83   pthread_mutex_unlock(&lock);
     84 
     85   return NULL;
     86 }
     87 
     88 static CURLcode test_lib1565(char *URL)
     89 {
     90   int still_running;
     91   int num;
     92   int i;
     93   int result;
     94   CURLcode res = CURLE_OK;
     95   CURL *started_handles[CONN_NUM];
     96   int started_num = 0;
     97   int finished_num = 0;
     98   pthread_t tid;
     99   bool tid_valid = false;
    100   struct CURLMsg *message;
    101 
    102   start_test_timing();
    103 
    104   global_init(CURL_GLOBAL_ALL);
    105 
    106   multi_init(testmulti);
    107 
    108   t1565_url = URL;
    109 
    110   result = pthread_create(&tid, NULL, t1565_run_thread, NULL);
    111   if(!result)
    112     tid_valid = true;
    113   else {
    114     curl_mfprintf(stderr, "%s:%d Couldn't create thread, errno %d\n",
    115                   __FILE__, __LINE__, result);
    116     goto test_cleanup;
    117   }
    118 
    119   while(1) {
    120     multi_perform(testmulti, &still_running);
    121 
    122     abort_on_test_timeout();
    123 
    124     while((message = curl_multi_info_read(testmulti, &num))) {
    125       if(message->msg == CURLMSG_DONE) {
    126         res = message->data.result;
    127         if(res)
    128           goto test_cleanup;
    129         multi_remove_handle(testmulti, message->easy_handle);
    130         finished_num++;
    131       }
    132       else {
    133         curl_mfprintf(stderr,
    134                       "%s:%d Got an unexpected message from curl: %i\n",
    135                       __FILE__, __LINE__, (int)message->msg);
    136         res = TEST_ERR_MAJOR_BAD;
    137         goto test_cleanup;
    138       }
    139 
    140       abort_on_test_timeout();
    141     }
    142 
    143     if(CONN_NUM == finished_num)
    144       break;
    145 
    146     multi_poll(testmulti, NULL, 0, TEST_HANG_TIMEOUT, &num);
    147 
    148     abort_on_test_timeout();
    149 
    150     pthread_mutex_lock(&lock);
    151 
    152     while(pending_num > 0) {
    153       res_multi_add_handle(testmulti, pending_handles[pending_num - 1]);
    154       if(res) {
    155         pthread_mutex_unlock(&lock);
    156         goto test_cleanup;
    157       }
    158 
    159       started_handles[started_num] = pending_handles[pending_num - 1];
    160       started_num++;
    161       pending_num--;
    162     }
    163 
    164     pthread_mutex_unlock(&lock);
    165 
    166     abort_on_test_timeout();
    167   }
    168 
    169   if(CONN_NUM != started_num) {
    170     curl_mfprintf(stderr, "%s:%d Not all connections started: %d of %d\n",
    171                   __FILE__, __LINE__, started_num, CONN_NUM);
    172     goto test_cleanup;
    173   }
    174 
    175   if(CONN_NUM != finished_num) {
    176     curl_mfprintf(stderr, "%s:%d Not all connections finished: %d of %d\n",
    177                   __FILE__, __LINE__, started_num, CONN_NUM);
    178     goto test_cleanup;
    179   }
    180 
    181 test_cleanup:
    182 
    183   pthread_mutex_lock(&lock);
    184   if(!t1565_test_failure)
    185     t1565_test_failure = res;
    186   pthread_mutex_unlock(&lock);
    187 
    188   if(tid_valid)
    189     pthread_join(tid, NULL);
    190 
    191   curl_multi_cleanup(testmulti);
    192   for(i = 0; i < pending_num; i++)
    193     curl_easy_cleanup(pending_handles[i]);
    194   for(i = 0; i < started_num; i++)
    195     curl_easy_cleanup(started_handles[i]);
    196   curl_global_cleanup();
    197 
    198   return t1565_test_failure;
    199 }
    200 
    201 #else /* without pthread, this test doesn't work */
    202 static CURLcode test_lib1565(char *URL)
    203 {
    204   (void)URL;
    205   return CURLE_OK;
    206 }
    207 #endif