Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / tests / libtest / lib536.c
blobe9b86b9ecec9a01c452c66cc545a7b5fbcafd97b
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: lib536.c,v 1.1.1.1 2008-09-23 16:32:06 hoffman Exp $
9 */
11 #include "test.h"
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
17 #include "testutil.h"
19 #define MAIN_LOOP_HANG_TIMEOUT 90 * 1000
20 #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
22 static CURLMcode perform(CURLM * multi)
24 int handles, maxfd;
25 CURLMcode code;
26 fd_set fdread, fdwrite, fdexcep;
27 struct timeval mp_start;
28 char mp_timedout = FALSE;
30 mp_timedout = FALSE;
31 mp_start = tutil_tvnow();
33 for (;;) {
34 code = curl_multi_perform(multi, &handles);
35 if (tutil_tvdiff(tutil_tvnow(), mp_start) >
36 MULTI_PERFORM_HANG_TIMEOUT) {
37 mp_timedout = TRUE;
38 break;
40 if (handles <= 0)
41 return CURLM_OK;
43 switch (code) {
44 case CURLM_OK:
45 break;
46 case CURLM_CALL_MULTI_PERFORM:
47 continue;
48 default:
49 return code;
52 FD_ZERO(&fdread);
53 FD_ZERO(&fdwrite);
54 FD_ZERO(&fdexcep);
55 curl_multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
56 if (maxfd < 0)
57 return (CURLMcode) ~CURLM_OK;
58 if (select(maxfd + 1, &fdread, &fdwrite, &fdexcep, 0) == -1)
59 return (CURLMcode) ~CURLM_OK;
62 /* We only reach this point if (mp_timedout) */
63 if (mp_timedout) fprintf(stderr, "mp_timedout\n");
64 fprintf(stderr, "ABORTING TEST, since it seems "
65 "that it would have run forever.\n");
66 return (CURLMcode) ~CURLM_OK;
69 int test(char *URL)
71 CURLM *multi;
72 CURL *easy;
73 int res = 0;
75 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
76 fprintf(stderr, "curl_global_init() failed\n");
77 return TEST_ERR_MAJOR_BAD;
80 if ((multi = curl_multi_init()) == NULL) {
81 fprintf(stderr, "curl_multi_init() failed\n");
82 curl_global_cleanup();
83 return TEST_ERR_MAJOR_BAD;
86 if ((easy = curl_easy_init()) == NULL) {
87 fprintf(stderr, "curl_easy_init() failed\n");
88 curl_multi_cleanup(multi);
89 curl_global_cleanup();
90 return TEST_ERR_MAJOR_BAD;
93 curl_multi_setopt(multi, CURLMOPT_PIPELINING, 1L);
95 curl_easy_setopt(easy, CURLOPT_WRITEFUNCTION, fwrite);
96 curl_easy_setopt(easy, CURLOPT_FAILONERROR, 1L);
97 curl_easy_setopt(easy, CURLOPT_URL, URL);
99 if (curl_multi_add_handle(multi, easy) != CURLM_OK) {
100 printf("curl_multi_add_handle() failed\n");
101 res = TEST_ERR_MAJOR_BAD;
102 } else {
103 if (perform(multi) != CURLM_OK)
104 printf("retrieve 1 failed\n");
106 curl_multi_remove_handle(multi, easy);
108 curl_easy_reset(easy);
110 curl_easy_setopt(easy, CURLOPT_FAILONERROR, 1L);
111 curl_easy_setopt(easy, CURLOPT_URL, libtest_arg2);
113 if (curl_multi_add_handle(multi, easy) != CURLM_OK) {
114 printf("curl_multi_add_handle() 2 failed\n");
115 res = TEST_ERR_MAJOR_BAD;
116 } else {
117 if (perform(multi) != CURLM_OK)
118 printf("retrieve 2 failed\n");
120 curl_multi_remove_handle(multi, easy);
122 curl_easy_cleanup(easy);
123 curl_multi_cleanup(multi);
124 curl_global_cleanup();
126 printf("Finished!\n");
128 return res;