mirror of
https://github.com/XRPLF/clio.git
synced 2025-12-06 17:27:58 +00:00
@@ -1,133 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
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));
|
||||
}
|
||||
@@ -519,7 +519,7 @@ struct LoadBalancerForwardToRippledTests : LoadBalancerConstructorTests, SyncAsi
|
||||
EXPECT_CALL(sourceFactory_.sourceAt(1), run);
|
||||
}
|
||||
|
||||
boost::json::object const request_{{"request", "value"}};
|
||||
boost::json::object const request_{{"command", "value"}};
|
||||
std::optional<std::string> const clientIP_ = "some_ip";
|
||||
boost::json::object const response_{{"response", "other_value"}};
|
||||
};
|
||||
@@ -699,6 +699,21 @@ TEST_F(LoadBalancerForwardToRippledTests, onLedgerClosedHookInvalidatesCache)
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(LoadBalancerForwardToRippledTests, commandLineMissing)
|
||||
{
|
||||
EXPECT_CALL(sourceFactory_, makeSource).Times(2);
|
||||
auto loadBalancer = makeLoadBalancer();
|
||||
|
||||
auto const request = boost::json::object{{"command2", "server_info"}};
|
||||
|
||||
runSpawn([&](boost::asio::yield_context yield) {
|
||||
EXPECT_EQ(
|
||||
loadBalancer->forwardToRippled(request, clientIP_, false, yield).error(),
|
||||
rpc::ClioError::rpcCOMMAND_IS_MISSING
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
struct LoadBalancerToJsonTests : LoadBalancerOnConnectHookTests {};
|
||||
|
||||
TEST_F(LoadBalancerToJsonTests, toJson)
|
||||
|
||||
Reference in New Issue
Block a user