aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests
diff options
context:
space:
mode:
authorHåvard Pettersen <havardpe@yahooinc.com>2023-02-14 10:15:12 +0000
committerHåvard Pettersen <havardpe@yahooinc.com>2023-02-14 15:40:46 +0000
commit98e408c667e3dcda3dc177594ffa4c4d0d99a33e (patch)
tree8086fedd3c4c11ac51fa86f12f68cfe61095ee55 /vespalib/src/tests
parent251bd6d0b21aa2ddac8ef82338f40d37ffc4990f (diff)
stop using fastos thread more places
- also stop using std::jthread - remove Active and Joinable interfaces - remove stop, stopped and slumber - remove currentThread - make start function static - override start for Runnable w/init or custom function - explicit stop/slumber where needed
Diffstat (limited to 'vespalib/src/tests')
-rw-r--r--vespalib/src/tests/simple_thread_bundle/simple_thread_bundle_test.cpp1
-rw-r--r--vespalib/src/tests/singleexecutor/singleexecutor_test.cpp1
-rw-r--r--vespalib/src/tests/thread/thread_test.cpp48
3 files changed, 22 insertions, 28 deletions
diff --git a/vespalib/src/tests/simple_thread_bundle/simple_thread_bundle_test.cpp b/vespalib/src/tests/simple_thread_bundle/simple_thread_bundle_test.cpp
index 59aeddfe8ca..e451f1e033d 100644
--- a/vespalib/src/tests/simple_thread_bundle/simple_thread_bundle_test.cpp
+++ b/vespalib/src/tests/simple_thread_bundle/simple_thread_bundle_test.cpp
@@ -4,6 +4,7 @@
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/vespalib/util/box.h>
#include <vespa/vespalib/util/small_vector.h>
+#include <vespa/vespalib/util/gate.h>
#include <thread>
#include <forward_list>
diff --git a/vespalib/src/tests/singleexecutor/singleexecutor_test.cpp b/vespalib/src/tests/singleexecutor/singleexecutor_test.cpp
index 56352ff3c0d..3b1d244eb13 100644
--- a/vespalib/src/tests/singleexecutor/singleexecutor_test.cpp
+++ b/vespalib/src/tests/singleexecutor/singleexecutor_test.cpp
@@ -5,6 +5,7 @@
#include <vespa/vespalib/util/singleexecutor.h>
#include <vespa/vespalib/util/lambdatask.h>
#include <vespa/vespalib/util/alloc.h>
+#include <vespa/vespalib/util/gate.h>
#include <atomic>
using namespace vespalib;
diff --git a/vespalib/src/tests/thread/thread_test.cpp b/vespalib/src/tests/thread/thread_test.cpp
index af1fb626462..f7e8753fd86 100644
--- a/vespalib/src/tests/thread/thread_test.cpp
+++ b/vespalib/src/tests/thread/thread_test.cpp
@@ -2,56 +2,48 @@
#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/vespalib/util/thread.h>
-#include <thread>
using namespace vespalib;
VESPA_THREAD_STACK_TAG(test_agent_thread);
struct Agent : public Runnable {
- bool started;
- int loopCnt;
- Agent() : started(false), loopCnt(0) {}
+ bool was_run;
+ Agent() : was_run(false) {}
void run() override {
- started = true;
- Thread &thread = Thread::currentThread();
- while (thread.slumber(60.0)) {
- ++loopCnt;
- }
+ was_run = true;
}
};
-TEST("thread never started") {
+void my_fun(bool *was_run) {
+ *was_run = true;
+}
+
+TEST("run vespalib::Runnable with init function") {
Agent agent;
{
- Thread thread(agent, test_agent_thread);
+ auto thread = Thread::start(agent, test_agent_thread);
}
- EXPECT_TRUE(!agent.started);
- EXPECT_EQUAL(0, agent.loopCnt);
+ EXPECT_TRUE(agent.was_run);
}
-TEST("normal operation") {
- Agent agent;
+TEST("run custom function") {
+ bool was_run = false;
{
- Thread thread(agent, test_agent_thread);
- thread.start();
- std::this_thread::sleep_for(20ms);
- thread.stop().join();
+ auto thread = Thread::start(my_fun, &was_run);
}
- EXPECT_TRUE(agent.started);
- EXPECT_EQUAL(0, agent.loopCnt);
+ EXPECT_TRUE(was_run);
}
-TEST("stop before start") {
- Agent agent;
+TEST("join multiple times (including destructor)") {
+ bool was_run = false;
{
- Thread thread(agent, test_agent_thread);
- thread.stop();
- thread.start();
+ auto thread = Thread::start(my_fun, &was_run);
+ thread.join();
+ thread.join();
thread.join();
}
- EXPECT_TRUE(!agent.started);
- EXPECT_EQUAL(0, agent.loopCnt);
+ EXPECT_TRUE(was_run);
}
TEST_MAIN() { TEST_RUN_ALL(); }