1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2012-2020 Couchbase, Inc.
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  */
17 #include "config.h"
18 #include <stdio.h>
19 /*
20  * The current test suite should not take more than 8 minutes to run.
21  * If you're testing on a really slow system you may set the
22  * environment variable LCB_MAX_TEST_DURATION to the maximum number of
23  * seconds you'd like the tests to take.
24  */
25 const int max_duration = 480;
26 
27 #ifdef _WIN32
28 static HANDLE hTimer;
test_timed_out(PVOID lpUnused, BOOLEAN bUnused)29 void CALLBACK test_timed_out(PVOID lpUnused, BOOLEAN bUnused)
30 {
31     (void)lpUnused;
32     (void)bUnused;
33     fprintf(stderr, "Tests are taking too long to run. Aborting..\n");
34     abort();
35 }
36 #endif
37 
setup_test_timeout_handler(void)38 void setup_test_timeout_handler(void)
39 {
40     char *ptr = getenv("LCB_MAX_TEST_DURATION");
41     int duration = 0;
42     if (ptr != NULL) {
43         duration = atoi(ptr);
44     }
45     if (duration == 0) {
46         duration = max_duration;
47     }
48 
49 #ifdef HAVE_SETITIMER
50     struct itimerval timer = {.it_value = {.tv_sec = duration}};
51     setitimer(ITIMER_REAL, &timer, NULL);
52 #elif defined(HAVE_ALARM)
53     alarm(duration);
54 #elif defined(_WIN32)
55     CreateTimerQueueTimer(&hTimer, NULL, test_timed_out, NULL, duration * 1000, 0, 0);
56 #else
57     /* print an error message so that we're using the duration variable
58      * and not generate a warning about unused variables ;) */
59     fprintf(stderr, "Tests may run longer than %d due to lack of an alarm\n", duration);
60 #endif
61 }
62