mirror of
https://github.com/XRPLF/clio.git
synced 2025-11-10 14:55:50 +00:00
Async framework needed a way to do repeating operations (think simplest cases like AmendmentBlockHandler). This PR implements the **absolute minimum**, barebones repeating operations that - Can't return any values (void) - Do not take any arguments in the user-provided function - Can't be scheduled (i.e. a delay before starting repeating) - Can't be stopped from inside the user block of code (i.e. does not have stop token or anything like that) - Can be stopped through the RepeatedOperation's `abort()` function (but not from the user-provided repeating function)
92 lines
3.0 KiB
C++
92 lines
3.0 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.
|
|
*/
|
|
//==============================================================================
|
|
|
|
#pragma once
|
|
|
|
#include "util/MockOperation.hpp"
|
|
#include "util/MockStopToken.hpp"
|
|
#include "util/MockStrand.hpp"
|
|
#include "util/async/AnyStopToken.hpp"
|
|
#include "util/async/Error.hpp"
|
|
|
|
#include <gmock/gmock.h>
|
|
|
|
#include <any>
|
|
#include <chrono>
|
|
#include <expected>
|
|
#include <functional>
|
|
#include <optional>
|
|
|
|
struct MockExecutionContext {
|
|
template <typename T>
|
|
using ValueType = std::expected<T, util::async::ExecutionError>;
|
|
|
|
using StopSource = MockStopSource;
|
|
using StopToken = MockStopToken;
|
|
using Strand = MockStrand;
|
|
|
|
template <typename T>
|
|
using Operation = MockOperation<T>;
|
|
|
|
template <typename T>
|
|
using StoppableOperation = MockStoppableOperation<T>;
|
|
|
|
template <typename T>
|
|
using ScheduledOperation = MockScheduledOperation<T>;
|
|
|
|
template <typename T>
|
|
using RepeatingOperation = MockRepeatingOperation<T>;
|
|
|
|
MOCK_METHOD(Operation<std::any> const&, execute, (std::function<std::any()>), ());
|
|
MOCK_METHOD(
|
|
Operation<std::any> const&,
|
|
execute,
|
|
(std::function<std::any()>, std::optional<std::chrono::milliseconds>),
|
|
()
|
|
);
|
|
MOCK_METHOD(
|
|
StoppableOperation<std::any> const&,
|
|
execute,
|
|
(std::function<std::any(util::async::AnyStopToken)>, std::optional<std::chrono::milliseconds>),
|
|
()
|
|
);
|
|
MOCK_METHOD(
|
|
ScheduledOperation<std::any> const&,
|
|
scheduleAfter,
|
|
(std::chrono::milliseconds, std::function<std::any(util::async::AnyStopToken)>),
|
|
()
|
|
);
|
|
MOCK_METHOD(
|
|
ScheduledOperation<std::any> const&,
|
|
scheduleAfter,
|
|
(std::chrono::milliseconds, std::function<std::any(util::async::AnyStopToken, bool)>),
|
|
()
|
|
);
|
|
MOCK_METHOD(
|
|
RepeatingOperation<std::any> const&,
|
|
executeRepeatedly,
|
|
(std::chrono::milliseconds, std::function<std::any()>),
|
|
()
|
|
);
|
|
|
|
MOCK_METHOD(MockStrand const&, makeStrand, (), ());
|
|
MOCK_METHOD(void, stop, (), (const));
|
|
MOCK_METHOD(void, join, (), ());
|
|
};
|