Implement basic transformer tests (#689)

This commit is contained in:
Alex Kremer
2023-06-13 11:16:52 +01:00
committed by GitHub
parent 01e4eed130
commit 14f9f98cf2
23 changed files with 580 additions and 126 deletions

View File

@@ -18,13 +18,13 @@
//==============================================================================
#include <util/Fixtures.h>
#include <util/StringUtils.h>
#include <backend/CassandraBackend.h>
#include <config/Config.h>
#include <etl/NFTHelpers.h>
#include <rpc/RPCHelpers.h>
#include <ripple/basics/base_uint.h>
#include <boost/json/parse.hpp>
#include <fmt/compile.h>
#include <gtest/gtest.h>
@@ -89,29 +89,6 @@ TEST_F(BackendCassandraTest, Basic)
"3E2232B33EF57CECAC2816E3122816E31A0A00F8377CD95DFA484CFAE282656A58"
"CE5AA29652EFFD80AC59CD91416E4E13DBBE";
auto hexStringToBinaryString = [](auto const& hex) {
auto blob = ripple::strUnHex(hex);
std::string strBlob;
for (auto c : *blob)
{
strBlob += c;
}
return strBlob;
};
[[maybe_unused]] auto binaryStringToUint256 = [](auto const& bin) -> ripple::uint256 {
ripple::uint256 uint;
return uint.fromVoid((void const*)bin.data());
};
[[maybe_unused]] auto ledgerInfoToBinaryString = [](auto const& info) {
auto blob = ledgerInfoToBlob(info, true);
std::string strBlob;
for (auto c : blob)
{
strBlob += c;
}
return strBlob;
};
std::string rawHeaderBlob = hexStringToBinaryString(rawHeader);
ripple::LedgerInfo lgrInfo = util::deserializeHeader(ripple::makeSlice(rawHeaderBlob));
@@ -906,29 +883,6 @@ TEST_F(BackendCassandraTest, CacheIntegration)
"142252F328CF91263417762570D67220CCB33B1370";
std::string accountIndexHex = "E0311EB450B6177F969B94DBDDA83E99B7A0576ACD9079573876F16C0C004F06";
auto hexStringToBinaryString = [](auto const& hex) {
auto blob = ripple::strUnHex(hex);
std::string strBlob;
for (auto c : *blob)
{
strBlob += c;
}
return strBlob;
};
auto binaryStringToUint256 = [](auto const& bin) -> ripple::uint256 {
ripple::uint256 uint;
return uint.fromVoid((void const*)bin.data());
};
auto ledgerInfoToBinaryString = [](auto const& info) {
auto blob = ledgerInfoToBlob(info, true);
std::string strBlob;
for (auto c : blob)
{
strBlob += c;
}
return strBlob;
};
std::string rawHeaderBlob = hexStringToBinaryString(rawHeader);
std::string accountBlob = hexStringToBinaryString(accountHex);
std::string accountIndexBlob = hexStringToBinaryString(accountIndexHex);

View File

@@ -33,9 +33,8 @@ using namespace testing;
class ETLExtractorTest : public NoLoggerFixture
{
protected:
using DataType = FakeFetchResponse;
using ExtractionDataPipeType = MockExtractionDataPipe<DataType>;
using LedgerFetcherType = MockLedgerFetcher<DataType>;
using ExtractionDataPipeType = MockExtractionDataPipe;
using LedgerFetcherType = MockLedgerFetcher;
using ExtractorType =
clio::detail::Extractor<ExtractionDataPipeType, MockNetworkValidatedLedgers, LedgerFetcherType>;

View File

@@ -0,0 +1,151 @@
//------------------------------------------------------------------------------
/*
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/Transformer.h>
#include <util/FakeFetchResponse.h>
#include <util/Fixtures.h>
#include <util/MockExtractionDataPipe.h>
#include <util/MockLedgerLoader.h>
#include <util/MockLedgerPublisher.h>
#include <util/StringUtils.h>
#include <gtest/gtest.h>
#include <memory>
using namespace testing;
// taken from BackendTests
constexpr static auto RAW_HEADER =
"03C3141A01633CD656F91B4EBB5EB89B791BD34DBC8A04BB6F407C5335BC54351E"
"DD733898497E809E04074D14D271E4832D7888754F9230800761563A292FA2315A"
"6DB6FE30CC5909B285080FCD6773CC883F9FE0EE4D439340AC592AADB973ED3CF5"
"3E2232B33EF57CECAC2816E3122816E31A0A00F8377CD95DFA484CFAE282656A58"
"CE5AA29652EFFD80AC59CD91416E4E13DBBE";
class ETLTransformerTest : public MockBackendTest
{
protected:
using DataType = FakeFetchResponse;
using ExtractionDataPipeType = MockExtractionDataPipe;
using LedgerLoaderType = MockLedgerLoader;
using LedgerPublisherType = MockLedgerPublisher;
using TransformerType = clio::detail::Transformer<ExtractionDataPipeType, LedgerLoaderType, LedgerPublisherType>;
ExtractionDataPipeType dataPipe_;
LedgerLoaderType ledgerLoader_;
LedgerPublisherType ledgerPublisher_;
SystemState state_;
std::unique_ptr<TransformerType> transformer_;
public:
void
SetUp() override
{
MockBackendTest::SetUp();
state_.isStopping = false;
state_.writeConflict = false;
state_.isReadOnly = false;
state_.isWriting = false;
}
void
TearDown() override
{
transformer_.reset();
MockBackendTest::TearDown();
}
};
TEST_F(ETLTransformerTest, StopsOnWriteConflict)
{
state_.writeConflict = true;
EXPECT_CALL(dataPipe_, popNext).Times(0);
EXPECT_CALL(ledgerPublisher_, publish(_)).Times(0);
transformer_ =
std::make_unique<TransformerType>(dataPipe_, mockBackendPtr, ledgerLoader_, ledgerPublisher_, 0, state_);
transformer_->waitTillFinished(); // explicitly joins the thread
}
TEST_F(ETLTransformerTest, StopsOnEmptyFetchResponse)
{
MockBackend* rawBackendPtr = static_cast<MockBackend*>(mockBackendPtr.get());
mockBackendPtr->cache().setFull(); // to avoid throwing exception in updateCache
auto const blob = hexStringToBinaryString(RAW_HEADER);
auto const response = std::make_optional<FakeFetchResponse>(blob);
ON_CALL(dataPipe_, popNext).WillByDefault([this, &response](auto) -> std::optional<FakeFetchResponse> {
if (state_.isStopping)
return std::nullopt;
return response;
});
ON_CALL(*rawBackendPtr, doFinishWrites).WillByDefault(Return(true));
// TODO: most of this should be hidden in a smaller entity that is injected into the transformer thread
EXPECT_CALL(dataPipe_, popNext).Times(AtLeast(1));
EXPECT_CALL(*rawBackendPtr, startWrites).Times(AtLeast(1));
EXPECT_CALL(*rawBackendPtr, writeLedger(_, _)).Times(AtLeast(1));
EXPECT_CALL(ledgerLoader_, insertTransactions).Times(AtLeast(1));
EXPECT_CALL(*rawBackendPtr, writeAccountTransactions).Times(AtLeast(1));
EXPECT_CALL(*rawBackendPtr, writeNFTs).Times(AtLeast(1));
EXPECT_CALL(*rawBackendPtr, writeNFTTransactions).Times(AtLeast(1));
EXPECT_CALL(*rawBackendPtr, doFinishWrites).Times(AtLeast(1));
EXPECT_CALL(ledgerPublisher_, publish(_)).Times(AtLeast(1));
transformer_ =
std::make_unique<TransformerType>(dataPipe_, mockBackendPtr, ledgerLoader_, ledgerPublisher_, 0, state_);
// after 10ms we start spitting out empty responses which means the extractor is finishing up
// this is normally combined with stopping the entire thing by setting the isStopping flag.
std::this_thread::sleep_for(std::chrono::milliseconds{10});
state_.isStopping = true;
}
TEST_F(ETLTransformerTest, DoesNotPublishIfCanNotBuildNextLedger)
{
MockBackend* rawBackendPtr = static_cast<MockBackend*>(mockBackendPtr.get());
mockBackendPtr->cache().setFull(); // to avoid throwing exception in updateCache
auto const blob = hexStringToBinaryString(RAW_HEADER);
auto const response = std::make_optional<FakeFetchResponse>(blob);
ON_CALL(dataPipe_, popNext).WillByDefault(Return(response));
ON_CALL(*rawBackendPtr, doFinishWrites).WillByDefault(Return(false)); // emulate write failure
// TODO: most of this should be hidden in a smaller entity that is injected into the transformer thread
EXPECT_CALL(dataPipe_, popNext).Times(AtLeast(1));
EXPECT_CALL(*rawBackendPtr, startWrites).Times(AtLeast(1));
EXPECT_CALL(*rawBackendPtr, writeLedger(_, _)).Times(AtLeast(1));
EXPECT_CALL(ledgerLoader_, insertTransactions).Times(AtLeast(1));
EXPECT_CALL(*rawBackendPtr, writeAccountTransactions).Times(AtLeast(1));
EXPECT_CALL(*rawBackendPtr, writeNFTs).Times(AtLeast(1));
EXPECT_CALL(*rawBackendPtr, writeNFTTransactions).Times(AtLeast(1));
EXPECT_CALL(*rawBackendPtr, doFinishWrites).Times(AtLeast(1));
// should not call publish
EXPECT_CALL(ledgerPublisher_, publish(_)).Times(0);
transformer_ =
std::make_unique<TransformerType>(dataPipe_, mockBackendPtr, ledgerLoader_, ledgerPublisher_, 0, state_);
}

View File

@@ -20,6 +20,119 @@
#pragma once
#include <cstddef>
#include <string>
#include <vector>
class FakeBook
{
std::string base_;
std::string first_;
public:
std::string*
mutable_first_book()
{
return &first_;
}
std::string
book_base() const
{
return base_;
}
std::string*
mutable_book_base()
{
return &base_;
}
};
class FakeBookSuccessors
{
std::vector<FakeBook> books_;
public:
auto
begin()
{
return books_.begin();
}
auto
end()
{
return books_.end();
}
};
class FakeLedgerObject
{
public:
enum ModType : int { MODIFIED, DELETED };
private:
std::string key_;
std::string data_;
std::string predecessor_;
std::string successor_;
ModType mod_ = MODIFIED;
public:
ModType
mod_type() const
{
return mod_;
}
std::string
key() const
{
return key_;
}
std::string*
mutable_key()
{
return &key_;
}
std::string
data() const
{
return data_;
}
std::string*
mutable_data()
{
return &data_;
}
std::string*
mutable_predecessor()
{
return &predecessor_;
}
std::string*
mutable_successor()
{
return &successor_;
}
};
class FakeLedgerObjects
{
std::vector<FakeLedgerObject> objects;
public:
std::vector<FakeLedgerObject>*
mutable_objects()
{
return &objects;
}
};
class FakeTransactionsList
{
@@ -33,11 +146,33 @@ public:
}
};
class FakeObjectsList
{
std::size_t size_ = 0;
public:
std::size_t
objects_size()
{
return size_;
}
};
struct FakeFetchResponse
{
uint32_t id;
bool objectNeighborsIncluded;
FakeLedgerObjects ledgerObjects;
std::string ledgerHeader;
FakeBookSuccessors bookSuccessors;
FakeFetchResponse(uint32_t id = 0) : id{id}
FakeFetchResponse(uint32_t id = 0, bool objectNeighborsIncluded = false)
: id{id}, objectNeighborsIncluded{objectNeighborsIncluded}
{
}
FakeFetchResponse(std::string blob, uint32_t id = 0, bool objectNeighborsIncluded = false)
: id{id}, objectNeighborsIncluded{objectNeighborsIncluded}, ledgerHeader{blob}
{
}
@@ -52,4 +187,40 @@ struct FakeFetchResponse
{
return {};
}
FakeObjectsList
ledger_objects() const
{
return {};
}
bool
object_neighbors_included() const
{
return objectNeighborsIncluded;
}
FakeLedgerObjects*
mutable_ledger_objects()
{
return &ledgerObjects;
}
std::string
ledger_header() const
{
return ledgerHeader;
}
std::string*
mutable_ledger_header()
{
return &ledgerHeader;
}
FakeBookSuccessors*
mutable_book_successors()
{
return &bookSuccessors;
}
};

View File

@@ -23,11 +23,10 @@
#include <chrono>
template <typename DataType>
struct MockExtractionDataPipe
{
MOCK_METHOD(void, push, (uint32_t, std::optional<DataType>&&), ());
MOCK_METHOD(std::optional<DataType>, popNext, (uint32_t), ());
MOCK_METHOD(void, push, (uint32_t, std::optional<FakeFetchResponse>&&), ());
MOCK_METHOD(std::optional<FakeFetchResponse>, popNext, (uint32_t), ());
MOCK_METHOD(uint32_t, getStride, (), (const));
MOCK_METHOD(void, finish, (uint32_t), ());
MOCK_METHOD(void, cleanup, (), ());

View File

@@ -19,13 +19,14 @@
#pragma once
#include <util/FakeFetchResponse.h>
#include <gmock/gmock.h>
#include <optional>
template <typename DataType>
struct MockLedgerFetcher
{
MOCK_METHOD(std::optional<DataType>, fetchData, (uint32_t), ());
MOCK_METHOD(std::optional<DataType>, fetchDataAndDiff, (uint32_t), ());
MOCK_METHOD(std::optional<FakeFetchResponse>, fetchData, (uint32_t), ());
MOCK_METHOD(std::optional<FakeFetchResponse>, fetchDataAndDiff, (uint32_t), ());
};

View File

@@ -0,0 +1,39 @@
//------------------------------------------------------------------------------
/*
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 <etl/impl/LedgerLoader.h>
#include <gmock/gmock.h>
#include <optional>
struct MockLedgerLoader
{
using GetLedgerResponseType = FakeFetchResponse;
using RawLedgerObjectType = FakeLedgerObject;
MOCK_METHOD(
FormattedTransactionsData,
insertTransactions,
(ripple::LedgerInfo const&, GetLedgerResponseType& data),
());
MOCK_METHOD(std::optional<ripple::LedgerInfo>, loadInitialLedger, (uint32_t sequence), ());
};

View File

@@ -0,0 +1,34 @@
//------------------------------------------------------------------------------
/*
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 <gmock/gmock.h>
#include <optional>
struct MockLedgerPublisher
{
MOCK_METHOD(bool, publish, (uint32_t, std::optional<uint32_t>), ());
MOCK_METHOD(void, publish, (ripple::LedgerInfo const&), ());
MOCK_METHOD(std::uint32_t, lastPublishAgeSeconds, (), (const));
MOCK_METHOD(std::chrono::time_point<std::chrono::system_clock>, getLastPublish, (), (const));
MOCK_METHOD(std::uint32_t, lastCloseAgeSeconds, (), (const));
MOCK_METHOD(std::optional<uint32_t>, getLastPublishedSequence, (), (const));
};

View File

@@ -20,20 +20,20 @@
#pragma once
#include <etl/Source.h>
#include <util/FakeFetchResponse.h>
#include <boost/asio/spawn.hpp>
#include <boost/json.hpp>
#include <gmock/gmock.h>
#include "org/xrpl/rpc/v1/xrp_ledger.grpc.pb.h"
#include <grpcpp/grpcpp.h>
#include <optional>
struct MockLoadBalancer
{
using RawLedgerObjectType = FakeLedgerObject;
MOCK_METHOD(void, loadInitialLedger, (std::uint32_t, bool), ());
MOCK_METHOD(std::optional<org::xrpl::rpc::v1::GetLedgerResponse>, fetchLedger, (uint32_t, bool, bool), ());
MOCK_METHOD(std::optional<FakeFetchResponse>, fetchLedger, (uint32_t, bool, bool), ());
MOCK_METHOD(bool, shouldPropagateTxnStream, (Source*), (const));
MOCK_METHOD(boost::json::value, toJson, (), (const));
MOCK_METHOD(

View File

@@ -0,0 +1,52 @@
//------------------------------------------------------------------------------
/*
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 <util/StringUtils.h>
#include <rpc/RPCHelpers.h>
std::string
hexStringToBinaryString(std::string const& hex)
{
auto const blob = ripple::strUnHex(hex);
std::string strBlob;
for (auto c : *blob)
strBlob += c;
return strBlob;
}
ripple::uint256
binaryStringToUint256(std::string const& bin)
{
ripple::uint256 uint;
return uint.fromVoid((void const*)bin.data());
}
std::string
ledgerInfoToBinaryString(ripple::LedgerInfo const& info)
{
auto const blob = RPC::ledgerInfoToBlob(info, true);
std::string strBlob;
for (auto c : blob)
strBlob += c;
return strBlob;
};

View File

@@ -0,0 +1,34 @@
//------------------------------------------------------------------------------
/*
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 <ripple/basics/base_uint.h>
#include <ripple/ledger/ReadView.h>
#include <string>
std::string
hexStringToBinaryString(std::string const& hex);
ripple::uint256
binaryStringToUint256(std::string const& bin);
std::string
ledgerInfoToBinaryString(ripple::LedgerInfo const& info);