mirror of
https://github.com/XRPLF/clio.git
synced 2025-12-06 17:27:58 +00:00
@@ -20,21 +20,21 @@
|
||||
#pragma once
|
||||
|
||||
#include "etl/ETLState.hpp"
|
||||
#include "etlng/ETLServiceInterface.hpp"
|
||||
|
||||
#include <boost/json.hpp>
|
||||
#include <boost/json/object.hpp>
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
|
||||
struct MockETLService {
|
||||
MOCK_METHOD(boost::json::object, getInfo, (), (const));
|
||||
MOCK_METHOD(std::chrono::time_point<std::chrono::system_clock>, getLastPublish, (), (const));
|
||||
MOCK_METHOD(std::uint32_t, lastPublishAgeSeconds, (), (const));
|
||||
MOCK_METHOD(std::uint32_t, lastCloseAgeSeconds, (), (const));
|
||||
MOCK_METHOD(bool, isAmendmentBlocked, (), (const));
|
||||
MOCK_METHOD(bool, isCorruptionDetected, (), (const));
|
||||
MOCK_METHOD(std::optional<etl::ETLState>, getETLState, (), (const));
|
||||
struct MockETLService : etlng::ETLServiceInterface {
|
||||
MOCK_METHOD(void, run, (), (override));
|
||||
MOCK_METHOD(void, stop, (), (override));
|
||||
MOCK_METHOD(boost::json::object, getInfo, (), (const, override));
|
||||
MOCK_METHOD(std::uint32_t, lastCloseAgeSeconds, (), (const, override));
|
||||
MOCK_METHOD(bool, isAmendmentBlocked, (), (const, override));
|
||||
MOCK_METHOD(bool, isCorruptionDetected, (), (const, override));
|
||||
MOCK_METHOD(std::optional<etl::ETLState>, getETLState, (), (const, override));
|
||||
};
|
||||
|
||||
@@ -38,22 +38,6 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
struct MockLoadBalancer {
|
||||
using RawLedgerObjectType = FakeLedgerObject;
|
||||
|
||||
MOCK_METHOD(void, loadInitialLedger, (std::uint32_t, bool), ());
|
||||
MOCK_METHOD(std::optional<FakeFetchResponse>, fetchLedger, (uint32_t, bool, bool), ());
|
||||
MOCK_METHOD(boost::json::value, toJson, (), (const));
|
||||
|
||||
using ForwardToRippledReturnType = std::expected<boost::json::object, rpc::ClioError>;
|
||||
MOCK_METHOD(
|
||||
ForwardToRippledReturnType,
|
||||
forwardToRippled,
|
||||
(boost::json::object const&, std::optional<std::string> const&, bool, boost::asio::yield_context),
|
||||
(const)
|
||||
);
|
||||
};
|
||||
|
||||
struct MockNgLoadBalancer : etlng::LoadBalancerInterface {
|
||||
using RawLedgerObjectType = FakeLedgerObject;
|
||||
|
||||
@@ -85,4 +69,7 @@ struct MockNgLoadBalancer : etlng::LoadBalancerInterface {
|
||||
(boost::json::object const&, std::optional<std::string> const&, bool, boost::asio::yield_context),
|
||||
(override)
|
||||
);
|
||||
MOCK_METHOD(void, stop, (boost::asio::yield_context), ());
|
||||
};
|
||||
|
||||
using MockLoadBalancer = MockNgLoadBalancer;
|
||||
|
||||
@@ -60,7 +60,7 @@ struct MockSource : etl::SourceBase {
|
||||
(uint32_t, bool, bool),
|
||||
(override)
|
||||
);
|
||||
MOCK_METHOD((std::pair<std::vector<std::string>, bool>), loadInitialLedger, (uint32_t, uint32_t, bool), (override));
|
||||
MOCK_METHOD((std::pair<std::vector<std::string>, bool>), loadInitialLedger, (uint32_t, uint32_t), (override));
|
||||
|
||||
using ForwardToRippledReturnType = std::expected<boost::json::object, rpc::ClioError>;
|
||||
MOCK_METHOD(
|
||||
@@ -132,9 +132,9 @@ public:
|
||||
}
|
||||
|
||||
std::pair<std::vector<std::string>, bool>
|
||||
loadInitialLedger(uint32_t sequence, uint32_t maxLedger, bool getObjects) override
|
||||
loadInitialLedger(uint32_t sequence, uint32_t maxLedger) override
|
||||
{
|
||||
return mock_->loadInitialLedger(sequence, maxLedger, getObjects);
|
||||
return mock_->loadInitialLedger(sequence, maxLedger);
|
||||
}
|
||||
|
||||
std::expected<boost::json::object, rpc::ClioError>
|
||||
|
||||
245
tests/common/util/MockSourceNg.hpp
Normal file
245
tests/common/util/MockSourceNg.hpp
Normal file
@@ -0,0 +1,245 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of clio: https://github.com/XRPLF/clio
|
||||
Copyright (c) 2023, 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 "etl/NetworkValidatedLedgersInterface.hpp"
|
||||
#include "etlng/InitialLoadObserverInterface.hpp"
|
||||
#include "etlng/Source.hpp"
|
||||
#include "feed/SubscriptionManagerInterface.hpp"
|
||||
#include "rpc/Errors.hpp"
|
||||
#include "util/newconfig/ObjectView.hpp"
|
||||
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/spawn.hpp>
|
||||
#include <boost/json/object.hpp>
|
||||
#include <boost/uuid/uuid.hpp>
|
||||
#include <gmock/gmock.h>
|
||||
#include <grpcpp/support/status.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <org/xrpl/rpc/v1/get_ledger.pb.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
struct MockSourceNg : etlng::SourceBase {
|
||||
MOCK_METHOD(void, run, (), (override));
|
||||
MOCK_METHOD(void, stop, (boost::asio::yield_context), (override));
|
||||
MOCK_METHOD(bool, isConnected, (), (const, override));
|
||||
MOCK_METHOD(void, setForwarding, (bool), (override));
|
||||
MOCK_METHOD(boost::json::object, toJson, (), (const, override));
|
||||
MOCK_METHOD(std::string, toString, (), (const, override));
|
||||
MOCK_METHOD(bool, hasLedger, (uint32_t), (const, override));
|
||||
MOCK_METHOD(
|
||||
(std::pair<grpc::Status, org::xrpl::rpc::v1::GetLedgerResponse>),
|
||||
fetchLedger,
|
||||
(uint32_t, bool, bool),
|
||||
(override)
|
||||
);
|
||||
MOCK_METHOD(
|
||||
(std::pair<std::vector<std::string>, bool>),
|
||||
loadInitialLedger,
|
||||
(uint32_t, uint32_t, etlng::InitialLoadObserverInterface&),
|
||||
(override)
|
||||
);
|
||||
|
||||
using ForwardToRippledReturnType = std::expected<boost::json::object, rpc::ClioError>;
|
||||
MOCK_METHOD(
|
||||
ForwardToRippledReturnType,
|
||||
forwardToRippled,
|
||||
(boost::json::object const&, std::optional<std::string> const&, std::string_view, boost::asio::yield_context),
|
||||
(const, override)
|
||||
);
|
||||
};
|
||||
|
||||
template <template <typename> typename MockType>
|
||||
using MockSourceNgPtr = std::shared_ptr<MockType<MockSourceNg>>;
|
||||
|
||||
template <template <typename> typename MockType>
|
||||
class MockSourceNgWrapper : public etlng::SourceBase {
|
||||
MockSourceNgPtr<MockType> mock_;
|
||||
|
||||
public:
|
||||
MockSourceNgWrapper(MockSourceNgPtr<MockType> mockData) : mock_(std::move(mockData))
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
run() override
|
||||
{
|
||||
mock_->run();
|
||||
}
|
||||
|
||||
void
|
||||
stop(boost::asio::yield_context yield) override
|
||||
{
|
||||
mock_->stop(yield);
|
||||
}
|
||||
|
||||
bool
|
||||
isConnected() const override
|
||||
{
|
||||
return mock_->isConnected();
|
||||
}
|
||||
|
||||
void
|
||||
setForwarding(bool isForwarding) override
|
||||
{
|
||||
mock_->setForwarding(isForwarding);
|
||||
}
|
||||
|
||||
boost::json::object
|
||||
toJson() const override
|
||||
{
|
||||
return mock_->toJson();
|
||||
}
|
||||
|
||||
std::string
|
||||
toString() const override
|
||||
{
|
||||
return mock_->toString();
|
||||
}
|
||||
|
||||
bool
|
||||
hasLedger(uint32_t sequence) const override
|
||||
{
|
||||
return mock_->hasLedger(sequence);
|
||||
}
|
||||
|
||||
std::pair<grpc::Status, org::xrpl::rpc::v1::GetLedgerResponse>
|
||||
fetchLedger(uint32_t sequence, bool getObjects, bool getObjectNeighbors) override
|
||||
{
|
||||
return mock_->fetchLedger(sequence, getObjects, getObjectNeighbors);
|
||||
}
|
||||
|
||||
std::pair<std::vector<std::string>, bool>
|
||||
loadInitialLedger(uint32_t sequence, uint32_t maxLedger, etlng::InitialLoadObserverInterface& observer) override
|
||||
{
|
||||
return mock_->loadInitialLedger(sequence, maxLedger, observer);
|
||||
}
|
||||
|
||||
std::expected<boost::json::object, rpc::ClioError>
|
||||
forwardToRippled(
|
||||
boost::json::object const& request,
|
||||
std::optional<std::string> const& forwardToRippledClientIp,
|
||||
std::string_view xUserValue,
|
||||
boost::asio::yield_context yield
|
||||
) const override
|
||||
{
|
||||
return mock_->forwardToRippled(request, forwardToRippledClientIp, xUserValue, yield);
|
||||
}
|
||||
};
|
||||
|
||||
struct MockSourceNgCallbacks {
|
||||
etlng::SourceBase::OnDisconnectHook onDisconnect;
|
||||
etlng::SourceBase::OnConnectHook onConnect;
|
||||
etlng::SourceBase::OnLedgerClosedHook onLedgerClosed;
|
||||
};
|
||||
|
||||
template <template <typename> typename MockType>
|
||||
struct MockSourceNgData {
|
||||
MockSourceNgPtr<MockType> source = std::make_shared<MockType<MockSourceNg>>();
|
||||
std::optional<MockSourceNgCallbacks> callbacks;
|
||||
};
|
||||
|
||||
template <template <typename> typename MockType = testing::NiceMock>
|
||||
class MockSourceNgFactoryImpl {
|
||||
std::vector<MockSourceNgData<MockType>> mockData_;
|
||||
|
||||
public:
|
||||
MockSourceNgFactoryImpl(size_t numSources)
|
||||
{
|
||||
setSourcesNumber(numSources);
|
||||
|
||||
ON_CALL(*this, makeSource)
|
||||
.WillByDefault([this](
|
||||
util::config::ObjectView const&,
|
||||
boost::asio::io_context&,
|
||||
std::shared_ptr<feed::SubscriptionManagerInterface>,
|
||||
std::shared_ptr<etl::NetworkValidatedLedgersInterface>,
|
||||
std::chrono::steady_clock::duration,
|
||||
etlng::SourceBase::OnConnectHook onConnect,
|
||||
etlng::SourceBase::OnDisconnectHook onDisconnect,
|
||||
etlng::SourceBase::OnLedgerClosedHook onLedgerClosed
|
||||
) {
|
||||
auto it = std::ranges::find_if(mockData_, [](auto const& d) { return not d.callbacks.has_value(); });
|
||||
[&]() { ASSERT_NE(it, mockData_.end()) << "Make source called more than expected"; }();
|
||||
it->callbacks = MockSourceNgCallbacks{
|
||||
.onDisconnect = std::move(onDisconnect),
|
||||
.onConnect = std::move(onConnect),
|
||||
.onLedgerClosed = std::move(onLedgerClosed)
|
||||
};
|
||||
|
||||
return std::make_unique<MockSourceNgWrapper<MockType>>(it->source);
|
||||
});
|
||||
}
|
||||
|
||||
void
|
||||
setSourcesNumber(size_t numSources)
|
||||
{
|
||||
mockData_.clear();
|
||||
mockData_.reserve(numSources);
|
||||
std::ranges::generate_n(std::back_inserter(mockData_), numSources, [] { return MockSourceNgData<MockType>{}; });
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
etlng::SourcePtr
|
||||
operator()(Args&&... args)
|
||||
{
|
||||
return makeSource(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
MOCK_METHOD(
|
||||
etlng::SourcePtr,
|
||||
makeSource,
|
||||
(util::config::ObjectView const&,
|
||||
boost::asio::io_context&,
|
||||
std::shared_ptr<feed::SubscriptionManagerInterface>,
|
||||
std::shared_ptr<etl::NetworkValidatedLedgersInterface>,
|
||||
std::chrono::steady_clock::duration,
|
||||
etlng::SourceBase::OnConnectHook,
|
||||
etlng::SourceBase::OnDisconnectHook,
|
||||
etlng::SourceBase::OnLedgerClosedHook)
|
||||
);
|
||||
|
||||
MockType<MockSourceNg>&
|
||||
sourceAt(size_t index)
|
||||
{
|
||||
return *mockData_.at(index).source;
|
||||
}
|
||||
|
||||
MockSourceNgCallbacks&
|
||||
callbacksAt(size_t index)
|
||||
{
|
||||
auto& callbacks = mockData_.at(index).callbacks;
|
||||
[&]() { ASSERT_TRUE(callbacks.has_value()) << "Callbacks not set"; }();
|
||||
return *callbacks;
|
||||
}
|
||||
};
|
||||
|
||||
using MockSourceNgFactory = testing::NiceMock<MockSourceNgFactoryImpl<>>;
|
||||
using StrictMockSourceNgFactory = testing::StrictMock<MockSourceNgFactoryImpl<testing::StrictMock>>;
|
||||
Reference in New Issue
Block a user