//------------------------------------------------------------------------------
/*
    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/CallWithTimeout.hpp"
#include "util/Repeat.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::callWithTimeout(std::chrono::seconds{1}, [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)
{
    EXPECT_CALL(handlerMock, Call).Times(testing::AtMost(22));
    repeat.start(std::chrono::milliseconds{1}, handlerMock.AsStdFunction());
    runContextFor(std::chrono::milliseconds{20});
}

TEST_F(RepeatTests, StopsOnStop)
{
    withRunningContext([this]() {
        EXPECT_CALL(handlerMock, Call).Times(AtLeast(1));
        repeat.start(std::chrono::milliseconds{1}, handlerMock.AsStdFunction());
        std::this_thread::sleep_for(std::chrono::milliseconds{10});
        repeat.stop();
    });
}

TEST_F(RepeatTests, RunsAfterStop)
{
    withRunningContext([this]() {
        for ([[maybe_unused]] auto i : std::ranges::iota_view(0, 2)) {
            EXPECT_CALL(handlerMock, Call).Times(AtLeast(1));
            repeat.start(std::chrono::milliseconds{1}, handlerMock.AsStdFunction());
            std::this_thread::sleep_for(std::chrono::milliseconds{10});
            repeat.stop();
        }
    });
}
