Skip to content

Commit

Permalink
Tests should finish within 5 minutes
Browse files Browse the repository at this point in the history
I've seen the automated builds hang due to a hanging test.
This patch set up a timer to fire after 5 minutes causing
the tests to fail. Export LCB_MAX_TEST_DURATION to raise
the limit.

Change-Id: I850303b3998ee5cc2b0e217e87466ad42033c8b9
Reviewed-on: http://review.couchbase.org/20821
Tested-by: Trond Norbye <trond.norbye@gmail.com>
Reviewed-by: Sergey Avseyev <sergey.avseyev@gmail.com>
  • Loading branch information
trondn authored and avsej committed Sep 13, 2012
1 parent 2cec78b commit ac51b22
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 4 deletions.
7 changes: 4 additions & 3 deletions Makefile.am
Expand Up @@ -260,19 +260,20 @@ tests_unit_tests_SOURCES += \
tests/serverops-unit-test.cc \
tests/serverparams.h \
tests/testutil.cc \
tests/testutil.h
tests/testutil.h \
tests/timeout.c

check_PROGRAMS += \
tests/config-test \
tests/smoke-test
endif

tests_config_test_SOURCES = tests/test.h tests/config-test.c
tests_config_test_SOURCES = tests/test.h tests/config-test.c tests/timeout.c
tests_config_test_DEPENDENCIES = libcouchbase.la libmockserver.la libvbucket.la
tests_config_test_LDADD = $(tests_config_test_DEPENDENCIES)
tests_config_test_LDFLAGS = $(AM_LDFLAGS)

tests_smoke_test_SOURCES = tests/test.h tests/smoke-test.c
tests_smoke_test_SOURCES = tests/test.h tests/smoke-test.c tests/timeout.c
tests_smoke_test_LDADD = libcouchbase.la libmockserver.la

tools_cbc_DEPENDENCIES = libcouchbase.la libvbucket.la
Expand Down
3 changes: 2 additions & 1 deletion configure.ac
Expand Up @@ -334,7 +334,8 @@ AS_IF([test "x${ac_cv_enable_tools}" = "xyes"],

AM_CONDITIONAL(BUILD_TOOLS, [test "x${ac_cv_enable_tools}" = "xyes"])

AC_CHECK_FUNCS_ONCE(gethrtime clock_gettime gettimeofday QueryPerformanceCounter)
AC_CHECK_FUNCS_ONCE([gethrtime clock_gettime gettimeofday
QueryPerformanceCounter alarm setitimer])
AM_CONDITIONAL(HAVE_GETHRTIME, [test "x${ac_cv_func_gethrtime}" = "xyes"])

AC_ARG_ENABLE([embed-libevent-plugin],
Expand Down
3 changes: 3 additions & 0 deletions tests/config-test.c
Expand Up @@ -26,6 +26,7 @@

#include "server.h"
#include "test.h"
#include "testutil.h"

int config_cnt;
int store_cnt;
Expand Down Expand Up @@ -244,6 +245,8 @@ int main(int argc, char **argv)
(void)argc;
(void)argv;

setup_test_timeout_handler();

if (getenv("LIBCOUCHBASE_VERBOSE_TESTS") == NULL) {
freopen("/dev/null", "w", stdout);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/smoke-test.c
Expand Up @@ -24,6 +24,7 @@
#include "internal.h" /* to look at the internals to check if sasl ok */
#include "server.h"
#include "test.h"
#include "testutil.h"

lcb_t session = NULL;
const struct test_server_info *mock = NULL;
Expand Down Expand Up @@ -610,6 +611,7 @@ int main(int argc, char **argv)
freopen("/dev/null", "w", stdout);
}

setup_test_timeout_handler();
total_node_count = 5;
snprintf(str_node_count, 16, "%d", total_node_count);
args[1] = str_node_count;
Expand Down
10 changes: 10 additions & 0 deletions tests/testutil.h
Expand Up @@ -20,6 +20,15 @@
#include <libcouchbase/couchbase.h>
#include <string.h>

#ifdef __cplusplus
extern "C" {
#endif

void setup_test_timeout_handler(void);

#ifdef __cplusplus
} // C linkage

struct Item {
void assign(const lcb_get_resp_t *resp, lcb_error_t e = LCB_SUCCESS) {
key.assign((const char*)resp->v.v0.key, resp->v.v0.nkey);
Expand Down Expand Up @@ -57,5 +66,6 @@ struct Item {
void storeKey(lcb_t instance, const std::string &key, const std::string &value);
void removeKey(lcb_t instance, const std::string &key);
void getKey(lcb_t instance, const std::string &key, Item &item);
#endif

#endif
50 changes: 50 additions & 0 deletions tests/timeout.c
@@ -0,0 +1,50 @@
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* Copyright 2012 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "config.h"
#include "testutil.h"

/*
* The current test suite should not take more than 5 minutes to run.
* If you're testing on a really slow system you may set the
* environment variable LCB_MAX_TEST_DURATION to the maximum number of
* seconds you'd like the tests to take.
*/
const int max_duration = 300;

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);
#else
/* print an error message so that we're using the duration variable
* and not generate a warning about unused variables ;) */
fprintf(stderr, "Tests may run longer than %d due to lack of an alarm\n",
duration);
#endif
}
3 changes: 3 additions & 0 deletions tests/unit_tests.cc
Expand Up @@ -20,11 +20,14 @@
#include "mock-environment.h"
#endif

#include "testutil.h"

int main(int argc, char **argv)
{
#ifdef HAVE_COUCHBASEMOCK
::testing::AddGlobalTestEnvironment(MockEnvironment::getInstance());
#endif
::testing::InitGoogleTest(&argc, argv);
setup_test_timeout_handler();
return RUN_ALL_TESTS();
}

0 comments on commit ac51b22

Please sign in to comment.