diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /vespalib/src/tests/thread |
Publish
Diffstat (limited to 'vespalib/src/tests/thread')
-rw-r--r-- | vespalib/src/tests/thread/.gitignore | 1 | ||||
-rw-r--r-- | vespalib/src/tests/thread/CMakeLists.txt | 8 | ||||
-rw-r--r-- | vespalib/src/tests/thread/FILES | 1 | ||||
-rw-r--r-- | vespalib/src/tests/thread/thread_test.cpp | 54 |
4 files changed, 64 insertions, 0 deletions
diff --git a/vespalib/src/tests/thread/.gitignore b/vespalib/src/tests/thread/.gitignore new file mode 100644 index 00000000000..0853d8fb1da --- /dev/null +++ b/vespalib/src/tests/thread/.gitignore @@ -0,0 +1 @@ +vespalib_thread_test_app diff --git a/vespalib/src/tests/thread/CMakeLists.txt b/vespalib/src/tests/thread/CMakeLists.txt new file mode 100644 index 00000000000..eae0fde2646 --- /dev/null +++ b/vespalib/src/tests/thread/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_thread_test_app + SOURCES + thread_test.cpp + DEPENDS + vespalib +) +vespa_add_test(NAME vespalib_thread_test_app COMMAND vespalib_thread_test_app) diff --git a/vespalib/src/tests/thread/FILES b/vespalib/src/tests/thread/FILES new file mode 100644 index 00000000000..9c62d0111c5 --- /dev/null +++ b/vespalib/src/tests/thread/FILES @@ -0,0 +1 @@ +thread_test.cpp diff --git a/vespalib/src/tests/thread/thread_test.cpp b/vespalib/src/tests/thread/thread_test.cpp new file mode 100644 index 00000000000..76c6ccdee1d --- /dev/null +++ b/vespalib/src/tests/thread/thread_test.cpp @@ -0,0 +1,54 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#include <vespa/fastos/fastos.h> +#include <vespa/vespalib/testkit/test_kit.h> +#include <vespa/vespalib/util/thread.h> + +using namespace vespalib; + +struct Agent : public Runnable { + bool started; + int loopCnt; + Agent() : started(false), loopCnt(0) {} + virtual void run() { + started = true; + Thread &thread = Thread::currentThread(); + while (thread.slumber(60.0)) { + ++loopCnt; + } + } +}; + +TEST("thread never started") { + Agent agent; + { + Thread thread(agent); + } + EXPECT_TRUE(!agent.started); + EXPECT_EQUAL(0, agent.loopCnt); +} + +TEST("normal operation") { + Agent agent; + { + Thread thread(agent); + thread.start(); + FastOS_Thread::Sleep(20); + thread.stop().join(); + } + EXPECT_TRUE(agent.started); + EXPECT_EQUAL(0, agent.loopCnt); +} + +TEST("stop before start") { + Agent agent; + { + Thread thread(agent); + thread.stop(); + thread.start(); + thread.join(); + } + EXPECT_TRUE(agent.started); + EXPECT_EQUAL(0, agent.loopCnt); +} + +TEST_MAIN() { TEST_RUN_ALL(); } |