Skip to content

Commit

Permalink
fix: LedgerEntryNotExist unittest failure (#1564)
Browse files Browse the repository at this point in the history
  • Loading branch information
cindyyan317 authored Jul 29, 2024
1 parent 3a3d8d4 commit 8d0e904
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/rpc/handlers/LedgerEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "rpc/RPCHelpers.hpp"
#include "rpc/common/Types.hpp"
#include "util/AccountUtils.hpp"
#include "util/Assert.hpp"

#include <boost/json/conversion.hpp>
#include <boost/json/object.hpp>
Expand All @@ -33,6 +34,7 @@
#include <xrpl/basics/strHex.h>
#include <xrpl/json/json_value.h>
#include <xrpl/protocol/AccountID.h>
#include <xrpl/protocol/ErrorCodes.h>
#include <xrpl/protocol/Indexes.h>
#include <xrpl/protocol/Issue.h>
#include <xrpl/protocol/LedgerFormats.h>
Expand Down Expand Up @@ -153,6 +155,7 @@ LedgerEntryHandler::process(LedgerEntryHandler::Input input, Context const& ctx)

// check ledger exists
auto const range = sharedPtrBackend_->fetchLedgerRange();
ASSERT(range.has_value(), "Ledger range must be available");
auto const lgrInfoOrStatus = getLedgerHeaderFromHashOrSeq(
*sharedPtrBackend_, ctx.yield, input.ledgerHash, input.ledgerIndex, range->maxSequence
);
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/rpc/handlers/LedgerEntryTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
#include "util/NameGenerator.hpp"
#include "util/TestObject.hpp"

#include <boost/asio/executor_work_guard.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/json/parse.hpp>
#include <boost/json/value.hpp>
#include <boost/json/value_to.hpp>
Expand Down Expand Up @@ -2726,6 +2729,7 @@ TEST_F(RPCLedgerEntryTest, LedgerEntryDeleted)
// Expected Result: return entryNotFound error
TEST_F(RPCLedgerEntryTest, LedgerEntryNotExist)
{
backend->setRange(RANGEMIN, RANGEMAX);
auto const ledgerinfo = CreateLedgerHeader(LEDGERHASH, RANGEMAX);
EXPECT_CALL(*backend, fetchLedgerBySequence(RANGEMAX, _)).WillRepeatedly(Return(ledgerinfo));
EXPECT_CALL(*backend, doFetchLedgerObject(ripple::uint256{INDEX1}, RANGEMAX, _))
Expand Down Expand Up @@ -2916,6 +2920,7 @@ TEST_F(RPCLedgerEntryTest, ObjectDeletedPreviously)
// Expected Result: return entryNotFound error
TEST_F(RPCLedgerEntryTest, ObjectSeqNotExist)
{
backend->setRange(RANGEMIN, RANGEMAX);
auto const ledgerinfo = CreateLedgerHeader(LEDGERHASH, RANGEMAX);
EXPECT_CALL(*backend, fetchLedgerBySequence(RANGEMAX, _)).WillRepeatedly(Return(ledgerinfo));
EXPECT_CALL(*backend, doFetchLedgerObject(ripple::uint256{INDEX1}, RANGEMAX, _))
Expand All @@ -2938,3 +2943,27 @@ TEST_F(RPCLedgerEntryTest, ObjectSeqNotExist)
EXPECT_EQ(myerr, "entryNotFound");
});
}

using RPCLedgerEntryDeathTest = RPCLedgerEntryTest;

TEST_F(RPCLedgerEntryDeathTest, RangeNotAvailable)
{
boost::asio::io_context ctx;
bool checkCalled = false;
spawn(ctx, [&, _ = make_work_guard(ctx)](boost::asio::yield_context yield) {
auto const handler = AnyHandler{LedgerEntryHandler{backend}};
auto const req = json::parse(fmt::format(
R"({{
"index": "{}"
}})",
INDEX1
));
checkCalled = true;
EXPECT_DEATH(
{ [[maybe_unused]] auto __ = handler.process(req, Context{yield}); }, "Ledger range must be available"
);
});

ctx.run();
ASSERT_TRUE(checkCalled);
}

0 comments on commit 8d0e904

Please sign in to comment.