mirror of
https://github.com/XRPLF/clio.git
synced 2025-11-19 19:25:53 +00:00
@@ -32,7 +32,7 @@ using namespace util;
|
||||
using namespace testing;
|
||||
|
||||
struct ETLStateTest : public NoLoggerFixture {
|
||||
MockSource const source = MockSource{};
|
||||
MockSource source = MockSource{};
|
||||
};
|
||||
|
||||
TEST_F(ETLStateTest, Error)
|
||||
|
||||
133
unittests/etl/ForwardingCacheTests.cpp
Normal file
133
unittests/etl/ForwardingCacheTests.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include "etl/impl/ForwardingCache.hpp"
|
||||
|
||||
#include <boost/json/object.hpp>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
using namespace etl::impl;
|
||||
|
||||
struct CacheEntryTests : public ::testing::Test {
|
||||
CacheEntry entry_;
|
||||
boost::json::object const object_ = {{"key", "value"}};
|
||||
};
|
||||
|
||||
TEST_F(CacheEntryTests, PutAndGet)
|
||||
{
|
||||
EXPECT_FALSE(entry_.get());
|
||||
|
||||
entry_.put(object_);
|
||||
auto result = entry_.get();
|
||||
|
||||
ASSERT_TRUE(result);
|
||||
EXPECT_EQ(*result, object_);
|
||||
}
|
||||
|
||||
TEST_F(CacheEntryTests, LastUpdated)
|
||||
{
|
||||
EXPECT_EQ(entry_.lastUpdated().time_since_epoch().count(), 0);
|
||||
|
||||
entry_.put(object_);
|
||||
auto const lastUpdated = entry_.lastUpdated();
|
||||
|
||||
EXPECT_GE(
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - lastUpdated).count(), 0
|
||||
);
|
||||
|
||||
entry_.put(boost::json::object{{"key", "new value"}});
|
||||
auto const newLastUpdated = entry_.lastUpdated();
|
||||
EXPECT_GT(newLastUpdated, lastUpdated);
|
||||
EXPECT_GE(
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - newLastUpdated)
|
||||
.count(),
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
TEST_F(CacheEntryTests, Invalidate)
|
||||
{
|
||||
entry_.put(object_);
|
||||
entry_.invalidate();
|
||||
EXPECT_FALSE(entry_.get());
|
||||
}
|
||||
|
||||
TEST(ForwardingCacheTests, ShouldCache)
|
||||
{
|
||||
for (auto const& command : ForwardingCache::CACHEABLE_COMMANDS) {
|
||||
auto const request = boost::json::object{{"command", command}};
|
||||
EXPECT_TRUE(ForwardingCache::shouldCache(request));
|
||||
}
|
||||
auto const request = boost::json::object{{"command", "tx"}};
|
||||
EXPECT_FALSE(ForwardingCache::shouldCache(request));
|
||||
|
||||
auto const requestWithoutCommand = boost::json::object{{"key", "value"}};
|
||||
EXPECT_FALSE(ForwardingCache::shouldCache(requestWithoutCommand));
|
||||
}
|
||||
|
||||
TEST(ForwardingCacheTests, Get)
|
||||
{
|
||||
ForwardingCache cache{std::chrono::seconds{100}};
|
||||
auto const request = boost::json::object{{"command", "server_info"}};
|
||||
auto const response = boost::json::object{{"key", "value"}};
|
||||
|
||||
cache.put(request, response);
|
||||
auto const result = cache.get(request);
|
||||
|
||||
ASSERT_TRUE(result);
|
||||
EXPECT_EQ(*result, response);
|
||||
}
|
||||
|
||||
TEST(ForwardingCacheTests, GetExpired)
|
||||
{
|
||||
ForwardingCache cache{std::chrono::milliseconds{1}};
|
||||
auto const request = boost::json::object{{"command", "server_info"}};
|
||||
auto const response = boost::json::object{{"key", "value"}};
|
||||
|
||||
cache.put(request, response);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{2});
|
||||
|
||||
auto const result = cache.get(request);
|
||||
EXPECT_FALSE(result);
|
||||
}
|
||||
|
||||
TEST(ForwardingCacheTests, GetAndPutNotCommand)
|
||||
{
|
||||
ForwardingCache cache{std::chrono::seconds{100}};
|
||||
auto const request = boost::json::object{{"key", "value"}};
|
||||
auto const response = boost::json::object{{"key", "value"}};
|
||||
cache.put(request, response);
|
||||
auto const result = cache.get(request);
|
||||
EXPECT_FALSE(result);
|
||||
}
|
||||
|
||||
TEST(ForwardingCache, Invalidate)
|
||||
{
|
||||
ForwardingCache cache{std::chrono::seconds{100}};
|
||||
auto const request = boost::json::object{{"command", "server_info"}};
|
||||
auto const response = boost::json::object{{"key", "value"}};
|
||||
|
||||
cache.put(request, response);
|
||||
cache.invalidate();
|
||||
|
||||
EXPECT_FALSE(cache.get(request));
|
||||
}
|
||||
@@ -28,6 +28,7 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
@@ -47,7 +48,7 @@ TEST_F(ForwardingSourceTests, ConnectionFailed)
|
||||
}
|
||||
|
||||
struct ForwardingSourceOperationsTests : ForwardingSourceTests {
|
||||
std::string const message_ = R"({"data":"some_data"})";
|
||||
std::string const message_ = R"({"data": "some_data"})";
|
||||
boost::json::object const reply_ = {{"reply", "some_reply"}};
|
||||
|
||||
TestWsConnection
|
||||
@@ -83,7 +84,7 @@ TEST_F(ForwardingSourceOperationsTests, ParseFailed)
|
||||
|
||||
auto receivedMessage = connection.receive(yield);
|
||||
[&]() { ASSERT_TRUE(receivedMessage); }();
|
||||
EXPECT_EQ(*receivedMessage, message_);
|
||||
EXPECT_EQ(boost::json::parse(*receivedMessage), boost::json::parse(message_)) << *receivedMessage;
|
||||
|
||||
auto sendError = connection.send("invalid_json", yield);
|
||||
[&]() { ASSERT_FALSE(sendError) << *sendError; }();
|
||||
@@ -97,6 +98,28 @@ TEST_F(ForwardingSourceOperationsTests, ParseFailed)
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(ForwardingSourceOperationsTests, GotNotAnObject)
|
||||
{
|
||||
boost::asio::spawn(ctx, [&](boost::asio::yield_context yield) {
|
||||
auto connection = serverConnection(yield);
|
||||
|
||||
auto receivedMessage = connection.receive(yield);
|
||||
[&]() { ASSERT_TRUE(receivedMessage); }();
|
||||
EXPECT_EQ(boost::json::parse(*receivedMessage), boost::json::parse(message_)) << *receivedMessage;
|
||||
|
||||
auto sendError = connection.send(R"(["some_value"])", yield);
|
||||
|
||||
[&]() { ASSERT_FALSE(sendError) << *sendError; }();
|
||||
|
||||
connection.close(yield);
|
||||
});
|
||||
|
||||
runSpawn([&](boost::asio::yield_context yield) {
|
||||
auto result = forwardingSource.forwardToRippled(boost::json::parse(message_).as_object(), {}, yield);
|
||||
EXPECT_FALSE(result);
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(ForwardingSourceOperationsTests, Success)
|
||||
{
|
||||
boost::asio::spawn(ctx, [&](boost::asio::yield_context yield) {
|
||||
@@ -104,7 +127,7 @@ TEST_F(ForwardingSourceOperationsTests, Success)
|
||||
|
||||
auto receivedMessage = connection.receive(yield);
|
||||
[&]() { ASSERT_TRUE(receivedMessage); }();
|
||||
EXPECT_EQ(*receivedMessage, message_);
|
||||
EXPECT_EQ(boost::json::parse(*receivedMessage), boost::json::parse(message_)) << *receivedMessage;
|
||||
|
||||
auto sendError = connection.send(boost::json::serialize(reply_), yield);
|
||||
[&]() { ASSERT_FALSE(sendError) << *sendError; }();
|
||||
|
||||
@@ -67,7 +67,8 @@ struct ForwardingSourceMock {
|
||||
MOCK_METHOD(
|
||||
ForwardToRippledReturnType,
|
||||
forwardToRippled,
|
||||
(boost::json::object const&, ClientIpOpt const&, boost::asio::yield_context)
|
||||
(boost::json::object const&, ClientIpOpt const&, boost::asio::yield_context),
|
||||
(const)
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -60,6 +60,7 @@ struct SubscriptionSourceConnectionTests : public NoLoggerFixture {
|
||||
|
||||
StrictMock<MockFunction<void()>> onConnectHook_;
|
||||
StrictMock<MockFunction<void()>> onDisconnectHook_;
|
||||
StrictMock<MockFunction<void()>> onLedgerClosedHook_;
|
||||
|
||||
std::unique_ptr<SubscriptionSource> subscriptionSource_ = std::make_unique<SubscriptionSource>(
|
||||
ioContext_,
|
||||
@@ -69,6 +70,7 @@ struct SubscriptionSourceConnectionTests : public NoLoggerFixture {
|
||||
subscriptionManager_,
|
||||
onConnectHook_.AsStdFunction(),
|
||||
onDisconnectHook_.AsStdFunction(),
|
||||
onLedgerClosedHook_.AsStdFunction(),
|
||||
std::chrono::milliseconds(1),
|
||||
std::chrono::milliseconds(1)
|
||||
);
|
||||
@@ -299,10 +301,37 @@ TEST_F(SubscriptionSourceReadTests, GotResultWithLedgerIndexAndValidatedLedgers)
|
||||
EXPECT_FALSE(subscriptionSource_->hasLedger(4));
|
||||
}
|
||||
|
||||
TEST_F(SubscriptionSourceReadTests, GotLedgerClosed)
|
||||
{
|
||||
boost::asio::spawn(ioContext_, [this](boost::asio::yield_context yield) {
|
||||
auto connection = connectAndSendMessage(R"({"type":"ledgerClosed"})", yield);
|
||||
connection.close(yield);
|
||||
});
|
||||
|
||||
EXPECT_CALL(onConnectHook_, Call());
|
||||
EXPECT_CALL(onDisconnectHook_, Call()).WillOnce([this]() { subscriptionSource_->stop(); });
|
||||
ioContext_.run();
|
||||
}
|
||||
|
||||
TEST_F(SubscriptionSourceReadTests, GotLedgerClosedForwardingIsSet)
|
||||
{
|
||||
subscriptionSource_->setForwarding(true);
|
||||
|
||||
boost::asio::spawn(ioContext_, [this](boost::asio::yield_context yield) {
|
||||
auto connection = connectAndSendMessage(R"({"type": "ledgerClosed"})", yield);
|
||||
connection.close(yield);
|
||||
});
|
||||
|
||||
EXPECT_CALL(onConnectHook_, Call());
|
||||
EXPECT_CALL(onLedgerClosedHook_, Call());
|
||||
EXPECT_CALL(onDisconnectHook_, Call()).WillOnce([this]() { subscriptionSource_->stop(); });
|
||||
ioContext_.run();
|
||||
}
|
||||
|
||||
TEST_F(SubscriptionSourceReadTests, GotLedgerClosedWithLedgerIndex)
|
||||
{
|
||||
boost::asio::spawn(ioContext_, [this](boost::asio::yield_context yield) {
|
||||
auto connection = connectAndSendMessage(R"({"type":"ledgerClosed","ledger_index":123})", yield);
|
||||
auto connection = connectAndSendMessage(R"({"type": "ledgerClosed","ledger_index": 123})", yield);
|
||||
connection.close(yield);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user