Skip to content

Commit

Permalink
Improve naming and spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
godexsoft committed Nov 20, 2024
1 parent 8de149d commit 4c3a352
Showing 1 changed file with 37 additions and 31 deletions.
68 changes: 37 additions & 31 deletions tests/unit/etlng/GrpcSourceTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
//==============================================================================

#include "data/DBHelpers.hpp"
#include "etl/ETLHelpers.hpp"
#include "etl/impl/GrpcSource.hpp"
#include "etlng/InitialLoadObserverInterface.hpp"
Expand Down Expand Up @@ -68,8 +69,8 @@ struct GrpcSourceNgTests : NoLoggerFixture, util::prometheus::WithPrometheus, te
{
}

testing::StrictMock<MockLoadObserver> loader_;
testing::StrictMock<etlng::impl::GrpcSource> grpcSource_;
testing::StrictMock<MockLoadObserver> observer_;
etlng::impl::GrpcSource grpcSource_;
};

class KeyStore {
Expand All @@ -83,6 +84,7 @@ class KeyStore {
{
auto const totalPerMarker = totalKeys / numMarkers;
auto const markers = etl::getMarkers(numMarkers);

for (auto mi = 0uz; mi < markers.size(); ++mi) {
for (auto i = 0uz; i < totalPerMarker; ++i) {
auto const mapKey = ripple::strHex(markers.at(mi)).substr(0, 2);
Expand All @@ -97,17 +99,17 @@ class KeyStore {
std::scoped_lock lock(mtx_);

auto const mapKey = ripple::strHex(marker).substr(0, 2);
auto k = store_.lower_bound(mapKey);
ASSERT(k != store_.end(), "Lower bound not found for '{}'", mapKey);
auto it = store_.lower_bound(mapKey);
ASSERT(it != store_.end(), "Lower bound not found for '{}'", mapKey);

auto& q = k->second;
if (q.empty())
auto& queue = it->second;
if (queue.empty())
return std::nullopt;

auto t = q.front();
q.pop();
auto data = queue.front();
queue.pop();

return std::make_optional<std::string>(reinterpret_cast<char const*>(t.data()), ripple::uint256::size());
return std::make_optional(uint256ToString(data));
};

std::optional<std::string>
Expand All @@ -116,23 +118,23 @@ class KeyStore {
std::scoped_lock lock(mtx_);

auto const mapKey = ripple::strHex(marker).substr(0, 2);
auto k = store_.lower_bound(mapKey);
ASSERT(k != store_.end(), "Lower bound not found for '{}'", mapKey);
auto it = store_.lower_bound(mapKey);
ASSERT(it != store_.end(), "Lower bound not found for '{}'", mapKey);

auto& q = k->second;
if (q.empty())
auto& queue = it->second;
if (queue.empty())
return std::nullopt;

auto t = q.front();
return std::make_optional<std::string>(reinterpret_cast<char const*>(t.data()), ripple::uint256::size());
auto data = queue.front();
return std::make_optional(uint256ToString(data));
};
};

} // namespace

TEST_F(GrpcSourceNgTests, BasicFetchLedger)
{
uint32_t const sequence = 123;
uint32_t const sequence = 123u;
bool const getObjects = true;
bool const getObjectNeighbors = false;

Expand All @@ -146,11 +148,14 @@ TEST_F(GrpcSourceNgTests, BasicFetchLedger)
EXPECT_EQ(request->get_objects(), getObjects);
EXPECT_EQ(request->get_object_neighbors(), getObjectNeighbors);
EXPECT_EQ(request->user(), "ETL");

response->set_validated(true);
response->set_is_unlimited(false);
response->set_object_neighbors_included(false);

return grpc::Status{};
});

auto const [status, response] = grpcSource_.fetchLedger(sequence, getObjects, getObjectNeighbors);
ASSERT_TRUE(status.ok());
EXPECT_TRUE(response.validated());
Expand All @@ -159,8 +164,8 @@ TEST_F(GrpcSourceNgTests, BasicFetchLedger)
}

struct GrpcSourceLoadInitialLedgerTests : GrpcSourceNgTests {
uint32_t const sequence_ = 123;
uint32_t const numMarkers_ = 4;
uint32_t const sequence_ = 123u;
uint32_t const numMarkers_ = 4u;
bool const cacheOnly_ = false;
};

Expand All @@ -173,18 +178,19 @@ TEST_F(GrpcSourceLoadInitialLedgerTests, GetLedgerDataNotFound)
org::xrpl::rpc::v1::GetLedgerDataResponse* /*response*/) {
EXPECT_EQ(request->ledger().sequence(), sequence_);
EXPECT_EQ(request->user(), "ETL");

return grpc::Status{grpc::StatusCode::NOT_FOUND, "Not found"};
});

