summaryrefslogtreecommitdiffstats
path: root/vespalib
diff options
context:
space:
mode:
authorArne H Juul <arnej@yahoo-inc.com>2016-09-09 15:30:47 +0200
committerArne H Juul <arnej@yahoo-inc.com>2016-09-09 15:30:47 +0200
commit55913e864baf3fd47051a5ae29ff7611392342d6 (patch)
treecc17f7de268f3766fcf928b657ea0eb05bbea93b /vespalib
parentf7b82574ed5c5d3403c257e87fe0cb4e46be92dd (diff)
add unit test
Diffstat (limited to 'vespalib')
-rw-r--r--vespalib/CMakeLists.txt1
-rw-r--r--vespalib/src/tests/time/CMakeLists.txt8
-rw-r--r--vespalib/src/tests/time/time_box_test.cpp36
-rw-r--r--vespalib/src/vespa/vespalib/time/time_box.cpp19
4 files changed, 45 insertions, 19 deletions
diff --git a/vespalib/CMakeLists.txt b/vespalib/CMakeLists.txt
index 92871fe1fca..2114afc2266 100644
--- a/vespalib/CMakeLists.txt
+++ b/vespalib/CMakeLists.txt
@@ -103,6 +103,7 @@ vespa_define_module(
src/tests/text/stringtokenizer
src/tests/text/utf8
src/tests/thread
+ src/tests/time
src/tests/time_tracker
src/tests/trace
src/tests/traits
diff --git a/vespalib/src/tests/time/CMakeLists.txt b/vespalib/src/tests/time/CMakeLists.txt
new file mode 100644
index 00000000000..70c758d6222
--- /dev/null
+++ b/vespalib/src/tests/time/CMakeLists.txt
@@ -0,0 +1,8 @@
+# Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+vespa_add_executable(vespalib_time_box_test_app TEST
+ SOURCES
+ time_box_test.cpp
+ DEPENDS
+ vespalib
+)
+vespa_add_test(NAME vespalib_time_box_test_app COMMAND vespalib_time_box_test_app)
diff --git a/vespalib/src/tests/time/time_box_test.cpp b/vespalib/src/tests/time/time_box_test.cpp
new file mode 100644
index 00000000000..478d749e15d
--- /dev/null
+++ b/vespalib/src/tests/time/time_box_test.cpp
@@ -0,0 +1,36 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+#include <vespa/vespalib/testkit/test_kit.h>
+#include <vespa/vespalib/time/time_box.h>
+
+TEST("require that long-lived timebox returns falling time left numbers") {
+ vespalib::TimeBox box(3600);
+ double last_timeLeft = box.timeLeft();
+ for (int i = 0; i < 10; i++) {
+ EXPECT_TRUE(box.hasTimeLeft());
+ double timeLeft = box.timeLeft();
+ EXPECT_TRUE(timeLeft <= last_timeLeft);
+ last_timeLeft = timeLeft;
+ FastOS_Thread::Sleep(10);
+ }
+}
+
+TEST("require that short-lived timebox times out") {
+ vespalib::TimeBox box(0.125);
+ FastOS_Thread::Sleep(150);
+ EXPECT_FALSE(box.hasTimeLeft());
+ EXPECT_EQUAL(box.timeLeft(), 0.0);
+}
+
+TEST("require that short-lived timebox always returns at least minimum time") {
+ vespalib::TimeBox box(0.250, 0.125);
+ for (int i = 0; i < 10; i++) {
+ double timeLeft = box.timeLeft();
+ EXPECT_TRUE(timeLeft <= 0.250);
+ EXPECT_TRUE(timeLeft >= 0.125);
+ FastOS_Thread::Sleep(30);
+ }
+ EXPECT_FALSE(box.hasTimeLeft());
+ EXPECT_EQUAL(box.timeLeft(), 0.125);
+}
+
+TEST_MAIN() { TEST_RUN_ALL(); }
diff --git a/vespalib/src/vespa/vespalib/time/time_box.cpp b/vespalib/src/vespa/vespalib/time/time_box.cpp
index aad958e10da..ae9b3cd3499 100644
--- a/vespalib/src/vespa/vespalib/time/time_box.cpp
+++ b/vespalib/src/vespa/vespalib/time/time_box.cpp
@@ -1,21 +1,2 @@
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "time_box.h"
-
-#if 0
-
-#include <time.h>
-#include <unistd.h>
-#include <stdio.h>
-
-int main(int, char **)
-{
- vespalib::TimeBox tb(3.0, 1.25);
- while (tb.hasTimeLeft()) {
- double tl = tb.timeLeft();
- printf("time left: %g seconds\n", tl);
- sleep(1);
- }
- printf("expired, time left: %g seconds\n", tb.timeLeft());
-}
-
-#endif