Skip to content

Commit

Permalink
Make stub protected
Browse files Browse the repository at this point in the history
  • Loading branch information
yperbasis committed Sep 18, 2024
1 parent 762dc1f commit 054dae6
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 34 deletions.
2 changes: 1 addition & 1 deletion silkworm/db/kv/grpc/client/remote_cursor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class RemoteCursorTest : public test_util::KVTestBase {

// Execute the test preconditions: start a new Tx RPC and read first incoming message (tx_id)
Task<::remote::Pair> start_and_read_tx_id() {
if (!co_await tx_rpc_.start(*stub)) {
if (!co_await tx_rpc_.start(*stub_)) {
const auto status = co_await tx_rpc_.finish();
throw boost::system::system_error{rpc::to_system_code(status.error_code())};
}
Expand Down
38 changes: 19 additions & 19 deletions silkworm/db/kv/grpc/client/remote_transaction_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ using testing::_;
namespace proto = ::remote;
namespace test = rpc::test;

struct RemoteTransactionTest : db::test_util::KVTestBase {
private:
api::CoherentStateCache state_cache_;

class RemoteTransactionTest : public db::test_util::KVTestBase {
public:
RemoteTransaction remote_tx{*stub,
RemoteTransaction remote_tx{*stub_,
grpc_context_,
&state_cache_,
{},
{}};

private:
api::CoherentStateCache state_cache_;
};

static remote::Pair make_fake_tx_created_pair() {
Expand Down Expand Up @@ -446,7 +446,7 @@ TEST_CASE_METHOD(RemoteTransactionTest, "RemoteTransaction::domain_get", "[db][k
};

rpc::test::StrictMockAsyncResponseReader<proto::DomainGetReply> reader;
EXPECT_CALL(*stub, AsyncDomainGetRaw).WillOnce(testing::Return(&reader));
EXPECT_CALL(*stub_, AsyncDomainGetRaw).WillOnce(testing::Return(&reader));

api::DomainPointResult result;

Expand Down Expand Up @@ -489,7 +489,7 @@ TEST_CASE_METHOD(RemoteTransactionTest, "RemoteTransaction::history_seek", "[db]
};

rpc::test::StrictMockAsyncResponseReader<proto::HistorySeekReply> reader;
EXPECT_CALL(*stub, AsyncHistorySeekRaw).WillOnce(testing::Return(&reader));
EXPECT_CALL(*stub_, AsyncHistorySeekRaw).WillOnce(testing::Return(&reader));

api::HistoryPointResult result;

Expand Down Expand Up @@ -541,7 +541,7 @@ TEST_CASE_METHOD(RemoteTransactionTest, "RemoteTransaction::index_range", "[db][
SECTION("throw on error") {
// Set the call expectations:
// 1. remote::KV::StubInterface::AsyncIndexRangeRaw call succeeds
EXPECT_CALL(*stub, AsyncIndexRangeRaw).WillOnce(Return(&reader));
EXPECT_CALL(*stub_, AsyncIndexRangeRaw).WillOnce(Return(&reader));
// 2. AsyncResponseReader<>::Finish call fails
EXPECT_CALL(reader, Finish).WillOnce(test::finish_error_aborted(grpc_context_, proto::IndexRangeReply{}));
// Execute the test: trying to *use* index_range lazy result should throw
Expand All @@ -550,7 +550,7 @@ TEST_CASE_METHOD(RemoteTransactionTest, "RemoteTransaction::index_range", "[db][
SECTION("success: empty") {
// Set the call expectations:
// 1. remote::KV::StubInterface::AsyncIndexRangeRaw call succeeds
EXPECT_CALL(*stub, AsyncIndexRangeRaw).WillRepeatedly(Return(&reader));
EXPECT_CALL(*stub_, AsyncIndexRangeRaw).WillRepeatedly(Return(&reader));
// 2. AsyncResponseReader<>::Finish call succeeds 3 times
EXPECT_CALL(reader, Finish)
.WillOnce(test::finish_with(grpc_context_, make_index_range_reply({}, /*has_more*/ false)));
Expand All @@ -561,7 +561,7 @@ TEST_CASE_METHOD(RemoteTransactionTest, "RemoteTransaction::index_range", "[db][
SECTION("success: one page") {
// Set the call expectations:
// 1. remote::KV::StubInterface::AsyncIndexRangeRaw call succeeds
EXPECT_CALL(*stub, AsyncIndexRangeRaw).WillRepeatedly(Return(&reader));
EXPECT_CALL(*stub_, AsyncIndexRangeRaw).WillRepeatedly(Return(&reader));
// 2. AsyncResponseReader<>::Finish call succeeds
EXPECT_CALL(reader, Finish)
.WillOnce(test::finish_with(grpc_context_, make_index_range_reply({19}, /*has_more*/ false)));
Expand All @@ -584,7 +584,7 @@ TEST_CASE_METHOD(RemoteTransactionTest, "RemoteTransaction::index_range", "[db][
// 4. AsyncReaderWriter<remote::Cursor, remote::Pair>::Finish call succeeds w/ status OK
EXPECT_CALL(reader_writer_, Finish).WillOnce(test::finish_streaming_ok(grpc_context_));
// 5. remote::KV::StubInterface::AsyncIndexRangeRaw call succeeds
EXPECT_CALL(*stub, AsyncIndexRangeRaw).WillRepeatedly(Return(&reader));
EXPECT_CALL(*stub_, AsyncIndexRangeRaw).WillRepeatedly(Return(&reader));
// 6. AsyncResponseReader<>::Finish call succeeds 3 times
EXPECT_CALL(reader, Finish)
.WillOnce(test::finish_with(grpc_context_, make_index_range_reply({1, 2, 3}, /*has_more*/ true)))
Expand Down Expand Up @@ -637,7 +637,7 @@ TEST_CASE_METHOD(RemoteTransactionTest, "RemoteTransaction::history_range", "[db
SECTION("throw on error") {
// Set the call expectations:
// 1. remote::KV::StubInterface::AsyncHistoryRangeRaw call succeeds
EXPECT_CALL(*stub, AsyncHistoryRangeRaw).WillOnce(Return(&reader));
EXPECT_CALL(*stub_, AsyncHistoryRangeRaw).WillOnce(Return(&reader));
// 2. AsyncResponseReader<>::Finish call fails
EXPECT_CALL(reader, Finish).WillOnce(test::finish_error_aborted(grpc_context_, proto::Pairs{}));
// Execute the test: trying to *use* index_range lazy result should throw
Expand All @@ -646,7 +646,7 @@ TEST_CASE_METHOD(RemoteTransactionTest, "RemoteTransaction::history_range", "[db
SECTION("success: empty") {
// Set the call expectations:
// 1. remote::KV::StubInterface::AsyncHistoryRangeRaw call succeeds
EXPECT_CALL(*stub, AsyncHistoryRangeRaw).WillRepeatedly(Return(&reader));
EXPECT_CALL(*stub_, AsyncHistoryRangeRaw).WillRepeatedly(Return(&reader));
// 2. AsyncResponseReader<>::Finish call succeeds 3 times
EXPECT_CALL(reader, Finish)
.WillOnce(test::finish_with(grpc_context_, make_key_value_range_reply({}, /*has_more*/ false)));
Expand All @@ -657,7 +657,7 @@ TEST_CASE_METHOD(RemoteTransactionTest, "RemoteTransaction::history_range", "[db
SECTION("success: one page") {
// Set the call expectations:
// 1. remote::KV::StubInterface::AsyncHistoryRangeRaw call succeeds
EXPECT_CALL(*stub, AsyncHistoryRangeRaw).WillRepeatedly(Return(&reader));
EXPECT_CALL(*stub_, AsyncHistoryRangeRaw).WillRepeatedly(Return(&reader));
// 2. AsyncResponseReader<>::Finish call succeeds
EXPECT_CALL(reader, Finish)
.WillOnce(test::finish_with(grpc_context_, make_key_value_range_reply({kv1}, /*has_more*/ false)));
Expand All @@ -680,7 +680,7 @@ TEST_CASE_METHOD(RemoteTransactionTest, "RemoteTransaction::history_range", "[db
// 4. AsyncReaderWriter<remote::Cursor, remote::Pair>::Finish call succeeds w/ status OK
EXPECT_CALL(reader_writer_, Finish).WillOnce(test::finish_streaming_ok(grpc_context_));
// 5. remote::KV::StubInterface::AsyncHistoryRangeRaw call succeeds
EXPECT_CALL(*stub, AsyncHistoryRangeRaw).WillRepeatedly(Return(&reader));
EXPECT_CALL(*stub_, AsyncHistoryRangeRaw).WillRepeatedly(Return(&reader));
// 6. AsyncResponseReader<>::Finish call succeeds 3 times
EXPECT_CALL(reader, Finish)
.WillOnce(test::finish_with(grpc_context_, make_key_value_range_reply({kv1, kv2}, /*has_more*/ true)))
Expand Down Expand Up @@ -723,7 +723,7 @@ TEST_CASE_METHOD(RemoteTransactionTest, "RemoteTransaction::domain_range", "[db]
SECTION("throw on error") {
// Set the call expectations:
// 1. remote::KV::StubInterface::AsyncDomainRangeRaw call succeeds
EXPECT_CALL(*stub, AsyncDomainRangeRaw).WillOnce(Return(&reader));
EXPECT_CALL(*stub_, AsyncDomainRangeRaw).WillOnce(Return(&reader));
// 2. AsyncResponseReader<>::Finish call fails
EXPECT_CALL(reader, Finish).WillOnce(test::finish_error_aborted(grpc_context_, proto::Pairs{}));
// Execute the test: trying to *use* index_range lazy result should throw
Expand All @@ -732,7 +732,7 @@ TEST_CASE_METHOD(RemoteTransactionTest, "RemoteTransaction::domain_range", "[db]
SECTION("success: empty") {
// Set the call expectations:
// 1. remote::KV::StubInterface::AsyncDomainRangeRaw call succeeds
EXPECT_CALL(*stub, AsyncDomainRangeRaw).WillRepeatedly(Return(&reader));
EXPECT_CALL(*stub_, AsyncDomainRangeRaw).WillRepeatedly(Return(&reader));
// 2. AsyncResponseReader<>::Finish call succeeds 3 times
EXPECT_CALL(reader, Finish)
.WillOnce(test::finish_with(grpc_context_, make_key_value_range_reply({}, /*has_more*/ false)));
Expand All @@ -743,7 +743,7 @@ TEST_CASE_METHOD(RemoteTransactionTest, "RemoteTransaction::domain_range", "[db]
SECTION("success: one page") {
// Set the call expectations:
// 1. remote::KV::StubInterface::AsyncDomainRangeRaw call succeeds
EXPECT_CALL(*stub, AsyncDomainRangeRaw).WillRepeatedly(Return(&reader));
EXPECT_CALL(*stub_, AsyncDomainRangeRaw).WillRepeatedly(Return(&reader));
// 2. AsyncResponseReader<>::Finish call succeeds
EXPECT_CALL(reader, Finish)
.WillOnce(test::finish_with(grpc_context_, make_key_value_range_reply({kv1}, /*has_more*/ false)));
Expand All @@ -766,7 +766,7 @@ TEST_CASE_METHOD(RemoteTransactionTest, "RemoteTransaction::domain_range", "[db]
// 4. AsyncReaderWriter<remote::Cursor, remote::Pair>::Finish call succeeds w/ status OK
EXPECT_CALL(reader_writer_, Finish).WillOnce(test::finish_streaming_ok(grpc_context_));
// 5. remote::KV::StubInterface::AsyncDomainRangeRaw call succeeds
EXPECT_CALL(*stub, AsyncDomainRangeRaw).WillRepeatedly(Return(&reader));
EXPECT_CALL(*stub_, AsyncDomainRangeRaw).WillRepeatedly(Return(&reader));
// 6. AsyncResponseReader<>::Finish call succeeds 3 times
EXPECT_CALL(reader, Finish)
.WillOnce(test::finish_with(grpc_context_, make_key_value_range_reply({kv1, kv2}, /*has_more*/ true)))
Expand Down
18 changes: 9 additions & 9 deletions silkworm/db/kv/state_changes_stream_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ TEST_CASE_METHOD(RemoteStateChangesStreamTest, "RemoteStateChangesStreamTest::op
// 2. AsyncReader<remote::StateChangeBatch>::Finish call succeeds w/ status cancelled
EXPECT_CALL(*statechanges_reader_, Finish).WillOnce(test::finish_streaming_cancelled(grpc_context_));
// Execute the test: opening the stream should succeed until finishes
RemoteClient remote_client{make_remote_client(std::move(stub))};
RemoteClient remote_client{make_remote_client(std::move(stub_))};
StateChangesStream stream{context_, remote_client};
std::future<void> run_completed;
CHECK_NOTHROW(run_completed = stream.open());
Expand All @@ -125,7 +125,7 @@ TEST_CASE_METHOD(RemoteStateChangesStreamTest, "RemoteStateChangesStreamTest::ru
SECTION("stream closed-by-peer") {
// Set the call expectations:
// 1. remote::KV::StubInterface::PrepareAsyncStateChangesRaw calls succeed
EXPECT_CALL(*stub, PrepareAsyncStateChangesRaw)
EXPECT_CALL(*stub_, PrepareAsyncStateChangesRaw)
.WillOnce(InvokeWithoutArgs([&]() {
// 2. AsyncReader<remote::StateChangeBatch>::StartCall call succeed
EXPECT_CALL(*statechanges_reader_, StartCall)
Expand All @@ -146,7 +146,7 @@ TEST_CASE_METHOD(RemoteStateChangesStreamTest, "RemoteStateChangesStreamTest::ru
return statechanges_reader_ptr_.release();
}));

RemoteClient remote_client{make_remote_client(std::move(stub))};
RemoteClient remote_client{make_remote_client(std::move(stub_))};
// remote_client.set_min_backoff_timeout(10ms);
// remote_client.set_max_backoff_timeout(100ms);
StateChangesStream stream{context_, remote_client};
Expand All @@ -163,7 +163,7 @@ TEST_CASE_METHOD(RemoteStateChangesStreamTest, "RemoteStateChangesStreamTest::ru
// 3. AsyncReader<remote::StateChangeBatch>::Finish call succeeds w/ status aborted
EXPECT_CALL(*statechanges_reader_, Finish).WillOnce(test::finish_streaming_aborted(grpc_context_));

RemoteClient remote_client{make_remote_client(std::move(stub))};
RemoteClient remote_client{make_remote_client(std::move(stub_))};
StateChangesStream stream{context_, remote_client};

// Execute the test: running the stream should succeed until finishes
Expand All @@ -180,7 +180,7 @@ TEST_CASE_METHOD(RemoteStateChangesStreamTest, "RemoteStateChangesStreamTest::ru
// 3. AsyncReader<remote::StateChangeBatch>::Finish call succeeds w/ status aborted
EXPECT_CALL(*statechanges_reader_, Finish).WillOnce(test::finish_streaming_aborted(grpc_context_));

RemoteClient remote_client{make_remote_client(std::move(stub))};
RemoteClient remote_client{make_remote_client(std::move(stub_))};
StateChangesStream stream{context_, remote_client};

// Execute the test: running the stream should succeed until finishes
Expand All @@ -194,7 +194,7 @@ TEST_CASE_METHOD(RemoteStateChangesStreamTest, "RemoteStateChangesStreamTest::cl
SECTION("while requesting w/ error every 10ms") {
// Set the call expectations:
// 1. remote::KV::StubInterface::PrepareAsyncStateChangesRaw calls succeed
EXPECT_CALL(*stub, PrepareAsyncStateChangesRaw)
EXPECT_CALL(*stub_, PrepareAsyncStateChangesRaw)
.WillRepeatedly(InvokeWithoutArgs([&]() {
static int counter{0};
if (counter > 0) {
Expand All @@ -216,7 +216,7 @@ TEST_CASE_METHOD(RemoteStateChangesStreamTest, "RemoteStateChangesStreamTest::cl
return statechanges_reader_ptr_.release();
}));

RemoteClient remote_client{make_remote_client(std::move(stub))};
RemoteClient remote_client{make_remote_client(std::move(stub_))};
remote_client.set_min_backoff_timeout(5ms);
remote_client.set_max_backoff_timeout(10ms);
StateChangesStream stream{context_, remote_client};
Expand All @@ -237,7 +237,7 @@ TEST_CASE_METHOD(RemoteStateChangesStreamTest, "RemoteStateChangesStreamTest::cl
SECTION("while reading w/ error every 10ms") {
// Set the call expectations:
// 1. remote::KV::StubInterface::PrepareAsyncStateChangesRaw calls succeed
EXPECT_CALL(*stub, PrepareAsyncStateChangesRaw)
EXPECT_CALL(*stub_, PrepareAsyncStateChangesRaw)
.WillRepeatedly(InvokeWithoutArgs([&]() {
static int counter{0};
if (counter > 0) {
Expand All @@ -263,7 +263,7 @@ TEST_CASE_METHOD(RemoteStateChangesStreamTest, "RemoteStateChangesStreamTest::cl
return statechanges_reader_ptr_.release();
}));

RemoteClient remote_client{make_remote_client(std::move(stub))};
RemoteClient remote_client{make_remote_client(std::move(stub_))};
remote_client.set_min_backoff_timeout(5ms);
remote_client.set_max_backoff_timeout(10ms);
StateChangesStream stream{context_, remote_client};
Expand Down
10 changes: 6 additions & 4 deletions silkworm/db/test_util/kv_test_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ using testing::Return;
class KVTestBase : public silkworm::test_util::ContextTestBase {
public:
testing::Expectation expect_request_async_tx(bool ok) {
return expect_request_async_tx(*stub, ok);
return expect_request_async_tx(*stub_, ok);
}

testing::Expectation expect_request_async_statechanges(bool ok) {
return expect_request_async_statechanges(*stub, ok);
return expect_request_async_statechanges(*stub_, ok);
}

testing::Expectation expect_request_async_tx(remote::MockKVStub& stb, bool ok) {
Expand All @@ -58,10 +58,12 @@ class KVTestBase : public silkworm::test_util::ContextTestBase {
using StrictMockKVTxAsyncReaderWriter = rpc::test::StrictMockAsyncReaderWriter<remote::Cursor, remote::Pair>;
using StrictMockKVStateChangesAsyncReader = rpc::test::StrictMockAsyncReader<remote::StateChangeBatch>;

//! Mocked stub of gRPC KV interface
std::unique_ptr<StrictMockKVStub> stub{std::make_unique<StrictMockKVStub>()};
StrictMockKVStub& stub() { return *stub_; }

protected:
//! Mocked stub of gRPC KV interface
std::unique_ptr<StrictMockKVStub> stub_{std::make_unique<StrictMockKVStub>()};

//! Mocked reader/writer for Tx bidi streaming RPC of gRPC KV interface
std::unique_ptr<StrictMockKVTxAsyncReaderWriter> reader_writer_ptr_{
std::make_unique<StrictMockKVTxAsyncReaderWriter>()};
Expand Down
2 changes: 1 addition & 1 deletion silkworm/rpc/core/estimate_gas_oracle_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ TEST_CASE("estimate gas") {
const silkworm::ChainConfig& config{kMainnetConfig};
RemoteDatabaseTest remote_db_test;
test::BackEndMock backend;
auto tx = std::make_unique<db::kv::grpc::client::RemoteTransaction>(*remote_db_test.stub,
auto tx = std::make_unique<db::kv::grpc::client::RemoteTransaction>(remote_db_test.stub(),
remote_db_test.grpc_context(),
&remote_db_test.state_cache,
ethdb::kv::block_provider(&backend),
Expand Down

0 comments on commit 054dae6

Please sign in to comment.