Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / tests / libtest / lib533.c
blob737d19b3517e6e3844679527fa00ca53de0cd77b
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: lib533.c,v 1.1.1.1 2008-09-23 16:32:06 hoffman Exp $
9 */
11 /* used for test case 533, 534 and 535 */
13 #include "test.h"
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
19 #include "testutil.h"
21 #define MAIN_LOOP_HANG_TIMEOUT 90 * 1000
22 #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
24 int test(char *URL)
26 int res = 0;
27 CURL *curl;
28 int running;
29 char done=FALSE;
30 CURLM *m;
31 int current=0;
32 struct timeval ml_start;
33 struct timeval mp_start;
34 char ml_timedout = FALSE;
35 char mp_timedout = FALSE;
37 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
38 fprintf(stderr, "curl_global_init() failed\n");
39 return TEST_ERR_MAJOR_BAD;
42 if ((curl = curl_easy_init()) == NULL) {
43 fprintf(stderr, "curl_easy_init() failed\n");
44 curl_global_cleanup();
45 return TEST_ERR_MAJOR_BAD;
48 curl_easy_setopt(curl, CURLOPT_URL, URL);
49 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
50 curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
52 if ((m = curl_multi_init()) == NULL) {
53 fprintf(stderr, "curl_multi_init() failed\n");
54 curl_easy_cleanup(curl);
55 curl_global_cleanup();
56 return TEST_ERR_MAJOR_BAD;
59 if ((res = (int)curl_multi_add_handle(m, curl)) != CURLM_OK) {
60 fprintf(stderr, "curl_multi_add_handle() failed, "
61 "with code %d\n", res);
62 curl_multi_cleanup(m);
63 curl_easy_cleanup(curl);
64 curl_global_cleanup();
65 return TEST_ERR_MAJOR_BAD;
68 ml_timedout = FALSE;
69 ml_start = tutil_tvnow();
71 fprintf(stderr, "Start at URL 0\n");
73 while (!done) {
74 fd_set rd, wr, exc;
75 int max_fd;
76 struct timeval interval;
78 interval.tv_sec = 1;
79 interval.tv_usec = 0;
81 if (tutil_tvdiff(tutil_tvnow(), ml_start) >
82 MAIN_LOOP_HANG_TIMEOUT) {
83 ml_timedout = TRUE;
84 break;
86 mp_timedout = FALSE;
87 mp_start = tutil_tvnow();
89 while (res == CURLM_CALL_MULTI_PERFORM) {
90 res = (int)curl_multi_perform(m, &running);
91 if (tutil_tvdiff(tutil_tvnow(), mp_start) >
92 MULTI_PERFORM_HANG_TIMEOUT) {
93 mp_timedout = TRUE;
94 break;
96 if (running <= 0) {
97 if(!current++) {
98 fprintf(stderr, "Advancing to URL 1\n");
99 /* remove the handle we use */
100 curl_multi_remove_handle(m, curl);
102 /* make us re-use the same handle all the time, and try resetting
103 the handle first too */
104 curl_easy_reset(curl);
105 curl_easy_setopt(curl, CURLOPT_URL, libtest_arg2);
106 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
107 curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
109 /* re-add it */
110 res = (int)curl_multi_add_handle(m, curl);
111 if(res) {
112 fprintf(stderr, "add handle failed: %d.\n", res);
113 res = 243;
114 break;
117 else
118 done = TRUE; /* bail out */
119 break;
122 if (mp_timedout || done)
123 break;
125 if (res != CURLM_OK) {
126 fprintf(stderr, "not okay???\n");
127 break;
130 FD_ZERO(&rd);
131 FD_ZERO(&wr);
132 FD_ZERO(&exc);
133 max_fd = 0;
135 if (curl_multi_fdset(m, &rd, &wr, &exc, &max_fd) != CURLM_OK) {
136 fprintf(stderr, "unexpected failured of fdset.\n");
137 res = 189;
138 break;
141 if (select_test(max_fd+1, &rd, &wr, &exc, &interval) == -1) {
142 fprintf(stderr, "bad select??\n");
143 res = 195;
144 break;
147 res = CURLM_CALL_MULTI_PERFORM;
150 if (ml_timedout || mp_timedout) {
151 if (ml_timedout) fprintf(stderr, "ml_timedout\n");
152 if (mp_timedout) fprintf(stderr, "mp_timedout\n");
153 fprintf(stderr, "ABORTING TEST, since it seems "
154 "that it would have run forever.\n");
155 res = TEST_ERR_RUNS_FOREVER;
158 curl_easy_cleanup(curl);
159 curl_multi_cleanup(m);
160 curl_global_cleanup();
162 return res;