Timer: add testing base
o9000 mrovi9000@gmail.com
5 files changed,
65 insertions(+),
2 deletions(-)
M
src/tint2conf/CMakeLists.txt
→
src/tint2conf/CMakeLists.txt
@@ -23,6 +23,9 @@ set(SOURCES ../util/common.c
../util/strnatcmp.c ../util/cache.c ../util/timer.c + ../util/test.c + ../util/print.c + ../util/signals.c ../config.c ../util/server.c ../util/strlcat.c
M
src/util/signals.c
→
src/util/signals.c
@@ -1,8 +1,10 @@
#include <errno.h> #include <fcntl.h> #include <glib.h> +#ifndef TINT2CONF #ifdef HAVE_SN #include <libsn/sn.h> +#endif #endif #include <signal.h> #include <stdio.h>@@ -34,6 +36,7 @@ sigemptyset(&signal_set);
sigprocmask(SIG_SETMASK, &signal_set, NULL); } +#ifndef TINT2CONF void init_signals() { // Set signal handlers@@ -164,3 +167,4 @@ int get_signal_pending()
{ return signal_pending; } +#endif
M
src/util/test.c
→
src/util/test.c
@@ -146,14 +146,16 @@ else
fprintf(stdout, BLUE "tint2: " RED "%lu" BLUE " out of %lu tests " RED "failed." RESET "\n", failed, count); } +#if 0 TEST(dummy) { int x = 2; int y = 2; ASSERT_EQUAL(x, y); } -TEST(dummyBad) { +TEST(dummy_bad) { int x = 2; int y = 3; ASSERT_EQUAL(x, y); } +#endif
M
src/util/timer.c
→
src/util/timer.c
@@ -21,6 +21,7 @@ #include <stdlib.h>
#include <stdio.h> #include "timer.h" +#include "test.h" GSList *timeout_list; struct timeval next_timeout;@@ -85,8 +86,26 @@ multi_timeouts = NULL;
} } +static struct timespec mock_time = { 0, 0 }; +void set_mock_time(struct timespec *tp) +{ + mock_time = *tp; +} + +void set_mock_time_ms(u_int64_t ms) +{ + struct timespec t; + t.tv_sec = ms / 1000; + t.tv_nsec = (ms % 1000) * 1000 * 1000; + set_mock_time(&t); +} + int gettime(struct timespec *tp) { + if (mock_time.tv_sec || mock_time.tv_nsec) { + *tp = mock_time; + return 0; + } // CLOCK_BOOTTIME under Linux is the same as CLOCK_MONOTONIC under *BSD. #ifdef CLOCK_BOOTTIME return clock_gettime(CLOCK_BOOTTIME, tp);@@ -471,3 +490,39 @@ double delta = t - profiling_get_time_old_time;
profiling_get_time_old_time = t; return delta; } + +TEST(mock_time) { + struct timespec t1 = {1000, 2}; + struct timespec t2 = {0, 0}; + struct timespec t3 = {2000, 3}; + int ret; + set_mock_time(&t1); + ret = gettime(&t2); + ASSERT_EQUAL(ret, 0); + ASSERT_EQUAL(t1.tv_sec, t2.tv_sec); + ASSERT_EQUAL(t1.tv_nsec, t2.tv_nsec); + + set_mock_time(&t3); + ret = gettime(&t2); + ASSERT_EQUAL(ret, 0); + ASSERT_EQUAL(t3.tv_sec, t2.tv_sec); + ASSERT_EQUAL(t3.tv_nsec, t2.tv_nsec); +} + +TEST(mock_time_ms) { + struct timespec t1 = {1000, 2 * 1000 * 1000}; + struct timespec t2 = {0, 0}; + struct timespec t3 = {2000, 3 * 1000 * 1000}; + int ret; + set_mock_time_ms(1000 * 1000 + 2); + ret = gettime(&t2); + ASSERT_EQUAL(ret, 0); + ASSERT_EQUAL(t1.tv_sec, t2.tv_sec); + ASSERT_EQUAL(t1.tv_nsec, t2.tv_nsec); + + set_mock_time_ms(2000 * 1000 + 3); + ret = gettime(&t2); + ASSERT_EQUAL(ret, 0); + ASSERT_EQUAL(t3.tv_sec, t2.tv_sec); + ASSERT_EQUAL(t3.tv_nsec, t2.tv_nsec); +}
M
tint2.config
→
tint2.config
@@ -10,7 +10,6 @@ #define HAVE_VERSION_H 1
#define INSTALL_PREFIX "/usr/local" #define LOCALEDIR "/usr/local/share/locale" #define SN_API_NOT_YET_FROZEN -#define TINT2CONF 1 #define _BSD_SOURCE #define _DEFAULT_SOURCE #define _POSIX_C_SOURCE=200809L