mirror of
https://github.com/XRPLF/clio.git
synced 2026-10-02 01:48:23 +00:00
106 lines
2.8 KiB
C++
106 lines
2.8 KiB
C++
#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
|