Skip to content

Commit

Permalink
rework GrpcApiTestBase
Browse files Browse the repository at this point in the history
  • Loading branch information
yperbasis committed Sep 18, 2024
1 parent 50906a3 commit ef22307
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 36 deletions.
1 change: 0 additions & 1 deletion silkworm/rpc/core/estimate_gas_oracle_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ namespace silkworm::rpc {
class RemoteDatabaseTest : public db::test_util::KVTestBase {
public:
db::kv::api::CoherentStateCache state_cache;
test::BackEndMock backend;
};

using testing::_;
Expand Down
16 changes: 8 additions & 8 deletions silkworm/rpc/ethbackend/remote_backend_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ using EthBackendTest = test_util::GrpcApiTestBase<ethbackend::RemoteBackEnd, Str
#ifndef SILKWORM_SANITIZE
TEST_CASE_METHOD(EthBackendTest, "BackEnd::etherbase", "[silkworm][rpc][ethbackend][backend]") {
test::StrictMockAsyncResponseReader<::remote::EtherbaseReply> reader;
EXPECT_CALL(*stub, AsyncEtherbaseRaw).WillOnce(testing::Return(&reader));
EXPECT_CALL(*stub_, AsyncEtherbaseRaw).WillOnce(testing::Return(&reader));

SECTION("call etherbase and get address") {
::remote::EtherbaseReply response;
Expand All @@ -88,7 +88,7 @@ TEST_CASE_METHOD(EthBackendTest, "BackEnd::etherbase", "[silkworm][rpc][ethbacke

TEST_CASE_METHOD(EthBackendTest, "BackEnd::protocol_version", "[silkworm][rpc][ethbackend][backend]") {
test::StrictMockAsyncResponseReader<::remote::ProtocolVersionReply> reader;
EXPECT_CALL(*stub, AsyncProtocolVersionRaw).WillOnce(testing::Return(&reader));
EXPECT_CALL(*stub_, AsyncProtocolVersionRaw).WillOnce(testing::Return(&reader));

SECTION("call protocol_version and get version") {
::remote::ProtocolVersionReply response;
Expand All @@ -112,7 +112,7 @@ TEST_CASE_METHOD(EthBackendTest, "BackEnd::protocol_version", "[silkworm][rpc][e

TEST_CASE_METHOD(EthBackendTest, "BackEnd::net_version", "[silkworm][rpc][ethbackend][backend]") {
test::StrictMockAsyncResponseReader<::remote::NetVersionReply> reader;
EXPECT_CALL(*stub, AsyncNetVersionRaw).WillOnce(testing::Return(&reader));
EXPECT_CALL(*stub_, AsyncNetVersionRaw).WillOnce(testing::Return(&reader));

SECTION("call net_version and get version") {
::remote::NetVersionReply response;
Expand All @@ -136,7 +136,7 @@ TEST_CASE_METHOD(EthBackendTest, "BackEnd::net_version", "[silkworm][rpc][ethbac

TEST_CASE_METHOD(EthBackendTest, "BackEnd::client_version", "[silkworm][rpc][ethbackend][backend]") {
test::StrictMockAsyncResponseReader<::remote::ClientVersionReply> reader;
EXPECT_CALL(*stub, AsyncClientVersionRaw).WillOnce(testing::Return(&reader));
EXPECT_CALL(*stub_, AsyncClientVersionRaw).WillOnce(testing::Return(&reader));

SECTION("call client_version and get version") {
::remote::ClientVersionReply response;
Expand All @@ -161,7 +161,7 @@ TEST_CASE_METHOD(EthBackendTest, "BackEnd::client_version", "[silkworm][rpc][eth
TEST_CASE_METHOD(EthBackendTest, "BackEnd::get_block_number_from_txn_hash", "[silkworm][rpc][ethbackend][backend]") {
test::StrictMockAsyncResponseReader<::remote::TxnLookupReply> reader;
const Hash hash;
EXPECT_CALL(*stub, AsyncTxnLookupRaw).WillOnce(testing::Return(&reader));
EXPECT_CALL(*stub_, AsyncTxnLookupRaw).WillOnce(testing::Return(&reader));

SECTION("call get_block_number_from_txn_hash and get number") {
::remote::TxnLookupReply response;
Expand All @@ -185,7 +185,7 @@ TEST_CASE_METHOD(EthBackendTest, "BackEnd::get_block_number_from_txn_hash", "[si

TEST_CASE_METHOD(EthBackendTest, "BackEnd::net_peer_count", "[silkworm][rpc][ethbackend][backend]") {
test::StrictMockAsyncResponseReader<::remote::NetPeerCountReply> reader;
EXPECT_CALL(*stub, AsyncNetPeerCountRaw).WillOnce(testing::Return(&reader));
EXPECT_CALL(*stub_, AsyncNetPeerCountRaw).WillOnce(testing::Return(&reader));

SECTION("call net_peer_count and get count") {
::remote::NetPeerCountReply response;
Expand All @@ -209,7 +209,7 @@ TEST_CASE_METHOD(EthBackendTest, "BackEnd::net_peer_count", "[silkworm][rpc][eth

TEST_CASE_METHOD(EthBackendTest, "BackEnd::node_info", "[silkworm][rpc][ethbackend][backend]") {
test::StrictMockAsyncResponseReader<::remote::NodesInfoReply> reader;
EXPECT_CALL(*stub, AsyncNodeInfoRaw).WillOnce(testing::Return(&reader));
EXPECT_CALL(*stub_, AsyncNodeInfoRaw).WillOnce(testing::Return(&reader));

SECTION("call node_info") {
::remote::NodesInfoReply response;
Expand Down Expand Up @@ -465,7 +465,7 @@ TEST_CASE_METHOD(EthBackendTest, "BackEnd::engine_forkchoice_updated", "[silkwor

TEST_CASE_METHOD(EthBackendTest, "BackEnd::peers", "[silkworm][rpc][ethbackend][backend]") {
test::StrictMockAsyncResponseReader<::remote::PeersReply> reader;
EXPECT_CALL(*stub, AsyncPeersRaw).WillOnce(testing::Return(&reader));
EXPECT_CALL(*stub_, AsyncPeersRaw).WillOnce(testing::Return(&reader));

SECTION("call peers") {
::remote::PeersReply response;
Expand Down
20 changes: 3 additions & 17 deletions silkworm/rpc/test_util/api_test_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,17 @@ class JsonApiTestBase : public ServiceContextTestBase {
}
};

template <typename JsonApi>
class JsonApiWithWorkersTestBase : public ServiceContextTestBase {
public:
explicit JsonApiWithWorkersTestBase() : ServiceContextTestBase(), workers_{1} {}

template <auto method, typename... Args>
auto run(Args&&... args) {
JsonApi api{io_context_, workers_};
return spawn_and_wait((api.*method)(std::forward<Args>(args)...));
}

private:
WorkerPool workers_;
};

template <typename GrpcApi, typename Stub>
class GrpcApiTestBase : public ServiceContextTestBase {
public:
template <auto method, typename... Args>
auto run(Args&&... args) {
GrpcApi api{io_context_.get_executor(), std::move(stub), grpc_context_};
GrpcApi api{io_context_.get_executor(), std::move(stub_), grpc_context_};
return spawn_and_wait((api.*method)(std::forward<Args>(args)...));
}

std::unique_ptr<Stub> stub{std::make_unique<Stub>()};
protected:
std::unique_ptr<Stub> stub_{std::make_unique<Stub>()};
};

} // namespace silkworm::rpc::test_util
10 changes: 5 additions & 5 deletions silkworm/rpc/txpool/miner_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ using MinerTest = test_util::GrpcApiTestBase<Miner, StrictMockMiningStub>;
#ifndef SILKWORM_SANITIZE
TEST_CASE_METHOD(MinerTest, "Miner::get_work", "[rpc][txpool][miner]") {
test::StrictMockAsyncResponseReader<::txpool::GetWorkReply> reader;
EXPECT_CALL(*stub, AsyncGetWorkRaw).WillOnce(testing::Return(&reader));
EXPECT_CALL(*stub_, AsyncGetWorkRaw).WillOnce(testing::Return(&reader));

SECTION("call get_work and get result") {
::txpool::GetWorkReply response;
Expand Down Expand Up @@ -75,7 +75,7 @@ TEST_CASE_METHOD(MinerTest, "Miner::get_work", "[rpc][txpool][miner]") {

TEST_CASE_METHOD(MinerTest, "Miner::get_hashrate", "[rpc][txpool][miner]") {
test::StrictMockAsyncResponseReader<::txpool::HashRateReply> reader;
EXPECT_CALL(*stub, AsyncHashRateRaw).WillOnce(testing::Return(&reader));
EXPECT_CALL(*stub_, AsyncHashRateRaw).WillOnce(testing::Return(&reader));

SECTION("call get_hashrate and get result") {
::txpool::HashRateReply response;
Expand All @@ -99,7 +99,7 @@ TEST_CASE_METHOD(MinerTest, "Miner::get_hashrate", "[rpc][txpool][miner]") {

TEST_CASE_METHOD(MinerTest, "Miner::get_mining", "[rpc][txpool][miner]") {
test::StrictMockAsyncResponseReader<::txpool::MiningReply> reader;
EXPECT_CALL(*stub, AsyncMiningRaw).WillOnce(testing::Return(&reader));
EXPECT_CALL(*stub_, AsyncMiningRaw).WillOnce(testing::Return(&reader));

const std::pair<bool, bool> enabled_running_pairs[] = {
{false, false},
Expand Down Expand Up @@ -134,7 +134,7 @@ TEST_CASE_METHOD(MinerTest, "Miner::get_mining", "[rpc][txpool][miner]") {

TEST_CASE_METHOD(MinerTest, "Miner::submit_work", "[rpc][txpool][miner]") {
test::StrictMockAsyncResponseReader<::txpool::SubmitWorkReply> reader;
EXPECT_CALL(*stub, AsyncSubmitWorkRaw).WillOnce(testing::Return(&reader));
EXPECT_CALL(*stub_, AsyncSubmitWorkRaw).WillOnce(testing::Return(&reader));

SECTION("call submit_work and get result") {
::txpool::SubmitWorkReply response;
Expand Down Expand Up @@ -167,7 +167,7 @@ TEST_CASE_METHOD(MinerTest, "Miner::submit_work", "[rpc][txpool][miner]") {

TEST_CASE_METHOD(MinerTest, "Miner::submit_hash_rate", "[rpc][txpool][miner]") {
test::StrictMockAsyncResponseReader<::txpool::SubmitHashRateReply> reader;
EXPECT_CALL(*stub, AsyncSubmitHashRateRaw).WillOnce(testing::Return(&reader));
EXPECT_CALL(*stub_, AsyncSubmitHashRateRaw).WillOnce(testing::Return(&reader));

SECTION("call submit_hash_rate and get result") {
::txpool::SubmitHashRateReply response;
Expand Down
10 changes: 5 additions & 5 deletions silkworm/rpc/txpool/transaction_pool_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ using TransactionPoolTest = test_util::GrpcApiTestBase<TransactionPool, StrictMo
#ifndef SILKWORM_SANITIZE
TEST_CASE_METHOD(TransactionPoolTest, "TransactionPool::add_transaction", "[rpc][txpool][transaction_pool]") {
test::StrictMockAsyncResponseReader<::txpool::AddReply> reader;
EXPECT_CALL(*stub, AsyncAddRaw).WillOnce(testing::Return(&reader));
EXPECT_CALL(*stub_, AsyncAddRaw).WillOnce(testing::Return(&reader));
const silkworm::Bytes tx_rlp{0x00, 0x01};

SECTION("call add_transaction and check import success") {
Expand Down Expand Up @@ -117,7 +117,7 @@ TEST_CASE_METHOD(TransactionPoolTest, "TransactionPool::add_transaction", "[rpc]

TEST_CASE_METHOD(TransactionPoolTest, "TransactionPool::get_transaction", "[rpc][txpool][transaction_pool]") {
test::StrictMockAsyncResponseReader<::txpool::TransactionsReply> reader;
EXPECT_CALL(*stub, AsyncTransactionsRaw).WillOnce(testing::Return(&reader));
EXPECT_CALL(*stub_, AsyncTransactionsRaw).WillOnce(testing::Return(&reader));
const auto tx_hash{0x3763e4f6e4198413383534c763f3f5dac5c5e939f0a81724e3beb96d6e2ad0d5_bytes32};

SECTION("call get_transaction and check success") {
Expand Down Expand Up @@ -154,7 +154,7 @@ TEST_CASE_METHOD(TransactionPoolTest, "TransactionPool::get_transaction", "[rpc]

TEST_CASE_METHOD(TransactionPoolTest, "TransactionPool::nonce", "[rpc][txpool][transaction_pool]") {
test::StrictMockAsyncResponseReader<::txpool::NonceReply> reader;
EXPECT_CALL(*stub, AsyncNonceRaw).WillOnce(testing::Return(&reader));
EXPECT_CALL(*stub_, AsyncNonceRaw).WillOnce(testing::Return(&reader));
const auto account{0x99f9b87991262f6ba471f09758cde1c0fc1de734_address};

SECTION("call nonce and check success") {
Expand Down Expand Up @@ -191,7 +191,7 @@ TEST_CASE_METHOD(TransactionPoolTest, "TransactionPool::nonce", "[rpc][txpool][t

TEST_CASE_METHOD(TransactionPoolTest, "TransactionPool::get_status", "[rpc][txpool][transaction_pool]") {
test::StrictMockAsyncResponseReader<::txpool::StatusReply> reader;
EXPECT_CALL(*stub, AsyncStatusRaw).WillOnce(testing::Return(&reader));
EXPECT_CALL(*stub_, AsyncStatusRaw).WillOnce(testing::Return(&reader));

SECTION("call get_status and check success") {
::txpool::StatusReply response;
Expand Down Expand Up @@ -221,7 +221,7 @@ TEST_CASE_METHOD(TransactionPoolTest, "TransactionPool::get_status", "[rpc][txpo

TEST_CASE_METHOD(TransactionPoolTest, "TransactionPool::get_transactions", "[rpc][txpool][transaction_pool]") {
test::StrictMockAsyncResponseReader<::txpool::AllReply> reader;
EXPECT_CALL(*stub, AsyncAllRaw).WillOnce(testing::Return(&reader));
EXPECT_CALL(*stub_, AsyncAllRaw).WillOnce(testing::Return(&reader));

SECTION("call get_transactions and check success [one tx]") {
::txpool::AllReply response;
Expand Down

0 comments on commit ef22307

Please sign in to comment.