diff options
Diffstat (limited to 'vespalib/src/tests/invokeservice/invokeservice_test.cpp')
-rw-r--r-- | vespalib/src/tests/invokeservice/invokeservice_test.cpp | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/vespalib/src/tests/invokeservice/invokeservice_test.cpp b/vespalib/src/tests/invokeservice/invokeservice_test.cpp new file mode 100644 index 00000000000..88a7969e153 --- /dev/null +++ b/vespalib/src/tests/invokeservice/invokeservice_test.cpp @@ -0,0 +1,47 @@ +// Copyright Yahoo. 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/util/invokeserviceimpl.h> + +using namespace vespalib; + +struct InvokeCounter { + InvokeCounter() : _count(0) {} + void inc() noexcept { _count++; } + std::atomic<uint64_t> _count; +}; + +TEST("require that wakeup is called") { + InvokeCounter a; + InvokeServiceImpl service(1ms); + EXPECT_EQUAL(0u, a._count); + auto ra = service.registerInvoke([&a]() noexcept { a.inc(); }); + EXPECT_TRUE(ra); + while (a._count == 0) { + std::this_thread::sleep_for(1ms); + } + ra.reset(); + uint64_t countAtStop = a._count; + std::this_thread::sleep_for(1s); + EXPECT_EQUAL(countAtStop, a._count); +} + +TEST("require that same wakeup can be registered multiple times.") { + InvokeCounter a; + InvokeServiceImpl service(1ms); + EXPECT_EQUAL(0u, a._count); + auto ra1 = service.registerInvoke([&a]() noexcept { a.inc(); }); + EXPECT_TRUE(ra1); + auto ra2 = service.registerInvoke([&a]() noexcept { a.inc(); }); + while (a._count == 0) { + std::this_thread::sleep_for(1ms); + } + ra1.reset(); + uint64_t countAtStop = a._count; + ra2 = service.registerInvoke([&a]() noexcept { a.inc(); }); + EXPECT_TRUE(ra2); + std::this_thread::sleep_for(1s); + EXPECT_LESS(countAtStop, a._count); +} + + +TEST_MAIN() { TEST_RUN_ALL(); } |