auto const [data, success] = grpcSource_.loadInitialLedger(sequence_, numMarkers_, loader_);
auto const [data, success] = grpcSource_.loadInitialLedger(sequence_, numMarkers_, observer_);
EXPECT_TRUE(data.empty());
EXPECT_FALSE(success);
}

TEST_F(GrpcSourceLoadInitialLedgerTests, LoaderCalledCorrectly)
TEST_F(GrpcSourceLoadInitialLedgerTests, ObserverCalledCorrectly)
{
auto const key = ripple::uint256{4};
std::string const keyStr{reinterpret_cast<char const*>(key.data()), ripple::uint256::size()};
auto const keyStr = uint256ToString(key);
auto const object = CreateTicketLedgerObject("rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn", sequence_);
auto const objectData = object.getSerializer().peekData();

Expand All @@ -198,28 +204,28 @@ TEST_F(GrpcSourceLoadInitialLedgerTests, LoaderCalledCorrectly)

response->set_is_unlimited(true);
auto newObject = response->mutable_ledger_objects()->add_objects();
newObject->set_key(reinterpret_cast<char const*>(key.data()), ripple::uint256::size());
newObject->set_key(uint256ToString(key));
newObject->set_data(objectData.data(), objectData.size());

return grpc::Status{};
});

EXPECT_CALL(loader_, onInitialLoadGotMoreObjects)
EXPECT_CALL(observer_, onInitialLoadGotMoreObjects)
.Times(numMarkers_)
.WillRepeatedly([&](uint32_t, std::vector<Object> const& data, std::optional<std::string> lastKey) {
EXPECT_FALSE(lastKey.has_value());
EXPECT_EQ(data.size(), 1);
});

auto const [data, success] = grpcSource_.loadInitialLedger(sequence_, numMarkers_, loader_);
auto const [data, success] = grpcSource_.loadInitialLedger(sequence_, numMarkers_, observer_);

EXPECT_TRUE(success);
EXPECT_EQ(data.size(), numMarkers_);

EXPECT_EQ(data, std::vector<std::string>(4, keyStr));
}

TEST_F(GrpcSourceLoadInitialLedgerTests, DataTransferredAndLoaderCalledCorrectly)
TEST_F(GrpcSourceLoadInitialLedgerTests, DataTransferredAndObserverCalledCorrectly)
{
auto const totalKeys = 256uz;
auto const totalPerMarker = totalKeys / numMarkers_;
Expand Down Expand Up @@ -253,31 +259,31 @@ TEST_F(GrpcSourceLoadInitialLedgerTests, DataTransferredAndLoaderCalledCorrectly
}

if (auto maybeNext = keyStore.peek(next); maybeNext.has_value())
response->set_marker(*maybeNext); // hack
response->set_marker(*maybeNext);

return grpc::Status::OK;
});

std::atomic_int total = 0;
testing::InSequence seqGuard;
std::atomic_uint total = 0u;
[[maybe_unused]] testing::InSequence seqGuard;

EXPECT_CALL(loader_, onInitialLoadGotMoreObjects)
EXPECT_CALL(observer_, onInitialLoadGotMoreObjects)
.Times(numMarkers_)
.WillRepeatedly([&](uint32_t, std::vector<Object> const& data, std::optional<std::string> lastKey) {
EXPECT_LE(data.size(), batchSize);
EXPECT_FALSE(lastKey.has_value());
total += data.size();
});

EXPECT_CALL(loader_, onInitialLoadGotMoreObjects)
EXPECT_CALL(observer_, onInitialLoadGotMoreObjects)
.Times((numMarkers_ - 1) * batchesPerMarker)
.WillRepeatedly([&](uint32_t, std::vector<Object> const& data, std::optional<std::string> lastKey) {
EXPECT_LE(data.size(), batchSize);
EXPECT_TRUE(lastKey.has_value());
total += data.size();
});

auto const [data, success] = grpcSource_.loadInitialLedger(sequence_, numMarkers_, loader_);
auto const [data, success] = grpcSource_.loadInitialLedger(sequence_, numMarkers_, observer_);

EXPECT_TRUE(success);
EXPECT_EQ(data.size(), numMarkers_);
Expand Down

0 comments on commit 4c3a352

Please sign in to comment.