Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / tests / libtest / lib530.c
blob1a140a9ac4faf3ecdb1eed9087926a0968f85b84
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: lib530.c,v 1.1.1.1 2008-09-23 16:32:06 hoffman Exp $
9 */
11 #include "test.h"
13 #ifdef HAVE_SYS_TYPES_H
14 #include <sys/types.h>
15 #endif
16 #ifdef HAVE_SYS_STAT_H
17 #include <sys/stat.h>
18 #endif
19 #ifdef HAVE_STRING_H
20 #include <string.h>
21 #endif
23 #include "testutil.h"
25 #define MAIN_LOOP_HANG_TIMEOUT 90 * 1000
26 #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
28 #define NUM_HANDLES 4
30 int test(char *URL)
32 int res = 0;
33 CURL *curl[NUM_HANDLES];
34 int running;
35 char done=FALSE;
36 CURLM *m;
37 int i, j;
38 struct timeval ml_start;
39 struct timeval mp_start;
40 char ml_timedout = FALSE;
41 char mp_timedout = FALSE;
42 char target_url[256];
44 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
45 fprintf(stderr, "curl_global_init() failed\n");
46 return TEST_ERR_MAJOR_BAD;
49 if ((m = curl_multi_init()) == NULL) {
50 fprintf(stderr, "curl_multi_init() failed\n");
51 curl_global_cleanup();
52 return TEST_ERR_MAJOR_BAD;
55 /* get NUM_HANDLES easy handles */
56 for(i=0; i < NUM_HANDLES; i++) {
57 curl[i] = curl_easy_init();
58 if(!curl[i]) {
59 fprintf(stderr, "curl_easy_init() failed "
60 "on handle #%d\n", i);
61 for (j=i-1; j >= 0; j--) {
62 curl_multi_remove_handle(m, curl[j]);
63 curl_easy_cleanup(curl[j]);
65 curl_multi_cleanup(m);
66 curl_global_cleanup();
67 return TEST_ERR_MAJOR_BAD + i;
69 sprintf(target_url, "%s%04i", URL, i + 1);
70 target_url[sizeof(target_url) - 1] = '\0';
71 curl_easy_setopt(curl[i], CURLOPT_URL, target_url);
73 /* go verbose */
74 curl_easy_setopt(curl[i], CURLOPT_VERBOSE, 1L);
76 /* include headers */
77 curl_easy_setopt(curl[i], CURLOPT_HEADER, 1L);
79 /* add handle to multi */
80 if ((res = (int)curl_multi_add_handle(m, curl[i])) != CURLM_OK) {
81 fprintf(stderr, "curl_multi_add_handle() failed, "
82 "on handle #%d with code %d\n", i, res);
83 curl_easy_cleanup(curl[i]);
84 for (j=i-1; j >= 0; j--) {
85 curl_multi_remove_handle(m, curl[j]);
86 curl_easy_cleanup(curl[j]);
88 curl_multi_cleanup(m);
89 curl_global_cleanup();
90 return TEST_ERR_MAJOR_BAD + i;
94 curl_multi_setopt(m, CURLMOPT_PIPELINING, 1L);
96 ml_timedout = FALSE;
97 ml_start = tutil_tvnow();
99 fprintf(stderr, "Start at URL 0\n");
101 while (!done) {
102 fd_set rd, wr, exc;
103 int max_fd;
104 struct timeval interval;
106 interval.tv_sec = 1;
107 interval.tv_usec = 0;
109 if (tutil_tvdiff(tutil_tvnow(), ml_start) >
110 MAIN_LOOP_HANG_TIMEOUT) {
111 ml_timedout = TRUE;
112 break;
114 mp_timedout = FALSE;
115 mp_start = tutil_tvnow();
117 while (res == CURLM_CALL_MULTI_PERFORM) {
118 res = (int)curl_multi_perform(m, &running);
119 if (tutil_tvdiff(tutil_tvnow(), mp_start) >
120 MULTI_PERFORM_HANG_TIMEOUT) {
121 mp_timedout = TRUE;
122 break;
124 if (running <= 0) {
125 done = TRUE; /* bail out */
126 break;
129 if (mp_timedout || done)
130 break;
132 if (res != CURLM_OK) {
133 fprintf(stderr, "not okay???\n");
134 break;
137 FD_ZERO(&rd);
138 FD_ZERO(&wr);
139 FD_ZERO(&exc);
140 max_fd = 0;
142 if (curl_multi_fdset(m, &rd, &wr, &exc, &max_fd) != CURLM_OK) {
143 fprintf(stderr, "unexpected failured of fdset.\n");
144 res = 189;
145 break;
148 if (select_test(max_fd+1, &rd, &wr, &exc, &interval) == -1) {
149 fprintf(stderr, "bad select??\n");
150 res = 195;
151 break;
154 res = CURLM_CALL_MULTI_PERFORM;
157 if (ml_timedout || mp_timedout) {
158 if (ml_timedout) fprintf(stderr, "ml_timedout\n");
159 if (mp_timedout) fprintf(stderr, "mp_timedout\n");
160 fprintf(stderr, "ABORTING TEST, since it seems "
161 "that it would have run forever.\n");
162 res = TEST_ERR_RUNS_FOREVER;
165 /* cleanup NUM_HANDLES easy handles */
166 for(i=0; i < NUM_HANDLES; i++) {
167 curl_multi_remove_handle(m, curl[i]);
168 curl_easy_cleanup(curl[i]);
171 curl_multi_cleanup(m);
172 curl_global_cleanup();
174 return res;