#include "config.h"
#include <stdio.h>
const int max_duration = 300;
#ifdef _WIN32
static HANDLE hTimer;
void CALLBACK test_timed_out(PVOID lpUnused, BOOLEAN bUnused)
{
(void)lpUnused;
(void)bUnused;
fprintf(stderr, "Tests are taking too long to run. Aborting..\n");
abort();
}
#endif
void setup_test_timeout_handler(void)
{
char *ptr = getenv("LCB_MAX_TEST_DURATION");
int duration = 0;
if (ptr != NULL) {
duration = atoi(ptr);
}
if (duration == 0) {
duration = max_duration;
}
#ifdef HAVE_SETITIMER
struct itimerval timer = { .it_value = { .tv_sec = duration } };
setitimer(ITIMER_REAL, &timer, NULL);
#elif defined(HAVE_ALARM)
alarm(duration);
#elif defined(_WIN32)
CreateTimerQueueTimer(
&hTimer,
NULL,
test_timed_out,
NULL,
duration * 1000,
0,
0);
#else
fprintf(stderr, "Tests may run longer than %d due to lack of an alarm\n",
duration);
#endif
}