mirror of
https://github.com/XRPLF/clio.git
synced 2025-11-04 11:55:51 +00:00
@@ -148,6 +148,7 @@ if(tests)
|
||||
unittests/etl/ExtractionDataPipeTest.cpp
|
||||
unittests/etl/ExtractorTest.cpp
|
||||
unittests/etl/TransformerTest.cpp
|
||||
unittests/etl/CacheLoaderTest.cpp
|
||||
# RPC
|
||||
unittests/rpc/ErrorTests.cpp
|
||||
unittests/rpc/BaseTests.cpp
|
||||
|
||||
153
unittests/etl/CacheLoaderTest.cpp
Normal file
153
unittests/etl/CacheLoaderTest.cpp
Normal file
@@ -0,0 +1,153 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <etl/impl/CacheLoader.h>
|
||||
#include <util/Fixtures.h>
|
||||
#include <util/MockCache.h>
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/json.hpp>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace json = boost::json;
|
||||
using namespace clio::detail;
|
||||
using namespace clio;
|
||||
using namespace Backend;
|
||||
using namespace testing;
|
||||
|
||||
constexpr static auto SEQ = 30;
|
||||
constexpr static auto INDEX1 = "E6DBAFC99223B42257915A63DFC6B0C032D4070F9A574B255AD97466726FC321";
|
||||
|
||||
struct CacheLoaderTest : public MockBackendTest
|
||||
{
|
||||
void
|
||||
SetUp() override
|
||||
{
|
||||
MockBackendTest::SetUp();
|
||||
work.emplace(ctx);
|
||||
for (auto i = 0; i < 2; ++i)
|
||||
optThreads.emplace_back([&] { ctx.run(); });
|
||||
}
|
||||
|
||||
void
|
||||
TearDown() override
|
||||
{
|
||||
work.reset();
|
||||
for (auto& optThread : optThreads)
|
||||
if (optThread.joinable())
|
||||
optThread.join();
|
||||
ctx.stop();
|
||||
MockBackendTest::TearDown();
|
||||
}
|
||||
|
||||
protected:
|
||||
MockCache cache;
|
||||
Config cfg{json::parse("{}")};
|
||||
std::optional<boost::asio::io_service::work> work;
|
||||
boost::asio::io_context ctx;
|
||||
std::vector<std::thread> optThreads;
|
||||
};
|
||||
|
||||
namespace {
|
||||
|
||||
std::vector<LedgerObject>
|
||||
getLatestDiff()
|
||||
{
|
||||
return std::vector<LedgerObject>{
|
||||
{.key = ripple::uint256{"05E1EAC2574BE082B00B16F907CE32E6058DEB8F9E81CF34A00E80A5D71FA4FE"}, .blob = Blob{'s'}},
|
||||
{.key = ripple::uint256{"110872C7196EE6EF7032952F1852B11BB461A96FF2D7E06A8003B4BB30FD130B"}, .blob = Blob{'s'}},
|
||||
{.key = ripple::uint256{"3B3A84E850C724E914293271785A31D0BFC8B9DD1B6332E527B149AD72E80E18"}, .blob = Blob{'s'}},
|
||||
{.key = ripple::uint256{"4EC98C5C3F34C44409BC058998CBD64F6AED3FF6C0CAAEC15F7F42DF14EE9F04"}, .blob = Blob{'s'}},
|
||||
{.key = ripple::uint256{"58CEC9F17733EA7BA68C88E6179B8F207D001EE04D4E0366F958CC04FF6AB834"}, .blob = Blob{'s'}},
|
||||
{.key = ripple::uint256{"64FB1712146BA604C274CC335C5DE7ADFE52D1F8C3E904A9F9765FE8158A3E01"}, .blob = Blob{'s'}},
|
||||
{.key = ripple::uint256{"700BE23B1D9EE3E6BF52543D05843D5345B85D9EDB3D33BBD6B4C3A13C54B38E"}, .blob = Blob{'s'}},
|
||||
{.key = ripple::uint256{"82C297FCBCD634C4424F263D17480AA2F13975DF5846A5BB57246022CEEBE441"}, .blob = Blob{'s'}},
|
||||
{.key = ripple::uint256{"A2AA4C212DC2CA2C49BF58805F7C63363BC981018A01AC9609A7CBAB2A02CEDF"}, .blob = Blob{'s'}},
|
||||
{.key = ripple::uint256{"BC0DAE09C0BFBC4A49AA94B849266588BFD6E1F554B184B5788AC55D6E07EB95"}, .blob = Blob{'s'}},
|
||||
{.key = ripple::uint256{"DCC8759A35CB946511763AA5553A82AA25F20B901C98C9BB74D423BCFAFF5F9D"}, .blob = Blob{'s'}},
|
||||
};
|
||||
}
|
||||
|
||||
}; // namespace
|
||||
|
||||
TEST_F(CacheLoaderTest, FromCache)
|
||||
{
|
||||
MockBackend* rawBackendPtr = static_cast<MockBackend*>(mockBackendPtr.get());
|
||||
CacheLoader loader{cfg, ctx, mockBackendPtr, cache};
|
||||
|
||||
auto const diffs = getLatestDiff();
|
||||
ON_CALL(*rawBackendPtr, fetchLedgerDiff(_, _)).WillByDefault(Return(diffs));
|
||||
EXPECT_CALL(*rawBackendPtr, fetchLedgerDiff(_, _)).Times(32);
|
||||
|
||||
auto const loops = diffs.size() + 1;
|
||||
auto const keysSize = 14;
|
||||
std::mutex keysMutex;
|
||||
|
||||
std::map<std::thread::id, uint32_t> threadKeysMap;
|
||||
ON_CALL(*rawBackendPtr, doFetchSuccessorKey(_, SEQ, _))
|
||||
.WillByDefault(Invoke([&]() -> std::optional<ripple::uint256> {
|
||||
// mock the result from doFetchSuccessorKey, be aware this function will be called from multiple threads
|
||||
// for each thread, the last 2 items must be end flag and nullopt, otherwise it will loop forever
|
||||
std::lock_guard<std::mutex> guard(keysMutex);
|
||||
threadKeysMap[std::this_thread::get_id()]++;
|
||||
|
||||
if (threadKeysMap[std::this_thread::get_id()] == keysSize - 1)
|
||||
{
|
||||
return lastKey;
|
||||
}
|
||||
else if (threadKeysMap[std::this_thread::get_id()] == keysSize)
|
||||
{
|
||||
threadKeysMap[std::this_thread::get_id()] = 0;
|
||||
return std::nullopt;
|
||||
}
|
||||
else
|
||||
{
|
||||
return ripple::uint256{INDEX1};
|
||||
}
|
||||
}));
|
||||
EXPECT_CALL(*rawBackendPtr, doFetchSuccessorKey).Times(keysSize * loops);
|
||||
|
||||
ON_CALL(*rawBackendPtr, doFetchLedgerObjects(_, SEQ, _))
|
||||
.WillByDefault(Return(std::vector<Blob>{keysSize - 1, Blob{'s'}}));
|
||||
EXPECT_CALL(*rawBackendPtr, doFetchLedgerObjects).Times(loops);
|
||||
|
||||
EXPECT_CALL(cache, size).Times(AtLeast(1));
|
||||
EXPECT_CALL(cache, update).Times(loops);
|
||||
EXPECT_CALL(cache, isFull).Times(1);
|
||||
|
||||
std::mutex m;
|
||||
std::condition_variable cv;
|
||||
bool cacheReady = false;
|
||||
ON_CALL(cache, setFull).WillByDefault(Invoke([&]() {
|
||||
{
|
||||
std::lock_guard lk(m);
|
||||
cacheReady = true;
|
||||
}
|
||||
cv.notify_one();
|
||||
}));
|
||||
// cache is successfully loaded
|
||||
EXPECT_CALL(cache, setFull).Times(1);
|
||||
|
||||
loader.load(SEQ);
|
||||
|
||||
{
|
||||
std::unique_lock lk(m);
|
||||
cv.wait_for(lk, std::chrono::milliseconds(300), [&] { return cacheReady; });
|
||||
}
|
||||
}
|
||||
@@ -123,13 +123,14 @@ struct AsyncAsioContextTest : virtual public NoLoggerFixture
|
||||
AsyncAsioContextTest()
|
||||
{
|
||||
work.emplace(ctx); // make sure ctx does not stop on its own
|
||||
runner.emplace([&] { ctx.run(); });
|
||||
}
|
||||
|
||||
~AsyncAsioContextTest()
|
||||
{
|
||||
work.reset();
|
||||
if (runner.joinable())
|
||||
runner.join();
|
||||
if (runner->joinable())
|
||||
runner->join();
|
||||
ctx.stop();
|
||||
}
|
||||
|
||||
@@ -137,9 +138,9 @@ struct AsyncAsioContextTest : virtual public NoLoggerFixture
|
||||
stop()
|
||||
{
|
||||
work.reset();
|
||||
if (runner->joinable())
|
||||
runner->join();
|
||||
ctx.stop();
|
||||
if (runner.joinable())
|
||||
runner.join();
|
||||
}
|
||||
|
||||
protected:
|
||||
@@ -147,7 +148,7 @@ protected:
|
||||
|
||||
private:
|
||||
std::optional<boost::asio::io_service::work> work;
|
||||
std::thread runner{[this] { ctx.run(); }};
|
||||
std::optional<std::thread> runner;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
49
unittests/util/MockCache.h
Normal file
49
unittests/util/MockCache.h
Normal file
@@ -0,0 +1,49 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
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 <backend/Types.h>
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
struct MockCache
|
||||
{
|
||||
MOCK_METHOD(void, update, (std::vector<Backend::LedgerObject> const& a, uint32_t b, bool c), ());
|
||||
|
||||
MOCK_METHOD(std::optional<Backend::Blob>, get, (ripple::uint256 const& a, uint32_t b), (const));
|
||||
|
||||
MOCK_METHOD(std::optional<Backend::LedgerObject>, getSuccessor, (ripple::uint256 const& a, uint32_t b), (const));
|
||||
|
||||
MOCK_METHOD(std::optional<Backend::LedgerObject>, getPredecessor, (ripple::uint256 const& a, uint32_t b), (const));
|
||||
|
||||
MOCK_METHOD(void, setDisabled, (), ());
|
||||
|
||||
MOCK_METHOD(void, setFull, (), ());
|
||||
|
||||
MOCK_METHOD(bool, isFull, (), (const));
|
||||
|
||||
MOCK_METHOD(uint32_t, latestLedgerSequence, (), (const));
|
||||
|
||||
MOCK_METHOD(size_t, size, (), (const));
|
||||
|
||||
MOCK_METHOD(float, getObjectHitRate, (), (const));
|
||||
|
||||
MOCK_METHOD(float, getSuccessorHitRate, (), (const));
|
||||
};
|
||||
@@ -188,10 +188,42 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
namespace {
|
||||
|
||||
template <class Executor>
|
||||
static std::shared_ptr<Server::HttpServer<Executor>>
|
||||
makeServerSync(
|
||||
clio::Config const& config,
|
||||
boost::asio::io_context& ioc,
|
||||
std::optional<std::reference_wrapper<boost::asio::ssl::context>> const& sslCtx,
|
||||
clio::DOSGuard& dosGuard,
|
||||
std::shared_ptr<Executor> const& handler)
|
||||
{
|
||||
auto server = std::shared_ptr<Server::HttpServer<Executor>>();
|
||||
std::mutex m;
|
||||
std::condition_variable cv;
|
||||
bool ready = false;
|
||||
boost::asio::dispatch(ioc.get_executor(), [&]() mutable {
|
||||
server = Server::make_HttpServer(config, ioc, sslCtx, dosGuard, handler);
|
||||
{
|
||||
std::lock_guard lk(m);
|
||||
ready = true;
|
||||
}
|
||||
cv.notify_one();
|
||||
});
|
||||
{
|
||||
std::unique_lock lk(m);
|
||||
cv.wait(lk, [&] { return ready; });
|
||||
}
|
||||
return server;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_F(WebServerTest, Http)
|
||||
{
|
||||
auto e = std::make_shared<EchoExecutor>();
|
||||
auto const server = Server::make_HttpServer(cfg, ctx, std::nullopt, dosGuard, e);
|
||||
auto const server = makeServerSync(cfg, ctx, std::nullopt, dosGuard, e);
|
||||
auto const res = HttpSyncClient::syncPost("localhost", "8888", R"({"Hello":1})");
|
||||
EXPECT_EQ(res, R"({"Hello":1})");
|
||||
}
|
||||
@@ -199,7 +231,7 @@ TEST_F(WebServerTest, Http)
|
||||
TEST_F(WebServerTest, Ws)
|
||||
{
|
||||
auto e = std::make_shared<EchoExecutor>();
|
||||
auto const server = Server::make_HttpServer(cfg, ctx, std::nullopt, dosGuard, e);
|
||||
auto const server = makeServerSync(cfg, ctx, std::nullopt, dosGuard, e);
|
||||
WebSocketSyncClient wsClient;
|
||||
wsClient.connect("localhost", "8888");
|
||||
auto const res = wsClient.syncPost(R"({"Hello":1})");
|
||||
@@ -210,7 +242,7 @@ TEST_F(WebServerTest, Ws)
|
||||
TEST_F(WebServerTest, HttpInternalError)
|
||||
{
|
||||
auto e = std::make_shared<ExceptionExecutor>();
|
||||
auto const server = Server::make_HttpServer(cfg, ctx, std::nullopt, dosGuard, e);
|
||||
auto const server = makeServerSync(cfg, ctx, std::nullopt, dosGuard, e);
|
||||
auto const res = HttpSyncClient::syncPost("localhost", "8888", R"({})");
|
||||
EXPECT_EQ(
|
||||
res,
|
||||
@@ -220,7 +252,7 @@ TEST_F(WebServerTest, HttpInternalError)
|
||||
TEST_F(WebServerTest, WsInternalError)
|
||||
{
|
||||
auto e = std::make_shared<ExceptionExecutor>();
|
||||
auto const server = Server::make_HttpServer(cfg, ctx, std::nullopt, dosGuard, e);
|
||||
auto const server = makeServerSync(cfg, ctx, std::nullopt, dosGuard, e);
|
||||
WebSocketSyncClient wsClient;
|
||||
wsClient.connect("localhost", "8888");
|
||||
auto const res = wsClient.syncPost(R"({"id":"id1"})");
|
||||
@@ -233,7 +265,7 @@ TEST_F(WebServerTest, WsInternalError)
|
||||
TEST_F(WebServerTest, WsInternalErrorNotJson)
|
||||
{
|
||||
auto e = std::make_shared<ExceptionExecutor>();
|
||||
auto const server = Server::make_HttpServer(cfg, ctx, std::nullopt, dosGuard, e);
|
||||
auto const server = makeServerSync(cfg, ctx, std::nullopt, dosGuard, e);
|
||||
WebSocketSyncClient wsClient;
|
||||
wsClient.connect("localhost", "8888");
|
||||
auto const res = wsClient.syncPost("not json");
|
||||
@@ -248,8 +280,7 @@ TEST_F(WebServerTest, Https)
|
||||
auto e = std::make_shared<EchoExecutor>();
|
||||
auto sslCtx = parseCertsForTest();
|
||||
auto const ctxSslRef = sslCtx ? std::optional<std::reference_wrapper<ssl::context>>{sslCtx.value()} : std::nullopt;
|
||||
|
||||
auto const server = Server::make_HttpServer(cfg, ctx, ctxSslRef, dosGuard, e);
|
||||
auto const server = makeServerSync(cfg, ctx, ctxSslRef, dosGuard, e);
|
||||
auto const res = HttpsSyncClient::syncPost("localhost", "8888", R"({"Hello":1})");
|
||||
EXPECT_EQ(res, R"({"Hello":1})");
|
||||
}
|
||||
@@ -260,7 +291,7 @@ TEST_F(WebServerTest, Wss)
|
||||
auto sslCtx = parseCertsForTest();
|
||||
auto const ctxSslRef = sslCtx ? std::optional<std::reference_wrapper<ssl::context>>{sslCtx.value()} : std::nullopt;
|
||||
|
||||
auto const server = Server::make_HttpServer(cfg, ctx, ctxSslRef, dosGuard, e);
|
||||
auto server = makeServerSync(cfg, ctx, ctxSslRef, dosGuard, e);
|
||||
WebServerSslSyncClient wsClient;
|
||||
wsClient.connect("localhost", "8888");
|
||||
auto const res = wsClient.syncPost(R"({"Hello":1})");
|
||||
@@ -271,7 +302,7 @@ TEST_F(WebServerTest, Wss)
|
||||
TEST_F(WebServerTest, HttpRequestOverload)
|
||||
{
|
||||
auto e = std::make_shared<EchoExecutor>();
|
||||
auto const server = Server::make_HttpServer(cfg, ctx, std::nullopt, dosGuardOverload, e);
|
||||
auto const server = makeServerSync(cfg, ctx, std::nullopt, dosGuardOverload, e);
|
||||
auto res = HttpSyncClient::syncPost("localhost", "8888", R"({})");
|
||||
EXPECT_EQ(res, "{}");
|
||||
res = HttpSyncClient::syncPost("localhost", "8888", R"({})");
|
||||
@@ -283,7 +314,7 @@ TEST_F(WebServerTest, HttpRequestOverload)
|
||||
TEST_F(WebServerTest, WsRequestOverload)
|
||||
{
|
||||
auto e = std::make_shared<EchoExecutor>();
|
||||
auto const server = Server::make_HttpServer(cfg, ctx, std::nullopt, dosGuardOverload, e);
|
||||
auto const server = makeServerSync(cfg, ctx, std::nullopt, dosGuardOverload, e);
|
||||
WebSocketSyncClient wsClient;
|
||||
wsClient.connect("localhost", "8888");
|
||||
auto res = wsClient.syncPost(R"({})");
|
||||
@@ -302,7 +333,7 @@ TEST_F(WebServerTest, HttpPayloadOverload)
|
||||
{
|
||||
std::string const s100(100, 'a');
|
||||
auto e = std::make_shared<EchoExecutor>();
|
||||
auto const server = Server::make_HttpServer(cfg, ctx, std::nullopt, dosGuardOverload, e);
|
||||
auto server = makeServerSync(cfg, ctx, std::nullopt, dosGuardOverload, e);
|
||||
auto const res = HttpSyncClient::syncPost("localhost", "8888", fmt::format(R"({{"payload":"{}"}})", s100));
|
||||
EXPECT_EQ(
|
||||
res,
|
||||
@@ -313,7 +344,7 @@ TEST_F(WebServerTest, WsPayloadOverload)
|
||||
{
|
||||
std::string const s100(100, 'a');
|
||||
auto e = std::make_shared<EchoExecutor>();
|
||||
auto const server = Server::make_HttpServer(cfg, ctx, std::nullopt, dosGuardOverload, e);
|
||||
auto server = makeServerSync(cfg, ctx, std::nullopt, dosGuardOverload, e);
|
||||
WebSocketSyncClient wsClient;
|
||||
wsClient.connect("localhost", "8888");
|
||||
auto const res = wsClient.syncPost(fmt::format(R"({{"payload":"{}"}})", s100));
|
||||
|
||||
Reference in New Issue
Block a user