mirror of
https://github.com/XRPLF/clio.git
synced 2025-11-19 19:25:53 +00:00
125 lines
3.8 KiB
C++
125 lines
3.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.
|
|
*/
|
|
//==============================================================================
|
|
|
|
#pragma once
|
|
|
|
#include <gmock/gmock.h>
|
|
#include <grpcpp/security/server_credentials.h>
|
|
#include <grpcpp/server.h>
|
|
#include <grpcpp/server_builder.h>
|
|
#include <grpcpp/server_context.h>
|
|
#include <grpcpp/support/status.h>
|
|
#include <gtest/gtest.h>
|
|
#include <org/xrpl/rpc/v1/get_ledger.pb.h>
|
|
#include <org/xrpl/rpc/v1/get_ledger_data.pb.h>
|
|
#include <org/xrpl/rpc/v1/get_ledger_diff.pb.h>
|
|
#include <org/xrpl/rpc/v1/get_ledger_entry.pb.h>
|
|
#include <xrpl/proto/org/xrpl/rpc/v1/xrp_ledger.grpc.pb.h>
|
|
|
|
#include <chrono>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <thread>
|
|
|
|
namespace tests::util {
|
|
|
|
struct MockXrpLedgerAPIService final : public org::xrpl::rpc::v1::XRPLedgerAPIService::Service {
|
|
~MockXrpLedgerAPIService() override = default;
|
|
|
|
MOCK_METHOD(
|
|
grpc::Status,
|
|
GetLedger,
|
|
(grpc::ServerContext * context,
|
|
org::xrpl::rpc::v1::GetLedgerRequest const* request,
|
|
org::xrpl::rpc::v1::GetLedgerResponse* response),
|
|
(override)
|
|
);
|
|
|
|
MOCK_METHOD(
|
|
grpc::Status,
|
|
GetLedgerEntry,
|
|
(grpc::ServerContext * context,
|
|
org::xrpl::rpc::v1::GetLedgerEntryRequest const* request,
|
|
org::xrpl::rpc::v1::GetLedgerEntryResponse* response),
|
|
(override)
|
|
);
|
|
|
|
MOCK_METHOD(
|
|
grpc::Status,
|
|
GetLedgerData,
|
|
(grpc::ServerContext * context,
|
|
org::xrpl::rpc::v1::GetLedgerDataRequest const* request,
|
|
org::xrpl::rpc::v1::GetLedgerDataResponse* response),
|
|
(override)
|
|
);
|
|
|
|
MOCK_METHOD(
|
|
grpc::Status,
|
|
GetLedgerDiff,
|
|
(grpc::ServerContext * context,
|
|
org::xrpl::rpc::v1::GetLedgerDiffRequest const* request,
|
|
org::xrpl::rpc::v1::GetLedgerDiffResponse* response),
|
|
(override)
|
|
);
|
|
};
|
|
|
|
struct WithMockXrpLedgerAPIService : virtual ::testing::Test {
|
|
WithMockXrpLedgerAPIService(std::string serverAddress)
|
|
{
|
|
grpc::ServerBuilder builder;
|
|
builder.AddListeningPort(serverAddress, grpc::InsecureServerCredentials(), &port_);
|
|
builder.RegisterService(&mockXrpLedgerAPIService);
|
|
server_ = builder.BuildAndStart();
|
|
serverThread_ = std::thread([this] { server_->Wait(); });
|
|
}
|
|
|
|
~WithMockXrpLedgerAPIService() override
|
|
{
|
|
shutdown();
|
|
}
|
|
|
|
int
|
|
getXRPLMockPort() const
|
|
{
|
|
return port_;
|
|
}
|
|
|
|
void
|
|
shutdown(std::optional<std::chrono::system_clock::duration> deadline = std::nullopt)
|
|
{
|
|
if (deadline.has_value()) {
|
|
server_->Shutdown(std::chrono::system_clock::now() + *deadline);
|
|
} else {
|
|
server_->Shutdown();
|
|
}
|
|
if (serverThread_.joinable())
|
|
serverThread_.join();
|
|
}
|
|
|
|
MockXrpLedgerAPIService mockXrpLedgerAPIService;
|
|
|
|
private:
|
|
std::unique_ptr<grpc::Server> server_;
|
|
std::thread serverThread_;
|
|
int port_{};
|
|
};
|
|
|
|
} // namespace tests::util
|