mirror of
https://github.com/XRPLF/clio.git
synced 2025-11-04 20:05:51 +00:00
Fixes #1590. Please review and commit clang-tidy fixes. Co-authored-by: kuznetsss <15742918+kuznetsss@users.noreply.github.com>
82 lines
2.8 KiB
C++
82 lines
2.8 KiB
C++
//------------------------------------------------------------------------------
|
|
/*
|
|
This file is part of clio: https://github.com/XRPLF/clio
|
|
Copyright (c) 2024, the clio developers.
|
|
|
|
Permission to use, copy, modify, and distribute this software for any
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
*/
|
|
//==============================================================================
|
|
|
|
#include "util/AsioContextTestFixture.hpp"
|
|
#include "util/Repeat.hpp"
|
|
#include "util/WithTimeout.hpp"
|
|
|
|
#include <boost/asio/executor_work_guard.hpp>
|
|
#include <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <chrono>
|
|
#include <functional>
|
|
#include <ranges>
|
|
#include <thread>
|
|
#include <utility>
|
|
|
|
using namespace util;
|
|
using testing::AtLeast;
|
|
|
|
struct RepeatTests : SyncAsioContextTest {
|
|
Repeat repeat{ctx};
|
|
testing::StrictMock<testing::MockFunction<void()>> handlerMock;
|
|
|
|
void
|
|
withRunningContext(std::function<void()> func)
|
|
{
|
|
tests::common::util::withTimeout(std::chrono::seconds{1000}, [this, func = std::move(func)]() {
|
|
auto workGuard = boost::asio::make_work_guard(ctx);
|
|
std::thread thread{[this]() { ctx.run(); }};
|
|
func();
|
|
workGuard.reset();
|
|
thread.join();
|
|
});
|
|
}
|
|
};
|
|
|
|
TEST_F(RepeatTests, CallsHandler)
|
|
{
|
|
repeat.start(std::chrono::milliseconds{1}, handlerMock.AsStdFunction());
|
|
EXPECT_CALL(handlerMock, Call).Times(AtLeast(10));
|
|
runContextFor(std::chrono::milliseconds{20});
|
|
}
|
|
|
|
TEST_F(RepeatTests, StopsOnStop)
|
|
{
|
|
withRunningContext([this]() {
|
|
repeat.start(std::chrono::milliseconds{1}, handlerMock.AsStdFunction());
|
|
EXPECT_CALL(handlerMock, Call).Times(AtLeast(1));
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{10});
|
|
repeat.stop();
|
|
});
|
|
}
|
|
|
|
TEST_F(RepeatTests, RunsAfterStop)
|
|
{
|
|
withRunningContext([this]() {
|
|
for ([[maybe_unused]] auto _ : std::ranges::iota_view(0, 2)) {
|
|
repeat.start(std::chrono::milliseconds{1}, handlerMock.AsStdFunction());
|
|
EXPECT_CALL(handlerMock, Call).Times(AtLeast(1));
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{10});
|
|
repeat.stop();
|
|
}
|
|
});
|
|
}
|