Skip to content

Commit 3927617

Browse files
committed
Add test
1 parent 45037ad commit 3927617

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

src/etlng/impl/GrpcSource.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ resolve(std::string const& ip, std::string const& port)
6565

6666
namespace etlng::impl {
6767

68-
GrpcSource::GrpcSource(std::string const& ip, std::string const& grpcPort)
68+
GrpcSource::GrpcSource(std::string const& ip, std::string const& grpcPort, std::chrono::system_clock::duration deadline)
6969
: log_(fmt::format("ETL_Grpc[{}:{}]", ip, grpcPort))
7070
, initialLoadShouldStop_(std::make_unique<std::atomic_bool>(false))
71+
, deadline_{deadline}
7172
{
7273
try {
7374
grpc::ChannelArguments chArgs;
@@ -97,7 +98,7 @@ GrpcSource::fetchLedger(uint32_t sequence, bool getObjects, bool getObjectNeighb
9798
org::xrpl::rpc::v1::GetLedgerRequest request;
9899
grpc::ClientContext context;
99100

100-
context.set_deadline(std::chrono::system_clock::now() + kDEADLINE); // Prevent indefinite blocking
101+
context.set_deadline(std::chrono::system_clock::now() + deadline_); // Prevent indefinite blocking
101102

102103
request.mutable_ledger()->set_sequence(sequence);
103104
request.set_transactions(true);

src/etlng/impl/GrpcSource.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class GrpcSource {
4141
util::Logger log_;
4242
std::unique_ptr<org::xrpl::rpc::v1::XRPLedgerAPIService::Stub> stub_;
4343
std::unique_ptr<std::atomic_bool> initialLoadShouldStop_;
44+
std::chrono::system_clock::duration deadline_;
4445

4546
static constexpr auto kKEEPALIVE_PING_INTERVAL_MS = 10000;
4647
static constexpr auto kKEEPALIVE_TIMEOUT_MS = 5000;
@@ -49,7 +50,11 @@ class GrpcSource {
4950
static constexpr auto kDEADLINE = std::chrono::seconds(30);
5051

5152
public:
52-
GrpcSource(std::string const& ip, std::string const& grpcPort);
53+
GrpcSource(
54+
std::string const& ip,
55+
std::string const& grpcPort,
56+
std::chrono::system_clock::duration deadline = kDEADLINE
57+
);
5358

5459
/**
5560
* @brief Fetch data for a specific ledger.

tests/common/util/MockXrpLedgerAPIService.hpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
#include <org/xrpl/rpc/v1/get_ledger_entry.pb.h>
3333
#include <xrpl/proto/org/xrpl/rpc/v1/xrp_ledger.grpc.pb.h>
3434

35+
#include <chrono>
3536
#include <memory>
37+
#include <optional>
3638
#include <string>
3739
#include <thread>
3840

@@ -90,15 +92,27 @@ struct WithMockXrpLedgerAPIService : virtual ::testing::Test {
9092

9193
~WithMockXrpLedgerAPIService() override
9294
{
93-
server_->Shutdown();
94-
serverThread_.join();
95+
shutdown();
9596
}
9697

9798
int
9899
getXRPLMockPort() const
99100
{
100101
return port_;
101102
}
103+
104+
void
105+
shutdown(std::optional<std::chrono::system_clock::duration> deadline = std::nullopt)
106+
{
107+
if (deadline.has_value()) {
108+
server_->Shutdown(std::chrono::system_clock::now() + *deadline);
109+
} else {
110+
server_->Shutdown();
111+
}
112+
if (serverThread_.joinable())
113+
serverThread_.join();
114+
}
115+
102116
MockXrpLedgerAPIService mockXrpLedgerAPIService;
103117

104118
private:

tests/unit/etlng/GrpcSourceTests.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,20 @@
4141
#include <xrpl/basics/strHex.h>
4242

4343
#include <atomic>
44+
#include <chrono>
4445
#include <condition_variable>
4546
#include <cstddef>
4647
#include <cstdint>
4748
#include <functional>
4849
#include <future>
4950
#include <map>
51+
#include <memory>
5052
#include <mutex>
5153
#include <optional>
5254
#include <queue>
55+
#include <semaphore>
5356
#include <string>
57+
#include <thread>
5458
#include <vector>
5559

5660
using namespace etlng::model;
@@ -357,3 +361,40 @@ TEST_F(GrpcSourceStopTests, LoadInitialLedgerStopsWhenRequested)
357361
ASSERT_FALSE(res.has_value());
358362
EXPECT_EQ(res.error(), etlng::InitialLedgerLoadError::Cancelled);
359363
}
364+
365+
TEST_F(GrpcSourceNgTests, DeadlineIsHandledCorrectly)
366+
{
367+
uint32_t const sequence = 123u;
368+
bool const getObjects = true;
369+
bool const getObjectNeighbors = false;
370+
371+
std::mutex mtx;
372+
std::condition_variable cv;
373+
bool finished = false;
374+
375+
auto grpcSource = std::make_unique<etlng::impl::GrpcSource>(
376+
"localhost", std::to_string(getXRPLMockPort()), std::chrono::milliseconds{1}
377+
);
378+
379+
EXPECT_CALL(mockXrpLedgerAPIService, GetLedger)
380+
.WillOnce([&](grpc::ServerContext*,
381+
org::xrpl::rpc::v1::GetLedgerRequest const*,
382+
org::xrpl::rpc::v1::GetLedgerResponse*) {
383+
std::unique_lock lk(mtx);
384+
cv.wait(lk, [&] { return finished; });
385+
386+
return grpc::Status{};
387+
});
388+
389+
auto const [status, response] = grpcSource->fetchLedger(sequence, getObjects, getObjectNeighbors);
390+
391+
{
392+
std::unique_lock lk(mtx);
393+
finished = true;
394+
}
395+
cv.notify_all();
396+
grpcSource.reset();
397+
398+
ASSERT_FALSE(status.ok());
399+
shutdown(std::chrono::milliseconds{10});
400+
}

0 commit comments

Comments
 (0)