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 /searchcore/src/tests/proton/initializer |
Publish
Diffstat (limited to 'searchcore/src/tests/proton/initializer')
5 files changed, 152 insertions, 0 deletions
diff --git a/searchcore/src/tests/proton/initializer/.gitignore b/searchcore/src/tests/proton/initializer/.gitignore new file mode 100644 index 00000000000..486dbb0b11e --- /dev/null +++ b/searchcore/src/tests/proton/initializer/.gitignore @@ -0,0 +1 @@ +searchcore_task_runner_test_app diff --git a/searchcore/src/tests/proton/initializer/CMakeLists.txt b/searchcore/src/tests/proton/initializer/CMakeLists.txt new file mode 100644 index 00000000000..0f20324cae3 --- /dev/null +++ b/searchcore/src/tests/proton/initializer/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(searchcore_task_runner_test_app + SOURCES + task_runner_test.cpp + DEPENDS + searchcore_initializer +) +vespa_add_test(NAME searchcore_task_runner_test_app COMMAND searchcore_task_runner_test_app) diff --git a/searchcore/src/tests/proton/initializer/DESC b/searchcore/src/tests/proton/initializer/DESC new file mode 100644 index 00000000000..be2743a181e --- /dev/null +++ b/searchcore/src/tests/proton/initializer/DESC @@ -0,0 +1 @@ +TaskRunner test. Take a look at task_runner_test.cpp for details. diff --git a/searchcore/src/tests/proton/initializer/FILES b/searchcore/src/tests/proton/initializer/FILES new file mode 100644 index 00000000000..bbbbe1c2d86 --- /dev/null +++ b/searchcore/src/tests/proton/initializer/FILES @@ -0,0 +1 @@ +task_runner_test.cpp diff --git a/searchcore/src/tests/proton/initializer/task_runner_test.cpp b/searchcore/src/tests/proton/initializer/task_runner_test.cpp new file mode 100644 index 00000000000..afa807fd0e6 --- /dev/null +++ b/searchcore/src/tests/proton/initializer/task_runner_test.cpp @@ -0,0 +1,141 @@ +// 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/log/log.h> +LOG_SETUP("task_runner_test"); +#include <vespa/vespalib/testkit/testapp.h> +#include <vespa/searchcore/proton/initializer/initializer_task.h> +#include <vespa/searchcore/proton/initializer/task_runner.h> +#include <vespa/vespalib/util/threadstackexecutor.h> +#include <vespa/vespalib/stllike/string.h> +#include <mutex> + +using proton::initializer::InitializerTask; +using proton::initializer::TaskRunner; + +struct TestLog +{ + std::mutex _lock; + vespalib::string _log; + using UP = std::unique_ptr<TestLog>; + + TestLog() + : _lock(), + _log() + { + } + + void append(vespalib::string str) { + std::lock_guard<std::mutex> guard(_lock); + _log += str; + } + + vespalib::string result() const { return _log; } +}; + +class NamedTask : public InitializerTask +{ +protected: + vespalib::string _name; + TestLog &_log; +public: + NamedTask(const vespalib::string &name, TestLog &log) + : _name(name), + _log(log) + { + } + + virtual void run() { _log.append(_name); } +}; + + +struct TestJob { + TestLog::UP _log; + InitializerTask::SP _root; + + TestJob(TestLog::UP log, InitializerTask::SP root) + : _log(std::move(log)), + _root(std::move(root)) + { + } + + static TestJob setupCDependsOnAandB() + { + TestLog::UP log = std::make_unique<TestLog>(); + InitializerTask::SP A(std::make_shared<NamedTask>("A", *log)); + InitializerTask::SP B(std::make_shared<NamedTask>("B", *log)); + InitializerTask::SP C(std::make_shared<NamedTask>("C", *log)); + C->addDependency(A); + C->addDependency(B); + return TestJob(std::move(log), std::move(C)); + } + + static TestJob setupDiamond() + { + TestLog::UP log = std::make_unique<TestLog>(); + InitializerTask::SP A(std::make_shared<NamedTask>("A", *log)); + InitializerTask::SP B(std::make_shared<NamedTask>("B", *log)); + InitializerTask::SP C(std::make_shared<NamedTask>("C", *log)); + InitializerTask::SP D(std::make_shared<NamedTask>("D", *log)); + C->addDependency(A); + C->addDependency(B); + A->addDependency(D); + B->addDependency(D); + return TestJob(std::move(log), std::move(C)); + } +}; + + +struct Fixture +{ + vespalib::ThreadStackExecutor _executor; + TaskRunner _taskRunner; + + Fixture(uint32_t numThreads = 1) + : _executor(numThreads, 128 * 1024), + _taskRunner(_executor) + { + } + + void run(const InitializerTask::SP &task) { _taskRunner.runTask(task); } +}; + + +TEST_F("1 thread, 2 dependees, 1 depender", Fixture(1)) +{ + TestJob job = TestJob::setupCDependsOnAandB(); + f.run(job._root); + EXPECT_EQUAL("ABC", job._log->result()); +} + +TEST_F("1 thread, dag graph", Fixture(1)) +{ + for (int iter = 0; iter < 1000; ++iter) { + TestJob job = TestJob::setupDiamond(); + f.run(job._root); + EXPECT_EQUAL("DABC", job._log->result()); + } +} + +TEST_F("multiple threads, dag graph", Fixture(10)) +{ + int dabc_count = 0; + int dbac_count = 0; + for (int iter = 0; iter < 1000; ++iter) { + TestJob job = TestJob::setupDiamond(); + f.run(job._root); + vespalib::string result = job._log->result(); + EXPECT_TRUE("DABC" == result || "DBAC" == result); + if ("DABC" == result) { + ++dabc_count; + } + if ("DBAC" == result) { + ++dbac_count; + } + } + LOG(info, "dabc=%d, dbac=%d", dabc_count, dbac_count); +} + +TEST_MAIN() +{ + TEST_RUN_ALL(); +} |