tests/libtest: use curltime from curlx

Replacing the local implementation.

Closes #17716
This commit is contained in:
Viktor Szakats 2025-06-23 09:01:02 +02:00
parent a2de3f08e3
commit 6425ce47df
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
10 changed files with 49 additions and 173 deletions

View file

@ -59,7 +59,7 @@ int test_argc;
char **test_argv;
int testnum;
struct timeval tv_test_start; /* for test timing */
struct curltime tv_test_start; /* for test timing */
int unitfail; /* for unittests */

View file

@ -25,7 +25,6 @@
#include <fcntl.h>
#include "testutil.h"
#include "memdebug.h"
static CURLcode test_lib1501(char *URL)
@ -63,9 +62,9 @@ static CURLcode test_lib1501(char *URL)
fd_set fdwrite;
fd_set fdexcep;
int maxfd = -99;
struct timeval before;
struct timeval after;
long e;
struct curltime before;
struct curltime after;
timediff_t e;
timeout.tv_sec = 0;
timeout.tv_usec = 100000L; /* 100 ms */
@ -83,15 +82,15 @@ static CURLcode test_lib1501(char *URL)
abort_on_test_timeout_custom(HANG_TIMEOUT);
curl_mfprintf(stderr, "ping\n");
before = tutil_tvnow();
before = curlx_now();
multi_perform(mhandle, &still_running);
abort_on_test_timeout_custom(HANG_TIMEOUT);
after = tutil_tvnow();
e = tutil_tvdiff(after, before);
curl_mfprintf(stderr, "pong = %ld\n", e);
after = curlx_now();
e = curlx_timediff(after, before);
curl_mfprintf(stderr, "pong = %ld\n", (long)e);
if(e > MAX_BLOCKED_TIME_MS) {
res = CURLE_TOO_LARGE;

View file

@ -23,7 +23,6 @@
***************************************************************************/
#include "first.h"
#include "testutil.h"
#include "timediff.h"
#include "memdebug.h"
@ -52,7 +51,7 @@ static CURLcode test_lib1507(char *URL)
CURL *curl = NULL;
CURLM *mcurl = NULL;
int still_running = 1;
struct timeval mp_start;
struct curltime mp_start;
struct curl_slist *rcpt_list = NULL;
curl_global_init(CURL_GLOBAL_DEFAULT);
@ -78,7 +77,7 @@ static CURLcode test_lib1507(char *URL)
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
multi_add_handle(mcurl, curl);
mp_start = tutil_tvnow();
mp_start = curlx_now();
/* we start some action by calling perform right away */
curl_multi_perform(mcurl, &still_running);
@ -122,7 +121,7 @@ static CURLcode test_lib1507(char *URL)
rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
if(tutil_tvdiff(tutil_tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
if(curlx_timediff(curlx_now(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
curl_mfprintf(stderr, "ABORTING TEST, since it seems "
"that it would have run forever.\n");
break;

View file

@ -23,7 +23,6 @@
***************************************************************************/
#include "first.h"
#include "testutil.h"
#include "memdebug.h"
#define WAKEUP_NUM 10
@ -34,7 +33,7 @@ static CURLcode test_lib1564(char *URL)
int numfds;
int i;
CURLcode res = CURLE_OK;
struct timeval time_before_wait, time_after_wait;
struct curltime time_before_wait, time_after_wait;
(void)URL;
@ -46,11 +45,11 @@ static CURLcode test_lib1564(char *URL)
/* no wakeup */
time_before_wait = tutil_tvnow();
time_before_wait = curlx_now();
multi_poll(multi, NULL, 0, 1000, &numfds);
time_after_wait = tutil_tvnow();
time_after_wait = curlx_now();
if(tutil_tvdiff(time_after_wait, time_before_wait) < 500) {
if(curlx_timediff(time_after_wait, time_before_wait) < 500) {
curl_mfprintf(stderr, "%s:%d curl_multi_poll returned too early\n",
__FILE__, __LINE__);
res = TEST_ERR_MAJOR_BAD;
@ -63,11 +62,11 @@ static CURLcode test_lib1564(char *URL)
res_multi_wakeup(multi);
time_before_wait = tutil_tvnow();
time_before_wait = curlx_now();
multi_poll(multi, NULL, 0, 1000, &numfds);
time_after_wait = tutil_tvnow();
time_after_wait = curlx_now();
if(tutil_tvdiff(time_after_wait, time_before_wait) > 500) {
if(curlx_timediff(time_after_wait, time_before_wait) > 500) {
curl_mfprintf(stderr, "%s:%d curl_multi_poll returned too late\n",
__FILE__, __LINE__);
res = TEST_ERR_MAJOR_BAD;
@ -78,11 +77,11 @@ static CURLcode test_lib1564(char *URL)
/* previous wakeup should not wake up this */
time_before_wait = tutil_tvnow();
time_before_wait = curlx_now();
multi_poll(multi, NULL, 0, 1000, &numfds);
time_after_wait = tutil_tvnow();
time_after_wait = curlx_now();
if(tutil_tvdiff(time_after_wait, time_before_wait) < 500) {
if(curlx_timediff(time_after_wait, time_before_wait) < 500) {
curl_mfprintf(stderr, "%s:%d curl_multi_poll returned too early\n",
__FILE__, __LINE__);
res = TEST_ERR_MAJOR_BAD;
@ -96,11 +95,11 @@ static CURLcode test_lib1564(char *URL)
for(i = 0; i < WAKEUP_NUM; ++i)
res_multi_wakeup(multi);
time_before_wait = tutil_tvnow();
time_before_wait = curlx_now();
multi_poll(multi, NULL, 0, 1000, &numfds);
time_after_wait = tutil_tvnow();
time_after_wait = curlx_now();
if(tutil_tvdiff(time_after_wait, time_before_wait) > 500) {
if(curlx_timediff(time_after_wait, time_before_wait) > 500) {
curl_mfprintf(stderr, "%s:%d curl_multi_poll returned too late\n",
__FILE__, __LINE__);
res = TEST_ERR_MAJOR_BAD;
@ -111,11 +110,11 @@ static CURLcode test_lib1564(char *URL)
/* Even lots of previous wakeups should not wake up this. */
time_before_wait = tutil_tvnow();
time_before_wait = curlx_now();
multi_poll(multi, NULL, 0, 1000, &numfds);
time_after_wait = tutil_tvnow();
time_after_wait = curlx_now();
if(tutil_tvdiff(time_after_wait, time_before_wait) < 500) {
if(curlx_timediff(time_after_wait, time_before_wait) < 500) {
curl_mfprintf(stderr, "%s:%d curl_multi_poll returned too early\n",
__FILE__, __LINE__);
res = TEST_ERR_MAJOR_BAD;

View file

@ -32,7 +32,6 @@
#include <fcntl.h>
#include "testutil.h"
#include "memdebug.h"
struct t530_Sockets {
@ -150,7 +149,7 @@ static int timer_calls = 0;
*/
static int t530_curlTimerCallback(CURLM *multi, long timeout_ms, void *userp)
{
struct timeval *timeout = userp;
struct curltime *timeout = userp;
(void)multi; /* unused */
curl_mfprintf(stderr, "CURLMOPT_TIMERFUNCTION called: %u\n", timer_calls++);
@ -159,7 +158,7 @@ static int t530_curlTimerCallback(CURLM *multi, long timeout_ms, void *userp)
return -1;
}
if(timeout_ms != -1) {
*timeout = tutil_tvnow();
*timeout = curlx_now();
timeout->tv_usec += (int)timeout_ms * 1000;
}
else {
@ -197,11 +196,11 @@ static int t530_checkForCompletion(CURLM *curl, int *success)
return result;
}
static int t530_getMicroSecondTimeout(struct timeval *timeout)
static int t530_getMicroSecondTimeout(struct curltime *timeout)
{
struct timeval now;
struct curltime now;
ssize_t result;
now = tutil_tvnow();
now = curlx_now();
result = (ssize_t)((timeout->tv_sec - now.tv_sec) * 1000000 +
timeout->tv_usec - now.tv_usec);
if(result < 0)
@ -268,7 +267,7 @@ static CURLcode testone(char *URL, int timercb, int socketcb)
CURL *curl = NULL; CURLM *m = NULL;
struct t530_ReadWriteSockets sockets = {{NULL, 0, 0}, {NULL, 0, 0}};
int success = 0;
struct timeval timeout = {0};
struct curltime timeout = {0};
timeout.tv_sec = (time_t)-1;
/* set the limits */

View file

@ -25,7 +25,6 @@
#include <fcntl.h>
#include "testutil.h"
#include "memdebug.h"
struct t582_Sockets {
@ -122,11 +121,11 @@ static int t582_curlSocketCallback(CURL *easy, curl_socket_t s, int action,
*/
static int t582_curlTimerCallback(CURLM *multi, long timeout_ms, void *userp)
{
struct timeval *timeout = userp;
struct curltime *timeout = userp;
(void)multi; /* unused */
if(timeout_ms != -1) {
*timeout = tutil_tvnow();
*timeout = curlx_now();
timeout->tv_usec += (int)timeout_ms * 1000;
}
else {
@ -164,11 +163,11 @@ static int t582_checkForCompletion(CURLM *curl, int *success)
return result;
}
static int t582_getMicroSecondTimeout(struct timeval *timeout)
static int t582_getMicroSecondTimeout(struct curltime *timeout)
{
struct timeval now;
struct curltime now;
ssize_t result;
now = tutil_tvnow();
now = curlx_now();
result = (ssize_t)((timeout->tv_sec - now.tv_sec) * 1000000 +
timeout->tv_usec - now.tv_usec);
if(result < 0)
@ -234,7 +233,7 @@ static CURLcode test_lib582(char *URL)
CURLM *m = NULL;
struct t582_ReadWriteSockets sockets = {{NULL, 0, 0}, {NULL, 0, 0}};
int success = 0;
struct timeval timeout = {0};
struct curltime timeout = {0};
timeout.tv_sec = (time_t)-1;
assert(test_argc >= 5);

View file

@ -32,6 +32,7 @@
#include "curl_setup.h"
#include <curl/curl.h>
#include <curlx/timeval.h>
#ifdef HAVE_SYS_SELECT_H
/* since so many tests use select(), we can just as well include it here */
@ -65,7 +66,7 @@ extern char *libtest_arg4; /* set by first.c to the argv[4] or NULL */
extern int test_argc;
extern char **test_argv;
extern int testnum;
extern struct timeval tv_test_start; /* for test timing */
extern struct curltime tv_test_start; /* for test timing */
extern int select_wrapper(int nfds, fd_set *rd, fd_set *wr, fd_set *exc,
struct timeval *tv);
@ -434,17 +435,17 @@ extern int unitfail;
/* ---------------------------------------------------------------- */
#define start_test_timing() do { \
tv_test_start = tutil_tvnow(); \
tv_test_start = curlx_now(); \
} while(0)
#define TEST_HANG_TIMEOUT 60 * 1000 /* global default */
#define exe_test_timedout(T,Y,Z) do { \
long timediff = tutil_tvdiff(tutil_tvnow(), tv_test_start); \
timediff_t timediff = curlx_timediff(curlx_now(), tv_test_start); \
if(timediff > (T)) { \
curl_mfprintf(stderr, "%s:%d ABORTING TEST, since it seems " \
"that it would have run forever (%ld ms > %ld ms)\n", \
(Y), (Z), timediff, (long) (TEST_HANG_TIMEOUT)); \
(Y), (Z), (long)timediff, (long)(TEST_HANG_TIMEOUT)); \
res = TEST_ERR_RUNS_FOREVER; \
} \
} while(0)

View file

@ -22,7 +22,8 @@
*
***************************************************************************/
#include "testtrace.h"
#include "testutil.h"
#include <curlx/timeval.h>
#include "memdebug.h"
@ -89,10 +90,8 @@ int libtest_debug_cb(CURL *handle, curl_infotype type,
{
struct libtest_trace_cfg *trace_cfg = userp;
const char *text;
struct timeval tv;
char timebuf[20];
char *timestr;
time_t secs;
(void)handle;
@ -101,7 +100,9 @@ int libtest_debug_cb(CURL *handle, curl_infotype type,
if(trace_cfg->tracetime) {
struct tm *now;
tv = tutil_tvnow();
struct curltime tv;
time_t secs;
tv = curlx_now();
if(!known_offset) {
epoch_offset = time(NULL) - tv.tv_sec;
known_offset = 1;

View file

@ -25,110 +25,6 @@
#include "memdebug.h"
#ifdef _WIN32
struct timeval tutil_tvnow(void)
{
/*
** GetTickCount() is available on _all_ Windows versions from W95 up
** to nowadays. Returns milliseconds elapsed since last system boot,
** increases monotonically and wraps once 49.7 days have elapsed.
*/
struct timeval now;
DWORD milliseconds = GetTickCount();
now.tv_sec = (long)(milliseconds / 1000);
now.tv_usec = (long)((milliseconds % 1000) * 1000);
return now;
}
#elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
struct timeval tutil_tvnow(void)
{
/*
** clock_gettime() is granted to be increased monotonically when the
** monotonic clock is queried. Time starting point is unspecified, it
** could be the system start-up time, the Epoch, or something else,
** in any case the time starting point does not change once that the
** system has started up.
*/
struct timeval now;
struct timespec tsnow;
if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
now.tv_sec = tsnow.tv_sec;
now.tv_usec = (int)(tsnow.tv_nsec / 1000);
}
/*
** Even when the configure process has truly detected monotonic clock
** availability, it might happen that it is not actually available at
** run-time. When this occurs simply fallback to other time source.
*/
#ifdef HAVE_GETTIMEOFDAY
else
(void)gettimeofday(&now, NULL);
#else
else {
now.tv_sec = time(NULL);
now.tv_usec = 0;
}
#endif
return now;
}
#elif defined(HAVE_GETTIMEOFDAY)
struct timeval tutil_tvnow(void)
{
/*
** gettimeofday() is not granted to be increased monotonically, due to
** clock drifting and external source time synchronization it can jump
** forward or backward in time.
*/
struct timeval now;
(void)gettimeofday(&now, NULL);
return now;
}
#else
struct timeval tutil_tvnow(void)
{
/*
** time() returns the value of time in seconds since the Epoch.
*/
struct timeval now;
now.tv_sec = time(NULL);
now.tv_usec = 0;
return now;
}
#endif
/*
* Make sure that the first argument is the more recent time, as otherwise
* we'll get a weird negative time-diff back...
*
* Returns: the time difference in number of milliseconds.
*/
long tutil_tvdiff(struct timeval newer, struct timeval older)
{
return (long)(newer.tv_sec-older.tv_sec)*1000+
(long)(newer.tv_usec-older.tv_usec)/1000;
}
/*
* Same as tutil_tvdiff but with full usec resolution.
*
* Returns: the time difference in seconds with subsecond resolution.
*/
double tutil_tvdiff_secs(struct timeval newer, struct timeval older)
{
if(newer.tv_sec != older.tv_sec)
return (double)(newer.tv_sec-older.tv_sec)+
(double)(newer.tv_usec-older.tv_usec)/1000000.0;
return (double)(newer.tv_usec-older.tv_usec)/1000000.0;
}
/* build request url */
char *tutil_suburl(const char *base, int i)
{

View file

@ -25,23 +25,6 @@
***************************************************************************/
#include "test.h"
struct timeval tutil_tvnow(void);
/*
* Make sure that the first argument (t1) is the more recent time and t2 is
* the older time, as otherwise you get a weird negative time-diff back...
*
* Returns: the time difference in number of milliseconds.
*/
long tutil_tvdiff(struct timeval t1, struct timeval t2);
/*
* Same as tutil_tvdiff but with full usec resolution.
*
* Returns: the time difference in seconds with subsecond resolution.
*/
double tutil_tvdiff_secs(struct timeval t1, struct timeval t2);
/* build request url */
char *tutil_suburl(const char *base, int i);