diff options
author | Geir Storli <geirst@yahooinc.com> | 2022-03-01 17:04:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-01 17:04:36 +0100 |
commit | 357f1577db6cced85770bc32d73eded8685c27ff (patch) | |
tree | 31db3cd4b8d9971e41f06973624c59204812fe07 /vespalib | |
parent | 945254170abf5d9e0c4f3b140e8f6384725e7f21 (diff) | |
parent | bdcc901a07111d134337a1812831a69722f66b2c (diff) |
Merge pull request #21478 from vespa-engine/toregge/convert-generation-handler-tests-to-gtest
Convert generation handler tests to gtest.
Diffstat (limited to 'vespalib')
4 files changed, 126 insertions, 109 deletions
diff --git a/vespalib/src/tests/util/generationhandler/CMakeLists.txt b/vespalib/src/tests/util/generationhandler/CMakeLists.txt index 677d5caa0e6..fdf54c59854 100644 --- a/vespalib/src/tests/util/generationhandler/CMakeLists.txt +++ b/vespalib/src/tests/util/generationhandler/CMakeLists.txt @@ -4,5 +4,6 @@ vespa_add_executable(vespalib_generationhandler_test_app TEST generationhandler_test.cpp DEPENDS vespalib + GTest::GTest ) vespa_add_test(NAME vespalib_generationhandler_test_app COMMAND vespalib_generationhandler_test_app) diff --git a/vespalib/src/tests/util/generationhandler/generationhandler_test.cpp b/vespalib/src/tests/util/generationhandler/generationhandler_test.cpp index f269fe729fa..00da752a749 100644 --- a/vespalib/src/tests/util/generationhandler/generationhandler_test.cpp +++ b/vespalib/src/tests/util/generationhandler/generationhandler_test.cpp @@ -1,157 +1,137 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -#include <vespa/vespalib/testkit/testapp.h> +#include <vespa/vespalib/gtest/gtest.h> #include <vespa/vespalib/util/generationhandler.h> #include <deque> namespace vespalib { -typedef GenerationHandler::Guard GenGuard; +using GenGuard = GenerationHandler::Guard; -class Test : public vespalib::TestApp { -private: - void requireThatGenerationCanBeIncreased(); - void requireThatReadersCanTakeGuards(); - void requireThatGuardsCanBeCopied(); - void requireThatTheFirstUsedGenerationIsCorrect(); - void requireThatGenerationCanGrowLarge(); -public: - int Main() override; +class GenerationHandlerTest : public ::testing::Test { +protected: + GenerationHandler gh; + GenerationHandlerTest(); + ~GenerationHandlerTest() override; }; -void -Test::requireThatGenerationCanBeIncreased() +GenerationHandlerTest::GenerationHandlerTest() + : ::testing::Test(), + gh() { - GenerationHandler gh; - EXPECT_EQUAL(0u, gh.getCurrentGeneration()); - EXPECT_EQUAL(0u, gh.getFirstUsedGeneration()); +} + +GenerationHandlerTest::~GenerationHandlerTest() = default; + +TEST_F(GenerationHandlerTest, require_that_generation_can_be_increased) +{ + EXPECT_EQ(0u, gh.getCurrentGeneration()); + EXPECT_EQ(0u, gh.getFirstUsedGeneration()); gh.incGeneration(); - EXPECT_EQUAL(1u, gh.getCurrentGeneration()); - EXPECT_EQUAL(1u, gh.getFirstUsedGeneration()); + EXPECT_EQ(1u, gh.getCurrentGeneration()); + EXPECT_EQ(1u, gh.getFirstUsedGeneration()); } -void -Test::requireThatReadersCanTakeGuards() +TEST_F(GenerationHandlerTest, require_that_readers_can_take_guards) { - GenerationHandler gh; - EXPECT_EQUAL(0u, gh.getGenerationRefCount(0)); + EXPECT_EQ(0u, gh.getGenerationRefCount(0)); { GenGuard g1 = gh.takeGuard(); - EXPECT_EQUAL(1u, gh.getGenerationRefCount(0)); + EXPECT_EQ(1u, gh.getGenerationRefCount(0)); { GenGuard g2 = gh.takeGuard(); - EXPECT_EQUAL(2u, gh.getGenerationRefCount(0)); + EXPECT_EQ(2u, gh.getGenerationRefCount(0)); gh.incGeneration(); { GenGuard g3 = gh.takeGuard(); - EXPECT_EQUAL(2u, gh.getGenerationRefCount(0)); - EXPECT_EQUAL(1u, gh.getGenerationRefCount(1)); - EXPECT_EQUAL(3u, gh.getGenerationRefCount()); + EXPECT_EQ(2u, gh.getGenerationRefCount(0)); + EXPECT_EQ(1u, gh.getGenerationRefCount(1)); + EXPECT_EQ(3u, gh.getGenerationRefCount()); } - EXPECT_EQUAL(2u, gh.getGenerationRefCount(0)); - EXPECT_EQUAL(0u, gh.getGenerationRefCount(1)); + EXPECT_EQ(2u, gh.getGenerationRefCount(0)); + EXPECT_EQ(0u, gh.getGenerationRefCount(1)); gh.incGeneration(); { GenGuard g3 = gh.takeGuard(); - EXPECT_EQUAL(2u, gh.getGenerationRefCount(0)); - EXPECT_EQUAL(0u, gh.getGenerationRefCount(1)); - EXPECT_EQUAL(1u, gh.getGenerationRefCount(2)); + EXPECT_EQ(2u, gh.getGenerationRefCount(0)); + EXPECT_EQ(0u, gh.getGenerationRefCount(1)); + EXPECT_EQ(1u, gh.getGenerationRefCount(2)); } - EXPECT_EQUAL(2u, gh.getGenerationRefCount(0)); - EXPECT_EQUAL(0u, gh.getGenerationRefCount(1)); - EXPECT_EQUAL(0u, gh.getGenerationRefCount(2)); + EXPECT_EQ(2u, gh.getGenerationRefCount(0)); + EXPECT_EQ(0u, gh.getGenerationRefCount(1)); + EXPECT_EQ(0u, gh.getGenerationRefCount(2)); } - EXPECT_EQUAL(1u, gh.getGenerationRefCount(0)); - EXPECT_EQUAL(0u, gh.getGenerationRefCount(1)); - EXPECT_EQUAL(0u, gh.getGenerationRefCount(2)); + EXPECT_EQ(1u, gh.getGenerationRefCount(0)); + EXPECT_EQ(0u, gh.getGenerationRefCount(1)); + EXPECT_EQ(0u, gh.getGenerationRefCount(2)); } - EXPECT_EQUAL(0u, gh.getGenerationRefCount(0)); - EXPECT_EQUAL(0u, gh.getGenerationRefCount(1)); - EXPECT_EQUAL(0u, gh.getGenerationRefCount(2)); + EXPECT_EQ(0u, gh.getGenerationRefCount(0)); + EXPECT_EQ(0u, gh.getGenerationRefCount(1)); + EXPECT_EQ(0u, gh.getGenerationRefCount(2)); } -void -Test::requireThatGuardsCanBeCopied() +TEST_F(GenerationHandlerTest, require_that_guards_can_be_copied) { - GenerationHandler gh; GenGuard g1 = gh.takeGuard(); - EXPECT_EQUAL(1u, gh.getGenerationRefCount(0)); + EXPECT_EQ(1u, gh.getGenerationRefCount(0)); GenGuard g2(g1); - EXPECT_EQUAL(2u, gh.getGenerationRefCount(0)); + EXPECT_EQ(2u, gh.getGenerationRefCount(0)); gh.incGeneration(); GenGuard g3 = gh.takeGuard(); - EXPECT_EQUAL(2u, gh.getGenerationRefCount(0)); - EXPECT_EQUAL(1u, gh.getGenerationRefCount(1)); + EXPECT_EQ(2u, gh.getGenerationRefCount(0)); + EXPECT_EQ(1u, gh.getGenerationRefCount(1)); g3 = g2; - EXPECT_EQUAL(3u, gh.getGenerationRefCount(0)); - EXPECT_EQUAL(0u, gh.getGenerationRefCount(1)); + EXPECT_EQ(3u, gh.getGenerationRefCount(0)); + EXPECT_EQ(0u, gh.getGenerationRefCount(1)); } -void -Test::requireThatTheFirstUsedGenerationIsCorrect() +TEST_F(GenerationHandlerTest, require_that_the_first_used_generation_is_correct) { - GenerationHandler gh; - EXPECT_EQUAL(0u, gh.getFirstUsedGeneration()); + EXPECT_EQ(0u, gh.getFirstUsedGeneration()); gh.incGeneration(); - EXPECT_EQUAL(1u, gh.getFirstUsedGeneration()); + EXPECT_EQ(1u, gh.getFirstUsedGeneration()); { GenGuard g1 = gh.takeGuard(); gh.incGeneration(); - EXPECT_EQUAL(1u, gh.getGenerationRefCount()); - EXPECT_EQUAL(1u, gh.getFirstUsedGeneration()); + EXPECT_EQ(1u, gh.getGenerationRefCount()); + EXPECT_EQ(1u, gh.getFirstUsedGeneration()); } - EXPECT_EQUAL(1u, gh.getFirstUsedGeneration()); + EXPECT_EQ(1u, gh.getFirstUsedGeneration()); gh.updateFirstUsedGeneration(); // Only writer should call this - EXPECT_EQUAL(0u, gh.getGenerationRefCount()); - EXPECT_EQUAL(2u, gh.getFirstUsedGeneration()); + EXPECT_EQ(0u, gh.getGenerationRefCount()); + EXPECT_EQ(2u, gh.getFirstUsedGeneration()); { GenGuard g1 = gh.takeGuard(); gh.incGeneration(); gh.incGeneration(); - EXPECT_EQUAL(1u, gh.getGenerationRefCount()); - EXPECT_EQUAL(2u, gh.getFirstUsedGeneration()); + EXPECT_EQ(1u, gh.getGenerationRefCount()); + EXPECT_EQ(2u, gh.getFirstUsedGeneration()); { GenGuard g2 = gh.takeGuard(); - EXPECT_EQUAL(2u, gh.getFirstUsedGeneration()); + EXPECT_EQ(2u, gh.getFirstUsedGeneration()); } } - EXPECT_EQUAL(2u, gh.getFirstUsedGeneration()); + EXPECT_EQ(2u, gh.getFirstUsedGeneration()); gh.updateFirstUsedGeneration(); // Only writer should call this - EXPECT_EQUAL(0u, gh.getGenerationRefCount()); - EXPECT_EQUAL(4u, gh.getFirstUsedGeneration()); + EXPECT_EQ(0u, gh.getGenerationRefCount()); + EXPECT_EQ(4u, gh.getFirstUsedGeneration()); } -void -Test::requireThatGenerationCanGrowLarge() +TEST_F(GenerationHandlerTest, require_that_generation_can_grow_large) { - GenerationHandler gh; std::deque<GenGuard> guards; for (size_t i = 0; i < 10000; ++i) { - EXPECT_EQUAL(i, gh.getCurrentGeneration()); + EXPECT_EQ(i, gh.getCurrentGeneration()); guards.push_back(gh.takeGuard()); // take guard on current generation if (i >= 128) { - EXPECT_EQUAL(i - 128, gh.getFirstUsedGeneration()); + EXPECT_EQ(i - 128, gh.getFirstUsedGeneration()); guards.pop_front(); - EXPECT_EQUAL(128u, gh.getGenerationRefCount()); + EXPECT_EQ(128u, gh.getGenerationRefCount()); } gh.incGeneration(); } } -int -Test::Main() -{ - TEST_INIT("generationhandler_test"); - - TEST_DO(requireThatGenerationCanBeIncreased()); - TEST_DO(requireThatReadersCanTakeGuards()); - TEST_DO(requireThatGuardsCanBeCopied()); - TEST_DO(requireThatTheFirstUsedGenerationIsCorrect()); - TEST_DO(requireThatGenerationCanGrowLarge()); - - TEST_DONE(); -} - } -TEST_APPHOOK(vespalib::Test); +GTEST_MAIN_RUN_ALL_TESTS() diff --git a/vespalib/src/tests/util/generationhandler_stress/CMakeLists.txt b/vespalib/src/tests/util/generationhandler_stress/CMakeLists.txt index 569db489e3c..7e5a5af79b5 100644 --- a/vespalib/src/tests/util/generationhandler_stress/CMakeLists.txt +++ b/vespalib/src/tests/util/generationhandler_stress/CMakeLists.txt @@ -4,5 +4,6 @@ vespa_add_executable(vespalib_generation_handler_stress_test_app generation_handler_stress_test.cpp DEPENDS vespalib + GTest::GTest ) -vespa_add_test(NAME vespalib_generation_handler_stress_test_app COMMAND vespalib_generation_handler_stress_test_app BENCHMARK) +vespa_add_test(NAME vespalib_generation_handler_stress_test_app COMMAND vespalib_generation_handler_stress_test_app --smoke-test) diff --git a/vespalib/src/tests/util/generationhandler_stress/generation_handler_stress_test.cpp b/vespalib/src/tests/util/generationhandler_stress/generation_handler_stress_test.cpp index fa2c525b518..0689909da09 100644 --- a/vespalib/src/tests/util/generationhandler_stress/generation_handler_stress_test.cpp +++ b/vespalib/src/tests/util/generationhandler_stress/generation_handler_stress_test.cpp @@ -1,7 +1,7 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include <vespa/log/log.h> LOG_SETUP("generation_handler_stress_test"); -#include <vespa/vespalib/testkit/testapp.h> +#include <vespa/vespalib/gtest/gtest.h> #include <vespa/vespalib/util/generationhandler.h> #include <vespa/vespalib/util/threadstackexecutor.h> #include <vespa/vespalib/util/size_literals.h> @@ -10,6 +10,12 @@ using vespalib::Executor; using vespalib::GenerationHandler; using vespalib::ThreadStackExecutor; +namespace { + +bool smoke_test = false; +const vespalib::string smoke_test_option = "--smoke-test"; + +} struct WorkContext { @@ -21,26 +27,28 @@ struct WorkContext } }; -struct Fixture { +class Fixture : public ::testing::Test { +protected: GenerationHandler _generationHandler; uint32_t _readThreads; ThreadStackExecutor _writer; // 1 write thread - ThreadStackExecutor _readers; // multiple reader threads + std::unique_ptr<ThreadStackExecutor> _readers; // multiple reader threads std::atomic<long> _readSeed; std::atomic<long> _doneWriteWork; std::atomic<long> _doneReadWork; std::atomic<int> _stopRead; bool _reportWork; - Fixture(uint32_t readThreads = 1); - + Fixture(); ~Fixture(); - void readWork(const WorkContext &context); - void writeWork(uint32_t cnt, WorkContext &context); + void set_read_threads(uint32_t read_threads); + uint32_t getReadThreads() const { return _readThreads; } void stressTest(uint32_t writeCnt); - +public: + void readWork(const WorkContext &context); + void writeWork(uint32_t cnt, WorkContext &context); private: Fixture(const Fixture &index) = delete; Fixture(Fixture &&index) = delete; @@ -49,23 +57,27 @@ private: }; -Fixture::Fixture(uint32_t readThreads) - : _generationHandler(), - _readThreads(readThreads), +Fixture::Fixture() + : ::testing::Test(), + _generationHandler(), + _readThreads(1), _writer(1, 128_Ki), - _readers(readThreads, 128_Ki), + _readers(), _doneWriteWork(0), _doneReadWork(0), _stopRead(0), _reportWork(false) { + set_read_threads(1); } Fixture::~Fixture() { - _readers.sync(); - _readers.shutdown(); + if (_readers) { + _readers->sync(); + _readers->shutdown(); + } _writer.sync(); _writer.shutdown(); if (_reportWork) { @@ -75,6 +87,16 @@ Fixture::~Fixture() } } +void +Fixture::set_read_threads(uint32_t read_threads) +{ + if (_readers) { + _readers->sync(); + _readers->shutdown(); + } + _readThreads = read_threads; + _readers = std::make_unique<ThreadStackExecutor>(read_threads, 128_Ki); +} void Fixture::readWork(const WorkContext &context) @@ -85,7 +107,7 @@ Fixture::readWork(const WorkContext &context) for (i = 0; i < cnt && _stopRead.load() == 0; ++i) { auto guard = _generationHandler.takeGuard(); auto generation = context._generation.load(std::memory_order_relaxed); - EXPECT_GREATER_EQUAL(generation, guard.getGeneration()); + EXPECT_GE(generation, guard.getGeneration()); } _doneReadWork += i; LOG(info, "done %u read work", i); @@ -150,19 +172,32 @@ Fixture::stressTest(uint32_t writeCnt) auto context = std::make_shared<WorkContext>(); _writer.execute(std::make_unique<WriteWorkTask>(*this, writeCnt, context)); for (uint32_t i = 0; i < readThreads; ++i) { - _readers.execute(std::make_unique<ReadWorkTask>(*this, context)); + _readers->execute(std::make_unique<ReadWorkTask>(*this, context)); } + _writer.sync(); + _readers->sync(); } +using GenerationHandlerStressTest = Fixture; -TEST_F("stress test, 2 readers", Fixture(2)) +TEST_F(GenerationHandlerStressTest, stress_test_2_readers) { - f.stressTest(1000000); + set_read_threads(2); + stressTest(smoke_test ? 10000 : 1000000); } -TEST_F("stress test, 4 readers", Fixture(4)) +TEST_F(GenerationHandlerStressTest, stress_test_4_readers) { - f.stressTest(1000000); + set_read_threads(4); + stressTest(smoke_test ? 10000 : 1000000); } -TEST_MAIN() { TEST_RUN_ALL(); } +int main(int argc, char **argv) { + if (argc > 1 && argv[1] == smoke_test_option) { + smoke_test = true; + ++argv; + --argc; + } + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} |