curl/tests/libtest/lib677.c
Viktor Szakats 3efba94f77
cmake: allow building tests in unity mode
Makes building tests noticeably faster.

Apply changes/fixes/workarounds to make Unity work:
- rename test variables to avoid collisions or shadowing each other when
  combined into single units.
- add workaround to avoid applying `lib/memdebug.h` overrides to system
  headers declaring/defining `getaddrinfo()`/`freeaddrinfo()` for
  `tests/server/resolve.c`. This replaces a previous workaround that
  worked for that specific source.
- rename test macro `CTRL` clashing with Cygwin `sys/ioctl.h`.
- add include guard to `test.h`.

Also:
- exclude `tests/http/clients` which are all single-source. (like
  `docs/examples`.)

Build time improvements for tests:
- AppVeyor CI:
  - MSVC 2008, 2010: 1 minute faster (4m8s -> 2m56s, 3m19s -> 2m24s)
  - MSVC 2022 arm64: 3.5 minutes faster (10m18s -> 6m48s)
  before: https://ci.appveyor.com/project/curlorg/curl/builds/50522785
  after: https://ci.appveyor.com/project/curlorg/curl/builds/50522942
- GHA:
  - Cygwin: 1.5 minutes faster (3m13s -> 1m43s)
    before: https://github.com/curl/curl/actions/runs/10681535327/job/29605384398
    after: https://github.com/curl/curl/actions/runs/10680818726/job/29603130637
  - Windows:
    before: https://github.com/curl/curl/actions/runs/10680818713
    after: https://github.com/curl/curl/actions/runs/10683850187
    - MSYS2, mingw-w64: 1 minute faster
    - MSVC: 30 seconds faster (3m17s -> 2m48s)
  - macOS: double speed (39s -> 18s)
    before: https://github.com/curl/curl/actions/runs/10680818753/job/29603133447
    after: https://github.com/curl/curl/actions/runs/10683850174/job/29612914515
  - Linux: almost double speed (30/31s -> 18s)
    before: https://github.com/curl/curl/actions/runs/10681535311/job/29605387156
    after: https://github.com/curl/curl/actions/runs/10680818721/job/29603133976
  - non-native: no obvious effect.
    before: https://github.com/curl/curl/actions/runs/10680818722
    after: https://github.com/curl/curl/actions/runs/10683850187
  - Old Linux: Unity mode not supported by old CMake, no effect.

Closes #14765
2024-09-19 21:32:58 +02:00

137 lines
3.8 KiB
C

/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include "test.h"
#include "testutil.h"
#include "warnless.h"
#include "memdebug.h"
static const char cmd[] = "A1 IDLE\r\n";
static char testbuf[1024];
CURLcode test(char *URL)
{
CURLM *mcurl;
CURL *curl = NULL;
int mrun;
curl_socket_t sock = CURL_SOCKET_BAD;
time_t start = time(NULL);
int state = 0;
ssize_t pos = 0;
CURLcode res = CURLE_OK;
global_init(CURL_GLOBAL_DEFAULT);
multi_init(mcurl);
easy_init(curl);
easy_setopt(curl, CURLOPT_VERBOSE, 1L);
easy_setopt(curl, CURLOPT_URL, URL);
easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
if(curl_multi_add_handle(mcurl, curl))
goto test_cleanup;
while(time(NULL) - start < 5) {
struct curl_waitfd waitfd;
multi_perform(mcurl, &mrun);
for(;;) {
int i;
struct CURLMsg *m = curl_multi_info_read(mcurl, &i);
if(!m)
break;
if(m->msg == CURLMSG_DONE && m->easy_handle == curl) {
curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sock);
if(sock == CURL_SOCKET_BAD)
goto test_cleanup;
printf("Connected fine, extracted socket. Moving on\n");
}
}
if(sock != CURL_SOCKET_BAD) {
waitfd.events = state ? CURL_WAIT_POLLIN : CURL_WAIT_POLLOUT;
waitfd.revents = 0;
curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sock);
waitfd.fd = sock;
}
curl_multi_wait(mcurl, &waitfd, sock == CURL_SOCKET_BAD ? 0 : 1, 50,
&mrun);
if((sock != CURL_SOCKET_BAD) && (waitfd.revents & waitfd.events)) {
size_t len = 0;
if(!state) {
CURLcode ec;
ec = curl_easy_send(curl, cmd + pos, sizeof(cmd) - 1 - pos, &len);
if(ec == CURLE_AGAIN) {
continue;
}
else if(ec) {
fprintf(stderr, "curl_easy_send() failed, with code %d (%s)\n",
(int)ec, curl_easy_strerror(ec));
res = ec;
goto test_cleanup;
}
if(len > 0)
pos += len;
else
pos = 0;
if(pos == sizeof(cmd) - 1) {
state++;
pos = 0;
}
}
else if(pos < (ssize_t)sizeof(testbuf)) {
CURLcode ec;
ec = curl_easy_recv(curl, testbuf + pos, sizeof(testbuf) - pos, &len);
if(ec == CURLE_AGAIN) {
continue;
}
else if(ec) {
fprintf(stderr, "curl_easy_recv() failed, with code %d (%s)\n",
(int)ec, curl_easy_strerror(ec));
res = ec;
goto test_cleanup;
}
if(len > 0)
pos += len;
}
if(len <= 0)
sock = CURL_SOCKET_BAD;
}
}
if(state) {
fwrite(testbuf, pos, 1, stdout);
putchar('\n');
}
curl_multi_remove_handle(mcurl, curl);
test_cleanup:
curl_easy_cleanup(curl);
curl_multi_cleanup(mcurl);
curl_global_cleanup();
return res;
}