aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeels Hofmeyr <neels@hofmeyr.de>2018-05-31 16:46:31 +0200
committerNeels Hofmeyr <neels@hofmeyr.de>2018-05-31 16:46:42 +0200
commit5ef63db2994cfe39c72326d13f7b247c1476fed3 (patch)
treef62b11ff854beb69c9b099d113f13078bb01cfa5
parent74cac58f79afa205226c112d5e6463f098f507b0 (diff)
add tests/timer/timer_misc_test to test remaining time
Reading the osmo_timer code, I was intrigued by the condition in osmo_timer_remaining(): it looked like a time difference below 1 would cause faulty behavior (which is not the case). Add timer_misc_test to test osmo_timer_remaining(). timer_test already exists, but it hogs the main function and actually calls exit() on the last timer, so create a separate test program. Enrich the osmo_timer_remaining() comment to indicate the minus-one behavior. Change-Id: I8d0668ff2ba516c99a1907a28fc0e7c681091b5c
-rw-r--r--src/timer.c6
-rw-r--r--tests/Makefile.am8
-rw-r--r--tests/testsuite.at6
-rw-r--r--tests/timer/timer_misc_test.c117
-rw-r--r--tests/timer/timer_misc_test.ok1
5 files changed, 136 insertions, 2 deletions
diff --git a/src/timer.c b/src/timer.c
index 02160a10..9b82ec91 100644
--- a/src/timer.c
+++ b/src/timer.c
@@ -148,6 +148,12 @@ int osmo_timer_pending(struct osmo_timer_list *timer)
*
* This function can be used to determine the amount of time
* remaining until the expiration of the timer.
+ *
+ * Note: If the timer has already expired, the remaining time returned reflects
+ * a negative value, but this is *not* the accurate time since the timer should
+ * have fired. Rather, it is the negative time since expiry with tv_usec
+ * decremented one second. This is an implementation detail to ensure a
+ * negative sign even for fractional values. See timersub.
*/
int osmo_timer_remaining(const struct osmo_timer_list *timer,
const struct timeval *now,
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 70b5cb81..bc97a777 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -23,7 +23,8 @@ check_PROGRAMS = timer/timer_test sms/sms_test ussd/ussd_test \
coding/coding_test conv/conv_gsm0503_test \
abis/abis_test endian/endian_test sercomm/sercomm_test \
prbs/prbs_test gsm23003/gsm23003_test \
- codec/codec_ecu_fr_test timer/clk_override_test
+ codec/codec_ecu_fr_test timer/clk_override_test \
+ timer/timer_misc_test
if ENABLE_MSGFILE
check_PROGRAMS += msgfile/msgfile_test
@@ -122,6 +123,8 @@ sms_sms_test_LDADD = $(LDADD) $(top_builddir)/src/gsm/libosmogsm.la
timer_timer_test_SOURCES = timer/timer_test.c
+timer_timer_misc_test_SOURCES = timer/timer_misc_test.c
+
timer_clk_override_test_SOURCES = timer/clk_override_test.c
ussd_ussd_test_SOURCES = ussd/ussd_test.c
@@ -253,7 +256,8 @@ EXTRA_DIST = testsuite.at $(srcdir)/package.m4 $(TESTSUITE) \
conv/conv_gsm0503_test.ok endian/endian_test.ok \
sercomm/sercomm_test.ok prbs/prbs_test.ok \
gsm23003/gsm23003_test.ok \
- timer/clk_override_test.ok
+ timer/clk_override_test.ok \
+ timer/timer_misc_test.c
DISTCLEANFILES = atconfig atlocal conv/gsm0503_test_vectors.c
BUILT_SOURCES = conv/gsm0503_test_vectors.c
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 15a89b6d..668ed7a6 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -246,6 +246,12 @@ cat $abs_srcdir/timer/clk_override_test.ok > expout
AT_CHECK([$abs_top_builddir/tests/timer/clk_override_test], [0], [expout], [ignore])
AT_CLEANUP
+AT_SETUP([timer_misc])
+AT_KEYWORDS([timer_misc])
+cat $abs_srcdir/timer/timer_misc_test.ok > expout
+AT_CHECK([$abs_top_builddir/tests/timer/timer_misc_test], [0], [expout], [ignore])
+AT_CLEANUP
+
AT_SETUP([tlv])
AT_KEYWORDS([tlv])
cat $abs_srcdir/tlv/tlv_test.ok > expout
diff --git a/tests/timer/timer_misc_test.c b/tests/timer/timer_misc_test.c
new file mode 100644
index 00000000..9e604b5a
--- /dev/null
+++ b/tests/timer/timer_misc_test.c
@@ -0,0 +1,117 @@
+/*
+ * (C) 2018 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
+ * All Rights Reserved
+ *
+ * Authors: Neels Hofmeyr <neels@hofmeyr.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include <stdio.h>
+#include <osmocom/core/utils.h>
+#include <osmocom/core/timer.h>
+
+struct timer_remaining_testcase {
+ struct timeval now;
+ struct timeval timeout;
+ int expect_rc;
+ struct timeval expect_diff;
+};
+
+struct timer_remaining_testcase timer_remaining_data[] = {
+ {
+ .now = { .tv_sec = 1000000, .tv_usec = 0 },
+ .timeout = { .tv_sec = 1000123, .tv_usec = 1 },
+ .expect_rc = 0,
+ .expect_diff = { .tv_sec = 123, .tv_usec = 1 },
+ },
+ {
+ .now = { .tv_sec = 1000000, .tv_usec = 0 },
+ .timeout = { .tv_sec = 1000000, .tv_usec = 1 },
+ .expect_rc = 0,
+ .expect_diff = { .tv_sec = 0, .tv_usec = 1 },
+ },
+ {
+ .now = { .tv_sec = 1000000, .tv_usec = 1 },
+ .timeout = { .tv_sec = 1000000, .tv_usec = 0 },
+ .expect_rc = -1,
+ .expect_diff = { .tv_sec = 0 - 1, .tv_usec = 999999 },
+ },
+ {
+ .now = { .tv_sec = 1000001, .tv_usec = 1 },
+ .timeout = { .tv_sec = 1000000, .tv_usec = 0 },
+ .expect_rc = -1,
+ .expect_diff = { .tv_sec = -1 - 1, .tv_usec = 999999 },
+ },
+ {
+ .now = { .tv_sec = 1000123, .tv_usec = 1 },
+ .timeout = { .tv_sec = 1000000, .tv_usec = 0 },
+ .expect_rc = -1,
+ .expect_diff = { .tv_sec = -123 - 1, .tv_usec = 999999 },
+ },
+};
+
+void test_timer_remaining()
+{
+ int i;
+ bool all_ok = true;
+ printf("\n--- start: %s\n", __func__);
+
+ for (i = 0; i < ARRAY_SIZE(timer_remaining_data); i++) {
+ struct timer_remaining_testcase *tc = &timer_remaining_data[i];
+ struct osmo_timer_list t = {
+ .timeout = tc->timeout,
+ .active = 1,
+ };
+ struct timeval diff;
+ int rc;
+ bool ok = true;
+
+ rc = osmo_timer_remaining(&t, &tc->now, &diff);
+
+ printf("timeout:%ld.%06ld - now:%ld.%06ld = diff:%ld.%06ld; rc=%d\n",
+ t.timeout.tv_sec, t.timeout.tv_usec,
+ tc->now.tv_sec, tc->now.tv_usec,
+ diff.tv_sec, diff.tv_usec,
+ rc);
+
+ if (rc != tc->expect_rc) {
+ printf(" ERROR: expected rc = %d\n", tc->expect_rc);
+ ok = false;
+ }
+
+ if (diff.tv_sec != tc->expect_diff.tv_sec || diff.tv_usec != tc->expect_diff.tv_usec) {
+ printf(" ERROR: expected diff = %ld.%06ld\n",
+ tc->expect_diff.tv_sec, tc->expect_diff.tv_usec);
+ ok = false;
+ }
+
+ if (!ok)
+ all_ok = false;
+ }
+
+ if (!all_ok) {
+ printf("--- FAILURE: %s\n", __func__);
+ exit(EXIT_FAILURE);
+ }
+
+ printf("--- done: %s\n", __func__);
+}
+
+int main(int argc, char *argv[])
+{
+ test_timer_remaining();
+}
diff --git a/tests/timer/timer_misc_test.ok b/tests/timer/timer_misc_test.ok
new file mode 100644
index 00000000..8bd6648e
--- /dev/null
+++ b/tests/timer/timer_misc_test.ok
@@ -0,0 +1 @@
+asdf